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