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