From 35a06a4364a5ef1e0e7ea5ab0e4d6ad80fcfeeb8 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Wed, 31 Jul 2013 16:11:47 +0200 Subject: [PATCH] Auto-connect to new servers. --- src/api.js | 4 +- src/collection.js | 5 +- src/xo.js | 134 +++++++++++++++++++++++----------------------- 3 files changed, 72 insertions(+), 71 deletions(-) diff --git a/src/api.js b/src/api.js index b3a9385..7a73a7c 100644 --- a/src/api.js +++ b/src/api.js @@ -481,9 +481,7 @@ Api.fn.server = { 'password': p_password, }); }).then(function (server) { - // @todo Connect the server. - - return (''+ server.get('id')); + return (''+ server.id); }); }, diff --git a/src/collection.js b/src/collection.js index f6fc020..9a5a352 100644 --- a/src/collection.js +++ b/src/collection.js @@ -40,7 +40,6 @@ Collection.prototype.add = function (models) { if ( !(model instanceof this.model) ) { model = new this.model(model); - models[i] = model; } var error = model.validate(); @@ -64,9 +63,11 @@ Collection.prototype.add = function (models) { return Q.reject('cannot add existing models!'); } - this.models[id] = model.properties; + this.models[id] = models[i] = model.properties; } + this.emit('add', models); + /* jshint newcap: false */ return Q(array ? models : models[0]); }; diff --git a/src/xo.js b/src/xo.js index ab1a036..d28efea 100644 --- a/src/xo.js +++ b/src/xo.js @@ -235,75 +235,17 @@ function Xo() return new Xo(); } + var xo = this; + //-------------------------------------- // Main objects (@todo should be persistent). - this.servers = new Servers(); - this.tokens = new Tokens(); - this.users = new Users(); - - // This events are used to automatically close connections if the - // associated credentials are invalidated. - var self = this; - this.tokens.on('remove', function (token_ids) { - _.each(token_ids, function (token_id) { - self.emit('token.revoked:'+ token_id); - }); - }); - this.users.on('remove', function (user_ids) { - _.each(user_ids, function (user_id) { - self.emit('user.revoked:'+ user_id); - }); - }); - - // Connections to Xen pools/servers. - this.connections = {}; - - //-------------------------------------- - // Xen objects. - - this.pools = new Pools(); - this.hosts = new Hosts(); - this.vms = new VMs(); - - this.networks = new Networks(); - this.srs = new SRs(); - this.vdis = new VDIs(); - - // Connecting classes. (@todo VBD & SR). - this.vifs = new VIFs(); - this.pifs = new PIFs(); - - // ------------------------------------- - // Temporary data for testing purposes. - - this.servers.add([{ - 'host': '192.168.1.116', - 'username': 'root', - 'password': 'qwerty', - }]).done(); - this.users.add([{ - 'email': 'bob@gmail.com', - 'pw_hash': '$2a$10$PsSOXflmnNMEOd0I5ohJQ.cLty0R29koYydD0FBKO9Rb7.jvCelZq', - 'permission': 'admin', - }, { - 'email': 'toto@gmail.com', - 'pw_hash': '$2a$10$PsSOXflmnNMEOd0I5ohJQ.cLty0R29koYydD0FBKO9Rb7.jvCelZq', - 'permission': 'none', - }]).done(); -} -require('util').inherits(Xo, require('events').EventEmitter); - -Xo.prototype.start = function () { - - var xo = this; - - // @todo Connect to persistent collection. - - // @todo Connect to Xen servers & fetch data. - xo.servers.get().then(function (servers) { - /* jshint maxparams:99 */ + xo.servers = new Servers(); + xo.tokens = new Tokens(); + xo.users = new Users(); + // When a server is added we should connect to it and fetch data. + xo.servers.on('add', function (servers) { _.each(servers, function (server) { var xapi = new Xapi(server.host); xo.connections[server.id] = xapi; @@ -573,7 +515,67 @@ Xo.prototype.start = function () { ]); }).done(); }); - }).done(); + }); + xo.servers.on('remove', function (server_ids) { + // @todo + }); + + // xo events are used to automatically close connections if the + // associated credentials are invalidated. + xo.tokens.on('remove', function (token_ids) { + _.each(token_ids, function (token_id) { + xo.emit('token.revoked:'+ token_id); + }); + }); + xo.users.on('remove', function (user_ids) { + _.each(user_ids, function (user_id) { + xo.emit('user.revoked:'+ user_id); + }); + }); + + // Connections to Xen pools/servers. + xo.connections = {}; + + //-------------------------------------- + // Xen objects. + + xo.pools = new Pools(); + xo.hosts = new Hosts(); + xo.vms = new VMs(); + + xo.networks = new Networks(); + xo.srs = new SRs(); + xo.vdis = new VDIs(); + + // Connecting classes. (@todo VBD & SR). + xo.vifs = new VIFs(); + xo.pifs = new PIFs(); + + // ------------------------------------- + // Temporary data for testing purposes. + + xo.servers.add([{ + 'host': '192.168.1.116', + 'username': 'root', + 'password': 'qwerty', + }]).done(); + xo.users.add([{ + 'email': 'bob@gmail.com', + 'pw_hash': '$2a$10$PsSOXflmnNMEOd0I5ohJQ.cLty0R29koYydD0FBKO9Rb7.jvCelZq', + 'permission': 'admin', + }, { + 'email': 'toto@gmail.com', + 'pw_hash': '$2a$10$PsSOXflmnNMEOd0I5ohJQ.cLty0R29koYydD0FBKO9Rb7.jvCelZq', + 'permission': 'none', + }]).done(); +} +require('util').inherits(Xo, require('events').EventEmitter); + +Xo.prototype.start = function () { + + var xo = this; + + // @todo Connect to persistent collection. //--------------------------------------