root / ui / static / synnefo.js @ ef7770cf
History | View | Annotate | Download (22.7 kB)
1 |
var flavors = [], images = [], servers = [], disks = [], cpus = [], ram = [];
|
---|---|
2 |
var changes_since = 0, deferred = 0, update_request = false, load_request = false, pending_actions = []; |
3 |
var API_URL = "/api/v1.1"; |
4 |
|
5 |
function ISODateString(d){ |
6 |
//return a date in an ISO 8601 format using UTC.
|
7 |
//do not include time zone info (Z) at the end
|
8 |
//taken from the Mozilla Developer Center
|
9 |
function pad(n){ return n<10 ? '0'+n : n } |
10 |
return d.getUTCFullYear()+ '-' + |
11 |
pad(d.getUTCMonth()+1) + '-' + |
12 |
pad(d.getUTCDate()) + 'T' +
|
13 |
pad(d.getUTCHours()) + ':' +
|
14 |
pad(d.getUTCMinutes()) + ':' +
|
15 |
pad(d.getUTCSeconds()) +'Z'
|
16 |
} |
17 |
|
18 |
function parse_error(responseText){ |
19 |
var errors = [];
|
20 |
if (responseText.length == 0){ |
21 |
errors[0] = {'code': 503}; |
22 |
} else {
|
23 |
responseObj = JSON.parse(responseText); |
24 |
//console.info(inp);
|
25 |
for (var err in responseObj){ |
26 |
errors[errors.length] = responseObj[err]; |
27 |
} |
28 |
} |
29 |
return errors;
|
30 |
} |
31 |
|
32 |
// indexOf prototype for IE
|
33 |
if (!Array.prototype.indexOf) {
|
34 |
Array.prototype.indexOf = function(elt /*, from*/) { |
35 |
var len = this.length; |
36 |
var from = Number(arguments[1]) || 0; |
37 |
from = (from < 0)
|
38 |
? Math.ceil(from) |
39 |
: Math.floor(from); |
40 |
if (from < 0) |
41 |
from += len; |
42 |
|
43 |
for (; from < len; from++) {
|
44 |
if (from in this && |
45 |
this[from] === elt)
|
46 |
return from;
|
47 |
} |
48 |
return -1; |
49 |
}; |
50 |
} |
51 |
|
52 |
|
53 |
function update_confirmations(){ |
54 |
// hide all confirm boxes to begin with
|
55 |
$('div.confirm_single').hide(); |
56 |
$('div.confirm_multiple').hide(); |
57 |
|
58 |
// standard view only
|
59 |
if ($.cookie("list") != '1') { |
60 |
for (var i=0;i<pending_actions.length;i++){ |
61 |
// show single confirms
|
62 |
$("div.machine#"+pending_actions[i][1]+' .confirm_single').show(); |
63 |
} |
64 |
} |
65 |
|
66 |
// if more than one pending action show multiple confirm box
|
67 |
if (pending_actions.length>1 || $.cookie("list") == '1' && pending_actions.length == 1){ |
68 |
$('div.confirm_multiple span.actionLen').text(pending_actions.length); |
69 |
$('div.confirm_multiple').show(); |
70 |
} |
71 |
} |
72 |
|
73 |
function list_view() { |
74 |
changes_since = 0; // to reload full list |
75 |
pending_actions = []; // clear pending actions
|
76 |
update_confirmations(); |
77 |
clearTimeout(deferred); // clear old deferred calls
|
78 |
try {
|
79 |
update_request.abort(); // cancel pending ajax updates
|
80 |
load_request.abort(); |
81 |
}catch(err){}
|
82 |
$.cookie("list", '1'); // set list cookie |
83 |
|
84 |
uri = $("#list").attr("href"); |
85 |
load_request = $.ajax({
|
86 |
url: uri,
|
87 |
type: "GET", |
88 |
timeout: TIMEOUT,
|
89 |
dataType: "html", |
90 |
error: function(jqXHR, textStatus, errorThrown) { |
91 |
return false; |
92 |
}, |
93 |
success: function(data, textStatus, jqXHR) { |
94 |
$("a#list")[0].className += ' activelink'; |
95 |
$("a#standard")[0].className = ''; |
96 |
$("div#machinesview").html(data); |
97 |
} |
98 |
}); |
99 |
|
100 |
return false; |
101 |
} |
102 |
|
103 |
function standard_view() { |
104 |
changes_since = 0; // to reload full list |
105 |
pending_actions = []; // clear pending actions
|
106 |
update_confirmations(); |
107 |
clearTimeout(deferred); // clear old deferred calls
|
108 |
try {
|
109 |
update_request.abort() // cancel pending ajax updates
|
110 |
load_request.abort(); |
111 |
}catch(err){}
|
112 |
$.cookie("list", '0'); |
113 |
|
114 |
uri = $("a#standard").attr("href"); |
115 |
load_request = $.ajax({
|
116 |
url: uri,
|
117 |
type: "GET", |
118 |
timeout: TIMEOUT,
|
119 |
dataType: "html", |
120 |
error: function(jqXHR, textStatus, errorThrown) { |
121 |
return false; |
122 |
}, |
123 |
success: function(data, textStatus, jqXHR) { |
124 |
$("a#standard")[0].className += ' activelink'; |
125 |
$("a#list")[0].className = ''; |
126 |
$("div#machinesview").html(data); |
127 |
} |
128 |
}); |
129 |
|
130 |
return false; |
131 |
} |
132 |
|
133 |
function choose_view() { |
134 |
if ($.cookie("list")=='1') { |
135 |
list_view(); |
136 |
} else {
|
137 |
standard_view(); |
138 |
} |
139 |
} |
140 |
|
141 |
function toggleMenu() { |
142 |
var primary = $("ul.css-tabs li a.primary"); |
143 |
var secondary = $("ul.css-tabs li a.secondary"); |
144 |
var all = $("ul.css-tabs li a"); |
145 |
var toggled = $('ul.css-tabs li a.current').hasClass('secondary'); |
146 |
|
147 |
// if anything is still moving, do nothing
|
148 |
if ($(":animated").length) { |
149 |
return;
|
150 |
} |
151 |
|
152 |
// nothing is current to begin with
|
153 |
$('ul.css-tabs li a.current').removeClass('current'); |
154 |
|
155 |
// move stuff around
|
156 |
all.animate({top:'30px'}, {complete: function() { |
157 |
$(this).hide(); |
158 |
if (toggled) {
|
159 |
primary.show(); |
160 |
primary.animate({top:'9px'}, {complete: function() { |
161 |
$('ul.css-tabs li a.primary#machines').addClass('current'); |
162 |
$('a#machines').click(); |
163 |
}}); |
164 |
} else {
|
165 |
secondary.show(); |
166 |
secondary.animate({top:'9px'}, {complete: function() { |
167 |
$('ul.css-tabs li a.secondary#files').addClass('current'); |
168 |
$('a#files').click(); |
169 |
}}); |
170 |
} |
171 |
}}); |
172 |
|
173 |
// rotate arrow icon
|
174 |
if (toggled) {
|
175 |
$("#arrow").rotate({animateAngle: (0), bind:[{"click":function(){toggleMenu()}}]}); |
176 |
$("#arrow").rotateAnimation(0); |
177 |
} else {
|
178 |
$("#arrow").rotate({animateAngle: (-180), bind:[{"click":function(){toggleMenu()}}]}); |
179 |
$("#arrow").rotateAnimation(-180); |
180 |
} |
181 |
} |
182 |
|
183 |
// confirmation overlay generation
|
184 |
function confirm_action(action_string, action_function, serverIDs, serverNames) { |
185 |
if (serverIDs.length == 1){ |
186 |
$("#yes-no h3").text('You are about to ' + action_string + ' vm ' + serverNames[0]); |
187 |
} else if (serverIDs.length > 1){ |
188 |
$("#yes-no h3").text('You are about to ' + action_string + ' ' + serverIDs.length + ' machines'); |
189 |
} else {
|
190 |
return false; |
191 |
} |
192 |
// action confirmation overlay
|
193 |
var triggers = $("a#confirmation").overlay({ |
194 |
// some mask tweaks suitable for modal dialogs
|
195 |
mask: {
|
196 |
color: '#ebecff', |
197 |
opacity: '0.9' |
198 |
}, |
199 |
top: 'center', |
200 |
load: false |
201 |
}); |
202 |
// yes or no?
|
203 |
var buttons = $("#yes-no button").click(function(e) { |
204 |
// get user input
|
205 |
var yes = buttons.index(this) === 0; |
206 |
//close the confirmation window
|
207 |
$("a#confirmation").overlay().close(); |
208 |
// return true=yes or false=no
|
209 |
if (yes) {
|
210 |
action_function(serverIDs); |
211 |
} |
212 |
}); |
213 |
$("a#confirmation").data('overlay').load(); |
214 |
return false; |
215 |
} |
216 |
|
217 |
// get and show a list of running and terminated machines
|
218 |
function update_vms(interval) { |
219 |
try{ console.info('updating machines'); } catch(err){} |
220 |
var uri= API_URL + '/servers/detail'; |
221 |
|
222 |
if (changes_since != 0) |
223 |
uri+='?changes-since='+changes_since
|
224 |
|
225 |
update_request = $.ajax({
|
226 |
url: uri,
|
227 |
type: "GET", |
228 |
timeout: TIMEOUT,
|
229 |
dataType: "json", |
230 |
error: function(jqXHR, textStatus, errorThrown) { |
231 |
// don't forget to try again later
|
232 |
if (interval) {
|
233 |
clearTimeout(deferred); // clear old deferred calls
|
234 |
deferred = setTimeout(update_vms,interval,interval); |
235 |
} |
236 |
// as for now, just show an error message
|
237 |
try { console.info('update_vms errback:' + jqXHR.status ) } catch(err) {} |
238 |
ajax_error(jqXHR.status, undefined, 'Update VMs', jqXHR.responseText); |
239 |
return false; |
240 |
}, |
241 |
success: function(data, textStatus, jqXHR) { |
242 |
// create changes_since string if necessary
|
243 |
if (jqXHR.getResponseHeader('Date') != null){ |
244 |
changes_since_date = new Date(jqXHR.getResponseHeader('Date')); |
245 |
changes_since = ISODateString(changes_since_date); |
246 |
} |
247 |
|
248 |
if (interval) {
|
249 |
clearTimeout(deferred); // clear old deferred calls
|
250 |
deferred = setTimeout(update_vms,interval,interval); |
251 |
} |
252 |
|
253 |
if (jqXHR.status == 200 || jqXHR.status == 203) { |
254 |
try {
|
255 |
servers = data.servers.values; |
256 |
} catch(err) { ajax_error('400', undefined, 'Update VMs', jqXHR.responseText);} |
257 |
update_machines_view(data); |
258 |
} else if (jqXHR.status != 304){ |
259 |
try { console.info('update_vms callback:' + jqXHR.status ) } catch(err) {} |
260 |
//ajax_error(jqXHR.status, undefined, 'Update VMs', jqXHR.responseText);
|
261 |
} |
262 |
return false; |
263 |
} |
264 |
}); |
265 |
return false; |
266 |
} |
267 |
|
268 |
// get and show a list of available standard and custom images
|
269 |
function update_images() { |
270 |
$.ajax({
|
271 |
url: API_URL + '/images/detail', |
272 |
type: "GET", |
273 |
//async: false,
|
274 |
dataType: "json", |
275 |
timeout: TIMEOUT,
|
276 |
error: function(jqXHR, textStatus, errorThrown) { |
277 |
ajax_error(jqXHR.status, undefined, 'Update Images', jqXHR.responseText); |
278 |
}, |
279 |
success: function(data, textStatus, jqXHR) { |
280 |
try {
|
281 |
images = data.images.values; |
282 |
update_wizard_images(); |
283 |
} catch(err){
|
284 |
ajax_error("NO_IMAGES");
|
285 |
} |
286 |
} |
287 |
}); |
288 |
return false; |
289 |
} |
290 |
|
291 |
function update_wizard_images() { |
292 |
if ($("ul#standard-images li").toArray().length + $("ul#custom-images li").toArray().length == 0) { |
293 |
$.each(images, function(i,image){ |
294 |
var img = $('#image-template').clone().attr("id","img-"+image.id).fadeIn("slow"); |
295 |
img.find("label").attr('for',"img-radio-" + image.id); |
296 |
img.find(".image-title").text(image.name);
|
297 |
if (image.metadata) {
|
298 |
if (image.metadata.values.description != undefined) { |
299 |
img.find(".description").text(image.metadata.values.description);
|
300 |
} |
301 |
if (image.metadata.values.size != undefined) { |
302 |
img.find("#size").text(image.metadata.values.size);
|
303 |
} |
304 |
} |
305 |
img.find("input.radio").attr('id',"img-radio-" + image.id); |
306 |
if (i==0) img.find("input.radio").attr("checked","checked"); |
307 |
var image_logo = os_icon(image.metadata);
|
308 |
img.find("img.image-logo").attr('src','static/os_logos/'+image_logo+'.png'); |
309 |
if (image.metadata) {
|
310 |
if (image.metadata.values.serverId != undefined) { |
311 |
img.appendTo("ul#custom-images");
|
312 |
} else {
|
313 |
img.appendTo("ul#standard-images");
|
314 |
} |
315 |
} else {
|
316 |
img.appendTo("ul#standard-images");
|
317 |
} |
318 |
}); |
319 |
} |
320 |
} |
321 |
|
322 |
function update_wizard_flavors(){ |
323 |
// sliders for selecting VM flavor
|
324 |
$("#cpu:range").rangeinput({min:0, |
325 |
value:0, |
326 |
step:1, |
327 |
progress: true, |
328 |
max:cpus.length-1}); |
329 |
|
330 |
$("#storage:range").rangeinput({min:0, |
331 |
value:0, |
332 |
step:1, |
333 |
progress: true, |
334 |
max:disks.length-1}); |
335 |
|
336 |
$("#ram:range").rangeinput({min:0, |
337 |
value:0, |
338 |
step:1, |
339 |
progress: true, |
340 |
max:ram.length-1}); |
341 |
$("#small").click(); |
342 |
|
343 |
// update the indicators when sliding
|
344 |
$("#cpu:range").data().rangeinput.onSlide(function(event,value){ |
345 |
$("#cpu-indicator")[0].value = cpus[Number(value)]; |
346 |
$("#cpu-indicator").addClass('selectedrange'); |
347 |
}); |
348 |
$("#cpu:range").data().rangeinput.change(function(event,value){ |
349 |
$("#cpu-indicator")[0].value = cpus[Number(value)]; |
350 |
$("#custom").click(); |
351 |
$("#cpu-indicator").removeClass('selectedrange'); |
352 |
}); |
353 |
$("#ram:range").data().rangeinput.onSlide(function(event,value){ |
354 |
$("#ram-indicator")[0].value = ram[Number(value)]; |
355 |
$("#ram-indicator").addClass('selectedrange'); |
356 |
}); |
357 |
$("#ram:range").data().rangeinput.change(function(event,value){ |
358 |
$("#ram-indicator")[0].value = ram[Number(value)]; |
359 |
$("#custom").click(); |
360 |
$("#ram-indicator").removeClass('selectedrange'); |
361 |
}); |
362 |
$("#storage:range").data().rangeinput.onSlide(function(event,value){ |
363 |
$("#storage-indicator")[0].value = disks[Number(value)]; |
364 |
$("#storage-indicator").addClass('selectedrange'); |
365 |
}); |
366 |
$("#storage:range").data().rangeinput.change(function(event,value){ |
367 |
$("#storage-indicator")[0].value = disks[Number(value)]; |
368 |
$("#custom").click(); |
369 |
$("#storage-indicator").removeClass('selectedrange'); |
370 |
}); |
371 |
} |
372 |
|
373 |
Array.prototype.unique = function () { |
374 |
var r = new Array(); |
375 |
o:for(var i = 0, n = this.length; i < n; i++) |
376 |
{ |
377 |
for(var x = 0, y = r.length; x < y; x++) |
378 |
{ |
379 |
if(r[x]==this[i]) |
380 |
{ |
381 |
continue o;
|
382 |
} |
383 |
} |
384 |
r[r.length] = this[i];
|
385 |
} |
386 |
return r;
|
387 |
} |
388 |
|
389 |
// get and configure flavor selection
|
390 |
function update_flavors() { |
391 |
$.ajax({
|
392 |
url: API_URL + '/flavors/detail', |
393 |
type: "GET", |
394 |
//async: false,
|
395 |
dataType: "json", |
396 |
timeout: TIMEOUT,
|
397 |
error: function(jqXHR, textStatus, errorThrown) { |
398 |
try {
|
399 |
ajax_error(jqXHR.status, undefined, 'Update Flavors', jqXHR.responseText); |
400 |
} catch (err) {
|
401 |
ajax_error(err); |
402 |
} |
403 |
}, |
404 |
success: function(data, textStatus, jqXHR) { |
405 |
flavors = data.flavors.values; |
406 |
$.each(flavors, function(i, flavor) { |
407 |
cpus[i] = flavor['cpu'];
|
408 |
disks[i] = flavor['disk'];
|
409 |
ram[i] = flavor['ram'];
|
410 |
}); |
411 |
cpus = cpus.unique(); |
412 |
disks = disks.unique(); |
413 |
ram = ram.unique(); |
414 |
update_wizard_flavors(); |
415 |
} |
416 |
}); |
417 |
return false; |
418 |
} |
419 |
|
420 |
// return flavorRef from cpu, disk, ram values
|
421 |
function identify_flavor(cpu, disk, ram){ |
422 |
for (i=0;i<flavors.length;i++){ |
423 |
if (flavors[i]['cpu'] == cpu && flavors[i]['disk']==disk && flavors[i]['ram']==ram) { |
424 |
return flavors[i]['id'] |
425 |
} |
426 |
} |
427 |
return 0; |
428 |
} |
429 |
|
430 |
// return image entry from imageRef
|
431 |
function get_image(imageRef) { |
432 |
for (i=0;i<images.length;i++){ |
433 |
if (images[i]['id'] == imageRef) { |
434 |
return images[i];
|
435 |
} |
436 |
} |
437 |
return 0; |
438 |
} |
439 |
|
440 |
// update the actions in list view
|
441 |
function updateActions() { |
442 |
var states = [];
|
443 |
var on = [];
|
444 |
var checked = $("table.list-machines tbody input[type='checkbox']:checked"); |
445 |
// disable all actions to begin with
|
446 |
for (action in actions) { |
447 |
$("#action-" + action).removeClass('enabled'); |
448 |
} |
449 |
|
450 |
// are there multiple machines selected?
|
451 |
if (checked.length>1) |
452 |
states[0] = 'multiple'; |
453 |
|
454 |
// check the states of selected machines
|
455 |
checked.each(function(i,checkbox) {
|
456 |
states[states.length] = checkbox.className; |
457 |
var ip = $("#" + checkbox.id.replace('input-','') + ".ip span.public").text(); |
458 |
if (ip.replace('undefined','').length) |
459 |
states[states.length] = 'network';
|
460 |
}); |
461 |
|
462 |
// decide which actions should be enabled
|
463 |
for (a in actions) { |
464 |
var enabled = false; |
465 |
for (var s =0; s<states.length; s++) { |
466 |
if (actions[a].indexOf(states[s]) != -1 ) { |
467 |
enabled = true;
|
468 |
} else {
|
469 |
enabled = false;
|
470 |
break;
|
471 |
} |
472 |
} |
473 |
if (enabled)
|
474 |
on[on.length]=a; |
475 |
} |
476 |
// enable those actions
|
477 |
for (action in on) { |
478 |
$("#action-" + on[action]).addClass('enabled'); |
479 |
} |
480 |
} |
481 |
|
482 |
//create server action
|
483 |
function create_vm(machineName, imageRef, flavorRef){ |
484 |
|
485 |
var image_logo = os_icon(get_image(imageRef).metadata);
|
486 |
|
487 |
var payload = {
|
488 |
"server": {
|
489 |
"name": machineName,
|
490 |
"imageRef": imageRef,
|
491 |
"flavorRef" : flavorRef,
|
492 |
"metadata" : {
|
493 |
"OS" : image_logo
|
494 |
} |
495 |
} |
496 |
}; |
497 |
var uri = API_URL + '/servers'; |
498 |
|
499 |
$.ajax({
|
500 |
url: uri,
|
501 |
type: "POST", |
502 |
contentType: "application/json", |
503 |
dataType: "json", |
504 |
data: JSON.stringify(payload),
|
505 |
timeout: TIMEOUT,
|
506 |
error: function(jqXHR, textStatus, errorThrown) { |
507 |
ajax_error(jqXHR.status, undefined, 'Create VM', jqXHR.responseText); |
508 |
}, |
509 |
success: function(data, textStatus, jqXHR) { |
510 |
if ( jqXHR.status == '202') { |
511 |
ajax_success("CREATE_VM_SUCCESS", data.server.adminPass);
|
512 |
} else {
|
513 |
ajax_error(jqXHR.status, undefined, 'Create VM', jqXHR.responseText); |
514 |
} |
515 |
} |
516 |
}); |
517 |
} |
518 |
|
519 |
// reboot action
|
520 |
function reboot(serverIDs){ |
521 |
if (!serverIDs.length){
|
522 |
//ajax_success('DEFAULT');
|
523 |
return false; |
524 |
} |
525 |
// ajax post reboot call
|
526 |
var payload = {
|
527 |
"reboot": {"type" : "HARD"} |
528 |
}; |
529 |
var serverID = serverIDs.pop();
|
530 |
|
531 |
$.ajax({
|
532 |
url: API_URL + '/servers/' + serverID + '/action', |
533 |
type: "POST", |
534 |
contentType: "application/json", |
535 |
dataType: "json", |
536 |
data: JSON.stringify(payload),
|
537 |
timeout: TIMEOUT,
|
538 |
error: function(jqXHR, textStatus, errorThrown) { |
539 |
display_failure(jqXHR.status, serverID, 'Reboot', jqXHR.responseText)
|
540 |
}, |
541 |
success: function(data, textStatus, jqXHR) { |
542 |
if ( jqXHR.status == '202') { |
543 |
try {
|
544 |
console.info('rebooted ' + serverID);
|
545 |
} catch(err) {}
|
546 |
// indicate that the action succeeded
|
547 |
display_success(serverID); |
548 |
// continue with the rest of the servers
|
549 |
reboot(serverIDs); |
550 |
} else {
|
551 |
ajax_error(jqXHR.status, serverID, 'Reboot', jqXHR.responseText);
|
552 |
} |
553 |
} |
554 |
}); |
555 |
|
556 |
return false; |
557 |
} |
558 |
|
559 |
// shutdown action
|
560 |
function shutdown(serverIDs) { |
561 |
if (!serverIDs.length){
|
562 |
//ajax_success('DEFAULT');
|
563 |
return false; |
564 |
} |
565 |
// ajax post shutdown call
|
566 |
var payload = {
|
567 |
"shutdown": {"timeout" : "5"} |
568 |
}; |
569 |
|
570 |
var serverID = serverIDs.pop()
|
571 |
$.ajax({
|
572 |
url: API_URL + '/servers/' + serverID + '/action', |
573 |
type: "POST", |
574 |
contentType: "application/json", |
575 |
dataType: "json", |
576 |
data: JSON.stringify(payload),
|
577 |
timeout: TIMEOUT,
|
578 |
error: function(jqXHR, textStatus, errorThrown) { |
579 |
display_failure(jqXHR.status, serverID, 'Shutdown', jqXHR.responseText)
|
580 |
}, |
581 |
success: function(data, textStatus, jqXHR) { |
582 |
if ( jqXHR.status == '202') { |
583 |
try {
|
584 |
console.info('suspended ' + serverID);
|
585 |
} catch(err) {}
|
586 |
// indicate that the action succeeded
|
587 |
display_success(serverID); |
588 |
// continue with the rest of the servers
|
589 |
shutdown(serverIDs); |
590 |
} else {
|
591 |
ajax_error(jqXHR.status, serverID, 'Shutdown', jqXHR.responseText);
|
592 |
} |
593 |
} |
594 |
}); |
595 |
|
596 |
return false; |
597 |
} |
598 |
|
599 |
// destroy action
|
600 |
function destroy(serverIDs) { |
601 |
if (!serverIDs.length){
|
602 |
//ajax_success('DEFAULT');
|
603 |
return false; |
604 |
} |
605 |
// ajax post destroy call can have an empty request body
|
606 |
var payload = {};
|
607 |
|
608 |
serverID = serverIDs.pop() |
609 |
$.ajax({
|
610 |
url: API_URL + '/servers/' + serverID, |
611 |
type: "DELETE", |
612 |
contentType: "application/json", |
613 |
dataType: "json", |
614 |
data: JSON.stringify(payload),
|
615 |
timeout: TIMEOUT,
|
616 |
error: function(jqXHR, textStatus, errorThrown) { |
617 |
display_failure(jqXHR.status, serverID, 'Destroy', jqXHR.responseText)
|
618 |
}, |
619 |
success: function(data, textStatus, jqXHR) { |
620 |
if ( jqXHR.status == '204') { |
621 |
try {
|
622 |
console.info('destroyed ' + serverID);
|
623 |
} catch (err) {}
|
624 |
// indicate that the action succeeded
|
625 |
display_success(serverID); |
626 |
// continue with the rest of the servers
|
627 |
destroy(serverIDs); |
628 |
} else {
|
629 |
ajax_error(jqXHR.status, serverID, 'Destroy', jqXHR.responseText);
|
630 |
} |
631 |
} |
632 |
}); |
633 |
|
634 |
return false; |
635 |
} |
636 |
|
637 |
// start action
|
638 |
function start(serverIDs){ |
639 |
if (!serverIDs.length){
|
640 |
//ajax_success('DEFAULT');
|
641 |
return false; |
642 |
} |
643 |
// ajax post start call
|
644 |
var payload = {
|
645 |
"start": {"type" : "NORMAL"} |
646 |
}; |
647 |
|
648 |
var serverID = serverIDs.pop()
|
649 |
$.ajax({
|
650 |
url: API_URL + '/servers/' + serverID + '/action', |
651 |
type: "POST", |
652 |
contentType: "application/json", |
653 |
dataType: "json", |
654 |
data: JSON.stringify(payload),
|
655 |
timeout: TIMEOUT,
|
656 |
error: function(jqXHR, textStatus, errorThrown) { |
657 |
display_failure(jqXHR.status, serverID, 'Start', jqXHR.responseText)
|
658 |
}, |
659 |
success: function(data, textStatus, jqXHR) { |
660 |
if ( jqXHR.status == '202') { |
661 |
try {
|
662 |
console.info('started ' + serverID);
|
663 |
} catch(err) {}
|
664 |
// indicate that the action succeeded
|
665 |
display_success(serverID); |
666 |
// continue with the rest of the servers
|
667 |
start(serverIDs); |
668 |
} else {
|
669 |
ajax_error(jqXHR.status, serverID, 'Start', jqXHR.responseText);
|
670 |
} |
671 |
} |
672 |
}); |
673 |
|
674 |
return false; |
675 |
} |
676 |
|
677 |
// get server metadata
|
678 |
function get_metadata(serverID) { |
679 |
$.ajax({
|
680 |
url: API_URL + '/servers/' + serverID + '/meta', |
681 |
type: "GET", |
682 |
//async: false,
|
683 |
dataType: "json", |
684 |
timeout: TIMEOUT,
|
685 |
error: function(jqXHR, textStatus, errorThrown) { |
686 |
try {
|
687 |
ajax_error(jqXHR.status, undefined, 'Get metadata', jqXHR.responseText); |
688 |
} catch (err) {
|
689 |
ajax_error(err); |
690 |
} |
691 |
}, |
692 |
success: function(data, textStatus, jqXHR) { |
693 |
// to list the new results in the edit dialog
|
694 |
list_metadata(data); |
695 |
} |
696 |
}); |
697 |
return false; |
698 |
} |
699 |
|
700 |
// delete metadata key-value pair
|
701 |
function delete_metadata(serverID, meta_key) { |
702 |
$.ajax({
|
703 |
url: API_URL + '/servers/' + serverID + '/meta/' + meta_key, |
704 |
type: "DELETE", |
705 |
//async: false,
|
706 |
dataType: "json", |
707 |
timeout: TIMEOUT,
|
708 |
error: function(jqXHR, textStatus, errorThrown) { |
709 |
try {
|
710 |
ajax_error(jqXHR.status, undefined, 'Delete metadata', jqXHR.responseText); |
711 |
} catch (err) {
|
712 |
ajax_error(err); |
713 |
} |
714 |
}, |
715 |
success: function(data, textStatus, jqXHR) { |
716 |
// to GET new results and list them in the edit dialog
|
717 |
get_metadata(serverID); |
718 |
} |
719 |
}); |
720 |
return false; |
721 |
} |
722 |
|
723 |
|
724 |
// add metadata key-value pair
|
725 |
function add_metadata(serverID, meta_key, meta_value) { |
726 |
|
727 |
var payload = {
|
728 |
"meta": {
|
729 |
} |
730 |
}; |
731 |
payload["meta"][meta_key] = meta_value;
|
732 |
|
733 |
$.ajax({
|
734 |
url: API_URL + '/servers/' + serverID + '/meta/' + meta_key, |
735 |
type: "PUT", |
736 |
contentType: "application/json", |
737 |
dataType: "json", |
738 |
data: JSON.stringify(payload),
|
739 |
timeout: TIMEOUT,
|
740 |
error: function(jqXHR, textStatus, errorThrown) { |
741 |
try {
|
742 |
ajax_error(jqXHR.status, undefined, 'add metadata', jqXHR.responseText); |
743 |
} catch (err) {
|
744 |
ajax_error(err); |
745 |
} |
746 |
}, |
747 |
success: function(data, textStatus, jqXHR) { |
748 |
// to GET new results and list them in the edit dialog
|
749 |
get_metadata(serverID); |
750 |
} |
751 |
}); |
752 |
return false; |
753 |
} |
754 |
|
755 |
|
756 |
// show the welcome screen
|
757 |
function showWelcome() { |
758 |
$("#view-select").fadeOut("fast"); |
759 |
$("#machinesview.standard").fadeOut("fast"); |
760 |
$("#createcontainer").addClass('emptycreatecontainer') |
761 |
$("#create").addClass('emptycreate') |
762 |
$("#emptymachineslist").fadeIn("fast"); |
763 |
$("#createbody").fadeIn("fast"); |
764 |
$("#create").css("display", "block"); |
765 |
} |
766 |
|
767 |
// hide the welcome screen
|
768 |
function hideWelcome() { |
769 |
$("#emptymachineslist").fadeOut("fast"); |
770 |
$("#createbody").fadeOut("fast"); |
771 |
$("#createcontainer").removeClass('emptycreatecontainer') |
772 |
$("#create").removeClass('emptycreate') |
773 |
$("#view-select").fadeIn("fast"); |
774 |
$("#machinesview.standard").fadeIn("fast"); |
775 |
$("div#view-select").show(); |
776 |
$("#create").css("display", "inline"); |
777 |
} |
778 |
|