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