root / snf-cyclades-app / synnefo / ui / static / snf / js / models.js @ a45b30a7
History | View | Annotate | Download (87.9 kB)
1 |
// Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
//
|
3 |
// Redistribution and use in source and binary forms, with or
|
4 |
// without modification, are permitted provided that the following
|
5 |
// conditions are met:
|
6 |
//
|
7 |
// 1. Redistributions of source code must retain the above
|
8 |
// copyright notice, this list of conditions and the following
|
9 |
// disclaimer.
|
10 |
//
|
11 |
// 2. Redistributions in binary form must reproduce the above
|
12 |
// copyright notice, this list of conditions and the following
|
13 |
// disclaimer in the documentation and/or other materials
|
14 |
// provided with the distribution.
|
15 |
//
|
16 |
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
// POSSIBILITY OF SUCH DAMAGE.
|
28 |
//
|
29 |
// The views and conclusions contained in the software and
|
30 |
// documentation are those of the authors and should not be
|
31 |
// interpreted as representing official policies, either expressed
|
32 |
// or implied, of GRNET S.A.
|
33 |
//
|
34 |
|
35 |
;(function(root){
|
36 |
|
37 |
// root
|
38 |
var root = root;
|
39 |
|
40 |
// setup namepsaces
|
41 |
var snf = root.synnefo = root.synnefo || {};
|
42 |
var models = snf.models = snf.models || {}
|
43 |
var storage = snf.storage = snf.storage || {};
|
44 |
var util = snf.util = snf.util || {};
|
45 |
|
46 |
// shortcuts
|
47 |
var bb = root.Backbone;
|
48 |
var slice = Array.prototype.slice
|
49 |
|
50 |
// logging
|
51 |
var logger = new snf.logging.logger("SNF-MODELS"); |
52 |
var debug = _.bind(logger.debug, logger);
|
53 |
|
54 |
// get url helper
|
55 |
var getUrl = function(baseurl) { |
56 |
var baseurl = baseurl || snf.config.api_urls[this.api_type]; |
57 |
var append = "/"; |
58 |
if (baseurl.split("").reverse()[0] == "/") { |
59 |
append = "";
|
60 |
} |
61 |
return baseurl + append + this.path; |
62 |
} |
63 |
|
64 |
// i18n
|
65 |
BUILDING_MESSAGES = window.BUILDING_MESSAGES || {'INIT': 'init', 'COPY': '{0}, {1}, {2}', 'FINAL': 'final'}; |
66 |
|
67 |
// Base object for all our models
|
68 |
models.Model = bb.Model.extend({ |
69 |
sync: snf.api.sync,
|
70 |
api: snf.api,
|
71 |
api_type: 'compute', |
72 |
has_status: false, |
73 |
auto_bind: [],
|
74 |
|
75 |
|
76 |
initialize: function() { |
77 |
var self = this; |
78 |
|
79 |
this._proxy_model_cache = {};
|
80 |
_.each(this.auto_bind, function(fname) { |
81 |
self[fname] = _.bind(self[fname], self); |
82 |
}); |
83 |
|
84 |
if (this.has_status) { |
85 |
this.bind("change:status", this.handle_remove); |
86 |
this.handle_remove();
|
87 |
} |
88 |
|
89 |
this.api_call = _.bind(this.api.call, this); |
90 |
|
91 |
if (this.proxy_attrs) { |
92 |
this.init_proxy_attrs();
|
93 |
} |
94 |
|
95 |
if (this.storage_attrs) { |
96 |
this.init_storage_attrs();
|
97 |
} |
98 |
|
99 |
if (this.model_actions) { |
100 |
this.init_model_actions();
|
101 |
} |
102 |
|
103 |
models.Model.__super__.initialize.apply(this, arguments); |
104 |
|
105 |
}, |
106 |
|
107 |
// Initialize model actions object
|
108 |
// For each entry in model's model_action object register the relevant
|
109 |
// model proxy `can_<actionname>` attributes.
|
110 |
init_model_actions: function() { |
111 |
var actions = _.keys(this.model_actions); |
112 |
this.set({
|
113 |
"actions": new models._ActionsModel({}, { |
114 |
actions: actions,
|
115 |
model: this |
116 |
}) |
117 |
}); |
118 |
this.actions = this.get("actions"); |
119 |
this.actions.bind("set-pending", function(action) { |
120 |
this.trigger("action:set-pending", action, this.actions, this); |
121 |
}, this);
|
122 |
this.actions.bind("unset-pending", function(action) { |
123 |
this.trigger("action:unset-pending", action, this.actions, this); |
124 |
}, this);
|
125 |
this.actions.bind("reset-pending", function() { |
126 |
this.trigger("action:reset-pending", this.actions, this); |
127 |
}, this);
|
128 |
|
129 |
_.each(this.model_actions, function(params, key){ |
130 |
var attr = 'can_' + key; |
131 |
if (params.length == 0) { return } |
132 |
var deps = params[0]; |
133 |
var cb = _.bind(params[1], this); |
134 |
_.each(deps, function(dep) {
|
135 |
this._set_proxy_attr(attr, dep, cb);
|
136 |
}, this);
|
137 |
}, this);
|
138 |
}, |
139 |
|
140 |
// Initialize proxy storage model attributes. These attribues allows
|
141 |
// us to automatically access cross collection associated objects.
|
142 |
init_storage_attrs: function() { |
143 |
_.each(this.storage_attrs, function(params, attr) { |
144 |
var store, key, attr_name;
|
145 |
store = synnefo.storage[params[0]];
|
146 |
key = params[1];
|
147 |
attr_resolver = params[2];
|
148 |
if (!attr_resolver) {
|
149 |
attr_resolver = function(model, attr) { |
150 |
return model.get(attr);
|
151 |
} |
152 |
} |
153 |
attr_name = attr; |
154 |
|
155 |
var resolve_related_instance = function(storage, attr_name, val) { |
156 |
var data = {};
|
157 |
|
158 |
if (!val) {
|
159 |
// update with undefined and return
|
160 |
data[key] = undefined;
|
161 |
this.set(data);
|
162 |
return;
|
163 |
}; |
164 |
|
165 |
// retrieve related object (check if its a Model??)
|
166 |
var obj = store.get(val);
|
167 |
|
168 |
if (obj) {
|
169 |
// set related object
|
170 |
data[attr_name] = obj; |
171 |
this.set(data, {silent:true}) |
172 |
this.trigger("change:" + attr_name, obj); |
173 |
} else {
|
174 |
var self = this; |
175 |
var retry_to_resolve = function(store, val, key) { |
176 |
var retries = 0; |
177 |
var retry = window.setInterval(function(){ |
178 |
retries++; |
179 |
if (retries > 200) { |
180 |
clearInterval(retry); |
181 |
} |
182 |
var obj = store.get(val);
|
183 |
if (obj) {
|
184 |
data[key] = obj; |
185 |
self.set(data, {silent:false}); |
186 |
clearInterval(retry); |
187 |
} |
188 |
}, 500);
|
189 |
return retry
|
190 |
} |
191 |
retry_to_resolve(store, val, key); |
192 |
} |
193 |
} |
194 |
|
195 |
var self = this; |
196 |
function init_bindings(instance, store, key, attr, attr_resolver) { |
197 |
instance.bind('change:' + attr, function(model) { |
198 |
resolve_related_instance.call(model, store, key, attr_resolver(model, attr)); |
199 |
}, this);
|
200 |
|
201 |
instance.bind('add', function(model) { |
202 |
resolve_related_instance.call(model, store, key, attr_resolver(model, attr)); |
203 |
}, this);
|
204 |
} |
205 |
|
206 |
init_bindings(this, store, key, attr, attr_resolver);
|
207 |
resolve_related_instance.call(this, store, key, attr_resolver(this, attr)); |
208 |
}, this);
|
209 |
}, |
210 |
|
211 |
_proxy_model_cache: {},
|
212 |
|
213 |
_bind_model: function(model, attr, check_attr, cb) { |
214 |
var proxy_cache_key = attr + '_' + check_attr; |
215 |
if (this._proxy_model_cache[proxy_cache_key]) { |
216 |
var proxy = this._proxy_model_cache[proxy_cache_key]; |
217 |
proxy[0].unbind('change', proxy[1]); |
218 |
} |
219 |
var data = {};
|
220 |
var changebind = _.bind(function() { |
221 |
data[attr] = cb.call(this, this.get(check_attr)); |
222 |
this.set(data);
|
223 |
}, this);
|
224 |
model.bind('change', changebind);
|
225 |
this._proxy_model_cache[proxy_cache_key] = [model, changebind];
|
226 |
}, |
227 |
|
228 |
_bind_attr: function(attr, check_attr, cb) { |
229 |
this.bind('change:' + check_attr, function() { |
230 |
if (this.get(check_attr) instanceof models.Model) { |
231 |
var model = this.get(check_attr); |
232 |
this._bind_model(model, attr, check_attr, cb);
|
233 |
} |
234 |
var val = cb.call(this, this.get(check_attr)); |
235 |
var data = {};
|
236 |
if (this.get(attr) !== val) { |
237 |
data[attr] = val; |
238 |
this.set(data);
|
239 |
} |
240 |
}, this);
|
241 |
}, |
242 |
|
243 |
_set_proxy_attr: function(attr, check_attr, cb) { |
244 |
// initial set
|
245 |
var data = {};
|
246 |
data[attr] = cb.call(this, this.get(check_attr)); |
247 |
if (data[attr] !== undefined) { |
248 |
this.set(data, {silent:true}); |
249 |
} |
250 |
if(this.get(check_attr) instanceof models.Model) { |
251 |
this._bind_model(this.get(check_attr), attr, check_attr, cb); |
252 |
} |
253 |
this._bind_attr(attr, check_attr, cb);
|
254 |
}, |
255 |
|
256 |
init_proxy_attrs: function() { |
257 |
_.each(this.proxy_attrs, function(opts, attr){ |
258 |
var cb = opts[1]; |
259 |
_.each(opts[0], function(check_attr){ |
260 |
this._set_proxy_attr(attr, check_attr, cb)
|
261 |
}, this);
|
262 |
}, this);
|
263 |
}, |
264 |
|
265 |
handle_remove: function() { |
266 |
if (this.get("status") == 'DELETED') { |
267 |
if (this.collection) { |
268 |
try { this.clear_pending_action();} catch (err) {}; |
269 |
try { this.reset_pending_actions();} catch (err) {}; |
270 |
try { this.stop_stats_update();} catch (err) {}; |
271 |
this.collection.remove(this.id); |
272 |
} |
273 |
} |
274 |
}, |
275 |
|
276 |
// custom set method to allow submodels to use
|
277 |
// set_<attr> methods for handling the value of each
|
278 |
// attribute and overriding the default set method
|
279 |
// for specific parameters
|
280 |
set: function(params, options) { |
281 |
_.each(params, _.bind(function(value, key){
|
282 |
if (this["set_" + key]) { |
283 |
params[key] = this["set_" + key](value); |
284 |
} |
285 |
}, this))
|
286 |
var ret = bb.Model.prototype.set.call(this, params, options); |
287 |
return ret;
|
288 |
}, |
289 |
|
290 |
url: function(options) { |
291 |
return getUrl.call(this, this.base_url) + "/" + this.id; |
292 |
}, |
293 |
|
294 |
api_path: function(options) { |
295 |
return this.path + "/" + this.id; |
296 |
}, |
297 |
|
298 |
parse: function(resp, xhr) { |
299 |
}, |
300 |
|
301 |
remove: function(complete, error, success) { |
302 |
this.api_call(this.api_path(), "delete", undefined, complete, error, success); |
303 |
}, |
304 |
|
305 |
changedKeys: function() { |
306 |
return _.keys(this.changedAttributes() || {}); |
307 |
}, |
308 |
|
309 |
// return list of changed attributes that included in passed list
|
310 |
// argument
|
311 |
getKeysChanged: function(keys) { |
312 |
return _.intersection(keys, this.changedKeys()); |
313 |
}, |
314 |
|
315 |
// boolean check of keys changed
|
316 |
keysChanged: function(keys) { |
317 |
return this.getKeysChanged(keys).length > 0; |
318 |
}, |
319 |
|
320 |
// check if any of the passed attribues has changed
|
321 |
hasOnlyChange: function(keys) { |
322 |
var ret = false; |
323 |
_.each(keys, _.bind(function(key) {
|
324 |
if (this.changedKeys().length == 1 && this.changedKeys().indexOf(key) > -1) { ret = true}; |
325 |
}, this));
|
326 |
return ret;
|
327 |
} |
328 |
|
329 |
}) |
330 |
|
331 |
// Base object for all our model collections
|
332 |
models.Collection = bb.Collection.extend({ |
333 |
sync: snf.api.sync,
|
334 |
api: snf.api,
|
335 |
api_type: 'compute', |
336 |
supportIncUpdates: true, |
337 |
|
338 |
initialize: function() { |
339 |
models.Collection.__super__.initialize.apply(this, arguments); |
340 |
this.api_call = _.bind(this.api.call, this); |
341 |
if (this.sortFields) { |
342 |
_.each(this.sortFields, function(f) { |
343 |
this.bind("change:" + f, _.bind(this.resort, this)); |
344 |
}, this);
|
345 |
} |
346 |
}, |
347 |
|
348 |
resort: function() { |
349 |
this.sort();
|
350 |
}, |
351 |
|
352 |
url: function(options, method) { |
353 |
return getUrl.call(this, this.base_url) + ( |
354 |
options.details || this.details && method != 'create' ? '/detail' : ''); |
355 |
}, |
356 |
|
357 |
fetch: function(options) { |
358 |
if (!options) { options = {} };
|
359 |
// default to update
|
360 |
if (!this.noUpdate) { |
361 |
if (options.update === undefined) { options.update = true }; |
362 |
if (!options.removeMissing && options.refresh) {
|
363 |
options.removeMissing = true;
|
364 |
}; |
365 |
// for collections which associated models don't support
|
366 |
// deleted state identification through attributes, resolve
|
367 |
// deleted entries by checking for missing objects in fetch
|
368 |
// responses.
|
369 |
if (this.updateEntries && options.removeMissing === undefined) { |
370 |
options.removeMissing = true;
|
371 |
} |
372 |
} else {
|
373 |
if (options.refresh === undefined) { |
374 |
options.refresh = true;
|
375 |
if (this.updateEntries) { |
376 |
options.update = true;
|
377 |
options.removeMissing = true;
|
378 |
} |
379 |
} |
380 |
} |
381 |
// custom event foreach fetch
|
382 |
return bb.Collection.prototype.fetch.call(this, options) |
383 |
}, |
384 |
|
385 |
create: function(model, options) { |
386 |
var coll = this; |
387 |
options || (options = {}); |
388 |
model = this._prepareModel(model, options);
|
389 |
if (!model) return false; |
390 |
var success = options.success;
|
391 |
options.success = function(nextModel, resp, xhr) { |
392 |
if (coll.add_on_create) {
|
393 |
coll.add(nextModel, options); |
394 |
} |
395 |
if (success) success(nextModel, resp, xhr);
|
396 |
}; |
397 |
model.save(null, options);
|
398 |
return model;
|
399 |
}, |
400 |
|
401 |
get_fetcher: function(interval, increase, fast, increase_after_calls, |
402 |
max, initial_call, params, fetcher_id) { |
403 |
var fetch_params = params || {};
|
404 |
var handler_options = {};
|
405 |
|
406 |
fetch_params.skips_timeouts = true;
|
407 |
handler_options.interval = interval; |
408 |
handler_options.increase = increase; |
409 |
handler_options.fast = fast; |
410 |
handler_options.increase_after_calls = increase_after_calls; |
411 |
handler_options.max= max; |
412 |
handler_options.id = fetcher_id || "collection";
|
413 |
|
414 |
var last_ajax = undefined; |
415 |
var callback = _.bind(function() { |
416 |
// clone to avoid referenced objects
|
417 |
var params = _.clone(fetch_params);
|
418 |
updater._ajax = last_ajax; |
419 |
|
420 |
// wait for previous request to finish
|
421 |
if (last_ajax && last_ajax.readyState < 4 && last_ajax.statusText != "timeout") { |
422 |
// opera readystate for 304 responses is 0
|
423 |
if (!($.browser.opera && last_ajax.readyState == 0 && last_ajax.status == 304)) { |
424 |
return;
|
425 |
} |
426 |
} |
427 |
last_ajax = this.fetch(params);
|
428 |
}, this);
|
429 |
handler_options.callback = callback; |
430 |
|
431 |
var updater = new snf.api.updateHandler(_.clone(_.extend(handler_options, fetch_params))); |
432 |
snf.api.bind("call", _.throttle(_.bind(function(){ updater.faster(true)}, this)), 1000); |
433 |
return updater;
|
434 |
} |
435 |
}); |
436 |
|
437 |
// Image model
|
438 |
models.Image = models.Model.extend({ |
439 |
path: 'images', |
440 |
|
441 |
get_size: function() { |
442 |
return parseInt(this.get('metadata') ? this.get('metadata').size : -1) |
443 |
}, |
444 |
|
445 |
get_description: function(escape) { |
446 |
if (escape == undefined) { escape = true }; |
447 |
if (escape) { return this.escape('description') || "No description available"} |
448 |
return this.get('description') || "No description available." |
449 |
}, |
450 |
|
451 |
get_meta: function(key) { |
452 |
if (this.get('metadata') && this.get('metadata')) { |
453 |
if (!this.get('metadata')[key]) { return null } |
454 |
return _.escape(this.get('metadata')[key]); |
455 |
} else {
|
456 |
return null; |
457 |
} |
458 |
}, |
459 |
|
460 |
get_meta_keys: function() { |
461 |
if (this.get('metadata') && this.get('metadata')) { |
462 |
return _.keys(this.get('metadata')); |
463 |
} else {
|
464 |
return [];
|
465 |
} |
466 |
}, |
467 |
|
468 |
get_owner: function() { |
469 |
return this.get('owner') || _.keys(synnefo.config.system_images_owners)[0]; |
470 |
}, |
471 |
|
472 |
get_owner_uuid: function() { |
473 |
return this.get('owner_uuid'); |
474 |
}, |
475 |
|
476 |
is_system_image: function() { |
477 |
var owner = this.get_owner(); |
478 |
return _.include(_.keys(synnefo.config.system_images_owners), owner)
|
479 |
}, |
480 |
|
481 |
owned_by: function(user) { |
482 |
if (!user) { user = synnefo.user }
|
483 |
return user.get_username() == this.get('owner_uuid'); |
484 |
}, |
485 |
|
486 |
display_owner: function() { |
487 |
var owner = this.get_owner(); |
488 |
if (_.include(_.keys(synnefo.config.system_images_owners), owner)) {
|
489 |
return synnefo.config.system_images_owners[owner];
|
490 |
} else {
|
491 |
return owner;
|
492 |
} |
493 |
}, |
494 |
|
495 |
get_readable_size: function() { |
496 |
if (this.is_deleted()) { |
497 |
return synnefo.config.image_deleted_size_title || '(none)'; |
498 |
} |
499 |
return this.get_size() > 0 ? util.readablizeBytes(this.get_size() * 1024 * 1024) : '(none)'; |
500 |
}, |
501 |
|
502 |
get_os: function() { |
503 |
return this.get_meta('OS'); |
504 |
}, |
505 |
|
506 |
get_gui: function() { |
507 |
return this.get_meta('GUI'); |
508 |
}, |
509 |
|
510 |
get_created_users: function() { |
511 |
try {
|
512 |
var users = this.get_meta('users').split(" "); |
513 |
} catch (err) { users = null } |
514 |
if (!users) {
|
515 |
var osfamily = this.get_meta('osfamily'); |
516 |
if (osfamily == 'windows') { |
517 |
users = ['Administrator'];
|
518 |
} else {
|
519 |
users = ['root'];
|
520 |
} |
521 |
} |
522 |
return users;
|
523 |
}, |
524 |
|
525 |
get_sort_order: function() { |
526 |
return parseInt(this.get('metadata') ? this.get('metadata').sortorder : -1) |
527 |
}, |
528 |
|
529 |
get_vm: function() { |
530 |
var vm_id = this.get("serverRef"); |
531 |
var vm = undefined; |
532 |
vm = storage.vms.get(vm_id); |
533 |
return vm;
|
534 |
}, |
535 |
|
536 |
is_public: function() { |
537 |
return this.get('is_public') == undefined ? true : this.get('is_public'); |
538 |
}, |
539 |
|
540 |
is_deleted: function() { |
541 |
return this.get('status') == "DELETED" |
542 |
}, |
543 |
|
544 |
ssh_keys_paths: function() { |
545 |
return _.map(this.get_created_users(), function(username) { |
546 |
prepend = '';
|
547 |
if (username != 'root') { |
548 |
prepend = '/home'
|
549 |
} |
550 |
return {'user': username, 'path': '{1}/{0}/.ssh/authorized_keys'.format(username, |
551 |
prepend)}; |
552 |
}); |
553 |
}, |
554 |
|
555 |
_supports_ssh: function() { |
556 |
var exclude_list = synnefo.config.ssh_support_osfamily_exclude_list || [];
|
557 |
var os = this.get_os(); |
558 |
if (exclude_list.indexOf(os) > -1) { |
559 |
return false; |
560 |
} |
561 |
return true; |
562 |
}, |
563 |
|
564 |
supports: function(feature) { |
565 |
if (feature == "ssh") { |
566 |
return this._supports_ssh() |
567 |
} |
568 |
return false; |
569 |
}, |
570 |
|
571 |
personality_data_for_keys: function(keys) { |
572 |
return _.map(this.ssh_keys_paths(), function(pathinfo) { |
573 |
var contents = ''; |
574 |
_.each(keys, function(key){
|
575 |
contents = contents + key.get("content") + "\n" |
576 |
}); |
577 |
contents = $.base64.encode(contents);
|
578 |
|
579 |
return {
|
580 |
path: pathinfo.path,
|
581 |
contents: contents,
|
582 |
mode: 0600, |
583 |
owner: pathinfo.user
|
584 |
} |
585 |
}); |
586 |
} |
587 |
}); |
588 |
|
589 |
// Flavor model
|
590 |
models.Flavor = models.Model.extend({ |
591 |
path: 'flavors', |
592 |
|
593 |
details_string: function() { |
594 |
return "{0} CPU, {1}MB, {2}GB".format(this.get('cpu'), this.get('ram'), this.get('disk')); |
595 |
}, |
596 |
|
597 |
get_disk_size: function() { |
598 |
return parseInt(this.get("disk") * 1024) |
599 |
}, |
600 |
|
601 |
get_ram_size: function() { |
602 |
return parseInt(this.get("ram")) |
603 |
}, |
604 |
|
605 |
get_disk_template_info: function() { |
606 |
var info = snf.config.flavors_disk_templates_info[this.get("disk_template")]; |
607 |
if (!info) {
|
608 |
info = { name: this.get("disk_template"), description:'' }; |
609 |
} |
610 |
return info
|
611 |
}, |
612 |
|
613 |
disk_to_bytes: function() { |
614 |
return parseInt(this.get("disk")) * 1024 * 1024 * 1024; |
615 |
}, |
616 |
|
617 |
ram_to_bytes: function() { |
618 |
return parseInt(this.get("ram")) * 1024 * 1024; |
619 |
}, |
620 |
|
621 |
}); |
622 |
|
623 |
models.ParamsList = function(){this.initialize.apply(this, arguments)}; |
624 |
_.extend(models.ParamsList.prototype, bb.Events, { |
625 |
|
626 |
initialize: function(parent, param_name) { |
627 |
this.parent = parent;
|
628 |
this.actions = {};
|
629 |
this.param_name = param_name;
|
630 |
this.length = 0; |
631 |
}, |
632 |
|
633 |
has_action: function(action) { |
634 |
return this.actions[action] ? true : false; |
635 |
}, |
636 |
|
637 |
_parse_params: function(arguments) { |
638 |
if (arguments.length <= 1) { |
639 |
return [];
|
640 |
} |
641 |
|
642 |
var args = _.toArray(arguments); |
643 |
return args.splice(1); |
644 |
}, |
645 |
|
646 |
contains: function(action, params) { |
647 |
params = this._parse_params(arguments); |
648 |
var has_action = this.has_action(action); |
649 |
if (!has_action) { return false }; |
650 |
|
651 |
var paramsEqual = false; |
652 |
_.each(this.actions[action], function(action_params) { |
653 |
if (_.isEqual(action_params, params)) {
|
654 |
paramsEqual = true;
|
655 |
} |
656 |
}); |
657 |
|
658 |
return paramsEqual;
|
659 |
}, |
660 |
|
661 |
is_empty: function() { |
662 |
return _.isEmpty(this.actions); |
663 |
}, |
664 |
|
665 |
add: function(action, params) { |
666 |
params = this._parse_params(arguments); |
667 |
if (this.contains.apply(this, arguments)) { return this }; |
668 |
var isnew = false |
669 |
if (!this.has_action(action)) { |
670 |
this.actions[action] = [];
|
671 |
isnew = true;
|
672 |
}; |
673 |
|
674 |
this.actions[action].push(params);
|
675 |
this.parent.trigger("change:" + this.param_name, this.parent, this); |
676 |
if (isnew) {
|
677 |
this.trigger("add", action, params); |
678 |
} else {
|
679 |
this.trigger("change", action, params); |
680 |
} |
681 |
return this; |
682 |
}, |
683 |
|
684 |
remove_all: function(action) { |
685 |
if (this.has_action(action)) { |
686 |
delete this.actions[action]; |
687 |
this.parent.trigger("change:" + this.param_name, this.parent, this); |
688 |
this.trigger("remove", action); |
689 |
} |
690 |
return this; |
691 |
}, |
692 |
|
693 |
reset: function() { |
694 |
this.actions = {};
|
695 |
this.parent.trigger("change:" + this.param_name, this.parent, this); |
696 |
this.trigger("reset"); |
697 |
this.trigger("remove"); |
698 |
}, |
699 |
|
700 |
remove: function(action, params) { |
701 |
params = this._parse_params(arguments); |
702 |
if (!this.has_action(action)) { return this }; |
703 |
var index = -1; |
704 |
_.each(this.actions[action], _.bind(function(action_params) { |
705 |
if (_.isEqual(action_params, params)) {
|
706 |
index = this.actions[action].indexOf(action_params);
|
707 |
} |
708 |
}, this));
|
709 |
|
710 |
if (index > -1) { |
711 |
this.actions[action].splice(index, 1); |
712 |
if (_.isEmpty(this.actions[action])) { |
713 |
delete this.actions[action]; |
714 |
} |
715 |
this.parent.trigger("change:" + this.param_name, this.parent, this); |
716 |
this.trigger("remove", action, params); |
717 |
} |
718 |
} |
719 |
|
720 |
}); |
721 |
|
722 |
// Virtualmachine model
|
723 |
models.VM = models.Model.extend({ |
724 |
|
725 |
path: 'servers', |
726 |
has_status: true, |
727 |
proxy_attrs: {
|
728 |
'busy': [
|
729 |
['status', 'state'], function() { |
730 |
return !_.contains(['ACTIVE', 'STOPPED'], this.get('status')); |
731 |
} |
732 |
], |
733 |
'in_progress': [
|
734 |
['status', 'state'], function() { |
735 |
return this.in_transition(); |
736 |
} |
737 |
] |
738 |
}, |
739 |
|
740 |
initialize: function(params) { |
741 |
var self = this; |
742 |
this.ports = new Backbone.FilteredCollection(undefined, { |
743 |
collection: synnefo.storage.ports,
|
744 |
collectionFilter: function(m) { |
745 |
return self.id == m.get('device_id') |
746 |
}}); |
747 |
|
748 |
this.pending_firewalls = {};
|
749 |
|
750 |
models.VM.__super__.initialize.apply(this, arguments); |
751 |
|
752 |
|
753 |
this.set({state: params.status || "ERROR"}); |
754 |
this.log = new snf.logging.logger("VM " + this.id); |
755 |
this.pending_action = undefined; |
756 |
|
757 |
// init stats parameter
|
758 |
this.set({'stats': undefined}, {silent: true}); |
759 |
// defaults to not update the stats
|
760 |
// each view should handle this vm attribute
|
761 |
// depending on if it displays stat images or not
|
762 |
this.do_update_stats = false; |
763 |
|
764 |
// interval time
|
765 |
// this will dynamicaly change if the server responds that
|
766 |
// images get refreshed on different intervals
|
767 |
this.stats_update_interval = synnefo.config.STATS_INTERVAL || 5000; |
768 |
this.stats_available = false; |
769 |
|
770 |
// initialize interval
|
771 |
this.init_stats_intervals(this.stats_update_interval); |
772 |
|
773 |
// handle progress message on instance change
|
774 |
this.bind("change", _.bind(this.update_status_message, this)); |
775 |
this.bind("change:task_state", _.bind(this.update_status, this)); |
776 |
// force update of progress message
|
777 |
this.update_status_message(true); |
778 |
|
779 |
// default values
|
780 |
this.bind("change:state", _.bind(function(){ |
781 |
if (this.state() == "DESTROY") { |
782 |
this.handle_destroy()
|
783 |
} |
784 |
}, this));
|
785 |
|
786 |
}, |
787 |
|
788 |
get_public_ips: function() { |
789 |
var ips = [];
|
790 |
this.ports.filter(function(port) { |
791 |
if (port.get('network') && !port.get('network').get('is_public')) { return } |
792 |
if (!port.get("ips")) { return } |
793 |
port.get("ips").each(function(ip) { |
794 |
ips.push(ip); |
795 |
}); |
796 |
}); |
797 |
return ips;
|
798 |
}, |
799 |
|
800 |
has_public_ip: function() { |
801 |
return this.ports.filter(function(port) { |
802 |
return port.get("network") && |
803 |
port.get("network").get("is_public") && |
804 |
port.get("ips").length > 0; |
805 |
}).length > 0;
|
806 |
}, |
807 |
|
808 |
has_public_ipv6: function() { |
809 |
return this.has_ip_version("v6", true); |
810 |
}, |
811 |
|
812 |
has_public_ipv4: function() { |
813 |
return this.has_ip_version("v4", true); |
814 |
}, |
815 |
|
816 |
has_ip_version: function(ver, public) { |
817 |
var found = false; |
818 |
this.ports.each(function(port) { |
819 |
if (found) { return } |
820 |
if (public !== undefined) { |
821 |
if (port.get("network") && |
822 |
port.get("network").get("is_public") != public) { |
823 |
return
|
824 |
} |
825 |
} |
826 |
port.get('ips').each(function(ip) { |
827 |
if (found) { return } |
828 |
if (ip.get("type") == ver) { |
829 |
found = true
|
830 |
} |
831 |
}) |
832 |
}, this)
|
833 |
return found;
|
834 |
}, |
835 |
|
836 |
status: function(st) { |
837 |
if (!st) { return this.get("status")} |
838 |
return this.set({status:st}); |
839 |
}, |
840 |
|
841 |
update_status: function() { |
842 |
this.set_status(this.get('status')); |
843 |
}, |
844 |
|
845 |
set_status: function(st) { |
846 |
var new_state = this.state_for_api_status(st); |
847 |
var transition = false; |
848 |
|
849 |
if (this.state() != new_state) { |
850 |
if (models.VM.STATES_TRANSITIONS[this.state()]) { |
851 |
transition = this.state();
|
852 |
} |
853 |
} |
854 |
|
855 |
// call it silently to avoid double change trigger
|
856 |
var state = this.state_for_api_status(st); |
857 |
this.set({'state': state}, {silent: true}); |
858 |
|
859 |
// trigger transition
|
860 |
if (transition && models.VM.TRANSITION_STATES.indexOf(new_state) == -1) { |
861 |
this.trigger("transition", {from:transition, to:new_state}) |
862 |
}; |
863 |
return st;
|
864 |
}, |
865 |
|
866 |
get_diagnostics: function(success) { |
867 |
this.__make_api_call(this.get_diagnostics_url(), |
868 |
"read", // create so that sync later uses POST to make the call |
869 |
null, // payload |
870 |
function(data) {
|
871 |
success(data); |
872 |
}, |
873 |
null, 'diagnostics'); |
874 |
}, |
875 |
|
876 |
has_diagnostics: function() { |
877 |
return this.get("diagnostics") && this.get("diagnostics").length; |
878 |
}, |
879 |
|
880 |
get_progress_info: function() { |
881 |
// details about progress message
|
882 |
// contains a list of diagnostic messages
|
883 |
return this.get("status_messages"); |
884 |
}, |
885 |
|
886 |
get_status_message: function() { |
887 |
return this.get('status_message'); |
888 |
}, |
889 |
|
890 |
// extract status message from diagnostics
|
891 |
status_message_from_diagnostics: function(diagnostics) { |
892 |
var valid_sources_map = synnefo.config.diagnostics_status_messages_map;
|
893 |
var valid_sources = valid_sources_map[this.get('status')]; |
894 |
if (!valid_sources) { return null }; |
895 |
|
896 |
// filter messsages based on diagnostic source
|
897 |
var messages = _.filter(diagnostics, function(diag) { |
898 |
return valid_sources.indexOf(diag.source) > -1; |
899 |
}); |
900 |
|
901 |
var msg = messages[0]; |
902 |
if (msg) {
|
903 |
var message = msg.message;
|
904 |
var message_tpl = snf.config.diagnostic_messages_tpls[msg.source];
|
905 |
|
906 |
if (message_tpl) {
|
907 |
message = message_tpl.replace('MESSAGE', msg.message);
|
908 |
} |
909 |
return message;
|
910 |
} |
911 |
|
912 |
// no message to display, but vm in build state, display
|
913 |
// finalizing message.
|
914 |
if (this.is_building() == 'BUILD') { |
915 |
return synnefo.config.BUILDING_MESSAGES['FINAL']; |
916 |
} |
917 |
return null; |
918 |
}, |
919 |
|
920 |
update_status_message: function(force) { |
921 |
// update only if one of the specified attributes has changed
|
922 |
if (
|
923 |
!this.keysChanged(['diagnostics', 'progress', 'status', 'state']) |
924 |
&& !force |
925 |
) { return };
|
926 |
|
927 |
// if user requested to destroy the vm set the appropriate
|
928 |
// message.
|
929 |
if (this.get('state') == "DESTROY") { |
930 |
message = "Terminating..."
|
931 |
this.set({status_message: message}) |
932 |
return;
|
933 |
} |
934 |
|
935 |
// set error message, if vm has diagnostic message display it as
|
936 |
// progress message
|
937 |
if (this.in_error_state()) { |
938 |
var d = this.get('diagnostics'); |
939 |
if (d && d.length) {
|
940 |
var message = this.status_message_from_diagnostics(d); |
941 |
this.set({status_message: message}); |
942 |
} else {
|
943 |
this.set({status_message: null}); |
944 |
} |
945 |
return;
|
946 |
} |
947 |
|
948 |
// identify building status message
|
949 |
if (this.is_building()) { |
950 |
var self = this; |
951 |
var success = function(msg) { |
952 |
self.set({status_message: msg});
|
953 |
} |
954 |
this.get_building_status_message(success);
|
955 |
return;
|
956 |
} |
957 |
|
958 |
this.set({status_message:null}); |
959 |
}, |
960 |
|
961 |
// get building status message. Asynchronous function since it requires
|
962 |
// access to vm image.
|
963 |
get_building_status_message: function(callback) { |
964 |
// no progress is set, vm is in initial build status
|
965 |
var progress = this.get("progress"); |
966 |
if (progress == 0 || !progress) { |
967 |
return callback(BUILDING_MESSAGES['INIT']); |
968 |
} |
969 |
|
970 |
// vm has copy progress, display copy percentage
|
971 |
if (progress > 0 && progress <= 99) { |
972 |
this.get_copy_details(true, undefined, _.bind( |
973 |
function(details){
|
974 |
callback(BUILDING_MESSAGES['COPY'].format(details.copy,
|
975 |
details.size, |
976 |
details.progress)); |
977 |
}, this));
|
978 |
return;
|
979 |
} |
980 |
|
981 |
// copy finished display FINAL message or identify status message
|
982 |
// from diagnostics.
|
983 |
if (progress >= 100) { |
984 |
if (!this.has_diagnostics()) { |
985 |
callback(BUILDING_MESSAGES['FINAL']);
|
986 |
} else {
|
987 |
var d = this.get("diagnostics"); |
988 |
var msg = this.status_message_from_diagnostics(d); |
989 |
if (msg) {
|
990 |
callback(msg); |
991 |
} |
992 |
} |
993 |
} |
994 |
}, |
995 |
|
996 |
get_copy_details: function(human, image, callback) { |
997 |
var human = human || false; |
998 |
var image = image || this.get_image(_.bind(function(image){ |
999 |
var progress = this.get('progress'); |
1000 |
var size = image.get_size();
|
1001 |
var size_copied = (size * progress / 100).toFixed(2); |
1002 |
|
1003 |
if (human) {
|
1004 |
size = util.readablizeBytes(size*1024*1024); |
1005 |
size_copied = util.readablizeBytes(size_copied*1024*1024); |
1006 |
} |
1007 |
|
1008 |
callback({'progress': progress, 'size': size, 'copy': size_copied}) |
1009 |
}, this));
|
1010 |
}, |
1011 |
|
1012 |
start_stats_update: function(force_if_empty) { |
1013 |
var prev_state = this.do_update_stats; |
1014 |
|
1015 |
this.do_update_stats = true; |
1016 |
|
1017 |
// fetcher initialized ??
|
1018 |
if (!this.stats_fetcher) { |
1019 |
this.init_stats_intervals();
|
1020 |
} |
1021 |
|
1022 |
|
1023 |
// fetcher running ???
|
1024 |
if (!this.stats_fetcher.running || !prev_state) { |
1025 |
this.stats_fetcher.start();
|
1026 |
} |
1027 |
|
1028 |
if (force_if_empty && this.get("stats") == undefined) { |
1029 |
this.update_stats(true); |
1030 |
} |
1031 |
}, |
1032 |
|
1033 |
stop_stats_update: function(stop_calls) { |
1034 |
this.do_update_stats = false; |
1035 |
|
1036 |
if (stop_calls) {
|
1037 |
this.stats_fetcher.stop();
|
1038 |
} |
1039 |
}, |
1040 |
|
1041 |
// clear and reinitialize update interval
|
1042 |
init_stats_intervals: function (interval) { |
1043 |
this.stats_fetcher = this.get_stats_fetcher(this.stats_update_interval); |
1044 |
this.stats_fetcher.start();
|
1045 |
}, |
1046 |
|
1047 |
get_stats_fetcher: function(timeout) { |
1048 |
var cb = _.bind(function(data){ |
1049 |
this.update_stats();
|
1050 |
}, this);
|
1051 |
var fetcher = new snf.api.updateHandler({'callback': cb, interval: timeout, id:'stats'}); |
1052 |
return fetcher;
|
1053 |
}, |
1054 |
|
1055 |
// do the api call
|
1056 |
update_stats: function(force) { |
1057 |
// do not update stats if flag not set
|
1058 |
if ((!this.do_update_stats && !force) || this.updating_stats) { |
1059 |
return;
|
1060 |
} |
1061 |
|
1062 |
// make the api call, execute handle_stats_update on sucess
|
1063 |
// TODO: onError handler ???
|
1064 |
stats_url = this.url() + "/stats"; |
1065 |
this.updating_stats = true; |
1066 |
this.sync("read", this, { |
1067 |
handles_error:true, |
1068 |
url: stats_url,
|
1069 |
refresh:true, |
1070 |
success: _.bind(this.handle_stats_update, this), |
1071 |
error: _.bind(this.handle_stats_error, this), |
1072 |
complete: _.bind(function(){this.updating_stats = false;}, this), |
1073 |
critical: false, |
1074 |
log_error: false, |
1075 |
skips_timeouts: true |
1076 |
}); |
1077 |
}, |
1078 |
|
1079 |
get_attachment: function(id) { |
1080 |
var attachment = undefined; |
1081 |
_.each(this.get("attachments"), function(a) { |
1082 |
if (a.id == id) {
|
1083 |
attachment = a; |
1084 |
} |
1085 |
}); |
1086 |
return attachment
|
1087 |
}, |
1088 |
|
1089 |
_set_stats: function(stats) { |
1090 |
var silent = silent === undefined ? false : silent; |
1091 |
// unavailable stats while building
|
1092 |
if (this.get("status") == "BUILD") { |
1093 |
this.stats_available = false; |
1094 |
} else { this.stats_available = true; } |
1095 |
|
1096 |
if (this.get("status") == "DESTROY") { this.stats_available = false; } |
1097 |
|
1098 |
this.set({stats: stats}, {silent:true}); |
1099 |
this.trigger("stats:update", stats); |
1100 |
}, |
1101 |
|
1102 |
unbind: function() { |
1103 |
models.VM.__super__.unbind.apply(this, arguments); |
1104 |
}, |
1105 |
|
1106 |
can_start: function(flv, count_current) { |
1107 |
var get_quota = function(key) { |
1108 |
return synnefo.storage.quotas.get(key).get('available'); |
1109 |
} |
1110 |
var flavor = flv || this.get_flavor(); |
1111 |
var vm_ram_current = 0, vm_cpu_current = 0; |
1112 |
if (flv && this.is_active() || flv && count_current) { |
1113 |
var current = this.get_flavor(); |
1114 |
vm_ram_current = current.ram_to_bytes(); |
1115 |
vm_cpu_current = parseInt(current.get('cpu'));
|
1116 |
} |
1117 |
var vm_ram = flavor.ram_to_bytes();
|
1118 |
var vm_cpu = parseInt(flavor.get('cpu')); |
1119 |
var available_cpu = get_quota('cyclades.cpu') + vm_cpu_current; |
1120 |
var available_ram = get_quota('cyclades.ram') + vm_ram_current; |
1121 |
if (vm_ram > available_ram || vm_cpu > available_cpu) { return false } |
1122 |
return true |
1123 |
}, |
1124 |
|
1125 |
can_connect: function() { |
1126 |
if (!synnefo.config.hotplug_enabled && this.is_active()) { return false } |
1127 |
return _.contains(["ACTIVE", "STOPPED"], this.get("status")) && |
1128 |
!this.get('suspended') |
1129 |
}, |
1130 |
|
1131 |
can_disconnect: function() { |
1132 |
return _.contains(["ACTIVE", "STOPPED"], this.get("status")) |
1133 |
}, |
1134 |
|
1135 |
can_resize: function() { |
1136 |
return this.get('status') == 'STOPPED'; |
1137 |
}, |
1138 |
|
1139 |
handle_stats_error: function() { |
1140 |
stats = {}; |
1141 |
_.each(['cpuBar', 'cpuTimeSeries', 'netBar', 'netTimeSeries'], function(k) { |
1142 |
stats[k] = false;
|
1143 |
}); |
1144 |
|
1145 |
this.set({'stats': stats}); |
1146 |
}, |
1147 |
|
1148 |
// this method gets executed after a successful vm stats api call
|
1149 |
handle_stats_update: function(data) { |
1150 |
var self = this; |
1151 |
// avoid browser caching
|
1152 |
|
1153 |
if (data.stats && _.size(data.stats) > 0) { |
1154 |
var ts = $.now(); |
1155 |
var stats = data.stats;
|
1156 |
var images_loaded = 0; |
1157 |
var images = {};
|
1158 |
|
1159 |
function check_images_loaded() { |
1160 |
images_loaded++; |
1161 |
|
1162 |
if (images_loaded == 4) { |
1163 |
self._set_stats(images); |
1164 |
} |
1165 |
} |
1166 |
_.each(['cpuBar', 'cpuTimeSeries', 'netBar', 'netTimeSeries'], function(k) { |
1167 |
|
1168 |
stats[k] = stats[k] + "?_=" + ts;
|
1169 |
|
1170 |
var stat = k.slice(0,3); |
1171 |
var type = k.slice(3,6) == "Bar" ? "bar" : "time"; |
1172 |
var img = $("<img />"); |
1173 |
var val = stats[k];
|
1174 |
|
1175 |
// load stat image to a temporary dom element
|
1176 |
// update model stats on image load/error events
|
1177 |
img.load(function() {
|
1178 |
images[k] = val; |
1179 |
check_images_loaded(); |
1180 |
}); |
1181 |
|
1182 |
img.error(function() {
|
1183 |
images[stat + type] = false;
|
1184 |
check_images_loaded(); |
1185 |
}); |
1186 |
|
1187 |
img.attr({'src': stats[k]});
|
1188 |
}) |
1189 |
data.stats = stats; |
1190 |
} |
1191 |
|
1192 |
// do we need to change the interval ??
|
1193 |
if (data.stats.refresh * 1000 != this.stats_update_interval) { |
1194 |
this.stats_update_interval = data.stats.refresh * 1000; |
1195 |
this.stats_fetcher.interval = this.stats_update_interval; |
1196 |
this.stats_fetcher.maximum_interval = this.stats_update_interval; |
1197 |
this.stats_fetcher.stop();
|
1198 |
this.stats_fetcher.start(false); |
1199 |
} |
1200 |
}, |
1201 |
|
1202 |
// helper method that sets the do_update_stats
|
1203 |
// in the future this method could also make an api call
|
1204 |
// immediaetly if needed
|
1205 |
enable_stats_update: function() { |
1206 |
this.do_update_stats = true; |
1207 |
}, |
1208 |
|
1209 |
handle_destroy: function() { |
1210 |
this.stats_fetcher.stop();
|
1211 |
}, |
1212 |
|
1213 |
require_reboot: function() { |
1214 |
if (this.is_active()) { |
1215 |
this.set({'reboot_required': true}); |
1216 |
} |
1217 |
}, |
1218 |
|
1219 |
set_pending_action: function(data) { |
1220 |
this.pending_action = data;
|
1221 |
return data;
|
1222 |
}, |
1223 |
|
1224 |
// machine has pending action
|
1225 |
update_pending_action: function(action, force) { |
1226 |
this.set({pending_action: action}); |
1227 |
}, |
1228 |
|
1229 |
clear_pending_action: function() { |
1230 |
this.set({pending_action: undefined}); |
1231 |
}, |
1232 |
|
1233 |
has_pending_action: function() { |
1234 |
return this.get("pending_action") ? this.get("pending_action") : false; |
1235 |
}, |
1236 |
|
1237 |
// machine is active
|
1238 |
is_active: function() { |
1239 |
return models.VM.ACTIVE_STATES.indexOf(this.state()) > -1; |
1240 |
}, |
1241 |
|
1242 |
// machine is building
|
1243 |
is_building: function() { |
1244 |
return models.VM.BUILDING_STATES.indexOf(this.state()) > -1; |
1245 |
}, |
1246 |
|
1247 |
is_rebooting: function() { |
1248 |
return this.state() == 'REBOOT'; |
1249 |
}, |
1250 |
|
1251 |
in_error_state: function() { |
1252 |
return this.state() === "ERROR" |
1253 |
}, |
1254 |
|
1255 |
// user can connect to machine
|
1256 |
is_connectable: function() { |
1257 |
return models.VM.CONNECT_STATES.indexOf(this.state()) > -1; |
1258 |
}, |
1259 |
|
1260 |
remove_meta: function(key, complete, error) { |
1261 |
var url = this.api_path() + "/metadata/" + key; |
1262 |
this.api_call(url, "delete", undefined, complete, error); |
1263 |
}, |
1264 |
|
1265 |
save_meta: function(meta, complete, error) { |
1266 |
var url = this.api_path() + "/metadata/" + meta.key; |
1267 |
var payload = {meta:{}}; |
1268 |
payload.meta[meta.key] = meta.value; |
1269 |
payload._options = { |
1270 |
critical:false, |
1271 |
error_params: {
|
1272 |
title: "Machine metadata error", |
1273 |
extra_details: {"Machine id": this.id} |
1274 |
}}; |
1275 |
|
1276 |
this.api_call(url, "update", payload, complete, error); |
1277 |
}, |
1278 |
|
1279 |
|
1280 |
// update/get the state of the machine
|
1281 |
state: function() { |
1282 |
var args = slice.call(arguments); |
1283 |
|
1284 |
if (args.length > 0 && models.VM.STATES.indexOf(args[0]) > -1) { |
1285 |
this.set({'state': args[0]}); |
1286 |
} |
1287 |
|
1288 |
return this.get('state'); |
1289 |
}, |
1290 |
|
1291 |
// get the state that the api status corresponds to
|
1292 |
state_for_api_status: function(status) { |
1293 |
return this.state_transition(this.state(), status); |
1294 |
}, |
1295 |
|
1296 |
// get transition state for the corresponging api status
|
1297 |
state_transition: function(state, new_status) { |
1298 |
var statuses = models.VM.STATES_TRANSITIONS[state];
|
1299 |
if (statuses) {
|
1300 |
if (statuses.indexOf(new_status) > -1) { |
1301 |
return new_status;
|
1302 |
} else {
|
1303 |
return state;
|
1304 |
} |
1305 |
} else {
|
1306 |
return new_status;
|
1307 |
} |
1308 |
}, |
1309 |
|
1310 |
// the current vm state is a transition state
|
1311 |
in_transition: function() { |
1312 |
return models.VM.TRANSITION_STATES.indexOf(this.state()) > -1 || |
1313 |
models.VM.TRANSITION_STATES.indexOf(this.get('status')) > -1; |
1314 |
}, |
1315 |
|
1316 |
// get image object
|
1317 |
get_image: function(callback) { |
1318 |
if (callback == undefined) { callback = function(){} } |
1319 |
var image = storage.images.get(this.get('image')); |
1320 |
if (!image) {
|
1321 |
storage.images.update_unknown_id(this.get('image'), callback); |
1322 |
return;
|
1323 |
} |
1324 |
callback(image); |
1325 |
return image;
|
1326 |
}, |
1327 |
|
1328 |
// get flavor object
|
1329 |
get_flavor: function() { |
1330 |
var flv = storage.flavors.get(this.get('flavor')); |
1331 |
if (!flv) {
|
1332 |
storage.flavors.update_unknown_id(this.get('flavor')); |
1333 |
flv = storage.flavors.get(this.get('flavor')); |
1334 |
} |
1335 |
return flv;
|
1336 |
}, |
1337 |
|
1338 |
get_resize_flavors: function() { |
1339 |
var vm_flavor = this.get_flavor(); |
1340 |
var flavors = synnefo.storage.flavors.filter(function(f){ |
1341 |
return f.get('disk_template') == |
1342 |
vm_flavor.get('disk_template') && f.get('disk') == |
1343 |
vm_flavor.get('disk');
|
1344 |
}); |
1345 |
return flavors;
|
1346 |
}, |
1347 |
|
1348 |
get_flavor_quotas: function() { |
1349 |
var flavor = this.get_flavor(); |
1350 |
return {
|
1351 |
cpu: flavor.get('cpu'), |
1352 |
ram: flavor.get_ram_size(),
|
1353 |
disk:flavor.get_disk_size()
|
1354 |
} |
1355 |
}, |
1356 |
|
1357 |
get_meta: function(key, deflt) { |
1358 |
if (this.get('metadata') && this.get('metadata')) { |
1359 |
if (!this.get('metadata')[key]) { return deflt } |
1360 |
return _.escape(this.get('metadata')[key]); |
1361 |
} else {
|
1362 |
return deflt;
|
1363 |
} |
1364 |
}, |
1365 |
|
1366 |
get_meta_keys: function() { |
1367 |
if (this.get('metadata') && this.get('metadata')) { |
1368 |
return _.keys(this.get('metadata')); |
1369 |
} else {
|
1370 |
return [];
|
1371 |
} |
1372 |
}, |
1373 |
|
1374 |
// get metadata OS value
|
1375 |
get_os: function() { |
1376 |
var image = this.get_image(); |
1377 |
return this.get_meta('OS') || (image ? |
1378 |
image.get_os() || "okeanos" : "okeanos"); |
1379 |
}, |
1380 |
|
1381 |
get_gui: function() { |
1382 |
return this.get_meta('GUI'); |
1383 |
}, |
1384 |
|
1385 |
get_hostname: function() { |
1386 |
return this.get_meta('hostname') || this.get('fqdn') || synnefo.config.no_fqdn_message; |
1387 |
}, |
1388 |
|
1389 |
// get actions that the user can execute
|
1390 |
// depending on the vm state/status
|
1391 |
get_available_actions: function() { |
1392 |
if (this.get('suspended')) { return [] } |
1393 |
return models.VM.AVAILABLE_ACTIONS[this.state()]; |
1394 |
}, |
1395 |
|
1396 |
set_profile: function(profile, net_id) { |
1397 |
}, |
1398 |
|
1399 |
// call rename api
|
1400 |
rename: function(new_name) { |
1401 |
//this.set({'name': new_name});
|
1402 |
this.sync("update", this, { |
1403 |
critical: true, |
1404 |
data: {
|
1405 |
'server': {
|
1406 |
'name': new_name
|
1407 |
} |
1408 |
}, |
1409 |
success: _.bind(function(){ |
1410 |
snf.api.trigger("call");
|
1411 |
}, this)
|
1412 |
}); |
1413 |
}, |
1414 |
|
1415 |
get_console_url: function(data) { |
1416 |
var url_params = {
|
1417 |
machine: this.get("name"), |
1418 |
host_ip: this.get_hostname(), |
1419 |
host_ip_v6: this.get_hostname(), |
1420 |
host: data.host,
|
1421 |
port: data.port,
|
1422 |
password: data.password
|
1423 |
} |
1424 |
return synnefo.config.ui_console_url + '?' + $.param(url_params); |
1425 |
}, |
1426 |
|
1427 |
set_firewall: function(nic, value, success_cb, error_cb) { |
1428 |
var self = this; |
1429 |
var success = function() { self.require_reboot(); success_cb() } |
1430 |
var error = function() { error_cb() } |
1431 |
var data = {'nic': nic.id, 'profile': value, 'display': true}; |
1432 |
var url = this.url() + "/action"; |
1433 |
//var params = {skip_api_error: false, display: true};
|
1434 |
this.call('firewallProfile', success, error, data); |
1435 |
}, |
1436 |
|
1437 |
connect_floating_ip: function(ip, cb, error) { |
1438 |
var self = this; |
1439 |
var from_status = this.get('status'); |
1440 |
this.set({'status': 'CONNECTING'}); |
1441 |
synnefo.storage.ports.create({ |
1442 |
port: {
|
1443 |
network_id: ip.get('floating_network_id'), |
1444 |
device_id: this.id, |
1445 |
fixed_ips: [{'ip_address': ip.get('floating_ip_address')}] |
1446 |
} |
1447 |
}, { |
1448 |
success: cb,
|
1449 |
error: function() { error && error() }, |
1450 |
skip_api_error: false |
1451 |
}); |
1452 |
}, |
1453 |
|
1454 |
create_snapshot: function(snapshot_params, callback) { |
1455 |
var volume = this.get('volumes') && this.get('volumes').length ? |
1456 |
this.get('volumes')[0] : undefined; |
1457 |
var params = _.extend({
|
1458 |
'metadata': {},
|
1459 |
'volume_id': volume
|
1460 |
}, snapshot_params); |
1461 |
|
1462 |
snf.api.sync('create', undefined, { |
1463 |
url: synnefo.config.api_urls.volume + '/snapshots/', |
1464 |
data: JSON.stringify({snapshot:params}), |
1465 |
success: callback,
|
1466 |
skip_api_error: false, |
1467 |
contentType: 'application/json' |
1468 |
}); |
1469 |
}, |
1470 |
|
1471 |
// action helper
|
1472 |
call: function(action_name, success, error, params) { |
1473 |
var id_param = [this.id]; |
1474 |
|
1475 |
params = params || {}; |
1476 |
success = success || function() {};
|
1477 |
error = error || function() {};
|
1478 |
|
1479 |
var self = this; |
1480 |
|
1481 |
switch(action_name) {
|
1482 |
case 'start': |
1483 |
this.__make_api_call(this.get_action_url(), // vm actions url |
1484 |
"create", // create so that sync later uses POST to make the call |
1485 |
{start:{}}, // payload |
1486 |
function() {
|
1487 |
// set state after successful call
|
1488 |
self.state("START");
|
1489 |
success.apply(this, arguments); |
1490 |
snf.api.trigger("call");
|
1491 |
}, |
1492 |
error, 'start', params);
|
1493 |
break;
|
1494 |
case 'reboot': |
1495 |
this.__make_api_call(this.get_action_url(), // vm actions url |
1496 |
"create", // create so that sync later uses POST to make the call |
1497 |
{reboot:{}}, // payload |
1498 |
function() {
|
1499 |
// set state after successful call
|
1500 |
self.state("REBOOT");
|
1501 |
success.apply(this, arguments) |
1502 |
snf.api.trigger("call");
|
1503 |
self.set({'reboot_required': false}); |
1504 |
}, |
1505 |
error, 'reboot', params);
|
1506 |
break;
|
1507 |
case 'shutdown': |
1508 |
this.__make_api_call(this.get_action_url(), // vm actions url |
1509 |
"create", // create so that sync later uses POST to make the call |
1510 |
{shutdown:{}}, // payload |
1511 |
function() {
|
1512 |
// set state after successful call
|
1513 |
self.state("SHUTDOWN");
|
1514 |
success.apply(this, arguments) |
1515 |
snf.api.trigger("call");
|
1516 |
}, |
1517 |
error, 'shutdown', params);
|
1518 |
break;
|
1519 |
case 'console': |
1520 |
this.__make_api_call(this.url() + "/action", "create", |
1521 |
{'console': {'type':'vnc'}}, |
1522 |
function(data) {
|
1523 |
var cons_data = data.console;
|
1524 |
success.apply(this, [cons_data]);
|
1525 |
}, undefined, 'console', params) |
1526 |
break;
|
1527 |
case 'destroy': |
1528 |
this.__make_api_call(this.url(), // vm actions url |
1529 |
"delete", // create so that sync later uses POST to make the call |
1530 |
undefined, // payload |
1531 |
function() {
|
1532 |
// set state after successful call
|
1533 |
self.state('DESTROY');
|
1534 |
success.apply(this, arguments); |
1535 |
|
1536 |
}, |
1537 |
error, 'destroy', params);
|
1538 |
break;
|
1539 |
case 'resize': |
1540 |
this.__make_api_call(this.get_action_url(), // vm actions url |
1541 |
"create", // create so that sync later uses POST to make the call |
1542 |
{resize: {flavorRef:params.flavor}}, // payload |
1543 |
function() {
|
1544 |
self.state('RESIZE');
|
1545 |
success.apply(this, arguments); |
1546 |
snf.api.trigger("call");
|
1547 |
}, |
1548 |
error, 'resize', params);
|
1549 |
break;
|
1550 |
case 'destroy': |
1551 |
this.__make_api_call(this.url(), // vm actions url |
1552 |
"delete", // create so that sync later uses POST to make the call |
1553 |
undefined, // payload |
1554 |
function() {
|
1555 |
// set state after successful call
|
1556 |
self.state('DESTROY');
|
1557 |
success.apply(this, arguments); |
1558 |
synnefo.storage.quotas.get('cyclades.vm').decrease();
|
1559 |
|
1560 |
}, |
1561 |
error, 'destroy', params);
|
1562 |
break;
|
1563 |
case 'firewallProfile': |
1564 |
this.__make_api_call(this.get_action_url(), // vm actions url |
1565 |
"create",
|
1566 |
{firewallProfile:{nic:params.nic, profile:params.profile}}, // payload |
1567 |
function() {
|
1568 |
success.apply(this, arguments); |
1569 |
snf.api.trigger("call");
|
1570 |
}, |
1571 |
error, 'start', params);
|
1572 |
break;
|
1573 |
default:
|
1574 |
throw "Invalid VM action ("+action_name+")"; |
1575 |
} |
1576 |
}, |
1577 |
|
1578 |
__make_api_call: function(url, method, data, success, error, action, |
1579 |
extra_params) { |
1580 |
var self = this; |
1581 |
error = error || function(){};
|
1582 |
success = success || function(){};
|
1583 |
|
1584 |
var params = {
|
1585 |
url: url,
|
1586 |
data: data,
|
1587 |
success: function() { |
1588 |
self.handle_action_succeed.apply(self, arguments);
|
1589 |
success.apply(this, arguments) |
1590 |
}, |
1591 |
error: function() { |
1592 |
self.handle_action_fail.apply(self, arguments);
|
1593 |
error.apply(this, arguments) |
1594 |
}, |
1595 |
error_params: { ns: "Machines actions", |
1596 |
title: "'" + this.get("name") + "'" + " " + action + " failed", |
1597 |
extra_details: {
|
1598 |
'Machine ID': this.id, |
1599 |
'URL': url,
|
1600 |
'Action': action || "undefined" }, |
1601 |
allow_reload: false |
1602 |
}, |
1603 |
display: false, |
1604 |
critical: false |
1605 |
} |
1606 |
_.extend(params, extra_params); |
1607 |
this.sync(method, this, params); |
1608 |
}, |
1609 |
|
1610 |
handle_action_succeed: function() { |
1611 |
this.trigger("action:success", arguments); |
1612 |
}, |
1613 |
|
1614 |
reset_action_error: function() { |
1615 |
this.action_error = false; |
1616 |
this.trigger("action:fail:reset", this.action_error); |
1617 |
}, |
1618 |
|
1619 |
handle_action_fail: function() { |
1620 |
this.action_error = arguments; |
1621 |
this.trigger("action:fail", arguments); |
1622 |
}, |
1623 |
|
1624 |
get_action_url: function(name) { |
1625 |
return this.url() + "/action"; |
1626 |
}, |
1627 |
|
1628 |
get_diagnostics_url: function() { |
1629 |
return this.url() + "/diagnostics"; |
1630 |
}, |
1631 |
|
1632 |
get_users: function() { |
1633 |
var image;
|
1634 |
var users = [];
|
1635 |
try {
|
1636 |
var users = this.get_meta('users').split(" "); |
1637 |
} catch (err) { users = null } |
1638 |
if (!users) {
|
1639 |
image = this.get_image();
|
1640 |
if (image) {
|
1641 |
users = image.get_created_users(); |
1642 |
} |
1643 |
} |
1644 |
return users;
|
1645 |
}, |
1646 |
|
1647 |
get_connection_info: function(host_os, success, error) { |
1648 |
var url = synnefo.config.ui_connect_url;
|
1649 |
var users = this.get_users(); |
1650 |
|
1651 |
params = { |
1652 |
ip_address: this.get_hostname(), |
1653 |
hostname: this.get_hostname(), |
1654 |
os: this.get_os(), |
1655 |
host_os: host_os,
|
1656 |
ports: JSON.stringify(this.get('SNF:port_forwarding') || {}), |
1657 |
srv: this.id |
1658 |
} |
1659 |
|
1660 |
if (users.length) {
|
1661 |
params['username'] = _.last(users)
|
1662 |
} |
1663 |
|
1664 |
url = url + "?" + $.param(params); |
1665 |
|
1666 |
var ajax = snf.api.sync("read", undefined, { url: url, |
1667 |
error:error,
|
1668 |
success:success,
|
1669 |
handles_error:1}); |
1670 |
} |
1671 |
}); |
1672 |
|
1673 |
models.VM.ACTIONS = [ |
1674 |
'start',
|
1675 |
'shutdown',
|
1676 |
'reboot',
|
1677 |
'console',
|
1678 |
'destroy',
|
1679 |
'resize',
|
1680 |
'snapshot'
|
1681 |
] |
1682 |
|
1683 |
models.VM.TASK_STATE_STATUS_MAP = { |
1684 |
'BUILDING': 'BUILD', |
1685 |
'REBOOTING': 'REBOOT', |
1686 |
'STOPPING': 'SHUTDOWN', |
1687 |
'STARTING': 'START', |
1688 |
'RESIZING': 'RESIZE', |
1689 |
'CONNECTING': 'CONNECT', |
1690 |
'DISCONNECTING': 'DISCONNECT', |
1691 |
'DESTROYING': 'DESTROY' |
1692 |
} |
1693 |
|
1694 |
models.VM.AVAILABLE_ACTIONS = { |
1695 |
'UNKNWON' : ['destroy'], |
1696 |
'BUILD' : ['destroy'], |
1697 |
'REBOOT' : ['destroy'], |
1698 |
'STOPPED' : ['start', 'destroy', 'resize', 'snapshot'], |
1699 |
'ACTIVE' : ['shutdown', 'destroy', 'reboot', 'console', 'resize', 'snapshot'], |
1700 |
'ERROR' : ['destroy'], |
1701 |
'DELETED' : ['destroy'], |
1702 |
'DESTROY' : ['destroy'], |
1703 |
'SHUTDOWN' : ['destroy'], |
1704 |
'START' : ['destroy'], |
1705 |
'CONNECT' : ['destroy'], |
1706 |
'DISCONNECT' : ['destroy'], |
1707 |
'RESIZE' : ['destroy'] |
1708 |
} |
1709 |
|
1710 |
models.VM.AVAILABLE_ACTIONS_INACTIVE = {} |
1711 |
|
1712 |
// api status values
|
1713 |
models.VM.STATUSES = [ |
1714 |
'UNKNWON',
|
1715 |
'BUILD',
|
1716 |
'REBOOT',
|
1717 |
'STOPPED',
|
1718 |
'ACTIVE',
|
1719 |
'ERROR',
|
1720 |
'DELETED',
|
1721 |
'RESIZE'
|
1722 |
] |
1723 |
|
1724 |
// api status values
|
1725 |
models.VM.CONNECT_STATES = [ |
1726 |
'ACTIVE',
|
1727 |
'REBOOT',
|
1728 |
'SHUTDOWN'
|
1729 |
] |
1730 |
|
1731 |
// vm states
|
1732 |
models.VM.STATES = models.VM.STATUSES.concat([ |
1733 |
'DESTROY',
|
1734 |
'SHUTDOWN',
|
1735 |
'START',
|
1736 |
'CONNECT',
|
1737 |
'DISCONNECT',
|
1738 |
'FIREWALL',
|
1739 |
'RESIZE'
|
1740 |
]); |
1741 |
|
1742 |
models.VM.STATES_TRANSITIONS = { |
1743 |
'DESTROY' : ['DELETED'], |
1744 |
'SHUTDOWN': ['ERROR', 'STOPPED', 'DESTROY'], |
1745 |
'STOPPED': ['ERROR', 'ACTIVE', 'DESTROY'], |
1746 |
'ACTIVE': ['ERROR', 'STOPPED', 'REBOOT', 'SHUTDOWN', 'DESTROY'], |
1747 |
'START': ['ERROR', 'ACTIVE', 'DESTROY'], |
1748 |
'REBOOT': ['ERROR', 'ACTIVE', 'STOPPED', 'DESTROY'], |
1749 |
'BUILD': ['ERROR', 'ACTIVE', 'DESTROY'], |
1750 |
'RESIZE': ['ERROR', 'STOPPED'] |
1751 |
} |
1752 |
|
1753 |
models.VM.TRANSITION_STATES = [ |
1754 |
'DESTROY',
|
1755 |
'SHUTDOWN',
|
1756 |
'START',
|
1757 |
'REBOOT',
|
1758 |
'BUILD',
|
1759 |
'RESIZE',
|
1760 |
'DISCONNECT',
|
1761 |
'CONNECT'
|
1762 |
] |
1763 |
|
1764 |
models.VM.ACTIVE_STATES = [ |
1765 |
'BUILD', 'REBOOT', 'ACTIVE', |
1766 |
'SHUTDOWN', 'CONNECT', 'DISCONNECT' |
1767 |
] |
1768 |
|
1769 |
models.VM.BUILDING_STATES = [ |
1770 |
'BUILD'
|
1771 |
] |
1772 |
|
1773 |
models.Images = models.Collection.extend({ |
1774 |
model: models.Image,
|
1775 |
path: 'images', |
1776 |
details: true, |
1777 |
noUpdate: true, |
1778 |
supportIncUpdates: false, |
1779 |
meta_keys_as_attrs: ["OS", "description", "kernel", "size", "GUI"], |
1780 |
meta_labels: {},
|
1781 |
read_method: 'read', |
1782 |
|
1783 |
// update collection model with id passed
|
1784 |
// making a direct call to the image
|
1785 |
// api url
|
1786 |
update_unknown_id: function(id, callback) { |
1787 |
var url = getUrl.call(this) + "/" + id; |
1788 |
this.api_call(this.path + "/" + id, this.read_method, { |
1789 |
_options:{
|
1790 |
async:true, |
1791 |
skip_api_error:true} |
1792 |
}, undefined,
|
1793 |
_.bind(function() {
|
1794 |
if (!this.get(id)) { |
1795 |
if (this.fallback_service) { |
1796 |
// if current service has fallback_service attribute set
|
1797 |
// use this service to retrieve the missing image model
|
1798 |
var tmpservice = new this.fallback_service(); |
1799 |
tmpservice.update_unknown_id(id, _.bind(function(img){
|
1800 |
img.attributes.status = "DELETED";
|
1801 |
this.add(img.attributes);
|
1802 |
callback(this.get(id));
|
1803 |
}, this));
|
1804 |
} else {
|
1805 |
var title = synnefo.config.image_deleted_title || 'Deleted'; |
1806 |
// else add a dummy DELETED state image entry
|
1807 |
this.add({id:id, name:title, size:-1, |
1808 |
progress:100, status:"DELETED"}); |
1809 |
callback(this.get(id));
|
1810 |
} |
1811 |
} else {
|
1812 |
callback(this.get(id));
|
1813 |
} |
1814 |
}, this), _.bind(function(image, msg, xhr) { |
1815 |
if (!image) {
|
1816 |
var title = synnefo.config.image_deleted_title || 'Deleted'; |
1817 |
this.add({id:id, name:title, size:-1, |
1818 |
progress:100, status:"DELETED"}); |
1819 |
callback(this.get(id));
|
1820 |
return;
|
1821 |
} |
1822 |
var img_data = this._read_image_from_request(image, msg, xhr); |
1823 |
this.add(img_data);
|
1824 |
callback(this.get(id));
|
1825 |
}, this));
|
1826 |
}, |
1827 |
|
1828 |
_read_image_from_request: function(image, msg, xhr) { |
1829 |
return image.image;
|
1830 |
}, |
1831 |
|
1832 |
parse: function (resp, xhr) { |
1833 |
var parsed = _.map(resp.images, _.bind(this.parse_meta, this)); |
1834 |
parsed = this.fill_owners(parsed);
|
1835 |
return parsed;
|
1836 |
}, |
1837 |
|
1838 |
fill_owners: function(images) { |
1839 |
// do translate uuid->displayname if needed
|
1840 |
// store display name in owner attribute for compatibility
|
1841 |
var uuids = [];
|
1842 |
|
1843 |
var images = _.map(images, function(img, index) { |
1844 |
if (synnefo.config.translate_uuids) {
|
1845 |
uuids.push(img['owner']);
|
1846 |
} |
1847 |
img['owner_uuid'] = img['owner']; |
1848 |
return img;
|
1849 |
}); |
1850 |
|
1851 |
if (uuids.length > 0) { |
1852 |
var handle_results = function(data) { |
1853 |
_.each(images, function (img) {
|
1854 |
img['owner'] = data.uuid_catalog[img['owner_uuid']]; |
1855 |
}); |
1856 |
} |
1857 |
// notice the async false
|
1858 |
var uuid_map = this.translate_uuids(uuids, false, |
1859 |
handle_results) |
1860 |
} |
1861 |
return images;
|
1862 |
}, |
1863 |
|
1864 |
translate_uuids: function(uuids, async, cb) { |
1865 |
var url = synnefo.config.user_catalog_url;
|
1866 |
var data = JSON.stringify({'uuids': uuids}); |
1867 |
|
1868 |
// post to user_catalogs api
|
1869 |
snf.api.sync('create', undefined, { |
1870 |
url: url,
|
1871 |
data: data,
|
1872 |
async: async,
|
1873 |
success: cb
|
1874 |
}); |
1875 |
}, |
1876 |
|
1877 |
get_meta_key: function(img, key) { |
1878 |
if (img.metadata && img.metadata && img.metadata[key]) {
|
1879 |
return _.escape(img.metadata[key]);
|
1880 |
} |
1881 |
return undefined; |
1882 |
}, |
1883 |
|
1884 |
comparator: function(img) { |
1885 |
return -img.get_sort_order("sortorder") || 0; |
1886 |
}, |
1887 |
|
1888 |
parse_meta: function(img) { |
1889 |
_.each(this.meta_keys_as_attrs, _.bind(function(key){ |
1890 |
if (img[key]) { return }; |
1891 |
img[key] = this.get_meta_key(img, key) || ""; |
1892 |
}, this));
|
1893 |
return img;
|
1894 |
}, |
1895 |
|
1896 |
active: function() { |
1897 |
return this.filter(function(img){return img.get('status') != "DELETED"}); |
1898 |
}, |
1899 |
|
1900 |
predefined: function() { |
1901 |
return _.filter(this.active(), function(i) { return !i.get("serverRef")}); |
1902 |
}, |
1903 |
|
1904 |
fetch_for_type: function(type, complete, error) { |
1905 |
this.fetch({update:true, |
1906 |
success: complete,
|
1907 |
error: error,
|
1908 |
skip_api_error: true }); |
1909 |
}, |
1910 |
|
1911 |
get_images_for_type: function(type) { |
1912 |
if (this['get_{0}_images'.format(type)]) { |
1913 |
return this['get_{0}_images'.format(type)](); |
1914 |
} |
1915 |
|
1916 |
return this.active(); |
1917 |
}, |
1918 |
|
1919 |
update_images_for_type: function(type, onStart, onComplete, onError, force_load) { |
1920 |
var load = false; |
1921 |
error = onError || function() {};
|
1922 |
function complete(collection) { |
1923 |
onComplete(collection.get_images_for_type(type)); |
1924 |
} |
1925 |
|
1926 |
// do we need to fetch/update current collection entries
|
1927 |
if (load) {
|
1928 |
onStart(); |
1929 |
this.fetch_for_type(type, complete, error);
|
1930 |
} else {
|
1931 |
// fallback to complete
|
1932 |
complete(this);
|
1933 |
} |
1934 |
} |
1935 |
}) |
1936 |
|
1937 |
models.Flavors = models.Collection.extend({ |
1938 |
model: models.Flavor,
|
1939 |
path: 'flavors', |
1940 |
details: true, |
1941 |
noUpdate: true, |
1942 |
supportIncUpdates: false, |
1943 |
// update collection model with id passed
|
1944 |
// making a direct call to the flavor
|
1945 |
// api url
|
1946 |
update_unknown_id: function(id, callback) { |
1947 |
var url = getUrl.call(this) + "/" + id; |
1948 |
this.api_call(this.path + "/" + id, "read", {_options:{async:false, skip_api_error:true}}, undefined, |
1949 |
_.bind(function() {
|
1950 |
this.add({id:id, cpu:"Unknown", ram:"Unknown", disk:"Unknown", name: "Unknown", status:"DELETED"}) |
1951 |
}, this), _.bind(function(flv) { |
1952 |
if (!flv.flavor.status) { flv.flavor.status = "DELETED" }; |
1953 |
this.add(flv.flavor);
|
1954 |
}, this));
|
1955 |
}, |
1956 |
|
1957 |
parse: function (resp, xhr) { |
1958 |
return _.map(resp.flavors, function(o) { |
1959 |
o.cpu = o['vcpus'];
|
1960 |
o.disk_template = o['SNF:disk_template'];
|
1961 |
return o
|
1962 |
}); |
1963 |
}, |
1964 |
|
1965 |
comparator: function(flv) { |
1966 |
return flv.get("disk") * flv.get("cpu") * flv.get("ram"); |
1967 |
}, |
1968 |
|
1969 |
unavailable_values_for_quotas: function(quotas, flavors, extra) { |
1970 |
var flavors = flavors || this.active(); |
1971 |
var index = {cpu:[], disk:[], ram:[]}; |
1972 |
var extra = extra == undefined ? {cpu:0, disk:0, ram:0} : extra; |
1973 |
|
1974 |
_.each(flavors, function(el) {
|
1975 |
|
1976 |
var disk_available = quotas['disk'] + extra.disk; |
1977 |
var disk_size = el.get_disk_size();
|
1978 |
if (index.disk.indexOf(disk_size) == -1) { |
1979 |
var disk = el.disk_to_bytes();
|
1980 |
if (disk > disk_available) {
|
1981 |
index.disk.push(el.get('disk'));
|
1982 |
} |
1983 |
} |
1984 |
|
1985 |
var ram_available = quotas['ram'] + extra.ram * 1024 * 1024; |
1986 |
var ram_size = el.get_ram_size();
|
1987 |
if (index.ram.indexOf(ram_size) == -1) { |
1988 |
var ram = el.ram_to_bytes();
|
1989 |
if (ram > ram_available) {
|
1990 |
index.ram.push(el.get('ram'))
|
1991 |
} |
1992 |
} |
1993 |
|
1994 |
var cpu = el.get('cpu'); |
1995 |
var cpu_available = quotas['cpu'] + extra.cpu; |
1996 |
if (index.cpu.indexOf(cpu) == -1) { |
1997 |
if (cpu > cpu_available) {
|
1998 |
index.cpu.push(el.get('cpu'))
|
1999 |
} |
2000 |
} |
2001 |
}); |
2002 |
return index;
|
2003 |
}, |
2004 |
|
2005 |
unavailable_values_for_image: function(img, flavors) { |
2006 |
var flavors = flavors || this.active(); |
2007 |
var size = img.get_size();
|
2008 |
|
2009 |
var index = {cpu:[], disk:[], ram:[]}; |
2010 |
|
2011 |
_.each(this.active(), function(el) { |
2012 |
var img_size = size;
|
2013 |
var flv_size = el.get_disk_size();
|
2014 |
if (flv_size < img_size) {
|
2015 |
if (index.disk.indexOf(el.get("disk")) == -1) { |
2016 |
index.disk.push(el.get("disk"));
|
2017 |
} |
2018 |
}; |
2019 |
}); |
2020 |
|
2021 |
return index;
|
2022 |
}, |
2023 |
|
2024 |
get_flavor: function(cpu, mem, disk, disk_template, filter_list) { |
2025 |
if (!filter_list) { filter_list = this.models }; |
2026 |
|
2027 |
return this.select(function(flv){ |
2028 |
if (flv.get("cpu") == cpu + "" && |
2029 |
flv.get("ram") == mem + "" && |
2030 |
flv.get("disk") == disk + "" && |
2031 |
flv.get("disk_template") == disk_template &&
|
2032 |
filter_list.indexOf(flv) > -1) { return true; } |
2033 |
})[0];
|
2034 |
}, |
2035 |
|
2036 |
get_data: function(lst) { |
2037 |
var data = {'cpu': [], 'mem':[], 'disk':[], 'disk_template':[]}; |
2038 |
|
2039 |
_.each(lst, function(flv) {
|
2040 |
if (data.cpu.indexOf(flv.get("cpu")) == -1) { |
2041 |
data.cpu.push(flv.get("cpu"));
|
2042 |
} |
2043 |
if (data.mem.indexOf(flv.get("ram")) == -1) { |
2044 |
data.mem.push(flv.get("ram"));
|
2045 |
} |
2046 |
if (data.disk.indexOf(flv.get("disk")) == -1) { |
2047 |
data.disk.push(flv.get("disk"));
|
2048 |
} |
2049 |
if (data.disk_template.indexOf(flv.get("disk_template")) == -1) { |
2050 |
data.disk_template.push(flv.get("disk_template"));
|
2051 |
} |
2052 |
}) |
2053 |
|
2054 |
return data;
|
2055 |
}, |
2056 |
|
2057 |
active: function() { |
2058 |
return this.filter(function(flv){return flv.get('status') != "DELETED"}); |
2059 |
} |
2060 |
|
2061 |
}) |
2062 |
|
2063 |
models.VMS = models.Collection.extend({ |
2064 |
model: models.VM,
|
2065 |
path: 'servers', |
2066 |
details: true, |
2067 |
copy_image_meta: true, |
2068 |
|
2069 |
parse: function (resp, xhr) { |
2070 |
var data = resp;
|
2071 |
if (!resp) { return [] }; |
2072 |
data = _.filter(_.map(resp.servers, |
2073 |
_.bind(this.parse_vm_api_data, this)), |
2074 |
function(v){return v}); |
2075 |
return data;
|
2076 |
}, |
2077 |
|
2078 |
parse_vm_api_data: function(data) { |
2079 |
var status;
|
2080 |
// do not add non existing DELETED entries
|
2081 |
if (data.status && data.status == "DELETED") { |
2082 |
if (!this.get(data.id)) { |
2083 |
return false; |
2084 |
} |
2085 |
} |
2086 |
|
2087 |
if ('SNF:task_state' in data) { |
2088 |
data['task_state'] = data['SNF:task_state']; |
2089 |
// Update machine state based on task_state value
|
2090 |
// Do not apply task_state logic when machine is in ERROR state.
|
2091 |
// In that case only update from task_state only if equals to
|
2092 |
// DESTROY
|
2093 |
if (data['task_state']) { |
2094 |
if (data['status'] != 'ERROR' && data['task_state'] != 'DESTROY') { |
2095 |
status = models.VM.TASK_STATE_STATUS_MAP[data['task_state']];
|
2096 |
if (status) { data['status'] = status } |
2097 |
} |
2098 |
} |
2099 |
} |
2100 |
|
2101 |
// OS attribute
|
2102 |
if (this.has_meta(data)) { |
2103 |
data['OS'] = data.metadata.OS || snf.config.unknown_os;
|
2104 |
} |
2105 |
|
2106 |
if (!data.diagnostics) {
|
2107 |
data.diagnostics = []; |
2108 |
} |
2109 |
|
2110 |
// network metadata
|
2111 |
data['firewalls'] = {};
|
2112 |
data['fqdn'] = data['SNF:fqdn']; |
2113 |
|
2114 |
// if vm has no metadata, no metadata object
|
2115 |
// is in json response, reset it to force
|
2116 |
// value update
|
2117 |
if (!data['metadata']) { |
2118 |
data['metadata'] = {};
|
2119 |
} |
2120 |
|
2121 |
// v2.0 API returns objects
|
2122 |
data.image_obj = data.image; |
2123 |
data.image = data.image_obj.id; |
2124 |
data.flavor_obj = data.flavor; |
2125 |
data.flavor = data.flavor_obj.id; |
2126 |
|
2127 |
return data;
|
2128 |
}, |
2129 |
|
2130 |
get_reboot_required: function() { |
2131 |
return this.filter(function(vm){return vm.get("reboot_required") == true}) |
2132 |
}, |
2133 |
|
2134 |
has_pending_actions: function() { |
2135 |
return this.filter(function(vm){return vm.pending_action}).length > 0; |
2136 |
}, |
2137 |
|
2138 |
reset_pending_actions: function() { |
2139 |
this.each(function(vm) { |
2140 |
vm.clear_pending_action(); |
2141 |
}) |
2142 |
}, |
2143 |
|
2144 |
do_all_pending_actions: function(success, error) { |
2145 |
this.each(function(vm) { |
2146 |
if (vm.has_pending_action()) {
|
2147 |
vm.call(vm.pending_action, success, error); |
2148 |
vm.clear_pending_action(); |
2149 |
} |
2150 |
}) |
2151 |
}, |
2152 |
|
2153 |
do_all_reboots: function(success, error) { |
2154 |
this.each(function(vm) { |
2155 |
if (vm.get("reboot_required")) { |
2156 |
vm.call("reboot", success, error);
|
2157 |
} |
2158 |
}); |
2159 |
}, |
2160 |
|
2161 |
reset_reboot_required: function() { |
2162 |
this.each(function(vm) { |
2163 |
vm.set({'reboot_required': undefined}); |
2164 |
}) |
2165 |
}, |
2166 |
|
2167 |
stop_stats_update: function(exclude) { |
2168 |
var exclude = exclude || [];
|
2169 |
this.each(function(vm) { |
2170 |
if (exclude.indexOf(vm) > -1) { |
2171 |
return;
|
2172 |
} |
2173 |
vm.stop_stats_update(); |
2174 |
}) |
2175 |
}, |
2176 |
|
2177 |
has_meta: function(vm_data) { |
2178 |
return vm_data.metadata && vm_data.metadata
|
2179 |
}, |
2180 |
|
2181 |
has_addresses: function(vm_data) { |
2182 |
return vm_data.metadata && vm_data.metadata
|
2183 |
}, |
2184 |
|
2185 |
create: function (name, image, flavor, meta, extra, callback) { |
2186 |
|
2187 |
if (this.copy_image_meta) { |
2188 |
if (synnefo.config.vm_image_common_metadata) {
|
2189 |
_.each(synnefo.config.vm_image_common_metadata, |
2190 |
function(key){
|
2191 |
if (image.get_meta(key)) {
|
2192 |
meta[key] = image.get_meta(key); |
2193 |
} |
2194 |
}); |
2195 |
} |
2196 |
|
2197 |
if (image.get("OS")) { |
2198 |
meta['OS'] = image.get("OS"); |
2199 |
} |
2200 |
} |
2201 |
|
2202 |
opts = {name: name, imageRef: image.id, flavorRef: flavor.id, |
2203 |
metadata:meta}
|
2204 |
opts = _.extend(opts, extra); |
2205 |
|
2206 |
var cb = function(data) { |
2207 |
synnefo.storage.quotas.get('cyclades.vm').increase();
|
2208 |
callback(data); |
2209 |
} |
2210 |
|
2211 |
this.api_call(this.path, "create", {'server': opts}, undefined, |
2212 |
undefined, cb, {critical: true}); |
2213 |
}, |
2214 |
|
2215 |
load_missing_images: function(callback) { |
2216 |
var missing_ids = [];
|
2217 |
var resolved = 0; |
2218 |
|
2219 |
// fill missing_ids
|
2220 |
this.each(function(el) { |
2221 |
var imgid = el.get("image"); |
2222 |
var existing = synnefo.storage.images.get(imgid);
|
2223 |
if (!existing && missing_ids.indexOf(imgid) == -1) { |
2224 |
missing_ids.push(imgid); |
2225 |
} |
2226 |
}); |
2227 |
var check = function() { |
2228 |
// once all missing ids where resolved continue calling the
|
2229 |
// callback
|
2230 |
resolved++; |
2231 |
if (resolved == missing_ids.length) {
|
2232 |
callback(missing_ids) |
2233 |
} |
2234 |
} |
2235 |
if (missing_ids.length == 0) { |
2236 |
callback(missing_ids); |
2237 |
return;
|
2238 |
} |
2239 |
// start resolving missing image ids
|
2240 |
_(missing_ids).each(function(imgid){
|
2241 |
synnefo.storage.images.update_unknown_id(imgid, check); |
2242 |
}); |
2243 |
}, |
2244 |
|
2245 |
get_connectable: function() { |
2246 |
return storage.vms.filter(function(vm){ |
2247 |
return !vm.in_error_state() && !vm.is_building();
|
2248 |
}); |
2249 |
} |
2250 |
}) |
2251 |
|
2252 |
models.PublicKey = models.Model.extend({ |
2253 |
path: 'keys', |
2254 |
api_type: 'userdata', |
2255 |
detail: false, |
2256 |
model_actions: {
|
2257 |
'remove': [['name'], function() { |
2258 |
return true; |
2259 |
}] |
2260 |
}, |
2261 |
|
2262 |
get_public_key: function() { |
2263 |
return cryptico.publicKeyFromString(this.get("content")); |
2264 |
}, |
2265 |
|
2266 |
get_filename: function() { |
2267 |
return "{0}.pub".format(this.get("name")); |
2268 |
}, |
2269 |
|
2270 |
identify_type: function() { |
2271 |
try {
|
2272 |
var cont = snf.util.validatePublicKey(this.get("content")); |
2273 |
var type = cont.split(" ")[0]; |
2274 |
return synnefo.util.publicKeyTypesMap[type];
|
2275 |
} catch (err) { return false }; |
2276 |
}, |
2277 |
|
2278 |
rename: function(new_name) { |
2279 |
//this.set({'name': new_name});
|
2280 |
this.sync("update", this, { |
2281 |
critical: true, |
2282 |
data: {'name': new_name}, |
2283 |
success: _.bind(function(){ |
2284 |
snf.api.trigger("call");
|
2285 |
}, this)
|
2286 |
}); |
2287 |
}, |
2288 |
|
2289 |
do_remove: function() { |
2290 |
this.actions.reset_pending();
|
2291 |
this.remove(function() { |
2292 |
synnefo.storage.keys.fetch(); |
2293 |
}); |
2294 |
} |
2295 |
}) |
2296 |
|
2297 |
models._ActionsModel = models.Model.extend({ |
2298 |
defaults: { pending: null }, |
2299 |
actions: [],
|
2300 |
status: {
|
2301 |
INACTIVE: 0, |
2302 |
PENDING: 1, |
2303 |
CALLED: 2 |
2304 |
}, |
2305 |
|
2306 |
initialize: function(attrs, opts) { |
2307 |
models._ActionsModel.__super__.initialize.call(this, attrs);
|
2308 |
this.actions = opts.actions;
|
2309 |
this.model = opts.model;
|
2310 |
this.bind("change", function() { |
2311 |
this.set({'pending': this.get_pending()}); |
2312 |
}, this);
|
2313 |
this.clear();
|
2314 |
}, |
2315 |
|
2316 |
_in_status: function(st) { |
2317 |
var actions = null; |
2318 |
_.each(this.attributes, function(status, action){ |
2319 |
if (status == st) {
|
2320 |
if (!actions) {
|
2321 |
actions = [] |
2322 |
} |
2323 |
actions.push(action); |
2324 |
} |
2325 |
}); |
2326 |
return actions;
|
2327 |
}, |
2328 |
|
2329 |
get_pending: function() { |
2330 |
return this._in_status(this.status.PENDING); |
2331 |
}, |
2332 |
|
2333 |
unset_pending_action: function(action) { |
2334 |
var data = {};
|
2335 |
data[action] = this.status.INACTIVE;
|
2336 |
this.set(data);
|
2337 |
this.trigger("unset-pending", action); |
2338 |
}, |
2339 |
|
2340 |
set_pending_action: function(action, reset_pending) { |
2341 |
reset_pending = reset_pending === undefined ? true : reset_pending; |
2342 |
var data = {};
|
2343 |
data[action] = this.status.PENDING;
|
2344 |
if (reset_pending) {
|
2345 |
this.reset_pending();
|
2346 |
} |
2347 |
this.set(data);
|
2348 |
this.trigger("set-pending", action); |
2349 |
}, |
2350 |
|
2351 |
reset_pending: function() { |
2352 |
var data = {};
|
2353 |
_.each(this.actions, function(action) { |
2354 |
data[action] = this.status.INACTIVE;
|
2355 |
}, this);
|
2356 |
this.set(data);
|
2357 |
this.trigger("reset-pending"); |
2358 |
} |
2359 |
}); |
2360 |
|
2361 |
models.PublicPool = models.Model.extend({}); |
2362 |
models.PublicPools = models.Collection.extend({ |
2363 |
model: models.PublicPool,
|
2364 |
path: 'os-floating-ip-pools', |
2365 |
api_type: 'compute', |
2366 |
noUpdate: true, |
2367 |
|
2368 |
parse: function(data) { |
2369 |
return _.map(data.floating_ip_pools, function(pool) { |
2370 |
pool.id = pool.name; |
2371 |
return pool;
|
2372 |
}); |
2373 |
} |
2374 |
}); |
2375 |
|
2376 |
models.PublicKeys = models.Collection.extend({ |
2377 |
model: models.PublicKey,
|
2378 |
details: false, |
2379 |
path: 'keys', |
2380 |
api_type: 'userdata', |
2381 |
noUpdate: true, |
2382 |
updateEntries: true, |
2383 |
|
2384 |
generate_new: function(success, error) { |
2385 |
snf.api.sync('create', undefined, { |
2386 |
url: getUrl.call(this, this.base_url) + "/generate", |
2387 |
success: success,
|
2388 |
error: error,
|
2389 |
skip_api_error: true |
2390 |
}); |
2391 |
}, |
2392 |
|
2393 |
add_crypto_key: function(key, success, error, options) { |
2394 |
var options = options || {};
|
2395 |
var m = new models.PublicKey(); |
2396 |
|
2397 |
// guess a name
|
2398 |
var name_tpl = "my generated public key"; |
2399 |
var name = name_tpl;
|
2400 |
var name_count = 1; |
2401 |
|
2402 |
while(this.filter(function(m){ return m.get("name") == name }).length > 0) { |
2403 |
name = name_tpl + " " + name_count;
|
2404 |
name_count++; |
2405 |
} |
2406 |
|
2407 |
m.set({name: name});
|
2408 |
m.set({content: key});
|
2409 |
|
2410 |
options.success = function () { return success(m) }; |
2411 |
options.errror = error; |
2412 |
options.skip_api_error = true;
|
2413 |
|
2414 |
this.create(m.attributes, options);
|
2415 |
} |
2416 |
}); |
2417 |
|
2418 |
|
2419 |
models.Quota = models.Model.extend({ |
2420 |
|
2421 |
initialize: function() { |
2422 |
models.Quota.__super__.initialize.apply(this, arguments); |
2423 |
this.bind("change", this.check, this); |
2424 |
this.check();
|
2425 |
}, |
2426 |
|
2427 |
check: function() { |
2428 |
var usage, limit;
|
2429 |
usage = this.get('usage'); |
2430 |
limit = this.get('limit'); |
2431 |
if (usage >= limit) {
|
2432 |
this.trigger("available"); |
2433 |
} else {
|
2434 |
this.trigger("unavailable"); |
2435 |
} |
2436 |
}, |
2437 |
|
2438 |
increase: function(val) { |
2439 |
if (val === undefined) { val = 1}; |
2440 |
this.set({'usage': this.get('usage') + val}) |
2441 |
}, |
2442 |
|
2443 |
decrease: function(val) { |
2444 |
if (val === undefined) { val = 1}; |
2445 |
this.set({'usage': this.get('usage') - val}) |
2446 |
}, |
2447 |
|
2448 |
can_consume: function() { |
2449 |
var usage, limit;
|
2450 |
usage = this.get('usage'); |
2451 |
limit = this.get('limit'); |
2452 |
if (usage >= limit) {
|
2453 |
return false |
2454 |
} else {
|
2455 |
return true |
2456 |
} |
2457 |
}, |
2458 |
|
2459 |
is_bytes: function() { |
2460 |
return this.get('resource').get('unit') == 'bytes'; |
2461 |
}, |
2462 |
|
2463 |
get_available: function(active) { |
2464 |
suffix = '';
|
2465 |
if (active) { suffix = '_active'} |
2466 |
var value = this.get('limit'+suffix) - this.get('usage'+suffix); |
2467 |
if (active) {
|
2468 |
if (this.get('available') <= value) { |
2469 |
value = this.get('available'); |
2470 |
} |
2471 |
} |
2472 |
if (value < 0) { return value } |
2473 |
return value
|
2474 |
}, |
2475 |
|
2476 |
get_readable: function(key, active) { |
2477 |
var value;
|
2478 |
if (key == 'available') { |
2479 |
value = this.get_available(active);
|
2480 |
} else {
|
2481 |
value = this.get(key)
|
2482 |
} |
2483 |
if (value <= 0) { value = 0 } |
2484 |
// greater than max js int (assume infinite quota)
|
2485 |
if (value > Math.pow(2, 53)) { |
2486 |
return "Infinite" |
2487 |
} |
2488 |
|
2489 |
if (!this.is_bytes()) { |
2490 |
return value + ""; |
2491 |
} |
2492 |
|
2493 |
return snf.util.readablizeBytes(value);
|
2494 |
} |
2495 |
}); |
2496 |
|
2497 |
models.Quotas = models.Collection.extend({ |
2498 |
model: models.Quota,
|
2499 |
api_type: 'accounts', |
2500 |
path: 'quotas', |
2501 |
supportIncUpdates: false, |
2502 |
parse: function(resp) { |
2503 |
filtered = _.map(resp.system, function(value, key) {
|
2504 |
var available = (value.limit - value.usage) || 0; |
2505 |
var available_active = available;
|
2506 |
var keysplit = key.split("."); |
2507 |
var limit_active = value.limit;
|
2508 |
var usage_active = value.usage;
|
2509 |
keysplit[keysplit.length-1] = "total_" + keysplit[keysplit.length-1]; |
2510 |
var activekey = keysplit.join("."); |
2511 |
var exists = resp.system[activekey];
|
2512 |
if (exists) {
|
2513 |
available_active = exists.limit - exists.usage; |
2514 |
limit_active = exists.limit; |
2515 |
usage_active = exists.usage; |
2516 |
} |
2517 |
return _.extend(value, {'name': key, 'id': key, |
2518 |
'available': available,
|
2519 |
'available_active': available_active,
|
2520 |
'limit_active': limit_active,
|
2521 |
'usage_active': usage_active,
|
2522 |
'resource': snf.storage.resources.get(key)});
|
2523 |
}); |
2524 |
return filtered;
|
2525 |
}, |
2526 |
|
2527 |
get_by_id: function(k) { |
2528 |
return this.filter(function(q) { return q.get('name') == k})[0] |
2529 |
}, |
2530 |
|
2531 |
get_available_for_vm: function(options) { |
2532 |
var quotas = synnefo.storage.quotas;
|
2533 |
var key = 'available'; |
2534 |
var available_quota = {};
|
2535 |
_.each(['cyclades.ram', 'cyclades.cpu', 'cyclades.disk'], |
2536 |
function (key) {
|
2537 |
var value = quotas.get(key).get_available(true); |
2538 |
available_quota[key.replace('cyclades.', '')] = value; |
2539 |
}); |
2540 |
return available_quota;
|
2541 |
} |
2542 |
}) |
2543 |
|
2544 |
models.Resource = models.Model.extend({ |
2545 |
api_type: 'accounts', |
2546 |
path: 'resources' |
2547 |
}); |
2548 |
|
2549 |
models.Resources = models.Collection.extend({ |
2550 |
api_type: 'accounts', |
2551 |
path: 'resources', |
2552 |
model: models.Network,
|
2553 |
|
2554 |
parse: function(resp) { |
2555 |
return _.map(resp, function(value, key) { |
2556 |
return _.extend(value, {'name': key, 'id': key}); |
2557 |
}) |
2558 |
} |
2559 |
}); |
2560 |
|
2561 |
// storage initialization
|
2562 |
snf.storage.images = new models.Images();
|
2563 |
snf.storage.flavors = new models.Flavors();
|
2564 |
snf.storage.vms = new models.VMS();
|
2565 |
snf.storage.keys = new models.PublicKeys();
|
2566 |
snf.storage.resources = new models.Resources();
|
2567 |
snf.storage.quotas = new models.Quotas();
|
2568 |
snf.storage.public_pools = new models.PublicPools();
|
2569 |
|
2570 |
})(this);
|