pdu: list, create, show, delete
[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 function deletePDU(instance_name, instance_id) {
70 var url = '/instances/pdu/'+instance_id+'/delete';
71 bootbox.confirm("Are you sure want to delete " + instance_name + "?", function (result) {
72 if (result) {
73 var dialog = bootbox.dialog({
74 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
75 closeButton: true
76 });
77 $.ajax({
78 url: url,
79 type: 'GET',
80 dataType: "json",
81 contentType: "application/json;charset=utf-8",
82 success: function (result) {
83 if (result['error'] == true){
84 dialog.modal('hide');
85 bootbox.alert("An error occurred.");
86 }
87 else {
88 dialog.modal('hide');
89 location.reload();
90 }
91 },
92 error: function (error) {
93 dialog.modal('hide');
94 bootbox.alert("An error occurred.");
95 }
96 });
97 }
98 })
99 }
100
101 var addFormGroup = function (event) {
102 event.preventDefault();
103
104 var $formGroup = $(this).closest('.form-group');
105 var $formGroupClone = $formGroup.clone();
106
107 $(this)
108 .toggleClass('btn-success btn-add btn-danger btn-remove')
109 .html('–');
110
111 $formGroupClone.find('input').val('');
112 $formGroupClone.insertAfter($formGroup);
113
114 };
115
116 var removeFormGroup = function (event) {
117 event.preventDefault();
118 var $formGroup = $(this).closest('.form-group');
119 $formGroup.remove();
120 };
121
122 var addInterfaceGroup = function (event) {
123 event.preventDefault();
124
125 var $formGroup = $(this).closest('.interface-group');
126 var $formGroupClone = $formGroup.clone();
127
128 $(this)
129 .toggleClass('btn-success btn-add btn-danger btn-remove')
130 .html('–');
131
132 $formGroupClone.find('input').val('');
133 $formGroupClone.insertAfter($formGroup);
134
135 };
136
137 var removeInterfaceGroup = function (event) {
138 event.preventDefault();
139 var $formGroup = $(this).closest('.interface-group');
140 $formGroup.remove();
141 };
142
143 function showTopology(type, instance_id) {
144 var url = '/instances/'+type+'/'+instance_id+'/topology';
145 window.location = url;
146 }
147
148 function showInstanceDetails(type, instance_id) {
149 var url_info = '/instances/'+type+'/'+instance_id;
150 var dialog = bootbox.dialog({
151 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
152 closeButton: true
153 });
154 $.ajax({
155 url: url_info,
156 type: 'GET',
157 dataType: "json",
158 contentType: "application/json;charset=utf-8",
159 success: function (result) {
160
161 if (result['data'] !== undefined) {
162 editorJSON.setValue(JSON.stringify(result['data'], null, "\t"));
163 editorJSON.setOption("autoRefresh", true);
164 dialog.modal('hide');
165 $('#modal_show_instance').modal('show');
166 }
167 else {
168 dialog.modal('hide');
169 bootbox.alert("An error occurred while retrieving the information.");
170 }
171 },
172 error: function (result) {
173 dialog.modal('hide');
174 bootbox.alert("An error occurred while retrieving the information.");
175 }
176 });
177 }
178
179 var editorJSON;
180
181 $(document).ready(function () {
182 var json_editor_settings = {
183 mode: "javascript",
184 showCursorWhenSelecting: true,
185 autofocus: true,
186 lineNumbers: true,
187 lineWrapping: true,
188 foldGutter: true,
189 gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
190 autoCloseBrackets: true,
191 matchBrackets: true,
192 extraKeys: {
193 "F11": function (cm) {
194 cm.setOption("fullScreen", !cm.getOption("fullScreen"));
195 },
196 "Esc": function (cm) {
197 if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
198 },
199 "Ctrl-Q": function (cm) {
200 cm.foldCode(cm.getCursor());
201 }
202 },
203 theme: "neat",
204 keyMap: "sublime"
205 };
206 var myJsonTextArea = document.getElementById("instance_view_json");
207 editorJSON = CodeMirror(function (elt) {
208 myJsonTextArea.parentNode.replaceChild(elt, myJsonTextArea);
209 }, json_editor_settings);
210
211
212 $(document).on('click', '.primitive-group .btn-add', addFormGroup);
213 $(document).on('click', '.primitive-group .btn-remove', removeFormGroup);
214
215 $(document).on('click', '.interface-group .btn-add', addInterfaceGroup);
216 $(document).on('click', '.interface-group .btn-remove', removeInterfaceGroup);
217
218 $("#formCreatePDU").submit(function (event) {
219 event.preventDefault(); //prevent default action
220 var post_url = $(this).attr("action"); //get form action url
221 var request_method = $(this).attr("method"); //get form GET/POST method
222 var form_data = new FormData(this); //Encode form elements for submission
223 $.ajax({
224 url: post_url,
225 type: request_method,
226 data: form_data,
227 headers: {
228 "Accept": 'application/json'
229 },
230 contentType: false,
231 processData: false
232 }).done(function (response, textStatus, jqXHR) {
233 table.ajax.reload();
234 $('#modal_new_pdu').modal('hide');
235 }).fail(function (result) {
236 var data = result.responseJSON;
237 var title = "Error " + (data.code ? data.code : 'unknown');
238 var message = data.detail ? data.detail : 'No detail available.';
239 bootbox.alert({
240 title: title,
241 message: message
242 });
243 });
244 });
245
246 $("#formActionNS").submit(function (event) {
247 event.preventDefault(); //prevent default action
248 var post_url = $(this).attr("action"); //get form action url
249 var request_method = $(this).attr("method"); //get form GET/POST method
250 var form_data = new FormData(this); //Encode form elements for submission
251 console.log(post_url);
252 $.ajax({
253 url: post_url,
254 type: request_method,
255 data: form_data,
256 headers: {
257 "Accept": 'application/json'
258 },
259 contentType: false,
260 processData: false
261 }).done(function (response, textStatus, jqXHR) {
262 $('#modal_instance_new_action').modal('hide');
263 bootbox.alert({
264 title: "Action",
265 message: "Action received."
266 });
267 }).fail(function (result) {
268 var data = result.responseJSON;
269 var title = "Error " + (data.code ? data.code : 'unknown');
270 var message = data.detail ? data.detail : 'No detail available.';
271 bootbox.alert({
272 title: title,
273 message: message
274 });
275 });
276 });
277
278 $("#formAlarmNS").submit(function (event) {
279 event.preventDefault(); //prevent default action
280 var post_url = $(this).attr("action"); //get form action url
281 var request_method = $(this).attr("method"); //get form GET/POST method
282 var form_data = new FormData(this); //Encode form elements for submission
283 console.log(post_url);
284 $.ajax({
285 url: post_url,
286 type: request_method,
287 data: form_data,
288 headers: {
289 "Accept": 'application/json'
290 },
291 contentType: false,
292 processData: false
293 }).done(function (response, textStatus, jqXHR) {
294 $('#modal_instance_new_action').modal('hide');
295 bootbox.alert({
296 title: "Metric",
297 message: "Alarm created."
298 });
299 }).fail(function (result) {
300 var data = result.responseJSON;
301 var title = "Error " + (data.code ? data.code : 'unknown');
302 var message = data.detail ? data.detail : 'No detail available.';
303 bootbox.alert({
304 title: title,
305 message: message
306 });
307 });
308 });
309
310 $("#formExportMetricNS").submit(function (event) {
311 event.preventDefault(); //prevent default action
312 var post_url = $(this).attr("action"); //get form action url
313 var request_method = $(this).attr("method"); //get form GET/POST method
314 var form_data = new FormData(this); //Encode form elements for submission
315 console.log(post_url);
316 $.ajax({
317 url: post_url,
318 type: request_method,
319 data: form_data,
320 headers: {
321 "Accept": 'application/json'
322 },
323 contentType: false,
324 processData: false
325 }).done(function (response, textStatus, jqXHR) {
326 $('#modal_instance_new_action').modal('hide');
327 bootbox.alert({
328 title: "Metric",
329 message: "Metric exported."
330 });
331 }).fail(function (result) {
332 var data = result.responseJSON;
333 var title = "Error " + (data.code ? data.code : 'unknown');
334 var message = data.detail ? data.detail : 'No detail available.';
335 bootbox.alert({
336 title: title,
337 message: message
338 });
339 });
340 });
341
342 });