Ability to destroy tasks/messages.

This commit is contained in:
Julien Fontanet 2014-01-06 14:29:12 +01:00
parent d0ef14ccd8
commit 3f7ec9e0c2
5 changed files with 150 additions and 129 deletions

View File

@ -23,6 +23,7 @@
"nconf": "~0.6.9", "nconf": "~0.6.9",
"q": "~0.9.7", "q": "~0.9.7",
"redis": "~0.9.1", "redis": "~0.9.1",
"require-tree": "~0.3.2",
"sync": "~0.2.2", "sync": "~0.2.2",
"then-redis": "~0.3.9", "then-redis": "~0.3.9",
"underscore": "~1.5.2", "underscore": "~1.5.2",

View File

@ -1,5 +1,7 @@
var _ = require('underscore'); var _ = require('underscore');
var $requireTree = require('require-tree');
//-------------------------------------------------------------------- //--------------------------------------------------------------------
var $waitPromise = require('./fibers-utils').$waitPromise; var $waitPromise = require('./fibers-utils').$waitPromise;
@ -171,8 +173,6 @@ Api.prototype.throw = function (errorId) {
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
Api.fn = {};
var $register = function (path, fn) { var $register = function (path, fn) {
var component, current; var component, current;
@ -192,7 +192,7 @@ var $register = function (path, fn) {
{ {
current[path[n]] = fn; current[path[n]] = fn;
} }
else else if (_.isObject(fn))
{ {
// If it is not an function but an object, copies its // If it is not an function but an object, copies its
// properties. // properties.
@ -205,62 +205,45 @@ var $register = function (path, fn) {
current[prop] = fn[prop]; current[prop] = fn[prop];
} }
} }
}; else
{
// Session management. // Wrap this value in a function.
$register('session', require('./api/session')); current[path[n]] = function () {
return fn;
// Token management. };
$register('token', require('./api/token'));
// User management.
$register('user', require('./api/user'));
// Server management.
$register('server', require('./api/server'));
// Various XAPI methods.
$register('xapi', require('./api/xapi'));
//--------------------------------------------------------------------
Api.fn.api = {
'getVersion' : function () {
return '0.1';
},
};
// Extra methods not really bound to an object.
Api.fn.xo = {
'getAllObjects': function () {
return this.xo.xobjs.getAll();
} }
}; };
Api.fn.system = { Api.fn = $requireTree('./api');
// Returns the list of available methods similar to XML-RPC //--------------------------------------------------------------------
// introspection
// (http://xmlrpc-c.sourceforge.net/introspection.html).
'listMethods': function () {
var methods = [];
(function browse(container, path) { $register('api.getVersion', '0.1');
var n = path.length;
_.each(container, function (content, key) {
path[n] = key;
if (_.isFunction(content))
{
methods.push(path.join('.'));
}
else
{
browse(content, path);
}
});
path.pop();
})(Api.fn, []);
return methods; $register('xo.getAllObjects', function () {
}, return this.xo.xobjs.getAll();
}; });
// Returns the list of available methods similar to XML-RPC
// introspection (http://xmlrpc-c.sourceforge.net/introspection.html).
(function () {
var methods = [];
(function browse(container, path) {
var n = path.length;
_.each(container, function (content, key) {
path[n] = key;
if (_.isFunction(content))
{
methods.push(path.join('.'));
}
else
{
browse(content, path);
}
});
path.pop();
})(Api.fn, []);
$register('system.listMethods', methods);
})();

30
src/api/xapi/index.coffee Normal file
View File

@ -0,0 +1,30 @@
$_ = require 'underscore'
#=====================================================================
# Generates method to delete objects.
do ->
types = [
'message'
'task'
]
$_.each types, (type) ->
exports["#{type}.destroy"] = (session, request) ->
{id} = request.params
@throw 'INVALID_PARAMS' unless id?
# FIXME: For now, the current user must be an administrator, but
# eventually, the user only need to have the write permission
# over the object linked to the message or task.
@checkPermission session, 'admin'
# Retrieves the object.
object = @xo.objects.get id
@throw 'NO_SUCH_OBJECT' unless object?
# Gets the corresponding connection.
xapi = @xo.xapis[object.$pool]
xapi.call "#{type}.destroy", object.$ref
# Returns true.
true

View File

@ -22,9 +22,7 @@ defs = {
#===================================================================== #=====================================================================
# We are generating methods in the `xapi.vm` namespace. # Generates methods.
vm = exports.vm = {}
$_.each defs, (def, name) -> $_.each defs, (def, name) ->
method = name method = name
params = [] params = []
@ -37,10 +35,9 @@ $_.each defs, (def, name) ->
method = def.method if def.method? method = def.method if def.method?
params = def.params if def.params? params = def.params if def.params?
vm[name] = (session, req) -> exports[name] = (session, request) ->
# This method expect to the VM's UUID. # This method expect to the VM's UUID.
{id} = req.params.id {id} = request.params
@throw 'INVALID_PARAMS' unless id? @throw 'INVALID_PARAMS' unless id?
# The current session MUST have the `write` # The current session MUST have the `write`

View File

@ -61,8 +61,8 @@ module.exports = (refsToUUIDs) ->
$CPUs: @dynamic 0, $CPUs: @dynamic 0,
host: host:
# No `update`: `exit` then `enter` will be called instead. # No `update`: `exit` then `enter` will be called instead.
enter: (host) -> @field += +host.CPUs["cpu_count"] enter: (host) -> @field += +host.CPUs.cpu_count
exit: (host) -> @field -= +host.CPUs["cpu_count"] exit: (host) -> @field -= +host.CPUs.cpu_count
$running_VMs: @dynamic [], $running_VMs: @dynamic [],
VM: VM:
@ -109,9 +109,9 @@ module.exports = (refsToUUIDs) ->
UUID: -> @key UUID: -> @key
name_label: get('name_label') name_label: get 'name_label'
name_description: get('name_description') name_description: get 'name_description'
tags: -> retrieveTags @value.UUID tags: -> retrieveTags @value.UUID
@ -123,7 +123,7 @@ module.exports = (refsToUUIDs) ->
@field.push value.UUID @field.push value.UUID
exit: (value) -> remove @field, value.UUID exit: (value) -> remove @field, value.UUID
HA_enabled: get('ha_enabled') HA_enabled: get 'ha_enabled'
hosts: @dynamic [], hosts: @dynamic [],
host: host:
@ -132,7 +132,7 @@ module.exports = (refsToUUIDs) ->
@field.push value.UUID @field.push value.UUID
exit: (value) -> remove @field, value.UUID exit: (value) -> remove @field, value.UUID
master: get('master') master: get 'master'
$messages: @dynamic [], $messages: @dynamic [],
message: message:
@ -231,13 +231,13 @@ module.exports = (refsToUUIDs) ->
UUID: -> @key UUID: -> @key
name_label: get('name_label') name_label: get 'name_label'
name_description: get('name_description') name_description: get 'name_description'
tags: -> retrieveTags @value.UUID tags: -> retrieveTags @value.UUID
address: get('address') address: get 'address'
controller: @dynamic ( controller: @dynamic (
-> ->
@ -254,11 +254,11 @@ module.exports = (refsToUUIDs) ->
} }
} }
CPUs: get('cpu_info') CPUs: get 'cpu_info'
enabled: get('enabled') enabled: get 'enabled'
hostname: get('hostname') hostname: get 'hostname'
iSCSI_name: (value) -> value.other_config?.iscsi_iqn iSCSI_name: (value) -> value.other_config?.iscsi_iqn
@ -302,7 +302,7 @@ module.exports = (refsToUUIDs) ->
} }
} }
$PBDs: get('PBDs') $PBDs: get 'PBDs'
$PIFs: @dynamic [], { $PIFs: @dynamic [], {
PIF: { PIF: {
@ -339,7 +339,7 @@ module.exports = (refsToUUIDs) ->
exit: (task) -> exit: (task) ->
remove @field, task remove @field, task
$pool: get('$pool') $pool: get '$pool'
$running_VMs: [] # TODO $running_VMs: [] # TODO
@ -378,9 +378,9 @@ module.exports = (refsToUUIDs) ->
UUID: -> @key UUID: -> @key
name_label: get('name_label') name_label: get 'name_label'
name_description: get('name_description') name_description: get 'name_description'
tags: -> retrieveTags @value.UUID tags: -> retrieveTags @value.UUID
@ -442,14 +442,14 @@ module.exports = (refsToUUIDs) ->
if message.object is @key if message.object is @key
@field.push message.UUID @field.push message.UUID
power_state: get('power_state') power_state: get 'power_state'
# TODO: initialize this value with `VCPUs_at_startup`. # TODO: initialize this value with `VCPUs_at_startup`.
# TODO: Should we use a map like the XAPI? # TODO: Should we use a map like the XAPI?
# FIXME: use the RRDs to get this information. # FIXME: use the RRDs to get this information.
CPUs: @dynamic { CPUs: @dynamic {
number: number get('VCPUs_at_startup') number: number get 'VCPUs_at_startup'
}, { }, {
VM_metrics: { VM_metrics: {
update: (metrics, UUID) -> update: (metrics, UUID) ->
@ -472,7 +472,7 @@ module.exports = (refsToUUIDs) ->
# FIXME: $container should contains the pool UUID when the # FIXME: $container should contains the pool UUID when the
# VM is not on a host. # VM is not on a host.
$container: get('resident_on') $container: get 'resident_on'
# TODO: removes it when hooks have access to the generator. # TODO: removes it when hooks have access to the generator.
$pool: get '$pool' $pool: get '$pool'
@ -557,13 +557,13 @@ module.exports = (refsToUUIDs) ->
UUID: -> @key UUID: -> @key
name_label: get('name_label') name_label: get 'name_label'
name_description: get('name_description') name_description: get 'name_description'
tags: -> retrieveTags @value.UUID tags: -> retrieveTags @value.UUID
SR_type: get('type') SR_type: get 'type'
physical_usage: number get 'physical_utilisation' physical_usage: number get 'physical_utilisation'
@ -573,9 +573,9 @@ module.exports = (refsToUUIDs) ->
$container: null # TODO $container: null # TODO
$PBDs: get('PBDs') $PBDs: get 'PBDs'
$VDIs: get('VDIs') $VDIs: get 'VDIs'
# FIXME: only here for pools. # FIXME: only here for pools.
$pool: get '$pool' $pool: get '$pool'
@ -591,11 +591,11 @@ module.exports = (refsToUUIDs) ->
UUID: -> @key UUID: -> @key
attached: get('currently_attached') attached: get 'currently_attached'
host: get('host') host: get 'host'
SR: get('SR') SR: get 'SR'
PIF: PIF:
@ -612,28 +612,28 @@ module.exports = (refsToUUIDs) ->
UUID: -> @key UUID: -> @key
attached: get('currently_attached') attached: get 'currently_attached'
device: get('device') device: get 'device'
ip: get('IP') ip: get 'IP'
host: get('host') host: get 'host'
mac: get('MAC') mac: get 'MAC'
management: get('management') management: get 'management'
mode: get('ip_configuration_mode') mode: get 'ip_configuration_mode'
mtu: get('MTU') mtu: get 'MTU'
netmask: get('netmask') netmask: get 'netmask'
# TODO: networks # TODO: networks
network: get('network') network: get 'network'
physical: get('physical') physical: get 'physical'
VDI: VDI:
@ -646,26 +646,26 @@ module.exports = (refsToUUIDs) ->
UUID: -> @key UUID: -> @key
name_label: get('name_label') name_label: get 'name_label'
name_description: get('name_description') name_description: get 'name_description'
# TODO: determine whether or not tags are required for a VDI. # TODO: determine whether or not tags are required for a VDI.
#tags: -> retrieveTags @value.UUID #tags: -> retrieveTags @value.UUID
usage: get('physical_utilisation') usage: get 'physical_utilisation'
size: get('virtual_size') size: get 'virtual_size'
snapshot_of: get('snapshot_of') snapshot_of: get 'snapshot_of'
snapshots: get('snapshots') snapshots: get 'snapshots'
# TODO: Is the name fit? # TODO: Is the name fit?
#snapshot_time: get('snapshot_time') #snapshot_time: get 'snapshot_time'
# FIXME: SR.VDIs -> VDI instead of VDI.SR -> SR. # FIXME: SR.VDIs -> VDI instead of VDI.SR -> SR.
SR: get('SR') SR: get 'SR'
$VBD: (value) -> $VBD: (value) ->
{VBDs} = value {VBDs} = value
@ -690,9 +690,9 @@ module.exports = (refsToUUIDs) ->
UUID: -> @key UUID: -> @key
VDI: get('VDI') VDI: get 'VDI'
VM: get('VM') VM: get 'VM'
VIF: VIF:
@ -709,18 +709,18 @@ module.exports = (refsToUUIDs) ->
UUID: -> @key UUID: -> @key
attached: get('currently_attached') attached: get 'currently_attached'
device: get('device') device: get 'device'
mac: get('MAC') mac: get 'MAC'
mtu: get('MTU') mtu: get 'MTU'
# TODO: networks # TODO: networks
network: get('network') network: get 'network'
VM: get('VM') VM: get 'VM'
console: console:
@ -740,13 +740,18 @@ module.exports = (refsToUUIDs) ->
UUID: -> @key UUID: -> @key
name: get('name') name: get 'name'
body: get('body') body: get 'body'
object: get('obj_uuid') object: get 'obj_uuid'
time: get('timestamp') time: get 'timestamp'
$pool: get '$pool'
# The reference is necessary for operations with the XAPI.
$ref: -> @generatorKey
task: task:
@ -758,22 +763,27 @@ module.exports = (refsToUUIDs) ->
UUID: -> @key UUID: -> @key
name_label: get('name_label') name_label: get 'name_label'
name_description: get('name_description') name_description: get 'name_description'
progress: get number('progress') progress: get number('progress')
result: get('result') result: get 'result'
error: get('error_info') error: get 'error_info'
$container: get('resident_on') $container: get 'resident_on'
created: get('created') created: get 'created'
finished: get('finished') finished: get 'finished'
current_operations: get('current_operations') current_operations: get 'current_operations'
status: get('status') status: get 'status'
$pool: get '$pool'
# The reference is necessary for operations with the XAPI.
$ref: -> @generatorKey