More introspection.

This commit is contained in:
Julien Fontanet
2014-05-24 18:14:04 +02:00
parent e3f02fc4d6
commit b7e7a0df94
10 changed files with 119 additions and 155 deletions

View File

@@ -23,6 +23,12 @@ function $deprecated(fn)
};
}
var wrap = function (val) {
return function () {
return val;
};
};
//////////////////////////////////////////////////////////////////////
// TODO: Helper functions that could be written:
@@ -132,6 +138,11 @@ Api.prototype.exec = function (session, request) {
throw Api.err.INVALID_METHOD;
}
if ('permission' in method)
{
helpers.checkPermission.call(ctx, method.permission)
}
if (method.params)
{
helpers.getParams.call(ctx, method.params);
@@ -262,10 +273,7 @@ var $register = function (path, fn, params) {
}
else
{
// Wrap this value in a function.
current[path[n]] = function () {
return fn;
};
current[path[n]] = wrap(fn);
}
};
@@ -290,7 +298,11 @@ $register('xo.getAllObjects', function () {
path[n] = key;
if ($_.isFunction(content))
{
methods[path.join('.')] = content;
methods[path.join('.')] = {
description: content.description,
params: content.params || {},
permission: content.permission,
};
}
else
{
@@ -300,25 +312,26 @@ $register('xo.getAllObjects', function () {
path.pop();
})(Api.fn, []);
$register('system.listMethods', $_.keys(methods));
$register('system.listMethods', wrap($_.keys(methods)));
$register('system.methodSignature', function (params) {
var method = methods[params.name];
if (!method)
{
throw Api.err.NO_SUCH_OBJECT;
this.throw('NO_SUCH_OBJECT');
}
return {
name: params.name,
description: method.description || null,
params: method.params || null,
};
// XML-RPC can have multiple signatures per method.
return [
// XML-RPC requires the method name.
$_.extend({name: name}, method)
];
}, {
name: {
description: 'method to describe',
type: 'string',
},
});
$register('system.getMethodsInfo', wrap(methods));
})();

View File

@@ -3,9 +3,6 @@
#=====================================================================
exports.set = (params) ->
# Current user must be an administrator.
@checkPermission 'admin'
try
host = @getObject params.id
catch
@@ -23,15 +20,18 @@ exports.set = (params) ->
$wait xapi.call "host.set_#{field}", host.ref, params[param]
return
exports.set.params = {
id: { type: 'string' }
name_label: { type: 'string', optional: true }
name_description: { type: 'string', optional: true }
enabled: { type: 'boolean', optional: true }
}
exports.set.permission = 'admin'
exports.set.params =
id: type: 'string'
name_label:
type: 'string'
optional: true
name_description:
type: 'string'
optional: true
enabled:
type: 'boolean'
optional: true
exports.restart = ({id}) ->
@checkPermission 'admin'
@@ -47,13 +47,12 @@ exports.restart = ({id}) ->
$wait xapi.call 'host.reboot', host.ref
return true
exports.restart.permission = 'admin'
exports.restart.params = {
id: { type: 'string' }
}
exports.restart_agent = ({id}) ->
@checkPermission 'admin'
try
host = @getObject id
catch
@@ -64,13 +63,12 @@ exports.restart_agent = ({id}) ->
$wait xapi.call 'host.restart_agent', host.ref
return true
exports.restart_agent.permission = 'admin'
exports.restart_agent.params = {
id: { type: 'string' }
}
exports.stop = ({id}) ->
@checkPermission 'admin'
try
host = @getObject id
catch
@@ -82,13 +80,12 @@ exports.stop = ({id}) ->
$wait xapi.call 'host.shutdown', host.ref
return true
exports.stop.permission = 'admin'
exports.stop.params = {
id: { type: 'string' }
}
exports.detach = ({id}) ->
@checkPermission 'admin'
try
host = @getObject id
catch
@@ -99,6 +96,7 @@ exports.detach = ({id}) ->
$wait xapi.call 'pool.eject', host.ref
return true
exports.detach.permission = 'admin'
exports.detach.params = {
id: { type: 'string' }
}

View File

@@ -2,16 +2,7 @@
#=====================================================================
exports.delete = ->
{
id
} = @getParams {
id: { type: 'string' }
}
# Current user must be an administrator.
@checkPermission 'admin'
exports.delete = ({id}) ->
try
message = @getObject id
catch
@@ -21,4 +12,8 @@ exports.delete = ->
$wait xapi.call 'message.destroy', message.ref
return true
return true
exports.delete.permission = 'admin'
exports.delete.params =
id:
type: 'string'

View File

@@ -3,17 +3,6 @@
#=====================================================================
exports.set = ->
params = @getParams {
id: { type: 'string' }
name_label: { type: 'string', optional: true }
name_description: { type: 'string', optional: true }
}
# Current user must be an administrator.
@checkPermission 'admin'
try
pool = @getObject params.id
catch
@@ -30,3 +19,13 @@ exports.set = ->
$wait xapi.call "pool.set_#{field}", pool.ref, params[param]
return
exports.set.permission = 'admin'
exports.set.params =
id:
type: 'string'
name_label:
type: 'string'
optional: true
name_description:
type: 'string'
optional: true

View File

@@ -6,17 +6,7 @@
# Could we use tokens instead?
# Adds a new server.
exports.add = ->
{host, username, password} = @getParams {
host: { type: 'string' }
username: { type: 'string' }
password: { type: 'string' }
}
# Current user must be administrator.
@checkPermission 'admin'
# Adds the server.
exports.add = ({host, username, password}) ->
server = $wait @servers.add {
host
username
@@ -24,26 +14,28 @@ exports.add = ->
}
return server.id
exports.add.permission = 'admin'
exports.add.params =
host:
type: 'string'
username:
type: 'string'
password:
type: 'string'
# Removes an existing server.
exports.remove = ->
{id} = @getParams {
id: { type: 'string' }
}
# Current user must be administrator.
@checkPermission 'admin'
exports.remove = ({id}) ->
# Throws an error if the server did not exist.
@throw 'NO_SUCH_OBJECT' unless $wait @servers.remove id
return true
exports.remove.permission = 'admin'
exports.remove.params =
id:
type: 'string'
# Returns all servers.
exports.getAll = ->
# Only an administrator can see all servers.
@checkPermission 'admin'
# Retrieves the servers.
servers = $wait @servers.get()
@@ -52,19 +44,10 @@ exports.getAll = ->
servers[i] = @getServerPublicProperties server
return servers
exports.getAll.permission = 'admin'
# Changes the properties of an existing server.
exports.set = ->
{id, host, username, password} = @getParams {
id: { type: 'string' }
host: { type: 'string', optional: true }
username: { type: 'string', optional: true }
password: { type: 'string', optional: true }
}
# Only an administrator can modify an server.
@checkPermission 'admin'
exports.set = ({id, host, username, password}) ->
# Retrieves the server.
server = $wait @servers.first id
@@ -80,6 +63,20 @@ exports.set = ->
$wait @servers.update server
return true
exports.set.permission = 'admin'
exports.set.params =
id:
type: 'string'
host:
type: 'string'
optional: true
username:
type: 'string'
optional: true
password:
type: 'string'
optional: true
# Connects to an existing server.
exports.connect = ->

View File

@@ -3,9 +3,6 @@
#=====================================================================
exports.set = (params) ->
# Current user must be an administrator.
@checkPermission 'admin'
try
SR = @getObject params.id
catch
@@ -22,6 +19,7 @@ exports.set = (params) ->
$wait xapi.call "SR.set_#{field}", SR.ref, params[param]
return
exports.set.permission = 'admin'
exports.set.params = {
id: { type: 'string' }
@@ -31,12 +29,9 @@ exports.set.params = {
}
exports.scan = (params) ->
# Current user must be an administrator.
@checkPermission 'admin'
exports.scan = ({id}) ->
try
SR = @getObject params.id
SR = @getObject id
catch
@throw 'NO_SUCH_OBJECT'
@@ -45,6 +40,7 @@ exports.scan = (params) ->
$wait xapi.call 'SR.scan', SR.ref
return
exports.scan.permission = 'admin'
exports.scan.params = {
id: { type: 'string' }
}

View File

@@ -4,13 +4,11 @@
# Creates a new user.
exports.create = ({email, password, permission}) ->
# Current user must be administrator.
@checkPermission 'admin'
# Creates the user.
user = $wait @users.create email, password, permission
return user.id
exports.create.permission = 'admin'
exports.create.params = {
email: { type: 'string' }
password: { type: 'string' }
@@ -21,9 +19,6 @@ exports.create.params = {
#
# FIXME: a user should not be able to delete itself.
exports.delete = ({id}) ->
# Current user must be administrator.
@checkPermission 'admin'
# The user cannot delete himself.
@throw 'INVALID_PARAMS' if id is @session.get 'user_id'
@@ -31,15 +26,13 @@ exports.delete = ({id}) ->
@throw 'NO_SUCH_OBJECT' unless $wait @users.remove id
return true
exports.delete.permission = 'admin'
exports.delete.params = {
id: { type: 'string' }
}
# Changes the password of the current user.
exports.changePassword = ({old, new: newP}) ->
# Current user must be signed in.
@checkPermission()
# Gets the current user (which MUST exist).
user = $wait @users.first @session.get 'user_id'
@@ -53,6 +46,7 @@ exports.changePassword = ({old, new: newP}) ->
$wait @users.update user
return true
exports.changePassword.permission = '' # Signed in.
exports.changePassword.params = {
old: { type: 'string' }
new: { type: 'string' }
@@ -76,9 +70,6 @@ exports.get.params = {
# Returns all users.
exports.getAll = ->
# Only an administrator can see all users.
@checkPermission 'admin'
# Retrieves the users.
users = $wait @users.get()
@@ -87,12 +78,10 @@ exports.getAll = ->
users[i] = @getUserPublicProperties user
return users
exports.getAll.permission = 'admin'
# Changes the properties of an existing user.
exports.set = ({id, email, password, permission}) ->
# Only an administrator can modify an user.
@checkPermission 'admin'
# Retrieves the user.
user = $wait @users.first id
@@ -108,6 +97,7 @@ exports.set = ({id, email, password, permission}) ->
$wait @users.update user
return true
exports.set.permission = 'admin'
exports.set.params = {
id: { type: 'string' }
email: { type: 'string', optional: true }

View File

@@ -3,9 +3,6 @@
#=====================================================================
exports.delete = ({id}) ->
# Current user must be an administrator.
@checkPermission 'admin'
try
VBD = @getObject params.id
catch
@@ -17,14 +14,12 @@ exports.delete = ({id}) ->
$wait xapi.call "VBD.destroy", VBD.ref
return
exports.delete.permission = 'admin'
exports.delete.params = {
id: { type: 'string' }
}
exports.disconnect = ({id}) ->
# Current user must be an administrator.
@checkPermission 'admin'
try
VBD = @getObject params.id
catch
@@ -36,6 +31,7 @@ exports.disconnect = ({id}) ->
$wait xapi.call "VBD.unplug_force", VBD.ref
return
exports.disconnect.permission = 'admin'
exports.disconnect.params = {
id: { type: 'string' }
}

View File

@@ -3,15 +3,8 @@
#=====================================================================
exports.delete = ({id}) ->
params = @getParams {
id: { type: 'string' }
}
# Current user must be an administrator.
@checkPermission 'admin'
try
VDI = @getObject params.id
VDI = @getObject id
catch
@throw 'NO_SUCH_OBJECT'
@@ -21,11 +14,12 @@ exports.delete = ({id}) ->
$wait xapi.call "VDI.destroy", VDI.ref
return
exports.delete.permission = 'admin'
exports.delete.params =
id:
type: 'string'
exports.set = (params) ->
# Current user must be an administrator.
@checkPermission 'admin'
try
VDI = @getObject params.id
catch
@@ -58,6 +52,7 @@ exports.set = (params) ->
$wait xapi.call "VDI.set_#{field}", ref, "#{params[param]}"
return
exports.set.permission = 'admin'
exports.set.params = {
# Identifier of the VDI to update.
id: { type: 'string' }

View File

@@ -43,9 +43,6 @@ exports.create = ({
VDIs
VIFs
}) ->
# Current user must be an administrator.
@checkPermission 'admin'
# Gets the template.
template = @getObject template
@throw 'NO_SUCH_OBJECT' unless template
@@ -186,6 +183,7 @@ exports.create = ({
# The VM should be properly created.
return VM.uuid
exports.create.permission = 'admin'
exports.create.params = {
installation: {
type: 'object'
@@ -241,9 +239,6 @@ exports.create.params = {
}
exports.delete = ({id, delete_disks: deleteDisks}) ->
# Current user must be an administrator.
@checkPermission 'admin'
try
VM = @getObject id
catch
@@ -268,6 +263,7 @@ exports.delete = ({id, delete_disks: deleteDisks}) ->
$wait xapi.call 'VM.destroy', VM.ref
return true
exports.delete.permission = 'admin'
exports.delete.params = {
id: { type: 'string' }
@@ -277,7 +273,6 @@ exports.delete.params = {
}
}
# TODO: @checkPermission 'admin'
exports.ejectCd = ({id}) ->
try
VM = @getObject id
@@ -298,11 +293,11 @@ exports.ejectCd = ({id}) ->
$wait xapi.call 'VBD.destroy', cdDriveRef
return true
exports.ejectCd.permission = 'admin'
exports.ejectCd.params = {
id: { type: 'string' }
}
# TODO: @checkPermission 'admin'
exports.insertCd = ({id, cd_id, force}) ->
try
VM = @getObject id
@@ -344,6 +339,7 @@ exports.insertCd = ({id, cd_id, force}) ->
$wait xapi.call 'VBD.insert', cdDriveRef, VDI.ref
return true
exports.insertCd.permission = 'admin'
exports.insertCd.params = {
id: { type: 'string' }
cd_id: { type: 'string' }
@@ -351,9 +347,6 @@ exports.insertCd.params = {
}
exports.migrate = ({id, host_id}) ->
# Current user must be an administrator.
@checkPermission 'admin'
try
VM = @getObject id
host = @getObject host_id
@@ -368,6 +361,7 @@ exports.migrate = ({id, host_id}) ->
$wait xapi.call 'VM.pool_migrate', VM.ref, host.ref, {}
return true
exports.migrate.permission = 'admin'
exports.migrate.params = {
# Identifier of the VM to migrate.
id: { type: 'string' }
@@ -377,9 +371,6 @@ exports.migrate.params = {
}
exports.set = (params) ->
# Current user must be an administrator.
@checkPermission 'admin'
try
VM = @getObject params.id
catch
@@ -440,6 +431,7 @@ exports.set = (params) ->
$wait xapi.call "VM.set_#{field}", ref, "#{params[param]}"
return true
exports.set.permission = 'admin'
exports.set.params = {
# Identifier of the VM to update.
id: { type: 'string' }
@@ -458,8 +450,6 @@ exports.set.params = {
}
exports.restart = ({id, force}) ->
@checkPermission 'admin'
try
VM = @getObject id
catch
@@ -478,14 +468,13 @@ exports.restart = ({id, force}) ->
$wait xapi.call 'VM.hard_reboot', VM.ref
return true
exports.restart.permission = 'admin'
exports.restart.params = {
id: { type: 'string' }
force: { type: 'boolean' }
}
exports.clone = ({id, name, full_copy}) ->
@checkPermission 'admin'
try
VM = @getObject id
catch
@@ -498,6 +487,7 @@ exports.clone = ({id, name, full_copy}) ->
$wait xapi.call 'VM.clone', VM.ref, name
return true
exports.clone.permission = 'admin'
exports.clone.params = {
id: { type: 'string' }
name: { type: 'string' }
@@ -506,8 +496,6 @@ exports.clone.params = {
# TODO: rename convertToTemplate()
exports.convert = ({id}) ->
@checkPermission 'admin'
try
VM = @getObject id
catch
@@ -517,13 +505,12 @@ exports.convert = ({id}) ->
$wait xapi.call 'VM.set_is_a_template', VM.ref, true
return true
exports.convert.permission = 'admin'
exports.convert.params = {
id: { type: 'string' }
}
exports.snapshot = ({id, name}) ->
@checkPermission 'admin'
try
VM = @getObject id
catch
@@ -534,33 +521,31 @@ exports.snapshot = ({id, name}) ->
$wait xapi.call 'VM.snapshot', VM.ref, name
return true
exports.snapshot.permission = 'admin'
exports.snapshot.params = {
id: { type: 'string' }
name: { type: 'string' }
}
exports.start = ({id}) ->
@checkPermission 'admin'
try
VM = @getObject id
catch
@throw 'NO_SUCH_OBJECT'
(@getXAPI VM).call(
$wait (@getXAPI VM).call(
'VM.start', VM.ref
false # Start paused?
false # Skips the pre-boot checks?
)
return true
exports.start.permission = 'admin'
exports.start.params = {
id: { type: 'string' }
}
exports.stop = ({id, force}) ->
@checkPermission 'admin'
try
VM = @getObject id
catch
@@ -579,6 +564,7 @@ exports.stop = ({id, force}) ->
$wait xapi.call 'VM.hard_shutdown', VM.ref
return true
exports.stop.permission = 'admin'
exports.stop.params = {
id: { type: 'string' }
force: { type: 'boolean' }
@@ -586,8 +572,6 @@ exports.stop.params = {
# revert a snapshot to its parent VM
exports.revert = ({id}) ->
@checkPermission 'admin'
try
VM = @getObject id
catch
@@ -599,6 +583,7 @@ exports.revert = ({id}) ->
$wait xapi.call 'VM.revert', VM.ref
return true
exports.revert.permission = 'admin'
exports.revert.params = {
id: { type: 'string' }
}