Revision 198b546d snf-cyclades-app/synnefo/ui/static/snf/js/neutron.js

b/snf-cyclades-app/synnefo/ui/static/snf/js/neutron.js
1
// Copyright 2014 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

  
1 35
;(function(root){
2 36
    // Neutron api models, collections, helpers
3 37
  
......
60 94
    models.Network = models.NetworkModel.extend({
61 95
      path: 'networks',
62 96

  
97
      url: function(options, method) {
98
        var url = models.Network.__super__.url.call(this, method, options);
99
        if (options.data && options.data.reassign) {
100
          return url + '/action';
101
        }
102
        return url;
103
      },
104

  
63 105
      parse: function(obj) {
64 106
        return obj.network;
65 107
      },
......
83 125
        this.actions.reset_pending();
84 126
        this.destroy({
85 127
          success: _.bind(function() {
128
            synnefo.api.trigger("quotas:call", 10);
86 129
            this.set({status: 'REMOVING'});
87 130
            this.set({ext_status: 'REMOVING'});
88 131
            // force status display update
......
140 183
        'subnets': ['subnets', 'subnet', function(model, attr) {
141 184
          var subnets = model.get(attr);
142 185
          if (subnets && subnets.length) { return subnets[0] }
143
        }]
186
        }],
187
        'tenant_id': ['projects', 'project']
144 188
      },
145 189

  
146 190
      // call rename api
147 191
      rename: function(new_name, cb) {
192
          var self = this;
148 193
          this.sync("update", this, {
149 194
              critical: true,
150 195
              data: {
......
156 201
              success: _.bind(function(){
157 202
                  //this.set({name: new_name});
158 203
                  snf.api.trigger("call");
204
                  self.set({name: new_name});
159 205
              }, this),
160 206
              complete: cb || function() {}
161 207
          });
......
244 290
        this.pending_connections++;
245 291
        this.update_connecting_status();
246 292
        synnefo.storage.ports.create(data, {complete: cb});
247
      }
293
      },
294

  
295
      reassign_to_project: function(project, success, cb) {
296
        var project_id = project.id ? project.id : project;
297
        var self = this;
298
        var _success = function() {
299
          success();
300
          self.set({'tenant_id': project_id});
301
        }
302
        synnefo.api.sync('create', this, {
303
          success: _success,
304
          complete: cb,
305
          data: { 
306
            reassign: { 
307
              project: project_id 
308
            }
309
          }
310
        });
311
      },
248 312
    });
249 313
    
250 314
    models.CombinedPublicNetwork = models.Network.extend({
......
304 368
      },
305 369

  
306 370
      get_floating_ips_network: function() {
307
        return this.filter(function(n) { return n.get('is_public')})[1]
371
        return this.filter(function(n) { return n.get('is_public') })[1]
308 372
      },
309 373
      
310 374
      create_subnet: function(subnet_params, complete, error) {
......
314 378
        });
315 379
      },
316 380

  
317
      create: function (name, type, cidr, dhcp, callback) {
381
      create: function (project, name, type, cidr, dhcp, callback) {
318 382
        var quota = synnefo.storage.quotas;
319 383
        var params = {network:{name:name}};
320 384
        var subnet_params = {subnet:{network_id:undefined}};
321 385
        if (!type) { throw "Network type cannot be empty"; }
322 386

  
323 387
        params.network.type = type;
388
        params.network.project = project.id;
324 389
        if (cidr) { subnet_params.subnet.cidr = cidr; }
325 390
        if (dhcp) { subnet_params.subnet.dhcp_enabled = dhcp; }
326 391
        if (dhcp === false) { subnet_params.subnet.dhcp_enabled = false; }
327 392
        
328 393
        var cb = function() {
394
          synnefo.api.trigger("quotas:call");
329 395
          callback && callback();
330 396
        }
331 397
        
......
350 416
              created_network.destroy({no_skip: true});
351 417
            });
352 418
          }
353
          quota.get('cyclades.network.private').increase();
419
          project.quotas.get('cyclades.network.private').increase();
354 420
        }
355 421
        return this.api_call(this.path, "create", params, complete, error, success);
356 422
      }
......
502 568

  
503 569
    models.FloatingIP = models.NetworkModel.extend({
504 570
      path: 'floatingips',
571
    
572
      url: function(options, method) {
573
        var url = models.FloatingIP.__super__.url.call(this, method, options);
574
        if (options.data && options.data.reassign) {
575
          return url + '/action';
576
        }
577
        return url;
578
      },
505 579

  
506 580
      parse: function(obj) {
507 581
        return obj.floatingip;
508 582
      },
509 583

  
510 584
      storage_attrs: {
585
        'tenant_id': ['projects', 'project'],
511 586
        'port_id': ['ports', 'port'],
512 587
        'floating_network_id': ['networks', 'network'],
513 588
      },
......
531 606
        }]
532 607
      },
533 608
      
609
      reassign_to_project: function(project, success, cb) {
610
        var project_id = project.id ? project.id : project;
611
        var self = this;
612
        var _success = function() {
613
          success();
614
          self.set({'tenant_id': project_id});
615
        }
616
        synnefo.api.sync('create', this, {
617
          success: _success,
618
          complete: cb,
619
          data: { 
620
            reassign: { 
621
              project: project_id 
622
            }
623
          }
624
        });
625
      },
626

  
534 627
      do_remove: function(succ, err) { return this.do_destroy(succ, err) },
535 628
      do_destroy: function(succ, err) {
536 629
        this.actions.reset_pending();
537 630
        this.destroy({
538 631
          success: _.bind(function() {
632
            synnefo.api.trigger("quotas:call", 10);
539 633
            this.set({status: 'REMOVING'});
540 634
            succ && succ();
541 635
          }, this),

Also available in: Unified diff