a2c20036f52b15826f3540e07eecb12d06840c9c
[osm/LW-UI.git] / static / src / instancehandler / instance_list.js
1 /*
2 Copyright 2018 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 function performAction(instance_name, instance_id) {
18 var url = '/instances/ns/'+instance_id+'/action';
19 $("#formActionNS").attr("action", url);
20 $('#modal_instance_new_action').modal('show');
21 }
22
23 function exportMetricNs(instance_name, instance_id) {
24 var url = '/instances/ns/'+instance_id+'/monitoring/metric';
25 $("#formExportMetricNS").attr("action", url);
26 $('#modal_instance_export_metric').modal('show');
27 }
28
29 function newAlarmNs(instance_name, instance_id) {
30 var url = '/instances/ns/'+instance_id+'/monitoring/alarm';
31 $("#formAlarmNS").attr("action", url);
32 $('#modal_instance_new_alarm').modal('show');
33 }
34
35 function deleteNs(instance_name, instance_id, force) {
36 var url = '/instances/ns/'+instance_id+'/delete';
37 bootbox.confirm("Are you sure want to delete " + instance_name + "?", function (result) {
38 if (result) {
39 if (force)
40 url = url + '?force=true';
41 var dialog = bootbox.dialog({
42 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
43 closeButton: true
44 });
45 $.ajax({
46 url: url,
47 type: 'GET',
48 dataType: "json",
49 contentType: "application/json;charset=utf-8",
50 success: function (result) {
51 if (result['error'] == true){
52 dialog.modal('hide');
53 bootbox.alert("An error occurred.");
54 }
55 else {
56 dialog.modal('hide');
57 location.reload();
58 }
59 },
60 error: function (error) {
61 dialog.modal('hide');
62 bootbox.alert("An error occurred.");
63 }
64 });
65 }
66 })
67 }
68
69 var addFormGroup = function (event) {
70 event.preventDefault();
71
72 var $formGroup = $(this).closest('.form-group');
73 var $formGroupClone = $formGroup.clone();
74
75 $(this)
76 .toggleClass('btn-success btn-add btn-danger btn-remove')
77 .html('–');
78
79 $formGroupClone.find('input').val('');
80 $formGroupClone.insertAfter($formGroup);
81
82 };
83
84 var removeFormGroup = function (event) {
85 event.preventDefault();
86 var $formGroup = $(this).closest('.form-group');
87 $formGroup.remove();
88 };
89
90 function showTopology(type, instance_id) {
91 var url = '/instances/'+type+'/'+instance_id+'/topology';
92 window.location = url;
93 }
94
95 function showInstanceDetails(type, instance_id) {
96 var url_info = '/instances/'+type+'/'+instance_id;
97 var dialog = bootbox.dialog({
98 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
99 closeButton: true
100 });
101 $.ajax({
102 url: url_info,
103 type: 'GET',
104 dataType: "json",
105 contentType: "application/json;charset=utf-8",
106 success: function (result) {
107
108 if (result['data'] !== undefined) {
109 editorJSON.setValue(JSON.stringify(result['data'], null, "\t"));
110 editorJSON.setOption("autoRefresh", true);
111 dialog.modal('hide');
112 $('#modal_show_instance').modal('show');
113 }
114 else {
115 dialog.modal('hide');
116 bootbox.alert("An error occurred while retrieving the information.");
117 }
118 },
119 error: function (result) {
120 dialog.modal('hide');
121 bootbox.alert("An error occurred while retrieving the information.");
122 }
123 });
124 }
125
126 var editorJSON;
127
128 $(document).ready(function () {
129 var json_editor_settings = {
130 mode: "javascript",
131 showCursorWhenSelecting: true,
132 autofocus: true,
133 lineNumbers: true,
134 lineWrapping: true,
135 foldGutter: true,
136 gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
137 autoCloseBrackets: true,
138 matchBrackets: true,
139 extraKeys: {
140 "F11": function (cm) {
141 cm.setOption("fullScreen", !cm.getOption("fullScreen"));
142 },
143 "Esc": function (cm) {
144 if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
145 },
146 "Ctrl-Q": function (cm) {
147 cm.foldCode(cm.getCursor());
148 }
149 },
150 theme: "neat",
151 keyMap: "sublime"
152 };
153 var myJsonTextArea = document.getElementById("instance_view_json");
154 editorJSON = CodeMirror(function (elt) {
155 myJsonTextArea.parentNode.replaceChild(elt, myJsonTextArea);
156 }, json_editor_settings);
157
158
159 $(document).on('click', '.btn-add', addFormGroup);
160 $(document).on('click', '.btn-remove', removeFormGroup);
161
162 $("#formActionNS").submit(function (event) {
163 event.preventDefault(); //prevent default action
164 var post_url = $(this).attr("action"); //get form action url
165 var request_method = $(this).attr("method"); //get form GET/POST method
166 var form_data = new FormData(this); //Encode form elements for submission
167 console.log(post_url);
168 $.ajax({
169 url: post_url,
170 type: request_method,
171 data: form_data,
172 headers: {
173 "Accept": 'application/json'
174 },
175 contentType: false,
176 processData: false
177 }).done(function (response, textStatus, jqXHR) {
178 $('#modal_instance_new_action').modal('hide');
179 bootbox.alert({
180 title: "Action",
181 message: "Action received."
182 });
183 }).fail(function (result) {
184 var data = result.responseJSON;
185 var title = "Error " + (data.code ? data.code : 'unknown');
186 var message = data.detail ? data.detail : 'No detail available.';
187 bootbox.alert({
188 title: title,
189 message: message
190 });
191 });
192 });
193
194 $("#formAlarmNS").submit(function (event) {
195 event.preventDefault(); //prevent default action
196 var post_url = $(this).attr("action"); //get form action url
197 var request_method = $(this).attr("method"); //get form GET/POST method
198 var form_data = new FormData(this); //Encode form elements for submission
199 console.log(post_url);
200 $.ajax({
201 url: post_url,
202 type: request_method,
203 data: form_data,
204 headers: {
205 "Accept": 'application/json'
206 },
207 contentType: false,
208 processData: false
209 }).done(function (response, textStatus, jqXHR) {
210 $('#modal_instance_new_action').modal('hide');
211 bootbox.alert({
212 title: "Metric",
213 message: "Alarm created."
214 });
215 }).fail(function (result) {
216 var data = result.responseJSON;
217 var title = "Error " + (data.code ? data.code : 'unknown');
218 var message = data.detail ? data.detail : 'No detail available.';
219 bootbox.alert({
220 title: title,
221 message: message
222 });
223 });
224 });
225
226 $("#formExportMetricNS").submit(function (event) {
227 event.preventDefault(); //prevent default action
228 var post_url = $(this).attr("action"); //get form action url
229 var request_method = $(this).attr("method"); //get form GET/POST method
230 var form_data = new FormData(this); //Encode form elements for submission
231 console.log(post_url);
232 $.ajax({
233 url: post_url,
234 type: request_method,
235 data: form_data,
236 headers: {
237 "Accept": 'application/json'
238 },
239 contentType: false,
240 processData: false
241 }).done(function (response, textStatus, jqXHR) {
242 $('#modal_instance_new_action').modal('hide');
243 bootbox.alert({
244 title: "Metric",
245 message: "Metric exported."
246 });
247 }).fail(function (result) {
248 var data = result.responseJSON;
249 var title = "Error " + (data.code ? data.code : 'unknown');
250 var message = data.detail ? data.detail : 'No detail available.';
251 bootbox.alert({
252 title: title,
253 message: message
254 });
255 });
256 });
257
258 });