k8sClusters; k8sRepos
[osm/LW-UI.git] / static / src / k8sclusterhandler / k8sclusters_list.js
1 /*
2 Copyright 2019 EveryUP srl
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 openModalRegisterK8s(args) {
18 // load vim account list
19 select2_groups = $('#k8sc_vim_account').select2({
20 placeholder: 'Select VIM',
21 width: '100%',
22 ajax: {
23 url: args.vim_list_url,
24 dataType: 'json',
25 processResults: function (data) {
26 vims = [];
27 if (data['datacenters']) {
28 for (d in data['datacenters']) {
29 var datacenter = data['datacenters'][d];
30 vims.push({ id: datacenter['_id'], text: datacenter['name'] })
31 }
32 }
33
34 return {
35 results: vims
36 };
37 }
38 }
39 });
40
41
42 $('#modal_new_k8sc').modal('show');
43 }
44
45 function showK8sc(k8sc_id, k8sc_name) {
46 var url_info = '/k8scluster/' + k8sc_id;
47 var dialog = bootbox.dialog({
48 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
49 closeButton: true
50 });
51 $.ajax({
52 url: url_info,
53 type: 'GET',
54 dataType: "json",
55 contentType: "application/json;charset=utf-8",
56 success: function (result) {
57
58 if (result['data'] !== undefined) {
59 editorJSON.setValue(JSON.stringify(result['data'], null, "\t"));
60 editorJSON.setOption("autoRefresh", true);
61 dialog.modal('hide');
62 $('#modal_show_k8sc').modal('show');
63 }
64 else {
65 dialog.modal('hide');
66 bootbox.alert("An error occurred while retrieving the information.");
67 }
68 },
69 error: function (result) {
70 dialog.modal('hide');
71 bootbox.alert("An error occurred while retrieving the information.");
72 }
73 });
74 }
75
76 function deleteK8sc(k8sc_id, k8sc_name) {
77 var url = "/k8scluster/"+k8sc_id+"/delete";
78 bootbox.confirm("Are you sure want to delete " + k8sc_name + "?", function (result) {
79 if (result) {
80 var dialog = bootbox.dialog({
81 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
82 closeButton: true
83 });
84 $.ajax({
85 url: url,
86 type: 'GET',
87 dataType: "json",
88 contentType: "application/json;charset=utf-8",
89 success: function (result) {
90 if (result['error'] == true) {
91 dialog.modal('hide');
92 bootbox.alert("An error occurred.");
93 }
94 else {
95 table.ajax.reload();
96 dialog.modal('hide');
97 }
98 },
99 error: function (error) {
100 dialog.modal('hide');
101 bootbox.alert("An error occurred.");
102 }
103 });
104 }
105 })
106 }
107
108 var editorJSON;
109
110 $(document).ready(function () {
111
112 var json_editor_settings = {
113 mode: "javascript",
114 showCursorWhenSelecting: true,
115 autofocus: true,
116 lineNumbers: true,
117 lineWrapping: true,
118 foldGutter: true,
119 gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
120 autoCloseBrackets: true,
121 matchBrackets: true,
122 extraKeys: {
123 "F11": function (cm) {
124 cm.setOption("fullScreen", !cm.getOption("fullScreen"));
125 },
126 "Esc": function (cm) {
127 if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
128 },
129 "Ctrl-Q": function (cm) {
130 cm.foldCode(cm.getCursor());
131 }
132 },
133 theme: "neat",
134 keyMap: "sublime"
135 };
136 var myJsonTextArea = document.getElementById("k8sc_view_json");
137 editorJSON = CodeMirror(function (elt) {
138 myJsonTextArea.parentNode.replaceChild(elt, myJsonTextArea);
139 }, json_editor_settings);
140
141 $("#formCreatek8sc").submit(function (event) {
142 event.preventDefault(); //prevent default action
143 var post_url = $(this).attr("action"); //get form action url
144 var request_method = $(this).attr("method"); //get form GET/POST method
145 var form_data = new FormData(this); //Encode form elements for submission
146 $.ajax({
147 url: post_url,
148 type: request_method,
149 data: form_data,
150 headers: {
151 "Accept": 'application/json'
152 },
153 contentType: false,
154 processData: false
155 }).done(function (response, textStatus, jqXHR) {
156 table.ajax.reload();
157 $('#modal_new_k8sc').modal('hide');
158 }).fail(function (result) {
159 var data = result.responseJSON;
160 var title = "Error " + (data.code ? data.code : 'unknown');
161 var message = data.detail ? data.detail : 'No detail available.';
162 bootbox.alert({
163 title: title,
164 message: message
165 });
166 });
167 });
168
169 });