Various updates.

This commit is contained in:
Julien Fontanet 2013-07-31 18:50:20 +02:00
parent 35a06a4364
commit 8ae74ee4fe
3 changed files with 314 additions and 265 deletions

View File

@ -592,4 +592,33 @@ Api.fn.xapi = {
return this.xo[match[1] +'s'].get(); return this.xo[match[1] +'s'].get();
}, },
'vm': {
'pause': function (session, req) {
var p_id = req.params.id;
if (!p_id)
{
throw Api.err.INVALID_PARAMS;
}
var xo = this.xo;
var vm;
return this.checkPermission(session, 'write').then(function () {
return xo.vms.first(p_id);
}).then(function (tmp) {
vm = tmp;
if (!vm)
{
throw Api.err.NO_SUCH_OBJECT;
}
return xo.pools.first(vm.get('pool_uuid'));
}).then(function (pool) {
var xapi = xo.connections[pool.get('uuid')];
return xapi.call('VM.pause', vm.get('ref'));
}).thenResolve(true);
},
},
}; };

View File

@ -25,7 +25,7 @@ Collection.prototype.model = require('./model');
/** /**
* Adds new models to this collection. * Adds new models to this collection.
*/ */
Collection.prototype.add = function (models) { Collection.prototype.add = function (models, options) {
var array = true; var array = true;
if (!_.isArray(models)) if (!_.isArray(models))
{ {
@ -33,6 +33,9 @@ Collection.prototype.add = function (models) {
array = false; array = false;
} }
// @todo Temporary mesure, implement “set()” instead.
var replace = !!(options && options.replace);
for (var i = 0, n = models.length; i < n; ++i) for (var i = 0, n = models.length; i < n; ++i)
{ {
var model = models[i]; var model = models[i];
@ -58,7 +61,7 @@ Collection.prototype.add = function (models) {
} }
// Existing models are ignored. // Existing models are ignored.
if (this.models[id]) if (!replace && this.models[id])
{ {
return Q.reject('cannot add existing models!'); return Q.reject('cannot add existing models!');
} }

View File

@ -228,29 +228,9 @@ var VDIs = Collection.extend({
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
function Xo() // @todo Really ugly.
function refresh(xo, xapi)
{ {
if ( !(this instanceof Xo) )
{
return new Xo();
}
var xo = this;
//--------------------------------------
// Main objects (@todo should be persistent).
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;
xapi.connect(server.username, server.password).then(function () {
var get_records = function (classes) { var get_records = function (classes) {
var promises = []; var promises = [];
for (var i = 0, n = classes.length; i < n; i++) for (var i = 0, n = classes.length; i < n; i++)
@ -302,8 +282,7 @@ function Xo()
'VM_guest_metrics', 'VM_guest_metrics',
'VMPP', 'VMPP',
'VTPM', 'VTPM',
]); ]).spread(function (
}).spread(function (
pools, pools,
hosts, hosts,
vms, vms,
@ -340,6 +319,8 @@ function Xo()
pools = _.values(pools); pools = _.values(pools);
var pool_uuid = pools[0].id = pools[0].uuid; var pool_uuid = pools[0].id = pools[0].uuid;
xo.connections[pool_uuid] = xapi;
// @todo Remove: security concerns. // @todo Remove: security concerns.
pools[0].sessionId = xapi.sessionId; pools[0].sessionId = xapi.sessionId;
@ -497,22 +478,58 @@ function Xo()
// 2. For each object, an identifier based on its uuid is // 2. For each object, an identifier based on its uuid is
// created. // created.
var normalize = function (items) { var normalize = function (items) {
return _.map(items, function (item) { return _.map(items, function (item, ref) {
item.id = item.uuid; item.id = item.uuid;
item.pool_uuid = pool_uuid; item.pool_uuid = pool_uuid;
item.ref = ref;
return item; return item;
}); });
}; };
return Q.all([ var opts = {
xo.pools.add(pools), // Special case. 'replace': true,
xo.hosts.add(normalize(hosts)), };
xo.vms.add(normalize(vms)),
xo.networks.add(normalize(networks)), return Q.all([
xo.srs.add(normalize(srs)), xo.pools.add(pools, opts), // Special case.
xo.vdis.add(normalize(vdis)), xo.hosts.add(normalize(hosts), opts),
xo.vms.add(normalize(vms), opts),
xo.networks.add(normalize(networks), opts),
xo.srs.add(normalize(srs), opts),
xo.vdis.add(normalize(vdis), opts),
]); ]);
});
}
function Xo()
{
if ( !(this instanceof Xo) )
{
return new Xo();
}
var xo = this;
//--------------------------------------
// Main objects (@todo should be persistent).
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);
xapi.connect(server.username, server.password).then(function () {
// @todo Use events.
!function helper() {
refresh(xo, xapi).then(function () {
setTimeout(helper, 5000);
}).done();
}();
}).done(); }).done();
}); });
}); });