root / snf-cyclades-app / synnefo / ui / static / snf / js / neutron.js @ 1605639f
History | View | Annotate | Download (16.5 kB)
1 |
;(function(root){
|
---|---|
2 |
// Neutron api models, collections, helpers
|
3 |
|
4 |
// root
|
5 |
var root = root;
|
6 |
|
7 |
// setup namepsaces
|
8 |
var snf = root.synnefo = root.synnefo || {};
|
9 |
var snfmodels = snf.models = snf.models || {}
|
10 |
var models = snfmodels.networks = snfmodels.networks || {};
|
11 |
var storage = snf.storage = snf.storage || {};
|
12 |
var util = snf.util = snf.util || {};
|
13 |
|
14 |
// shortcuts
|
15 |
var bb = root.Backbone;
|
16 |
var slice = Array.prototype.slice
|
17 |
|
18 |
// logging
|
19 |
var logger = new snf.logging.logger("SNF-MODELS"); |
20 |
var debug = _.bind(logger.debug, logger);
|
21 |
|
22 |
// Neutron base model, extending existing synnefo model
|
23 |
models.NetworkModel = snfmodels.Model.extend({ |
24 |
api_type: 'network' |
25 |
}); |
26 |
|
27 |
// Neutron base collection, common neutron collection params are shared
|
28 |
models.NetworkCollection = snfmodels.Collection.extend({ |
29 |
api_type: 'network', |
30 |
details: true, |
31 |
noUpdate: true, |
32 |
updateEntries: true, |
33 |
add_on_create: true |
34 |
}); |
35 |
|
36 |
// Subnet model
|
37 |
models.Subnet = models.NetworkModel.extend(); |
38 |
|
39 |
// Subnet collection
|
40 |
models.Subnets = models.NetworkCollection.extend({ |
41 |
model: models.Subnet,
|
42 |
details: false, |
43 |
path: 'subnets', |
44 |
parse: function(resp) { |
45 |
return resp.subnets
|
46 |
} |
47 |
}); |
48 |
|
49 |
// Network
|
50 |
models.Network = models.NetworkModel.extend({ |
51 |
path: 'networks', |
52 |
|
53 |
parse: function(obj) { |
54 |
return obj.network;
|
55 |
}, |
56 |
|
57 |
// Available network actions.
|
58 |
// connect:
|
59 |
model_actions: {
|
60 |
'connect': [['status', 'is_public'], function() { |
61 |
//TODO: Also check network status
|
62 |
return !this.is_public() && _.contains(['ACTIVE'], this.get('status')); |
63 |
}], |
64 |
'remove': [['status', 'is_public', 'ports'], function() { |
65 |
if (this.ports && this.ports.length) { |
66 |
return false |
67 |
} |
68 |
return !this.is_public() && _.contains(['ACTIVE'], this.get('status')); |
69 |
}] |
70 |
}, |
71 |
|
72 |
proxy_attrs: {
|
73 |
'is_public': [
|
74 |
['router:external', 'public'], function() { |
75 |
return this.get('router:external') || this.get('public') |
76 |
} |
77 |
], |
78 |
'is_floating': [
|
79 |
['SNF:floating_ip_pool'], function() { |
80 |
return this.get('SNF:floating_ip_pool') |
81 |
} |
82 |
], |
83 |
'cidr': [
|
84 |
['subnet'], function() { |
85 |
var subnet = this.get('subnet'); |
86 |
if (subnet && subnet.get('cidr')) { |
87 |
return subnet.get('cidr') |
88 |
} else {
|
89 |
return undefined |
90 |
} |
91 |
} |
92 |
], |
93 |
'ext_status': [
|
94 |
['status', 'cidr'], function(st) { |
95 |
if (this.get('ext_status') == 'REMOVING') { |
96 |
return 'REMOVING' |
97 |
} |
98 |
if (this.pending_connections) { |
99 |
return 'CONNECTING' |
100 |
} else if (this.pending_disconnects) { |
101 |
return 'DISCONNECTING' |
102 |
} else {
|
103 |
return this.get('status') |
104 |
} |
105 |
}], |
106 |
'in_progress': [
|
107 |
['ext_status'], function() { |
108 |
return _.contains(['CONNECTING', |
109 |
'DISCONNECTING',
|
110 |
'REMOVING'],
|
111 |
this.get('ext_status')) |
112 |
} |
113 |
] |
114 |
}, |
115 |
|
116 |
storage_attrs: {
|
117 |
'subnets': ['subnets', 'subnet', function(model, attr) { |
118 |
var subnets = model.get(attr);
|
119 |
if (subnets && subnets.length) { return subnets[0] } |
120 |
}] |
121 |
}, |
122 |
|
123 |
// call rename api
|
124 |
rename: function(new_name, cb) { |
125 |
this.sync("update", this, { |
126 |
critical: true, |
127 |
data: {
|
128 |
'network': {
|
129 |
'name': new_name
|
130 |
} |
131 |
}, |
132 |
// do the rename after the method succeeds
|
133 |
success: _.bind(function(){ |
134 |
//this.set({name: new_name});
|
135 |
snf.api.trigger("call");
|
136 |
}, this),
|
137 |
complete: cb || function() {} |
138 |
}); |
139 |
}, |
140 |
|
141 |
pending_connections: 0, |
142 |
pending_disconnects: 0, |
143 |
|
144 |
initialize: function() { |
145 |
var self = this; |
146 |
this.subnets = new Backbone.FilteredCollection(undefined, { |
147 |
collection: synnefo.storage.subnets,
|
148 |
collectionFilter: function(m) { |
149 |
return self.id == m.get('network_id') |
150 |
}}); |
151 |
this.ports = new Backbone.FilteredCollection(undefined, { |
152 |
collection: synnefo.storage.ports,
|
153 |
collectionFilter: function(m) { |
154 |
return self.id == m.get('network_id') |
155 |
} |
156 |
}); |
157 |
this.ports.network = this; |
158 |
this.ports.bind("reset", function() { |
159 |
this.pending_connections = 0; |
160 |
this.pending_disconnects = 0; |
161 |
this.update_connecting_status();
|
162 |
this.update_actions();
|
163 |
}, this);
|
164 |
this.ports.bind("add", function() { |
165 |
this.pending_connections--;
|
166 |
this.update_connecting_status();
|
167 |
this.update_actions();
|
168 |
}, this);
|
169 |
this.ports.bind("remove", function() { |
170 |
this.pending_disconnects--;
|
171 |
this.update_connecting_status();
|
172 |
this.update_actions();
|
173 |
}, this);
|
174 |
this.set({ports: this.ports}); |
175 |
|
176 |
this.connectable_vms = new Backbone.FilteredCollection(undefined, { |
177 |
collection: synnefo.storage.vms,
|
178 |
collectionFilter: function(m) { |
179 |
return m.can_connect();
|
180 |
} |
181 |
}); |
182 |
models.Network.__super__.initialize.apply(this, arguments); |
183 |
this.update_actions();
|
184 |
}, |
185 |
|
186 |
update_actions: function() { |
187 |
if (this.ports.length) { |
188 |
this.set({can_remove: false}) |
189 |
} else {
|
190 |
this.set({can_remove: true}) |
191 |
} |
192 |
}, |
193 |
|
194 |
update_connecting_status: function() { |
195 |
if (this.pending_connections <= 0) { |
196 |
this.pending_connections = 0; |
197 |
} |
198 |
if (this.pending_disconnects <= 0) { |
199 |
this.pending_disconnects = 0; |
200 |
} |
201 |
this.trigger('change:status', this.get('status')); |
202 |
}, |
203 |
|
204 |
get_nics: function() { |
205 |
return this.nics.models |
206 |
}, |
207 |
|
208 |
is_public: function() { |
209 |
return this.get('router:external') |
210 |
}, |
211 |
|
212 |
connect_vm: function(vm, cb) { |
213 |
var self = this; |
214 |
var data = {
|
215 |
port: {
|
216 |
network_id: this.id, |
217 |
device_id: vm.id
|
218 |
} |
219 |
} |
220 |
|
221 |
this.pending_connections++;
|
222 |
this.update_connecting_status();
|
223 |
synnefo.storage.ports.create(data, {complete: cb});
|
224 |
} |
225 |
}); |
226 |
|
227 |
models.CombinedPublicNetwork = models.Network.extend({ |
228 |
defaults: {
|
229 |
'admin_state_up': true, |
230 |
'id': 'snf-combined-public-network', |
231 |
'name': 'Internet', |
232 |
'status': 'ACTIVE', |
233 |
'router:external': true, |
234 |
'shared': false, |
235 |
'rename_disabled': true, |
236 |
'subnets': []
|
237 |
}, |
238 |
|
239 |
initialize: function() { |
240 |
var self = this; |
241 |
this.ports = new Backbone.FilteredCollection(undefined, { |
242 |
collection: synnefo.storage.ports,
|
243 |
collectionFilter: function(m) { |
244 |
return m.get('network') && m.get('network').get('is_public'); |
245 |
} |
246 |
}); |
247 |
this.set({ports: this.ports}); |
248 |
this.floating_ips = synnefo.storage.floating_ips;
|
249 |
this.set({floating_ips: this.floating_ips}); |
250 |
|
251 |
this.available_floating_ips = new Backbone.FilteredCollection(undefined, { |
252 |
collection: synnefo.storage.floating_ips,
|
253 |
collectionFilter: function(m) { |
254 |
return !m.get('port_id'); |
255 |
} |
256 |
}); |
257 |
this.set({available_floating_ips: this.available_floating_ips}); |
258 |
models.Network.__super__.initialize.apply(this, arguments); |
259 |
}, |
260 |
|
261 |
}) |
262 |
|
263 |
models.Networks = models.NetworkCollection.extend({ |
264 |
model: models.Network,
|
265 |
path: 'networks', |
266 |
details: true, |
267 |
parse: function(resp) { |
268 |
return resp.networks
|
269 |
}, |
270 |
|
271 |
get_floating_ips_network: function() { |
272 |
return this.filter(function(n) { return n.get('is_public')})[1] |
273 |
}, |
274 |
|
275 |
create: function (name, type, cidr, dhcp, callback) { |
276 |
var quota = synnefo.storage.quotas;
|
277 |
var params = {network:{name:name}}; |
278 |
var subnet_params = {subnet:{network_id:undefined}}; |
279 |
if (!type) { throw "Network type cannot be empty"; } |
280 |
|
281 |
params.network.type = type; |
282 |
if (cidr) { subnet_params.subnet.cidr = cidr; }
|
283 |
if (dhcp) { subnet_params.subnet.dhcp_enabled = dhcp; }
|
284 |
if (dhcp === false) { subnet_params.subnet.dhcp_enabled = false; } |
285 |
|
286 |
var cb = function() { |
287 |
callback(); |
288 |
} |
289 |
|
290 |
var complete = function() {}; |
291 |
var error = function() { cb() }; |
292 |
// on network create success, try to create the requested
|
293 |
// network subnet
|
294 |
var success = function(resp) { |
295 |
var network = resp.network;
|
296 |
subnet_params.subnet.network_id = network.id; |
297 |
synnefo.storage.subnets.create(subnet_params, { |
298 |
complete: function () { cb && cb() }, |
299 |
error: function() { |
300 |
var created_network = new synnefo.models.networks.Network({id: network.id}); |
301 |
created_network.destroy({no_skip: true}); |
302 |
} |
303 |
}); |
304 |
quota.get('cyclades.network.private').increase();
|
305 |
} |
306 |
return this.api_call(this.path, "create", params, complete, error, success); |
307 |
} |
308 |
}); |
309 |
|
310 |
// dummy model/collection
|
311 |
models.FixedIP = models.NetworkModel.extend({ |
312 |
storage_attrs: {
|
313 |
'subnet_id': ['subnets', 'subnet'] |
314 |
} |
315 |
}); |
316 |
models.FixedIPs = models.NetworkCollection.extend({ |
317 |
model: models.FixedIP
|
318 |
}); |
319 |
|
320 |
models.Port = models.NetworkModel.extend({ |
321 |
path: 'ports', |
322 |
parse: function(obj) { |
323 |
return obj.port;
|
324 |
}, |
325 |
initialize: function() { |
326 |
models.Port.__super__.initialize.apply(this, arguments); |
327 |
var ips = new models.FixedIPs(); |
328 |
this.set({'ips': ips}); |
329 |
this.bind('change:fixed_ips', function() { |
330 |
var ips = this.get('ips'); |
331 |
//var ips = _.map(ips, function(ip) { ip.id = ip.a})
|
332 |
this.update_ips()
|
333 |
}, this);
|
334 |
this.update_ips();
|
335 |
this.set({'pending_firewall': null}); |
336 |
}, |
337 |
|
338 |
update_ips: function() { |
339 |
var self = this; |
340 |
var ips = _.map(this.get('fixed_ips'), function(ip_obj) { |
341 |
var ip = _.clone(ip_obj);
|
342 |
var type = "v4"; |
343 |
if (ip.ip_address.indexOf(":") >= 0) { |
344 |
type = "v6";
|
345 |
} |
346 |
ip.id = ip.ip_address; |
347 |
ip.type = type; |
348 |
ip.subnet_id = ip.subnet; |
349 |
ip.port_id = self.id; |
350 |
delete ip.subnet;
|
351 |
return ip;
|
352 |
}); |
353 |
this.get('ips').update(ips, {removeMissing: true}); |
354 |
}, |
355 |
|
356 |
model_actions: {
|
357 |
'disconnect': [['status', 'network', 'vm'], function() { |
358 |
var network = this.get('network'); |
359 |
if ((!network || network.get('is_public')) && (network && !network.get('is_floating'))) { |
360 |
return false |
361 |
} |
362 |
var status_ok = _.contains(['DOWN', 'ACTIVE', 'CONNECTED'], |
363 |
this.get('status')); |
364 |
var vm_status_ok = this.get('vm') && this.get('vm').can_connect(); |
365 |
var vm_status = this.get('vm') && this.get('vm').get('status'); |
366 |
return status_ok && vm_status_ok
|
367 |
}] |
368 |
}, |
369 |
|
370 |
storage_attrs: {
|
371 |
'device_id': ['vms', 'vm'], |
372 |
'network_id': ['networks', 'network'] |
373 |
}, |
374 |
|
375 |
proxy_attrs: {
|
376 |
'firewall_status': [
|
377 |
['vm'], function(vm) { |
378 |
var attachment = vm && vm.get_attachment(this.id); |
379 |
if (!attachment) { return "DISABLED" } |
380 |
return attachment.firewallProfile
|
381 |
} |
382 |
], |
383 |
'ext_status': [
|
384 |
['status'], function() { |
385 |
if (_.contains(["DISCONNECTING"], this.get('ext_status'))) { |
386 |
return this.get("ext_status") |
387 |
} |
388 |
return this.get("status") |
389 |
} |
390 |
], |
391 |
'in_progress': [
|
392 |
['ext_status', 'vm'], function() { |
393 |
var vm_progress = this.get('vm') && this.get('vm').get('in_progress'); |
394 |
if (vm_progress) { return true } |
395 |
return _.contains(["BUILD", "DISCONNECTING", "CONNECTING"], this.get("ext_status")) |
396 |
} |
397 |
], |
398 |
// check progress of port instance only
|
399 |
'in_progress_no_vm': [
|
400 |
['ext_status'], function() { |
401 |
return _.contains(["BUILD", "DISCONNECTING", "CONNECTING"], this.get("ext_status")) |
402 |
} |
403 |
], |
404 |
'firewall_running': [
|
405 |
['firewall_status', 'pending_firewall'], function(status, pending) { |
406 |
var pending = this.get('pending_firewall'); |
407 |
var status = this.get('firewall_status'); |
408 |
if (!pending) { return false } |
409 |
if (status == pending) {
|
410 |
this.set({'pending_firewall': null}); |
411 |
} |
412 |
return status != pending;
|
413 |
} |
414 |
], |
415 |
}, |
416 |
|
417 |
disconnect: function(cb) { |
418 |
var network = this.get('network'); |
419 |
var vm = this.get('vm'); |
420 |
network.pending_disconnects++; |
421 |
network.update_connecting_status(); |
422 |
var success = _.bind(function() { |
423 |
if (vm) {
|
424 |
vm.set({'status': 'DISCONNECTING'}); |
425 |
} |
426 |
this.set({'status': 'DISCONNECTING'}); |
427 |
cb(); |
428 |
}, this);
|
429 |
this.destroy({success: success, complete: cb, silent: true}); |
430 |
} |
431 |
}); |
432 |
|
433 |
models.Ports = models.NetworkCollection.extend({ |
434 |
model: models.Port,
|
435 |
path: 'ports', |
436 |
details: true, |
437 |
noUpdate: true, |
438 |
updateEntries: true, |
439 |
|
440 |
parse: function(data) { |
441 |
return data.ports;
|
442 |
}, |
443 |
|
444 |
comparator: function(m) { |
445 |
return parseInt(m.id);
|
446 |
} |
447 |
}); |
448 |
|
449 |
models.FloatingIP = models.NetworkModel.extend({ |
450 |
path: 'floatingips', |
451 |
|
452 |
parse: function(obj) { |
453 |
return obj.floatingip;
|
454 |
}, |
455 |
|
456 |
storage_attrs: {
|
457 |
'port_id': ['ports', 'port'], |
458 |
'floating_network_id': ['networks', 'network'], |
459 |
}, |
460 |
|
461 |
model_actions: {
|
462 |
'remove': [['status'], function() { |
463 |
var status_ok = _.contains(['DISCONNECTED'], this.get('status')) |
464 |
return status_ok
|
465 |
}], |
466 |
'connect': [['status'], function() { |
467 |
var status_ok = _.contains(['DISCONNECTED'], this.get('status')) |
468 |
return status_ok
|
469 |
}], |
470 |
'disconnect': [['status', 'port_id', 'port'], function() { |
471 |
var port = this.get('port'); |
472 |
var vm = port && port.get('vm'); |
473 |
if (!vm) { return false } |
474 |
if (vm && vm.get("task_state")) { return false } |
475 |
if (vm && vm.in_error_state()) { return false } |
476 |
var status_ok = _.contains(['ACTIVE', 'CONNECTED'], this.get('status')) |
477 |
var vm_status_ok = vm.can_disconnect();
|
478 |
return status_ok && vm_status_ok;
|
479 |
}] |
480 |
}, |
481 |
|
482 |
proxy_attrs: {
|
483 |
'ip': [
|
484 |
['floating_ip_adress'], function() { |
485 |
return this.get('floating_ip_address'); |
486 |
}], |
487 |
|
488 |
'in_progress': [
|
489 |
['status'], function() { |
490 |
return _.contains(['CONNECTING', 'DISCONNECTING', 'REMOVING'], |
491 |
this.get('status')) |
492 |
} |
493 |
], |
494 |
|
495 |
'status': [
|
496 |
['port_id', 'port'], function() { |
497 |
var port_id = this.get('port_id'); |
498 |
if (!port_id) {
|
499 |
return 'DISCONNECTED' |
500 |
} else {
|
501 |
var port = this.get('port'); |
502 |
if (port) {
|
503 |
var port_status = port.get('ext_status'); |
504 |
if (port_status == "DISCONNECTING") { |
505 |
return port_status
|
506 |
} |
507 |
if (port_status == "CONNECTING") { |
508 |
return port_status
|
509 |
} |
510 |
return 'CONNECTED' |
511 |
} |
512 |
return 'CONNECTING' |
513 |
} |
514 |
} |
515 |
] |
516 |
}, |
517 |
|
518 |
disconnect: function(cb) { |
519 |
this.get('port').disconnect(cb); |
520 |
} |
521 |
}); |
522 |
|
523 |
models.FloatingIPs = models.NetworkCollection.extend({ |
524 |
model: models.FloatingIP,
|
525 |
details: false, |
526 |
path: 'floatingips', |
527 |
parse: function(resp) { |
528 |
return resp.floatingips;
|
529 |
} |
530 |
}); |
531 |
|
532 |
models.Router = models.NetworkModel.extend({ |
533 |
}); |
534 |
|
535 |
models.Routers = models.NetworkCollection.extend({ |
536 |
model: models.Router,
|
537 |
path: 'routers', |
538 |
parse: function(resp) { |
539 |
return resp.routers
|
540 |
} |
541 |
}); |
542 |
|
543 |
snf.storage.floating_ips = new models.FloatingIPs();
|
544 |
snf.storage.routers = new models.Routers();
|
545 |
snf.storage.networks = new models.Networks();
|
546 |
snf.storage.ports = new models.Ports();
|
547 |
snf.storage.subnets = new models.Subnets();
|
548 |
|
549 |
})(this);
|