Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / static / snf / js / models.js @ b6ac9768

History | View | Annotate | Download (87.1 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, max, initial_call, params) {
402
            var fetch_params = params || {};
403
            var handler_options = {};
404

    
405
            fetch_params.skips_timeouts = true;
406
            handler_options.interval = interval;
407
            handler_options.increase = increase;
408
            handler_options.fast = fast;
409
            handler_options.increase_after_calls = increase_after_calls;
410
            handler_options.max= max;
411
            handler_options.id = "collection id";
412

    
413
            var last_ajax = undefined;
414
            var callback = _.bind(function() {
415
                // clone to avoid referenced objects
416
                var params = _.clone(fetch_params);
417
                updater._ajax = last_ajax;
418
                
419
                // wait for previous request to finish
420
                if (last_ajax && last_ajax.readyState < 4 && last_ajax.statusText != "timeout") {
421
                    // opera readystate for 304 responses is 0
422
                    if (!($.browser.opera && last_ajax.readyState == 0 && last_ajax.status == 304)) {
423
                        return;
424
                    }
425
                }
426
                last_ajax = this.fetch(params);
427
            }, this);
428
            handler_options.callback = callback;
429

    
430
            var updater = new snf.api.updateHandler(_.clone(_.extend(handler_options, fetch_params)));
431
            snf.api.bind("call", _.throttle(_.bind(function(){ updater.faster(true)}, this)), 1000);
432
            return updater;
433
        }
434
    });
435
    
436
    // Image model
437
    models.Image = models.Model.extend({
438
        path: 'images',
439
        
440
        get_size: function() {
441
            return parseInt(this.get('metadata') ? this.get('metadata').size : -1)
442
        },
443

    
444
        get_description: function(escape) {
445
            if (escape == undefined) { escape = true };
446
            if (escape) { return this.escape('description') || "No description available"}
447
            return this.get('description') || "No description available."
448
        },
449

    
450
        get_meta: function(key) {
451
            if (this.get('metadata') && this.get('metadata')) {
452
                if (!this.get('metadata')[key]) { return null }
453
                return _.escape(this.get('metadata')[key]);
454
            } else {
455
                return null;
456
            }
457
        },
458

    
459
        get_meta_keys: function() {
460
            if (this.get('metadata') && this.get('metadata')) {
461
                return _.keys(this.get('metadata'));
462
            } else {
463
                return [];
464
            }
465
        },
466

    
467
        get_owner: function() {
468
            return this.get('owner') || _.keys(synnefo.config.system_images_owners)[0];
469
        },
470

    
471
        get_owner_uuid: function() {
472
            return this.get('owner_uuid');
473
        },
474

    
475
        is_system_image: function() {
476
          var owner = this.get_owner();
477
          return _.include(_.keys(synnefo.config.system_images_owners), owner)
478
        },
479

    
480
        owned_by: function(user) {
481
          if (!user) { user = synnefo.user }
482
          return user.get_username() == this.get('owner_uuid');
483
        },
484

    
485
        display_owner: function() {
486
            var owner = this.get_owner();
487
            if (_.include(_.keys(synnefo.config.system_images_owners), owner)) {
488
                return synnefo.config.system_images_owners[owner];
489
            } else {
490
                return owner;
491
            }
492
        },
493
    
494
        get_readable_size: function() {
495
            if (this.is_deleted()) {
496
                return synnefo.config.image_deleted_size_title || '(none)';
497
            }
498
            return this.get_size() > 0 ? util.readablizeBytes(this.get_size() * 1024 * 1024) : '(none)';
499
        },
500

    
501
        get_os: function() {
502
            return this.get_meta('OS');
503
        },
504

    
505
        get_gui: function() {
506
            return this.get_meta('GUI');
507
        },
508

    
509
        get_created_users: function() {
510
            try {
511
              var users = this.get_meta('users').split(" ");
512
            } catch (err) { users = null }
513
            if (!users) {
514
                var osfamily = this.get_meta('osfamily');
515
                if (osfamily == 'windows') { 
516
                  users = ['Administrator'];
517
                } else {
518
                  users = ['root'];
519
                }
520
            }
521
            return users;
522
        },
523

    
524
        get_sort_order: function() {
525
            return parseInt(this.get('metadata') ? this.get('metadata').sortorder : -1)
526
        },
527

    
528
        get_vm: function() {
529
            var vm_id = this.get("serverRef");
530
            var vm = undefined;
531
            vm = storage.vms.get(vm_id);
532
            return vm;
533
        },
534

    
535
        is_public: function() {
536
            return this.get('is_public') == undefined ? true : this.get('is_public');
537
        },
538

    
539
        is_deleted: function() {
540
            return this.get('status') == "DELETED"
541
        },
542
        
543
        ssh_keys_paths: function() {
544
            return _.map(this.get_created_users(), function(username) {
545
                prepend = '';
546
                if (username != 'root') {
547
                    prepend = '/home'
548
                }
549
                return {'user': username, 'path': '{1}/{0}/.ssh/authorized_keys'.format(username, 
550
                                                             prepend)};
551
            });
552
        },
553

    
554
        _supports_ssh: function() {
555
            var exclude_list = synnefo.config.ssh_support_osfamily_exclude_list || [];
556
            var os = this.get_os();
557
            if (exclude_list.indexOf(os) > -1) {
558
                return false;
559
            }
560
            return true;
561
        },
562

    
563
        supports: function(feature) {
564
            if (feature == "ssh") {
565
                return this._supports_ssh()
566
            }
567
            return false;
568
        },
569

    
570
        personality_data_for_keys: function(keys) {
571
            return _.map(this.ssh_keys_paths(), function(pathinfo) {
572
                var contents = '';
573
                _.each(keys, function(key){
574
                    contents = contents + key.get("content") + "\n"
575
                });
576
                contents = $.base64.encode(contents);
577

    
578
                return {
579
                    path: pathinfo.path,
580
                    contents: contents,
581
                    mode: 0600,
582
                    owner: pathinfo.user
583
                }
584
            });
585
        }
586
    });
587

    
588
    // Flavor model
589
    models.Flavor = models.Model.extend({
590
        path: 'flavors',
591

    
592
        details_string: function() {
593
            return "{0} CPU, {1}MB, {2}GB".format(this.get('cpu'), this.get('ram'), this.get('disk'));
594
        },
595

    
596
        get_disk_size: function() {
597
            return parseInt(this.get("disk") * 1024)
598
        },
599

    
600
        get_ram_size: function() {
601
            return parseInt(this.get("ram"))
602
        },
603

    
604
        get_disk_template_info: function() {
605
            var info = snf.config.flavors_disk_templates_info[this.get("disk_template")];
606
            if (!info) {
607
                info = { name: this.get("disk_template"), description:'' };
608
            }
609
            return info
610
        },
611

    
612
        disk_to_bytes: function() {
613
            return parseInt(this.get("disk")) * 1024 * 1024 * 1024;
614
        },
615

    
616
        ram_to_bytes: function() {
617
            return parseInt(this.get("ram")) * 1024 * 1024;
618
        },
619

    
620
    });
621
    
622
    models.ParamsList = function(){this.initialize.apply(this, arguments)};
623
    _.extend(models.ParamsList.prototype, bb.Events, {
624

    
625
        initialize: function(parent, param_name) {
626
            this.parent = parent;
627
            this.actions = {};
628
            this.param_name = param_name;
629
            this.length = 0;
630
        },
631
        
632
        has_action: function(action) {
633
            return this.actions[action] ? true : false;
634
        },
635
            
636
        _parse_params: function(arguments) {
637
            if (arguments.length <= 1) {
638
                return [];
639
            }
640

    
641
            var args = _.toArray(arguments);
642
            return args.splice(1);
643
        },
644

    
645
        contains: function(action, params) {
646
            params = this._parse_params(arguments);
647
            var has_action = this.has_action(action);
648
            if (!has_action) { return false };
649

    
650
            var paramsEqual = false;
651
            _.each(this.actions[action], function(action_params) {
652
                if (_.isEqual(action_params, params)) {
653
                    paramsEqual = true;
654
                }
655
            });
656
                
657
            return paramsEqual;
658
        },
659
        
660
        is_empty: function() {
661
            return _.isEmpty(this.actions);
662
        },
663

    
664
        add: function(action, params) {
665
            params = this._parse_params(arguments);
666
            if (this.contains.apply(this, arguments)) { return this };
667
            var isnew = false
668
            if (!this.has_action(action)) {
669
                this.actions[action] = [];
670
                isnew = true;
671
            };
672

    
673
            this.actions[action].push(params);
674
            this.parent.trigger("change:" + this.param_name, this.parent, this);
675
            if (isnew) {
676
                this.trigger("add", action, params);
677
            } else {
678
                this.trigger("change", action, params);
679
            }
680
            return this;
681
        },
682
        
683
        remove_all: function(action) {
684
            if (this.has_action(action)) {
685
                delete this.actions[action];
686
                this.parent.trigger("change:" + this.param_name, this.parent, this);
687
                this.trigger("remove", action);
688
            }
689
            return this;
690
        },
691

    
692
        reset: function() {
693
            this.actions = {};
694
            this.parent.trigger("change:" + this.param_name, this.parent, this);
695
            this.trigger("reset");
696
            this.trigger("remove");
697
        },
698

    
699
        remove: function(action, params) {
700
            params = this._parse_params(arguments);
701
            if (!this.has_action(action)) { return this };
702
            var index = -1;
703
            _.each(this.actions[action], _.bind(function(action_params) {
704
                if (_.isEqual(action_params, params)) {
705
                    index = this.actions[action].indexOf(action_params);
706
                }
707
            }, this));
708
            
709
            if (index > -1) {
710
                this.actions[action].splice(index, 1);
711
                if (_.isEmpty(this.actions[action])) {
712
                    delete this.actions[action];
713
                }
714
                this.parent.trigger("change:" + this.param_name, this.parent, this);
715
                this.trigger("remove", action, params);
716
            }
717
        }
718

    
719
    });
720

    
721
    // Virtualmachine model
722
    models.VM = models.Model.extend({
723

    
724
        path: 'servers',
725
        has_status: true,
726
        proxy_attrs: {
727
          'busy': [
728
            ['status', 'state'], function() {
729
              return !_.contains(['ACTIVE', 'STOPPED'], this.get('status'));
730
            }
731
          ],
732
          'in_progress': [
733
            ['status', 'state'], function() {
734
              return this.in_transition();
735
            }
736
          ]
737
        },
738

    
739
        initialize: function(params) {
740
            var self = this;
741
            this.ports = new Backbone.FilteredCollection(undefined, {
742
              collection: synnefo.storage.ports,
743
              collectionFilter: function(m) {
744
                return self.id == m.get('device_id')
745
            }});
746

    
747
            this.pending_firewalls = {};
748
            
749
            models.VM.__super__.initialize.apply(this, arguments);
750

    
751

    
752
            this.set({state: params.status || "ERROR"});
753
            this.log = new snf.logging.logger("VM " + this.id);
754
            this.pending_action = undefined;
755
            
756
            // init stats parameter
757
            this.set({'stats': undefined}, {silent: true});
758
            // defaults to not update the stats
759
            // each view should handle this vm attribute 
760
            // depending on if it displays stat images or not
761
            this.do_update_stats = false;
762
            
763
            // interval time
764
            // this will dynamicaly change if the server responds that
765
            // images get refreshed on different intervals
766
            this.stats_update_interval = synnefo.config.STATS_INTERVAL || 5000;
767
            this.stats_available = false;
768

    
769
            // initialize interval
770
            this.init_stats_intervals(this.stats_update_interval);
771
            
772
            // handle progress message on instance change
773
            this.bind("change", _.bind(this.update_status_message, this));
774
            this.bind("change:task_state", _.bind(this.update_status, this));
775
            // force update of progress message
776
            this.update_status_message(true);
777
            
778
            // default values
779
            this.bind("change:state", _.bind(function(){
780
                if (this.state() == "DESTROY") { 
781
                    this.handle_destroy() 
782
                }
783
            }, this));
784

    
785
        },
786
        
787
        get_public_ips: function() {
788
          var ips = [];
789
          this.ports.filter(function(port) {
790
            if (port.get('network') && !port.get('network').get('is_public')) { return }
791
            if (!port.get("ips")) { return }
792
            port.get("ips").each(function(ip) {
793
              ips.push(ip);
794
            });
795
          });
796
          return ips;
797
        },
798

    
799
        has_public_ip: function() {
800
          return this.ports.filter(function(port) {
801
            return port.get("network") && 
802
                   port.get("network").get("is_public") && 
803
                   port.get("ips").length > 0;
804
          }).length > 0;
805
        },
806

    
807
        has_public_ipv6: function() {
808
          return this.has_ip_version("v6", true);
809
        },
810

    
811
        has_public_ipv4: function() {
812
          return this.has_ip_version("v4", true);
813
        },
814
        
815
        has_ip_version: function(ver, public) {
816
          var found = false;
817
          this.ports.each(function(port) {
818
            if (found) { return }
819
            if (public !== undefined) {
820
              if (port.get("network") && 
821
                  port.get("network").get("is_public") != public) {
822
                return
823
              }
824
            }
825
            port.get('ips').each(function(ip) {
826
              if (found) { return }
827
              if (ip.get("type") == ver) {
828
                found = true
829
              }
830
            })
831
          }, this)
832
          return found;
833
        },
834

    
835
        status: function(st) {
836
            if (!st) { return this.get("status")}
837
            return this.set({status:st});
838
        },
839
        
840
        update_status: function() {
841
            this.set_status(this.get('status'));
842
        },
843

    
844
        set_status: function(st) {
845
            var new_state = this.state_for_api_status(st);
846
            var transition = false;
847

    
848
            if (this.state() != new_state) {
849
                if (models.VM.STATES_TRANSITIONS[this.state()]) {
850
                    transition = this.state();
851
                }
852
            }
853
            
854
            // call it silently to avoid double change trigger
855
            var state = this.state_for_api_status(st);
856
            this.set({'state': state}, {silent: true});
857
            
858
            // trigger transition
859
            if (transition && models.VM.TRANSITION_STATES.indexOf(new_state) == -1) { 
860
                this.trigger("transition", {from:transition, to:new_state}) 
861
            };
862
            return st;
863
        },
864
            
865
        get_diagnostics: function(success) {
866
            this.__make_api_call(this.get_diagnostics_url(),
867
                                 "read", // create so that sync later uses POST to make the call
868
                                 null, // payload
869
                                 function(data) {
870
                                     success(data);
871
                                 },  
872
                                 null, 'diagnostics');
873
        },
874

    
875
        has_diagnostics: function() {
876
            return this.get("diagnostics") && this.get("diagnostics").length;
877
        },
878

    
879
        get_progress_info: function() {
880
            // details about progress message
881
            // contains a list of diagnostic messages
882
            return this.get("status_messages");
883
        },
884

    
885
        get_status_message: function() {
886
            return this.get('status_message');
887
        },
888
        
889
        // extract status message from diagnostics
890
        status_message_from_diagnostics: function(diagnostics) {
891
            var valid_sources_map = synnefo.config.diagnostics_status_messages_map;
892
            var valid_sources = valid_sources_map[this.get('status')];
893
            if (!valid_sources) { return null };
894
            
895
            // filter messsages based on diagnostic source
896
            var messages = _.filter(diagnostics, function(diag) {
897
                return valid_sources.indexOf(diag.source) > -1;
898
            });
899

    
900
            var msg = messages[0];
901
            if (msg) {
902
              var message = msg.message;
903
              var message_tpl = snf.config.diagnostic_messages_tpls[msg.source];
904

    
905
              if (message_tpl) {
906
                  message = message_tpl.replace('MESSAGE', msg.message);
907
              }
908
              return message;
909
            }
910
            
911
            // no message to display, but vm in build state, display
912
            // finalizing message.
913
            if (this.is_building() == 'BUILD') {
914
                return synnefo.config.BUILDING_MESSAGES['FINAL'];
915
            }
916
            return null;
917
        },
918

    
919
        update_status_message: function(force) {
920
            // update only if one of the specified attributes has changed
921
            if (
922
              !this.keysChanged(['diagnostics', 'progress', 'status', 'state'])
923
                && !force
924
            ) { return };
925
            
926
            // if user requested to destroy the vm set the appropriate 
927
            // message.
928
            if (this.get('state') == "DESTROY") { 
929
                message = "Terminating..."
930
                this.set({status_message: message})
931
                return;
932
            }
933
            
934
            // set error message, if vm has diagnostic message display it as
935
            // progress message
936
            if (this.in_error_state()) {
937
                var d = this.get('diagnostics');
938
                if (d && d.length) {
939
                    var message = this.status_message_from_diagnostics(d);
940
                    this.set({status_message: message});
941
                } else {
942
                    this.set({status_message: null});
943
                }
944
                return;
945
            }
946
            
947
            // identify building status message
948
            if (this.is_building()) {
949
                var self = this;
950
                var success = function(msg) {
951
                    self.set({status_message: msg});
952
                }
953
                this.get_building_status_message(success);
954
                return;
955
            }
956

    
957
            this.set({status_message:null});
958
        },
959
            
960
        // get building status message. Asynchronous function since it requires
961
        // access to vm image.
962
        get_building_status_message: function(callback) {
963
            // no progress is set, vm is in initial build status
964
            var progress = this.get("progress");
965
            if (progress == 0 || !progress) {
966
                return callback(BUILDING_MESSAGES['INIT']);
967
            }
968
            
969
            // vm has copy progress, display copy percentage
970
            if (progress > 0 && progress <= 99) {
971
                this.get_copy_details(true, undefined, _.bind(
972
                    function(details){
973
                        callback(BUILDING_MESSAGES['COPY'].format(details.copy, 
974
                                                           details.size, 
975
                                                           details.progress));
976
                }, this));
977
                return;
978
            }
979

    
980
            // copy finished display FINAL message or identify status message
981
            // from diagnostics.
982
            if (progress >= 100) {
983
                if (!this.has_diagnostics()) {
984
                        callback(BUILDING_MESSAGES['FINAL']);
985
                } else {
986
                        var d = this.get("diagnostics");
987
                        var msg = this.status_message_from_diagnostics(d);
988
                        if (msg) {
989
                              callback(msg);
990
                        }
991
                }
992
            }
993
        },
994

    
995
        get_copy_details: function(human, image, callback) {
996
            var human = human || false;
997
            var image = image || this.get_image(_.bind(function(image){
998
                var progress = this.get('progress');
999
                var size = image.get_size();
1000
                var size_copied = (size * progress / 100).toFixed(2);
1001
                
1002
                if (human) {
1003
                    size = util.readablizeBytes(size*1024*1024);
1004
                    size_copied = util.readablizeBytes(size_copied*1024*1024);
1005
                }
1006

    
1007
                callback({'progress': progress, 'size': size, 'copy': size_copied})
1008
            }, this));
1009
        },
1010

    
1011
        start_stats_update: function(force_if_empty) {
1012
            var prev_state = this.do_update_stats;
1013

    
1014
            this.do_update_stats = true;
1015
            
1016
            // fetcher initialized ??
1017
            if (!this.stats_fetcher) {
1018
                this.init_stats_intervals();
1019
            }
1020

    
1021

    
1022
            // fetcher running ???
1023
            if (!this.stats_fetcher.running || !prev_state) {
1024
                this.stats_fetcher.start();
1025
            }
1026

    
1027
            if (force_if_empty && this.get("stats") == undefined) {
1028
                this.update_stats(true);
1029
            }
1030
        },
1031

    
1032
        stop_stats_update: function(stop_calls) {
1033
            this.do_update_stats = false;
1034

    
1035
            if (stop_calls) {
1036
                this.stats_fetcher.stop();
1037
            }
1038
        },
1039

    
1040
        // clear and reinitialize update interval
1041
        init_stats_intervals: function (interval) {
1042
            this.stats_fetcher = this.get_stats_fetcher(this.stats_update_interval);
1043
            this.stats_fetcher.start();
1044
        },
1045
        
1046
        get_stats_fetcher: function(timeout) {
1047
            var cb = _.bind(function(data){
1048
                this.update_stats();
1049
            }, this);
1050
            var fetcher = new snf.api.updateHandler({'callback': cb, interval: timeout, id:'stats'});
1051
            return fetcher;
1052
        },
1053

    
1054
        // do the api call
1055
        update_stats: function(force) {
1056
            // do not update stats if flag not set
1057
            if ((!this.do_update_stats && !force) || this.updating_stats) {
1058
                return;
1059
            }
1060

    
1061
            // make the api call, execute handle_stats_update on sucess
1062
            // TODO: onError handler ???
1063
            stats_url = this.url() + "/stats";
1064
            this.updating_stats = true;
1065
            this.sync("read", this, {
1066
                handles_error:true, 
1067
                url: stats_url, 
1068
                refresh:true, 
1069
                success: _.bind(this.handle_stats_update, this),
1070
                error: _.bind(this.handle_stats_error, this),
1071
                complete: _.bind(function(){this.updating_stats = false;}, this),
1072
                critical: false,
1073
                log_error: false,
1074
                skips_timeouts: true
1075
            });
1076
        },
1077

    
1078
        get_attachment: function(id) {
1079
          var attachment = undefined;
1080
          _.each(this.get("attachments"), function(a) {
1081
            if (a.id == id) {
1082
              attachment = a;
1083
            }
1084
          });
1085
          return attachment
1086
        },
1087

    
1088
        _set_stats: function(stats) {
1089
            var silent = silent === undefined ? false : silent;
1090
            // unavailable stats while building
1091
            if (this.get("status") == "BUILD") { 
1092
                this.stats_available = false;
1093
            } else { this.stats_available = true; }
1094

    
1095
            if (this.get("status") == "DESTROY") { this.stats_available = false; }
1096
            
1097
            this.set({stats: stats}, {silent:true});
1098
            this.trigger("stats:update", stats);
1099
        },
1100

    
1101
        unbind: function() {
1102
            models.VM.__super__.unbind.apply(this, arguments);
1103
        },
1104
        
1105
        can_start: function(flv, count_current) {
1106
          var get_quota = function(key) {
1107
            return synnefo.storage.quotas.get(key).get('available');
1108
          }
1109
          var flavor = flv || this.get_flavor();
1110
          var vm_ram_current = 0, vm_cpu_current = 0;
1111
          if (flv && this.is_active() || flv && count_current) {
1112
            var current = this.get_flavor();
1113
            vm_ram_current = current.ram_to_bytes();
1114
            vm_cpu_current = parseInt(current.get('cpu'));
1115
          }
1116
          var vm_ram = flavor.ram_to_bytes();
1117
          var vm_cpu = parseInt(flavor.get('cpu'));
1118
          var available_cpu = get_quota('cyclades.cpu') + vm_cpu_current;
1119
          var available_ram = get_quota('cyclades.ram') + vm_ram_current;
1120
          if (vm_ram > available_ram || vm_cpu > available_cpu) { return false }
1121
          return true
1122
        },
1123

    
1124
        can_connect: function() {
1125
          if (!synnefo.config.hotplug_enabled && this.is_active()) { return false }
1126
          return _.contains(["ACTIVE", "STOPPED"], this.get("status")) && 
1127
                 !this.get('suspended')
1128
        },
1129

    
1130
        can_disconnect: function() {
1131
          return _.contains(["ACTIVE", "STOPPED"], this.get("status"))
1132
        },
1133

    
1134
        can_resize: function() {
1135
          return this.get('status') == 'STOPPED';
1136
        },
1137

    
1138
        handle_stats_error: function() {
1139
            stats = {};
1140
            _.each(['cpuBar', 'cpuTimeSeries', 'netBar', 'netTimeSeries'], function(k) {
1141
                stats[k] = false;
1142
            });
1143

    
1144
            this.set({'stats': stats});
1145
        },
1146

    
1147
        // this method gets executed after a successful vm stats api call
1148
        handle_stats_update: function(data) {
1149
            var self = this;
1150
            // avoid browser caching
1151
            
1152
            if (data.stats && _.size(data.stats) > 0) {
1153
                var ts = $.now();
1154
                var stats = data.stats;
1155
                var images_loaded = 0;
1156
                var images = {};
1157

    
1158
                function check_images_loaded() {
1159
                    images_loaded++;
1160

    
1161
                    if (images_loaded == 4) {
1162
                        self._set_stats(images);
1163
                    }
1164
                }
1165
                _.each(['cpuBar', 'cpuTimeSeries', 'netBar', 'netTimeSeries'], function(k) {
1166
                    
1167
                    stats[k] = stats[k] + "?_=" + ts;
1168
                    
1169
                    var stat = k.slice(0,3);
1170
                    var type = k.slice(3,6) == "Bar" ? "bar" : "time";
1171
                    var img = $("<img />");
1172
                    var val = stats[k];
1173
                    
1174
                    // load stat image to a temporary dom element
1175
                    // update model stats on image load/error events
1176
                    img.load(function() {
1177
                        images[k] = val;
1178
                        check_images_loaded();
1179
                    });
1180

    
1181
                    img.error(function() {
1182
                        images[stat + type] = false;
1183
                        check_images_loaded();
1184
                    });
1185

    
1186
                    img.attr({'src': stats[k]});
1187
                })
1188
                data.stats = stats;
1189
            }
1190

    
1191
            // do we need to change the interval ??
1192
            if (data.stats.refresh * 1000 != this.stats_update_interval) {
1193
                this.stats_update_interval = data.stats.refresh * 1000;
1194
                this.stats_fetcher.interval = this.stats_update_interval;
1195
                this.stats_fetcher.maximum_interval = this.stats_update_interval;
1196
                this.stats_fetcher.stop();
1197
                this.stats_fetcher.start(false);
1198
            }
1199
        },
1200

    
1201
        // helper method that sets the do_update_stats
1202
        // in the future this method could also make an api call
1203
        // immediaetly if needed
1204
        enable_stats_update: function() {
1205
            this.do_update_stats = true;
1206
        },
1207
        
1208
        handle_destroy: function() {
1209
            this.stats_fetcher.stop();
1210
        },
1211

    
1212
        require_reboot: function() {
1213
            if (this.is_active()) {
1214
                this.set({'reboot_required': true});
1215
            }
1216
        },
1217
        
1218
        set_pending_action: function(data) {
1219
            this.pending_action = data;
1220
            return data;
1221
        },
1222

    
1223
        // machine has pending action
1224
        update_pending_action: function(action, force) {
1225
            this.set({pending_action: action});
1226
        },
1227

    
1228
        clear_pending_action: function() {
1229
            this.set({pending_action: undefined});
1230
        },
1231

    
1232
        has_pending_action: function() {
1233
            return this.get("pending_action") ? this.get("pending_action") : false;
1234
        },
1235
        
1236
        // machine is active
1237
        is_active: function() {
1238
            return models.VM.ACTIVE_STATES.indexOf(this.state()) > -1;
1239
        },
1240
        
1241
        // machine is building 
1242
        is_building: function() {
1243
            return models.VM.BUILDING_STATES.indexOf(this.state()) > -1;
1244
        },
1245
        
1246
        is_rebooting: function() {
1247
            return this.state() == 'REBOOT';
1248
        },
1249

    
1250
        in_error_state: function() {
1251
            return this.state() === "ERROR"
1252
        },
1253

    
1254
        // user can connect to machine
1255
        is_connectable: function() {
1256
            return models.VM.CONNECT_STATES.indexOf(this.state()) > -1;
1257
        },
1258
        
1259
        remove_meta: function(key, complete, error) {
1260
            var url = this.api_path() + "/metadata/" + key;
1261
            this.api_call(url, "delete", undefined, complete, error);
1262
        },
1263

    
1264
        save_meta: function(meta, complete, error) {
1265
            var url = this.api_path() + "/metadata/" + meta.key;
1266
            var payload = {meta:{}};
1267
            payload.meta[meta.key] = meta.value;
1268
            payload._options = {
1269
                critical:false, 
1270
                error_params: {
1271
                    title: "Machine metadata error",
1272
                    extra_details: {"Machine id": this.id}
1273
            }};
1274

    
1275
            this.api_call(url, "update", payload, complete, error);
1276
        },
1277

    
1278

    
1279
        // update/get the state of the machine
1280
        state: function() {
1281
            var args = slice.call(arguments);
1282
                
1283
            if (args.length > 0 && models.VM.STATES.indexOf(args[0]) > -1) {
1284
                this.set({'state': args[0]});
1285
            }
1286

    
1287
            return this.get('state');
1288
        },
1289
        
1290
        // get the state that the api status corresponds to
1291
        state_for_api_status: function(status) {
1292
            return this.state_transition(this.state(), status);
1293
        },
1294
        
1295
        // get transition state for the corresponging api status
1296
        state_transition: function(state, new_status) {
1297
            var statuses = models.VM.STATES_TRANSITIONS[state];
1298
            if (statuses) {
1299
                if (statuses.indexOf(new_status) > -1) {
1300
                    return new_status;
1301
                } else {
1302
                    return state;
1303
                }
1304
            } else {
1305
                return new_status;
1306
            }
1307
        },
1308
        
1309
        // the current vm state is a transition state
1310
        in_transition: function() {
1311
            return models.VM.TRANSITION_STATES.indexOf(this.state()) > -1 || 
1312
                models.VM.TRANSITION_STATES.indexOf(this.get('status')) > -1;
1313
        },
1314
        
1315
        // get image object
1316
        get_image: function(callback) {
1317
            if (callback == undefined) { callback = function(){} }
1318
            var image = storage.images.get(this.get('image'));
1319
            if (!image) {
1320
                storage.images.update_unknown_id(this.get('image'), callback);
1321
                return;
1322
            }
1323
            callback(image);
1324
            return image;
1325
        },
1326
        
1327
        // get flavor object
1328
        get_flavor: function() {
1329
            var flv = storage.flavors.get(this.get('flavor'));
1330
            if (!flv) {
1331
                storage.flavors.update_unknown_id(this.get('flavor'));
1332
                flv = storage.flavors.get(this.get('flavor'));
1333
            }
1334
            return flv;
1335
        },
1336

    
1337
        get_resize_flavors: function() {
1338
          var vm_flavor = this.get_flavor();
1339
          var flavors = synnefo.storage.flavors.filter(function(f){
1340
              return f.get('disk_template') ==
1341
              vm_flavor.get('disk_template') && f.get('disk') ==
1342
              vm_flavor.get('disk');
1343
          });
1344
          return flavors;
1345
        },
1346

    
1347
        get_flavor_quotas: function() {
1348
          var flavor = this.get_flavor();
1349
          return {
1350
            cpu: flavor.get('cpu'), 
1351
            ram: flavor.get_ram_size(), 
1352
            disk:flavor.get_disk_size()
1353
          }
1354
        },
1355

    
1356
        get_meta: function(key, deflt) {
1357
            if (this.get('metadata') && this.get('metadata')) {
1358
                if (!this.get('metadata')[key]) { return deflt }
1359
                return _.escape(this.get('metadata')[key]);
1360
            } else {
1361
                return deflt;
1362
            }
1363
        },
1364

    
1365
        get_meta_keys: function() {
1366
            if (this.get('metadata') && this.get('metadata')) {
1367
                return _.keys(this.get('metadata'));
1368
            } else {
1369
                return [];
1370
            }
1371
        },
1372
        
1373
        // get metadata OS value
1374
        get_os: function() {
1375
            var image = this.get_image();
1376
            return this.get_meta('OS') || (image ? 
1377
                                            image.get_os() || "okeanos" : "okeanos");
1378
        },
1379

    
1380
        get_gui: function() {
1381
            return this.get_meta('GUI');
1382
        },
1383
        
1384
        get_hostname: function() {
1385
          return this.get_meta('hostname') || this.get('fqdn') || synnefo.config.no_fqdn_message;
1386
        },
1387

    
1388
        // get actions that the user can execute
1389
        // depending on the vm state/status
1390
        get_available_actions: function() {
1391
            return models.VM.AVAILABLE_ACTIONS[this.state()];
1392
        },
1393

    
1394
        set_profile: function(profile, net_id) {
1395
        },
1396
        
1397
        // call rename api
1398
        rename: function(new_name) {
1399
            //this.set({'name': new_name});
1400
            this.sync("update", this, {
1401
                critical: true,
1402
                data: {
1403
                    'server': {
1404
                        'name': new_name
1405
                    }
1406
                }, 
1407
                success: _.bind(function(){
1408
                    snf.api.trigger("call");
1409
                }, this)
1410
            });
1411
        },
1412
        
1413
        get_console_url: function(data) {
1414
            var url_params = {
1415
                machine: this.get("name"),
1416
                host_ip: this.get_hostname(),
1417
                host_ip_v6: this.get_hostname(),
1418
                host: data.host,
1419
                port: data.port,
1420
                password: data.password
1421
            }
1422
            return synnefo.config.ui_console_url + '?' + $.param(url_params);
1423
        },
1424
        
1425
        set_firewall: function(nic, value, success_cb, error_cb) {
1426
          var self = this;
1427
          var success = function() { self.require_reboot(); success_cb() }
1428
          var error = function() { error_cb() }
1429
          var data = {'nic': nic.id, 'profile': value, 'display': true};
1430
          var url = this.url() + "/action";
1431
          //var params = {skip_api_error: false, display: true};
1432
          this.call('firewallProfile', success, error, data);
1433
        },
1434

    
1435
        connect_floating_ip: function(ip, cb, error) {
1436
          var self = this;
1437
          var from_status = this.get('status');
1438
          this.set({'status': 'CONNECTING'});
1439
          synnefo.storage.ports.create({
1440
            port: {
1441
              network_id: ip.get('floating_network_id'),
1442
              device_id: this.id,
1443
              fixed_ips: [{'ip_address': ip.get('floating_ip_address')}]
1444
            }
1445
          }, {
1446
            success: cb, 
1447
            error: function() { error && error() },
1448
            skip_api_error: false
1449
          });
1450
        },
1451

    
1452
        // action helper
1453
        call: function(action_name, success, error, params) {
1454
            var id_param = [this.id];
1455
            
1456
            params = params || {};
1457
            success = success || function() {};
1458
            error = error || function() {};
1459

    
1460
            var self = this;
1461

    
1462
            switch(action_name) {
1463
                case 'start':
1464
                    this.__make_api_call(this.get_action_url(), // vm actions url
1465
                                         "create", // create so that sync later uses POST to make the call
1466
                                         {start:{}}, // payload
1467
                                         function() {
1468
                                             // set state after successful call
1469
                                             self.state("START"); 
1470
                                             success.apply(this, arguments);
1471
                                             snf.api.trigger("call");
1472
                                         },  
1473
                                         error, 'start', params);
1474
                    break;
1475
                case 'reboot':
1476
                    this.__make_api_call(this.get_action_url(), // vm actions url
1477
                                         "create", // create so that sync later uses POST to make the call
1478
                                         {reboot:{}}, // payload
1479
                                         function() {
1480
                                             // set state after successful call
1481
                                             self.state("REBOOT"); 
1482
                                             success.apply(this, arguments)
1483
                                             snf.api.trigger("call");
1484
                                             self.set({'reboot_required': false});
1485
                                         },
1486
                                         error, 'reboot', params);
1487
                    break;
1488
                case 'shutdown':
1489
                    this.__make_api_call(this.get_action_url(), // vm actions url
1490
                                         "create", // create so that sync later uses POST to make the call
1491
                                         {shutdown:{}}, // payload
1492
                                         function() {
1493
                                             // set state after successful call
1494
                                             self.state("SHUTDOWN"); 
1495
                                             success.apply(this, arguments)
1496
                                             snf.api.trigger("call");
1497
                                         },  
1498
                                         error, 'shutdown', params);
1499
                    break;
1500
                case 'console':
1501
                    this.__make_api_call(this.url() + "/action", "create", 
1502
                                         {'console': {'type':'vnc'}}, 
1503
                                         function(data) {
1504
                        var cons_data = data.console;
1505
                        success.apply(this, [cons_data]);
1506
                    }, undefined, 'console', params)
1507
                    break;
1508
                case 'destroy':
1509
                    this.__make_api_call(this.url(), // vm actions url
1510
                                         "delete", // create so that sync later uses POST to make the call
1511
                                         undefined, // payload
1512
                                         function() {
1513
                                             // set state after successful call
1514
                                             self.state('DESTROY');
1515
                                             success.apply(this, arguments);
1516

    
1517
                                         },  
1518
                                         error, 'destroy', params);
1519
                    break;
1520
                case 'resize':
1521
                    this.__make_api_call(this.get_action_url(), // vm actions url
1522
                                         "create", // create so that sync later uses POST to make the call
1523
                                         {resize: {flavorRef:params.flavor}}, // payload
1524
                                         function() {
1525
                                             self.state('RESIZE');
1526
                                             success.apply(this, arguments);
1527
                                             snf.api.trigger("call");
1528
                                         },  
1529
                                         error, 'resize', params);
1530
                    break;
1531
                case 'destroy':
1532
                    this.__make_api_call(this.url(), // vm actions url
1533
                                         "delete", // create so that sync later uses POST to make the call
1534
                                         undefined, // payload
1535
                                         function() {
1536
                                             // set state after successful call
1537
                                             self.state('DESTROY');
1538
                                             success.apply(this, arguments);
1539
                                             synnefo.storage.quotas.get('cyclades.vm').decrease();
1540

    
1541
                                         },  
1542
                                         error, 'destroy', params);
1543
                    break;
1544
                case 'firewallProfile':
1545
                    this.__make_api_call(this.get_action_url(), // vm actions url
1546
                                         "create",
1547
                                         {firewallProfile:{nic:params.nic, profile:params.profile}}, // payload
1548
                                         function() {
1549
                                             success.apply(this, arguments);
1550
                                             snf.api.trigger("call");
1551
                                         },  
1552
                                         error, 'start', params);
1553
                    break;
1554
                default:
1555
                    throw "Invalid VM action ("+action_name+")";
1556
            }
1557
        },
1558
        
1559
        __make_api_call: function(url, method, data, success, error, action, 
1560
                                  extra_params) {
1561
            var self = this;
1562
            error = error || function(){};
1563
            success = success || function(){};
1564

    
1565
            var params = {
1566
                url: url,
1567
                data: data,
1568
                success: function() { 
1569
                  self.handle_action_succeed.apply(self, arguments); 
1570
                  success.apply(this, arguments)
1571
                },
1572
                error: function() { 
1573
                  self.handle_action_fail.apply(self, arguments);
1574
                  error.apply(this, arguments)
1575
                },
1576
                error_params: { ns: "Machines actions", 
1577
                                title: "'" + this.get("name") + "'" + " " + action + " failed", 
1578
                                extra_details: {
1579
                                  'Machine ID': this.id, 
1580
                                  'URL': url, 
1581
                                  'Action': action || "undefined" },
1582
                                allow_reload: false
1583
                              },
1584
                display: false,
1585
                critical: false
1586
            }
1587
            _.extend(params, extra_params);
1588
            this.sync(method, this, params);
1589
        },
1590

    
1591
        handle_action_succeed: function() {
1592
            this.trigger("action:success", arguments);
1593
        },
1594
        
1595
        reset_action_error: function() {
1596
            this.action_error = false;
1597
            this.trigger("action:fail:reset", this.action_error);
1598
        },
1599

    
1600
        handle_action_fail: function() {
1601
            this.action_error = arguments;
1602
            this.trigger("action:fail", arguments);
1603
        },
1604

    
1605
        get_action_url: function(name) {
1606
            return this.url() + "/action";
1607
        },
1608

    
1609
        get_diagnostics_url: function() {
1610
            return this.url() + "/diagnostics";
1611
        },
1612

    
1613
        get_users: function() {
1614
            var image;
1615
            var users = [];
1616
            try {
1617
              var users = this.get_meta('users').split(" ");
1618
            } catch (err) { users = null }
1619
            if (!users) {
1620
              image = this.get_image();
1621
              if (image) {
1622
                  users = image.get_created_users();
1623
              }
1624
            }
1625
            return users;
1626
        },
1627

    
1628
        get_connection_info: function(host_os, success, error) {
1629
            var url = synnefo.config.ui_connect_url;
1630
            var users = this.get_users();
1631

    
1632
            params = {
1633
                ip_address: this.get_hostname(),
1634
                hostname: this.get_hostname(),
1635
                os: this.get_os(),
1636
                host_os: host_os,
1637
                ports: JSON.stringify(this.get('SNF:port_forwarding') || {}),
1638
                srv: this.id
1639
            }
1640
            
1641
            if (users.length) { 
1642
                params['username'] = _.last(users)
1643
            }
1644

    
1645
            url = url + "?" + $.param(params);
1646

    
1647
            var ajax = snf.api.sync("read", undefined, { url: url, 
1648
                                                         error:error, 
1649
                                                         success:success, 
1650
                                                         handles_error:1});
1651
        }
1652
    });
1653
    
1654
    models.VM.ACTIONS = [
1655
        'start',
1656
        'shutdown',
1657
        'reboot',
1658
        'console',
1659
        'destroy',
1660
        'resize',
1661
        'snapshot'
1662
    ]
1663

    
1664
    models.VM.TASK_STATE_STATUS_MAP = {
1665
      'BULDING': 'BUILD',
1666
      'REBOOTING': 'REBOOT',
1667
      'STOPPING': 'SHUTDOWN',
1668
      'STARTING': 'START',
1669
      'RESIZING': 'RESIZE',
1670
      'CONNECTING': 'CONNECT',
1671
      'DISCONNECTING': 'DISCONNECT',
1672
      'DESTROYING': 'DESTROY'
1673
    }
1674

    
1675
    models.VM.AVAILABLE_ACTIONS = {
1676
        'UNKNWON'       : ['destroy'],
1677
        'BUILD'         : ['destroy'],
1678
        'REBOOT'        : ['destroy'],
1679
        'STOPPED'       : ['start', 'destroy', 'resize', 'snapshot'],
1680
        'ACTIVE'        : ['shutdown', 'destroy', 'reboot', 'console', 'resize', 'snapshot'],
1681
        'ERROR'         : ['destroy'],
1682
        'DELETED'       : ['destroy'],
1683
        'DESTROY'       : ['destroy'],
1684
        'SHUTDOWN'      : ['destroy'],
1685
        'START'         : ['destroy'],
1686
        'CONNECT'       : ['destroy'],
1687
        'DISCONNECT'    : ['destroy'],
1688
        'RESIZE'        : ['destroy']
1689
    }
1690
    
1691
    models.VM.AVAILABLE_ACTIONS_INACTIVE = {}
1692

    
1693
    // api status values
1694
    models.VM.STATUSES = [
1695
        'UNKNWON',
1696
        'BUILD',
1697
        'REBOOT',
1698
        'STOPPED',
1699
        'ACTIVE',
1700
        'ERROR',
1701
        'DELETED',
1702
        'RESIZE'
1703
    ]
1704

    
1705
    // api status values
1706
    models.VM.CONNECT_STATES = [
1707
        'ACTIVE',
1708
        'REBOOT',
1709
        'SHUTDOWN'
1710
    ]
1711

    
1712
    // vm states
1713
    models.VM.STATES = models.VM.STATUSES.concat([
1714
        'DESTROY',
1715
        'SHUTDOWN',
1716
        'START',
1717
        'CONNECT',
1718
        'DISCONNECT',
1719
        'FIREWALL',
1720
        'RESIZE'
1721
    ]);
1722
    
1723
    models.VM.STATES_TRANSITIONS = {
1724
        'DESTROY' : ['DELETED'],
1725
        'SHUTDOWN': ['ERROR', 'STOPPED', 'DESTROY'],
1726
        'STOPPED': ['ERROR', 'ACTIVE', 'DESTROY'],
1727
        'ACTIVE': ['ERROR', 'STOPPED', 'REBOOT', 'SHUTDOWN', 'DESTROY'],
1728
        'START': ['ERROR', 'ACTIVE', 'DESTROY'],
1729
        'REBOOT': ['ERROR', 'ACTIVE', 'STOPPED', 'DESTROY'],
1730
        'BUILD': ['ERROR', 'ACTIVE', 'DESTROY'],
1731
        'RESIZE': ['ERROR', 'STOPPED']
1732
    }
1733

    
1734
    models.VM.TRANSITION_STATES = [
1735
        'DESTROY',
1736
        'SHUTDOWN',
1737
        'START',
1738
        'REBOOT',
1739
        'BUILD',
1740
        'RESIZE',
1741
        'DISCONNECT',
1742
        'CONNECT'
1743
    ]
1744

    
1745
    models.VM.ACTIVE_STATES = [
1746
        'BUILD', 'REBOOT', 'ACTIVE',
1747
        'SHUTDOWN', 'CONNECT', 'DISCONNECT'
1748
    ]
1749

    
1750
    models.VM.BUILDING_STATES = [
1751
        'BUILD'
1752
    ]
1753

    
1754
    models.Images = models.Collection.extend({
1755
        model: models.Image,
1756
        path: 'images',
1757
        details: true,
1758
        noUpdate: true,
1759
        supportIncUpdates: false,
1760
        meta_keys_as_attrs: ["OS", "description", "kernel", "size", "GUI"],
1761
        meta_labels: {},
1762
        read_method: 'read',
1763

    
1764
        // update collection model with id passed
1765
        // making a direct call to the image
1766
        // api url
1767
        update_unknown_id: function(id, callback) {
1768
            var url = getUrl.call(this) + "/" + id;
1769
            this.api_call(this.path + "/" + id, this.read_method, {
1770
              _options:{
1771
                async:true, 
1772
                skip_api_error:true}
1773
              }, undefined, 
1774
            _.bind(function() {
1775
                if (!this.get(id)) {
1776
                            if (this.fallback_service) {
1777
                        // if current service has fallback_service attribute set
1778
                        // use this service to retrieve the missing image model
1779
                        var tmpservice = new this.fallback_service();
1780
                        tmpservice.update_unknown_id(id, _.bind(function(img){
1781
                            img.attributes.status = "DELETED";
1782
                            this.add(img.attributes);
1783
                            callback(this.get(id));
1784
                        }, this));
1785
                    } else {
1786
                        var title = synnefo.config.image_deleted_title || 'Deleted';
1787
                        // else add a dummy DELETED state image entry
1788
                        this.add({id:id, name:title, size:-1, 
1789
                                  progress:100, status:"DELETED"});
1790
                        callback(this.get(id));
1791
                    }   
1792
                } else {
1793
                    callback(this.get(id));
1794
                }
1795
            }, this), _.bind(function(image, msg, xhr) {
1796
                if (!image) {
1797
                    var title = synnefo.config.image_deleted_title || 'Deleted';
1798
                    this.add({id:id, name:title, size:-1, 
1799
                              progress:100, status:"DELETED"});
1800
                    callback(this.get(id));
1801
                    return;
1802
                }
1803
                var img_data = this._read_image_from_request(image, msg, xhr);
1804
                this.add(img_data);
1805
                callback(this.get(id));
1806
            }, this));
1807
        },
1808

    
1809
        _read_image_from_request: function(image, msg, xhr) {
1810
            return image.image;
1811
        },
1812

    
1813
        parse: function (resp, xhr) {
1814
            var parsed = _.map(resp.images, _.bind(this.parse_meta, this));
1815
            parsed = this.fill_owners(parsed);
1816
            return parsed;
1817
        },
1818

    
1819
        fill_owners: function(images) {
1820
            // do translate uuid->displayname if needed
1821
            // store display name in owner attribute for compatibility
1822
            var uuids = [];
1823

    
1824
            var images = _.map(images, function(img, index) {
1825
                if (synnefo.config.translate_uuids) {
1826
                    uuids.push(img['owner']);
1827
                }
1828
                img['owner_uuid'] = img['owner'];
1829
                return img;
1830
            });
1831
            
1832
            if (uuids.length > 0) {
1833
                var handle_results = function(data) {
1834
                    _.each(images, function (img) {
1835
                        img['owner'] = data.uuid_catalog[img['owner_uuid']];
1836
                    });
1837
                }
1838
                // notice the async false
1839
                var uuid_map = this.translate_uuids(uuids, false, 
1840
                                                    handle_results)
1841
            }
1842
            return images;
1843
        },
1844

    
1845
        translate_uuids: function(uuids, async, cb) {
1846
            var url = synnefo.config.user_catalog_url;
1847
            var data = JSON.stringify({'uuids': uuids});
1848
          
1849
            // post to user_catalogs api
1850
            snf.api.sync('create', undefined, {
1851
                url: url,
1852
                data: data,
1853
                async: async,
1854
                success:  cb
1855
            });
1856
        },
1857

    
1858
        get_meta_key: function(img, key) {
1859
            if (img.metadata && img.metadata && img.metadata[key]) {
1860
                return _.escape(img.metadata[key]);
1861
            }
1862
            return undefined;
1863
        },
1864

    
1865
        comparator: function(img) {
1866
            return -img.get_sort_order("sortorder") || 0;
1867
        },
1868

    
1869
        parse_meta: function(img) {
1870
            _.each(this.meta_keys_as_attrs, _.bind(function(key){
1871
                if (img[key]) { return };
1872
                img[key] = this.get_meta_key(img, key) || "";
1873
            }, this));
1874
            return img;
1875
        },
1876

    
1877
        active: function() {
1878
            return this.filter(function(img){return img.get('status') != "DELETED"});
1879
        },
1880

    
1881
        predefined: function() {
1882
            return _.filter(this.active(), function(i) { return !i.get("serverRef")});
1883
        },
1884
        
1885
        fetch_for_type: function(type, complete, error) {
1886
            this.fetch({update:true, 
1887
                        success: complete, 
1888
                        error: error, 
1889
                        skip_api_error: true });
1890
        },
1891
        
1892
        get_images_for_type: function(type) {
1893
            if (this['get_{0}_images'.format(type)]) {
1894
                return this['get_{0}_images'.format(type)]();
1895
            }
1896

    
1897
            return this.active();
1898
        },
1899

    
1900
        update_images_for_type: function(type, onStart, onComplete, onError, force_load) {
1901
            var load = false;
1902
            error = onError || function() {};
1903
            function complete(collection) { 
1904
                onComplete(collection.get_images_for_type(type)); 
1905
            }
1906
            
1907
            // do we need to fetch/update current collection entries
1908
            if (load) {
1909
                onStart();
1910
                this.fetch_for_type(type, complete, error);
1911
            } else {
1912
                // fallback to complete
1913
                complete(this);
1914
            }
1915
        }
1916
    })
1917

    
1918
    models.Flavors = models.Collection.extend({
1919
        model: models.Flavor,
1920
        path: 'flavors',
1921
        details: true,
1922
        noUpdate: true,
1923
        supportIncUpdates: false,
1924
        // update collection model with id passed
1925
        // making a direct call to the flavor
1926
        // api url
1927
        update_unknown_id: function(id, callback) {
1928
            var url = getUrl.call(this) + "/" + id;
1929
            this.api_call(this.path + "/" + id, "read", {_options:{async:false, skip_api_error:true}}, undefined, 
1930
            _.bind(function() {
1931
                this.add({id:id, cpu:"Unknown", ram:"Unknown", disk:"Unknown", name: "Unknown", status:"DELETED"})
1932
            }, this), _.bind(function(flv) {
1933
                if (!flv.flavor.status) { flv.flavor.status = "DELETED" };
1934
                this.add(flv.flavor);
1935
            }, this));
1936
        },
1937

    
1938
        parse: function (resp, xhr) {
1939
            return _.map(resp.flavors, function(o) {
1940
              o.cpu = o['vcpus'];
1941
              o.disk_template = o['SNF:disk_template'];
1942
              return o
1943
            });
1944
        },
1945

    
1946
        comparator: function(flv) {
1947
            return flv.get("disk") * flv.get("cpu") * flv.get("ram");
1948
        },
1949
          
1950
        unavailable_values_for_quotas: function(quotas, flavors, extra) {
1951
            var flavors = flavors || this.active();
1952
            var index = {cpu:[], disk:[], ram:[]};
1953
            var extra = extra == undefined ? {cpu:0, disk:0, ram:0} : extra;
1954
            
1955
            _.each(flavors, function(el) {
1956

    
1957
                var disk_available = quotas['disk'] + extra.disk;
1958
                var disk_size = el.get_disk_size();
1959
                if (index.disk.indexOf(disk_size) == -1) {
1960
                  var disk = el.disk_to_bytes();
1961
                  if (disk > disk_available) {
1962
                    index.disk.push(el.get('disk'));
1963
                  }
1964
                }
1965
                
1966
                var ram_available = quotas['ram'] + extra.ram * 1024 * 1024;
1967
                var ram_size = el.get_ram_size();
1968
                if (index.ram.indexOf(ram_size) == -1) {
1969
                  var ram = el.ram_to_bytes();
1970
                  if (ram > ram_available) {
1971
                    index.ram.push(el.get('ram'))
1972
                  }
1973
                }
1974

    
1975
                var cpu = el.get('cpu');
1976
                var cpu_available = quotas['cpu'] + extra.cpu;
1977
                if (index.cpu.indexOf(cpu) == -1) {
1978
                  if (cpu > cpu_available) {
1979
                    index.cpu.push(el.get('cpu'))
1980
                  }
1981
                }
1982
            });
1983
            return index;
1984
        },
1985

    
1986
        unavailable_values_for_image: function(img, flavors) {
1987
            var flavors = flavors || this.active();
1988
            var size = img.get_size();
1989
            
1990
            var index = {cpu:[], disk:[], ram:[]};
1991

    
1992
            _.each(this.active(), function(el) {
1993
                var img_size = size;
1994
                var flv_size = el.get_disk_size();
1995
                if (flv_size < img_size) {
1996
                    if (index.disk.indexOf(el.get("disk")) == -1) {
1997
                        index.disk.push(el.get("disk"));
1998
                    }
1999
                };
2000
            });
2001
            
2002
            return index;
2003
        },
2004

    
2005
        get_flavor: function(cpu, mem, disk, disk_template, filter_list) {
2006
            if (!filter_list) { filter_list = this.models };
2007
            
2008
            return this.select(function(flv){
2009
                if (flv.get("cpu") == cpu + "" &&
2010
                   flv.get("ram") == mem + "" &&
2011
                   flv.get("disk") == disk + "" &&
2012
                   flv.get("disk_template") == disk_template &&
2013
                   filter_list.indexOf(flv) > -1) { return true; }
2014
            })[0];
2015
        },
2016
        
2017
        get_data: function(lst) {
2018
            var data = {'cpu': [], 'mem':[], 'disk':[], 'disk_template':[]};
2019

    
2020
            _.each(lst, function(flv) {
2021
                if (data.cpu.indexOf(flv.get("cpu")) == -1) {
2022
                    data.cpu.push(flv.get("cpu"));
2023
                }
2024
                if (data.mem.indexOf(flv.get("ram")) == -1) {
2025
                    data.mem.push(flv.get("ram"));
2026
                }
2027
                if (data.disk.indexOf(flv.get("disk")) == -1) {
2028
                    data.disk.push(flv.get("disk"));
2029
                }
2030
                if (data.disk_template.indexOf(flv.get("disk_template")) == -1) {
2031
                    data.disk_template.push(flv.get("disk_template"));
2032
                }
2033
            })
2034
            
2035
            return data;
2036
        },
2037

    
2038
        active: function() {
2039
            return this.filter(function(flv){return flv.get('status') != "DELETED"});
2040
        }
2041
            
2042
    })
2043

    
2044
    models.VMS = models.Collection.extend({
2045
        model: models.VM,
2046
        path: 'servers',
2047
        details: true,
2048
        copy_image_meta: true,
2049

    
2050
        parse: function (resp, xhr) {
2051
            var data = resp;
2052
            if (!resp) { return [] };
2053
            data = _.filter(_.map(resp.servers, 
2054
                                  _.bind(this.parse_vm_api_data, this)), 
2055
                                  function(v){return v});
2056
            return data;
2057
        },
2058

    
2059
        parse_vm_api_data: function(data) {
2060
            var status;
2061
            // do not add non existing DELETED entries
2062
            if (data.status && data.status == "DELETED") {
2063
                if (!this.get(data.id)) {
2064
                    return false;
2065
                }
2066
            }
2067
            
2068
            if ('SNF:task_state' in data) { 
2069
                data['task_state'] = data['SNF:task_state'];
2070
                // Update machine state based on task_state value
2071
                // Do not apply task_state logic when machine is in ERROR state.
2072
                // In that case only update from task_state only if equals to
2073
                // DESTROY
2074
                if (data['task_state']) {
2075
                    if (data['status'] != 'ERROR' && data['task_state'] != 'DESTROY') {
2076
                      status = models.VM.TASK_STATE_STATUS_MAP[data['task_state']];
2077
                      if (status) { data['status'] = status }
2078
                    }
2079
                }
2080
            }
2081

    
2082
            // OS attribute
2083
            if (this.has_meta(data)) {
2084
                data['OS'] = data.metadata.OS || snf.config.unknown_os;
2085
            }
2086
            
2087
            if (!data.diagnostics) {
2088
                data.diagnostics = [];
2089
            }
2090

    
2091
            // network metadata
2092
            data['firewalls'] = {};
2093
            data['fqdn'] = data['SNF:fqdn'];
2094

    
2095
            // if vm has no metadata, no metadata object
2096
            // is in json response, reset it to force
2097
            // value update
2098
            if (!data['metadata']) {
2099
                data['metadata'] = {};
2100
            }
2101
            
2102
            // v2.0 API returns objects
2103
            data.image_obj = data.image;
2104
            data.image = data.image_obj.id;
2105
            data.flavor_obj = data.flavor;
2106
            data.flavor = data.flavor_obj.id;
2107

    
2108
            return data;
2109
        },
2110

    
2111
        get_reboot_required: function() {
2112
            return this.filter(function(vm){return vm.get("reboot_required") == true})
2113
        },
2114

    
2115
        has_pending_actions: function() {
2116
            return this.filter(function(vm){return vm.pending_action}).length > 0;
2117
        },
2118

    
2119
        reset_pending_actions: function() {
2120
            this.each(function(vm) {
2121
                vm.clear_pending_action();
2122
            })
2123
        },
2124

    
2125
        do_all_pending_actions: function(success, error) {
2126
            this.each(function(vm) {
2127
                if (vm.has_pending_action()) {
2128
                    vm.call(vm.pending_action, success, error);
2129
                    vm.clear_pending_action();
2130
                }
2131
            })
2132
        },
2133
        
2134
        do_all_reboots: function(success, error) {
2135
            this.each(function(vm) {
2136
                if (vm.get("reboot_required")) {
2137
                    vm.call("reboot", success, error);
2138
                }
2139
            });
2140
        },
2141

    
2142
        reset_reboot_required: function() {
2143
            this.each(function(vm) {
2144
                vm.set({'reboot_required': undefined});
2145
            })
2146
        },
2147
        
2148
        stop_stats_update: function(exclude) {
2149
            var exclude = exclude || [];
2150
            this.each(function(vm) {
2151
                if (exclude.indexOf(vm) > -1) {
2152
                    return;
2153
                }
2154
                vm.stop_stats_update();
2155
            })
2156
        },
2157
        
2158
        has_meta: function(vm_data) {
2159
            return vm_data.metadata && vm_data.metadata
2160
        },
2161

    
2162
        has_addresses: function(vm_data) {
2163
            return vm_data.metadata && vm_data.metadata
2164
        },
2165

    
2166
        create: function (name, image, flavor, meta, extra, callback) {
2167

    
2168
            if (this.copy_image_meta) {
2169
                if (synnefo.config.vm_image_common_metadata) {
2170
                    _.each(synnefo.config.vm_image_common_metadata, 
2171
                        function(key){
2172
                            if (image.get_meta(key)) {
2173
                                meta[key] = image.get_meta(key);
2174
                            }
2175
                    });
2176
                }
2177

    
2178
                if (image.get("OS")) {
2179
                    meta['OS'] = image.get("OS");
2180
                }
2181
            }
2182
            
2183
            opts = {name: name, imageRef: image.id, flavorRef: flavor.id, 
2184
                    metadata:meta}
2185
            opts = _.extend(opts, extra);
2186
            
2187
            var cb = function(data) {
2188
              synnefo.storage.quotas.get('cyclades.vm').increase();
2189
              callback(data);
2190
            }
2191

    
2192
            this.api_call(this.path, "create", {'server': opts}, undefined, 
2193
                          undefined, cb, {critical: true});
2194
        },
2195

    
2196
        load_missing_images: function(callback) {
2197
          var missing_ids = [];
2198
          var resolved = 0;
2199

    
2200
          // fill missing_ids
2201
          this.each(function(el) {
2202
            var imgid = el.get("image");
2203
            var existing = synnefo.storage.images.get(imgid);
2204
            if (!existing && missing_ids.indexOf(imgid) == -1) {
2205
              missing_ids.push(imgid);
2206
            }
2207
          });
2208
          var check = function() {
2209
            // once all missing ids where resolved continue calling the 
2210
            // callback
2211
            resolved++;
2212
            if (resolved == missing_ids.length) {
2213
              callback(missing_ids)
2214
            }
2215
          }
2216
          if (missing_ids.length == 0) {
2217
            callback(missing_ids);
2218
            return;
2219
          }
2220
          // start resolving missing image ids
2221
          _(missing_ids).each(function(imgid){
2222
            synnefo.storage.images.update_unknown_id(imgid, check);
2223
          });
2224
        },
2225

    
2226
        get_connectable: function() {
2227
            return storage.vms.filter(function(vm){
2228
                return !vm.in_error_state() && !vm.is_building();
2229
            });
2230
        }
2231
    })
2232
    
2233
    models.PublicKey = models.Model.extend({
2234
        path: 'keys',
2235
        api_type: 'userdata',
2236
        detail: false,
2237
        model_actions: {
2238
          'remove': [['name'], function() {
2239
            return true;
2240
          }]
2241
        },
2242

    
2243
        get_public_key: function() {
2244
            return cryptico.publicKeyFromString(this.get("content"));
2245
        },
2246

    
2247
        get_filename: function() {
2248
            return "{0}.pub".format(this.get("name"));
2249
        },
2250

    
2251
        identify_type: function() {
2252
            try {
2253
                var cont = snf.util.validatePublicKey(this.get("content"));
2254
                var type = cont.split(" ")[0];
2255
                return synnefo.util.publicKeyTypesMap[type];
2256
            } catch (err) { return false };
2257
        },
2258

    
2259
        rename: function(new_name) {
2260
          //this.set({'name': new_name});
2261
          this.sync("update", this, {
2262
            critical: true,
2263
            data: {'name': new_name}, 
2264
            success: _.bind(function(){
2265
              snf.api.trigger("call");
2266
            }, this)
2267
          });
2268
        },
2269

    
2270
        do_remove: function() {
2271
          this.actions.reset_pending();
2272
          this.remove(function() {
2273
            synnefo.storage.keys.fetch();
2274
          });
2275
        }
2276
    })
2277
    
2278
    models._ActionsModel = models.Model.extend({
2279
      defaults: { pending: null },
2280
      actions: [],
2281
      status: {
2282
        INACTIVE: 0,
2283
        PENDING: 1,
2284
        CALLED: 2
2285
      },
2286

    
2287
      initialize: function(attrs, opts) {
2288
        models._ActionsModel.__super__.initialize.call(this, attrs);
2289
        this.actions = opts.actions;
2290
        this.model = opts.model;
2291
        this.bind("change", function() {
2292
          this.set({'pending': this.get_pending()});
2293
        }, this);
2294
        this.clear();
2295
      },
2296
      
2297
      _in_status: function(st) {
2298
        var actions = null;
2299
        _.each(this.attributes, function(status, action){
2300
          if (status == st) {
2301
            if (!actions) {
2302
              actions = []
2303
            }
2304
            actions.push(action);
2305
          }
2306
        });
2307
        return actions;
2308
      },
2309

    
2310
      get_pending: function() {
2311
        return this._in_status(this.status.PENDING);
2312
      },
2313

    
2314
      unset_pending_action: function(action) {
2315
        var data = {};
2316
        data[action] = this.status.INACTIVE;
2317
        this.set(data);
2318
        this.trigger("unset-pending", action);
2319
      },
2320

    
2321
      set_pending_action: function(action, reset_pending) {
2322
        reset_pending = reset_pending === undefined ? true : reset_pending;
2323
        var data = {};
2324
        data[action] = this.status.PENDING;
2325
        if (reset_pending) {
2326
          this.reset_pending();
2327
        }
2328
        this.set(data);
2329
        this.trigger("set-pending", action);
2330
      },
2331
      
2332
      reset_pending: function() {
2333
        var data = {};
2334
        _.each(this.actions, function(action) {
2335
          data[action] = this.status.INACTIVE;
2336
        }, this);
2337
        this.set(data);
2338
        this.trigger("reset-pending");
2339
      }
2340
    });
2341

    
2342
    models.PublicPool = models.Model.extend({});
2343
    models.PublicPools = models.Collection.extend({
2344
      model: models.PublicPool,
2345
      path: 'os-floating-ip-pools',
2346
      api_type: 'compute',
2347
      noUpdate: true,
2348

    
2349
      parse: function(data) {
2350
        return _.map(data.floating_ip_pools, function(pool) {
2351
          pool.id = pool.name;
2352
          return pool;
2353
        });
2354
      }
2355
    });
2356

    
2357
    models.PublicKeys = models.Collection.extend({
2358
        model: models.PublicKey,
2359
        details: false,
2360
        path: 'keys',
2361
        api_type: 'userdata',
2362
        noUpdate: true,
2363
        updateEntries: true,
2364

    
2365
        generate_new: function(success, error) {
2366
            snf.api.sync('create', undefined, {
2367
                url: getUrl.call(this, this.base_url) + "/generate", 
2368
                success: success, 
2369
                error: error,
2370
                skip_api_error: true
2371
            });
2372
        },
2373
        
2374
        add_crypto_key: function(key, success, error, options) {
2375
            var options = options || {};
2376
            var m = new models.PublicKey();
2377

    
2378
            // guess a name
2379
            var name_tpl = "my generated public key";
2380
            var name = name_tpl;
2381
            var name_count = 1;
2382
            
2383
            while(this.filter(function(m){ return m.get("name") == name }).length > 0) {
2384
                name = name_tpl + " " + name_count;
2385
                name_count++;
2386
            }
2387
            
2388
            m.set({name: name});
2389
            m.set({content: key});
2390
            
2391
            options.success = function () { return success(m) };
2392
            options.errror = error;
2393
            options.skip_api_error = true;
2394
            
2395
            this.create(m.attributes, options);
2396
        }
2397
    });
2398

    
2399
  
2400
    models.Quota = models.Model.extend({
2401

    
2402
        initialize: function() {
2403
            models.Quota.__super__.initialize.apply(this, arguments);
2404
            this.bind("change", this.check, this);
2405
            this.check();
2406
        },
2407
        
2408
        check: function() {
2409
            var usage, limit;
2410
            usage = this.get('usage');
2411
            limit = this.get('limit');
2412
            if (usage >= limit) {
2413
                this.trigger("available");
2414
            } else {
2415
                this.trigger("unavailable");
2416
            }
2417
        },
2418

    
2419
        increase: function(val) {
2420
            if (val === undefined) { val = 1};
2421
            this.set({'usage': this.get('usage') + val})
2422
        },
2423

    
2424
        decrease: function(val) {
2425
            if (val === undefined) { val = 1};
2426
            this.set({'usage': this.get('usage') - val})
2427
        },
2428

    
2429
        can_consume: function() {
2430
            var usage, limit;
2431
            usage = this.get('usage');
2432
            limit = this.get('limit');
2433
            if (usage >= limit) {
2434
                return false
2435
            } else {
2436
                return true
2437
            }
2438
        },
2439
        
2440
        is_bytes: function() {
2441
            return this.get('resource').get('unit') == 'bytes';
2442
        },
2443
        
2444
        get_available: function(active) {
2445
            suffix = '';
2446
            if (active) { suffix = '_active'}
2447
            var value = this.get('limit'+suffix) - this.get('usage'+suffix);
2448
            if (active) {
2449
              if (this.get('available') <= value) {
2450
                value = this.get('available');
2451
              }
2452
            }
2453
            if (value < 0) { return value }
2454
            return value
2455
        },
2456

    
2457
        get_readable: function(key, active) {
2458
            var value;
2459
            if (key == 'available') {
2460
                value = this.get_available(active);
2461
            } else {
2462
                value = this.get(key)
2463
            }
2464
            if (value <= 0) { value = 0 }
2465
            // greater than max js int (assume infinite quota)
2466
            if (value > Math.pow(2, 53)) { 
2467
              return "Infinite"
2468
            }
2469

    
2470
            if (!this.is_bytes()) {
2471
              return value + "";
2472
            }
2473
            
2474
            return snf.util.readablizeBytes(value);
2475
        }
2476
    });
2477

    
2478
    models.Quotas = models.Collection.extend({
2479
        model: models.Quota,
2480
        api_type: 'accounts',
2481
        path: 'quotas',
2482
        parse: function(resp) {
2483
            filtered = _.map(resp.system, function(value, key) {
2484
                var available = (value.limit - value.usage) || 0;
2485
                var available_active = available;
2486
                var keysplit = key.split(".");
2487
                var limit_active = value.limit;
2488
                var usage_active = value.usage;
2489
                keysplit[keysplit.length-1] = "total_" + keysplit[keysplit.length-1];
2490
                var activekey = keysplit.join(".");
2491
                var exists = resp.system[activekey];
2492
                if (exists) {
2493
                    available_active = exists.limit - exists.usage;
2494
                    limit_active = exists.limit;
2495
                    usage_active = exists.usage;
2496
                }
2497
                return _.extend(value, {'name': key, 'id': key, 
2498
                          'available': available,
2499
                          'available_active': available_active,
2500
                          'limit_active': limit_active,
2501
                          'usage_active': usage_active,
2502
                          'resource': snf.storage.resources.get(key)});
2503
            });
2504
            return filtered;
2505
        },
2506
        
2507
        get_by_id: function(k) {
2508
          return this.filter(function(q) { return q.get('name') == k})[0]
2509
        },
2510

    
2511
        get_available_for_vm: function(options) {
2512
          var quotas = synnefo.storage.quotas;
2513
          var key = 'available';
2514
          var available_quota = {};
2515
          _.each(['cyclades.ram', 'cyclades.cpu', 'cyclades.disk'], 
2516
            function (key) {
2517
              var value = quotas.get(key).get_available(true);
2518
              available_quota[key.replace('cyclades.', '')] = value;
2519
          });
2520
          return available_quota;
2521
        }
2522
    })
2523

    
2524
    models.Resource = models.Model.extend({
2525
        api_type: 'accounts',
2526
        path: 'resources'
2527
    });
2528

    
2529
    models.Resources = models.Collection.extend({
2530
        api_type: 'accounts',
2531
        path: 'resources',
2532
        model: models.Network,
2533

    
2534
        parse: function(resp) {
2535
            return _.map(resp, function(value, key) {
2536
                return _.extend(value, {'name': key, 'id': key});
2537
            })
2538
        }
2539
    });
2540
    
2541
    // storage initialization
2542
    snf.storage.images = new models.Images();
2543
    snf.storage.flavors = new models.Flavors();
2544
    snf.storage.vms = new models.VMS();
2545
    snf.storage.keys = new models.PublicKeys();
2546
    snf.storage.resources = new models.Resources();
2547
    snf.storage.quotas = new models.Quotas();
2548
    snf.storage.public_pools = new models.PublicPools();
2549

    
2550
})(this);