Revision ab3df8df snf-cyclades-app/synnefo/ui/static/snf/js/models.js

b/snf-cyclades-app/synnefo/ui/static/snf/js/models.js
1044 1044
            models.VM.__super__.unbind.apply(this, arguments);
1045 1045
        },
1046 1046

  
1047
        can_resize: function() {
1048
          return this.get('status') == 'STOPPED';
1049
        },
1050

  
1047 1051
        handle_stats_error: function() {
1048 1052
            stats = {};
1049 1053
            _.each(['cpuBar', 'cpuTimeSeries', 'netBar', 'netTimeSeries'], function(k) {
......
1249 1253
            return flv;
1250 1254
        },
1251 1255

  
1256
        get_resize_flavors: function() {
1257
          var vm_flavor = this.get_flavor();
1258
          var flavors = synnefo.storage.flavors.filter(function(f){
1259
              return f.get('disk_template') ==
1260
              vm_flavor.get('disk_template') && f.get('disk') ==
1261
              vm_flavor.get('disk');
1262
          });
1263
          return flavors;
1264
        },
1265

  
1266
        get_flavor_quotas: function() {
1267
          var flavor = this.get_flavor();
1268
          return {
1269
            cpu: flavor.get('cpu') + 1, 
1270
            ram: flavor.get_ram_size() + 1, 
1271
            disk:flavor.get_disk_size() + 1
1272
          }
1273
        },
1274

  
1252 1275
        get_meta: function(key, deflt) {
1253 1276
            if (this.get('metadata') && this.get('metadata')) {
1254 1277
                if (!this.get('metadata')[key]) { return deflt }
......
1449 1472
                                         },  
1450 1473
                                         error, 'destroy', params);
1451 1474
                    break;
1475
                case 'resize':
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
                                         {resize: {flavorRef:params.flavor}}, // payload
1479
                                         function() {
1480
                                             success.apply(this, arguments)
1481
                                             snf.api.trigger("call");
1482
                                         },  
1483
                                         error, 'resize', params);
1484
                    break;
1485
                case 'destroy':
1486
                    this.__make_api_call(this.url(), // vm actions url
1487
                                         "delete", // create so that sync later uses POST to make the call
1488
                                         undefined, // payload
1489
                                         function() {
1490
                                             // set state after successful call
1491
                                             self.state('DESTROY');
1492
                                             success.apply(this, arguments);
1493
                                             synnefo.storage.quotas.get('cyclades.vm').decrease();
1494

  
1495
                                         },  
1496
                                         error, 'destroy', params);
1497
                    break;
1452 1498
                default:
1453 1499
                    throw "Invalid VM action ("+action_name+")";
1454 1500
            }
......
1902 1948
        comparator: function(flv) {
1903 1949
            return flv.get("disk") * flv.get("cpu") * flv.get("ram");
1904 1950
        },
1905

  
1906
        unavailable_values_for_quotas: function(quotas, flavors) {
1951
          
1952
        unavailable_values_for_quotas: function(quotas, flavors, extra) {
1907 1953
            var flavors = flavors || this.active();
1908 1954
            var index = {cpu:[], disk:[], ram:[]};
1955
            var extra = extra == undefined ? {cpu:0, disk:0, ram:0} : extra;
1909 1956
            
1910 1957
            _.each(flavors, function(el) {
1911 1958

  
1912
                var disk_available = quotas['disk'];
1959
                var disk_available = quotas['disk'] + extra.disk;
1913 1960
                var disk_size = el.get_disk_size();
1914 1961
                if (index.disk.indexOf(disk_size) == -1) {
1915 1962
                  var disk = el.disk_to_bytes();
......
1917 1964
                    index.disk.push(disk_size);
1918 1965
                  }
1919 1966
                }
1920

  
1921
                var ram_available = quotas['ram'];
1967
                
1968
                var ram_available = quotas['ram'] + extra.ram * 1024 * 1024;
1922 1969
                var ram_size = el.get_ram_size();
1923 1970
                if (index.ram.indexOf(ram_size) == -1) {
1924 1971
                  var ram = el.ram_to_bytes();
......
1928 1975
                }
1929 1976

  
1930 1977
                var cpu = el.get('cpu');
1931
                var cpu_available = quotas['cpu'];
1932
                if (index.ram.indexOf(cpu) == -1) {
1978
                var cpu_available = quotas['cpu'] + extra.cpu;
1979
                if (index.cpu.indexOf(cpu) == -1) {
1933 1980
                  if (cpu > cpu_available) {
1934 1981
                    index.cpu.push(el.get('cpu'))
1935 1982
                  }
......
1970 2017
        },
1971 2018
        
1972 2019
        get_data: function(lst) {
1973
            var data = {'cpu': [], 'mem':[], 'disk':[]};
2020
            var data = {'cpu': [], 'mem':[], 'disk':[], 'disk_template':[]};
1974 2021

  
1975 2022
            _.each(lst, function(flv) {
1976 2023
                if (data.cpu.indexOf(flv.get("cpu")) == -1) {
......
1982 2029
                if (data.disk.indexOf(flv.get("disk")) == -1) {
1983 2030
                    data.disk.push(flv.get("disk"));
1984 2031
                }
2032
                if (data.disk_template.indexOf(flv.get("disk_template")) == -1) {
2033
                    data.disk_template.push(flv.get("disk_template"));
2034
                }
1985 2035
            })
1986 2036
            
1987 2037
            return data;
......
2444 2494
            return this.get('resource').get('unit') == 'bytes';
2445 2495
        },
2446 2496
        
2447
        get_available: function() {
2448
            var value = this.get('limit') - this.get('usage');
2497
        get_available: function(active) {
2498
            suffix = '';
2499
            if (active) { suffix = '_active'}
2500
            var value = this.get('limit'+suffix) - this.get('usage'+suffix);
2449 2501
            if (value < 0) { return value }
2450 2502
            return value
2451 2503
        },
2452 2504

  
2453
        get_readable: function(key) {
2505
        get_readable: function(key, active) {
2454 2506
            var value;
2455 2507
            if (key == 'available') {
2456
                value = this.get_available();
2508
                value = this.get_available(active);
2457 2509
            } else {
2458 2510
                value = this.get(key)
2459 2511
            }
......
2469 2521
        api_type: 'accounts',
2470 2522
        path: 'quotas',
2471 2523
        parse: function(resp) {
2472
            return _.map(resp.system, function(value, key) {
2524
            filtered = _.map(resp.system, function(value, key) {
2473 2525
                var available = (value.limit - value.usage) || 0;
2526
                var available_active = available;
2527
                var keysplit = key.split(".");
2528
                var limit_active = value.limit;
2529
                var usage_active = value.usage;
2530
                keysplit[keysplit.length-1] = "active_" + keysplit[keysplit.length-1];
2531
                var activekey = keysplit.join(".");
2532
                var exists = resp.system[activekey];
2533
                if (exists) {
2534
                    available_active = exists.limit - exists.usage;
2535
                    limit_active = exists.limit;
2536
                    usage_active = exists.usage;
2537
                }
2474 2538
                return _.extend(value, {'name': key, 'id': key, 
2475 2539
                          'available': available,
2540
                          'available_active': available_active,
2541
                          'limit_active': limit_active,
2542
                          'usage_active': usage_active,
2476 2543
                          'resource': snf.storage.resources.get(key)});
2477
            })
2544
            });
2545
            return filtered;
2546
        },
2547
        
2548
        get_by_id: function(k) {
2549
          return this.filter(function(q) { return q.get('name') == k})[0]
2550
        },
2551

  
2552
        get_available_for_vm: function(active) {
2553
          var quotas = synnefo.storage.quotas;
2554
          var key = 'available';
2555
          if (active) { key = 'available_active'; }
2556
          var quota = {
2557
            'ram': quotas.get('cyclades.ram').get(key),
2558
            'cpu': quotas.get('cyclades.cpu').get(key),
2559
            'disk': quotas.get('cyclades.disk').get(key)
2560
          }
2561
          return quota;
2478 2562
        }
2479 2563
    })
2480 2564

  

Also available in: Unified diff