More API namespaces externalized.
This commit is contained in:
parent
f6f35f3e49
commit
d0ef14ccd8
178
src/api.js
178
src/api.js
@ -207,12 +207,21 @@ var $register = function (path, fn) {
|
||||
}
|
||||
};
|
||||
|
||||
// Session management.
|
||||
$register('session', require('./api/session'));
|
||||
|
||||
// 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 = {
|
||||
@ -221,114 +230,6 @@ Api.fn.api = {
|
||||
},
|
||||
};
|
||||
|
||||
// Session management
|
||||
Api.fn.session = {
|
||||
'signInWithPassword': function (session, req) {
|
||||
var p_email = req.params.email;
|
||||
var p_pass = req.params.password;
|
||||
|
||||
if (!p_email || !p_pass)
|
||||
{
|
||||
throw Api.err.INVALID_PARAMS;
|
||||
}
|
||||
|
||||
if (session.has('user_id'))
|
||||
{
|
||||
throw Api.err.ALREADY_AUTHENTICATED;
|
||||
}
|
||||
|
||||
var user = $waitPromise(this.xo.users.first({'email': p_email}));
|
||||
if (!(user && user.checkPassword(p_pass)))
|
||||
{
|
||||
throw Api.err.INVALID_CREDENTIAL;
|
||||
}
|
||||
|
||||
session.set('user_id', user.get('id'));
|
||||
return this.getUserPublicProperties(user);
|
||||
},
|
||||
|
||||
'signInWithToken': function (session, req) {
|
||||
var p_token = req.params.token;
|
||||
|
||||
if (!p_token)
|
||||
{
|
||||
throw Api.err.INVALID_PARAMS;
|
||||
}
|
||||
|
||||
if (session.has('user_id'))
|
||||
{
|
||||
throw Api.err.ALREADY_AUTHENTICATED;
|
||||
}
|
||||
|
||||
var token = $waitPromise(this.xo.tokens.first(p_token));
|
||||
if (!token)
|
||||
{
|
||||
throw Api.err.INVALID_CREDENTIAL;
|
||||
}
|
||||
|
||||
var user_id = token.get('user_id');
|
||||
|
||||
session.set('token_id', token.get('id'));
|
||||
session.set('user_id', user_id);
|
||||
|
||||
var user = $waitPromise(this.xo.users.first(user_id));
|
||||
|
||||
return this.getUserPublicProperties(user);
|
||||
},
|
||||
|
||||
'getUser': $deprecated(function (session) {
|
||||
var user_id = session.get('user_id');
|
||||
if (undefined === user_id)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var user = $waitPromise(this.xo.users.first(user_id));
|
||||
|
||||
return this.getUserPublicProperties(user);
|
||||
}),
|
||||
|
||||
'getUserId': function (session) {
|
||||
return session.get('user_id', null);
|
||||
},
|
||||
|
||||
'createToken': 'token.create',
|
||||
|
||||
'destroyToken': 'token.delete',
|
||||
};
|
||||
|
||||
// Token management.
|
||||
Api.fn.token = {
|
||||
'create': function (session) {
|
||||
var user_id = session.get('user_id');
|
||||
if ((undefined === user_id)
|
||||
|| session.has('token_id'))
|
||||
{
|
||||
throw Api.err.UNAUTHORIZED;
|
||||
}
|
||||
|
||||
// TODO: Token permission.
|
||||
|
||||
var token = $waitPromise(this.xo.tokens.generate(user_id));
|
||||
return token.id;
|
||||
},
|
||||
|
||||
'delete': function (session, req) {
|
||||
var p_token = req.params.token;
|
||||
|
||||
var token = $waitPromise(this.xo.tokens.first(p_token));
|
||||
if (!token)
|
||||
{
|
||||
throw Api.err.INVALID_PARAMS;
|
||||
}
|
||||
|
||||
// TODO: Returns NO_SUCH_OBJECT if the token does not exists.
|
||||
$waitPromise(this.xo.tokens.remove(p_token));
|
||||
|
||||
return true;
|
||||
},
|
||||
};
|
||||
|
||||
// Extra methods not really bound to an object.
|
||||
Api.fn.xo = {
|
||||
'getAllObjects': function () {
|
||||
@ -336,67 +237,6 @@ Api.fn.xo = {
|
||||
}
|
||||
};
|
||||
|
||||
// `xapi.vm` methods.
|
||||
_.each({
|
||||
pause: [],
|
||||
|
||||
// TODO: If XS tools are unavailable, do a hard reboot.
|
||||
reboot: 'clean_reboot',
|
||||
|
||||
// TODO: If XS tools are unavailable, do a hard shutdown.
|
||||
shutdown: 'clean_shutdown',
|
||||
|
||||
start: [
|
||||
false, // Start paused?
|
||||
false, // Skip the pre-boot checks?
|
||||
],
|
||||
|
||||
unpause: [],
|
||||
}, function (def, name) {
|
||||
var method = name;
|
||||
var params = [];
|
||||
if (_.isString(def))
|
||||
{
|
||||
method = def;
|
||||
}
|
||||
else if (_.isArray(params))
|
||||
{
|
||||
params = def;
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: Handle more complex definition.
|
||||
/* jshint noempty:false */
|
||||
}
|
||||
|
||||
$register('xapi.vm.'+ name, function (session, req) {
|
||||
// This method expect to the VM's UUID.
|
||||
var p_id = req.params.id;
|
||||
if (!p_id)
|
||||
{
|
||||
throw Api.err.INVALID_PARAMS;
|
||||
}
|
||||
|
||||
// The current session MUST have the `write`
|
||||
// permission.
|
||||
this.checkPermission(session, 'write');
|
||||
|
||||
// Retrieves the VM with this UUID.
|
||||
var vm = this.xo.xobjs.get(p_id);
|
||||
if (!vm)
|
||||
{
|
||||
throw Api.err.NO_SUCH_OBJECT;
|
||||
}
|
||||
|
||||
// Gets the corresponding connection.
|
||||
var xapi = this.xo.xapis[vm.$pool];
|
||||
|
||||
xapi.call.apply(xapi, ['VM.'+ method, vm.$ref].concat(params));
|
||||
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
Api.fn.system = {
|
||||
|
||||
// Returns the list of available methods similar to XML-RPC
|
||||
|
54
src/api/session.coffee
Normal file
54
src/api/session.coffee
Normal file
@ -0,0 +1,54 @@
|
||||
{$waitPromise} = require '../fibers-utils'
|
||||
|
||||
#=====================================================================
|
||||
|
||||
# Signs a user in with its email/password.
|
||||
exports.signInWithPassword = (session, req) ->
|
||||
{email, password} = req.params
|
||||
@throw 'INVALID_PARAMS' unless email? and password?
|
||||
|
||||
@throw 'ALREADY_AUTHENTICATED' if session.has 'user_id'
|
||||
|
||||
# Gets the user.
|
||||
user = $waitPromise @xo.users.first {email: email}
|
||||
|
||||
# Invalid credentials if the user does not exists or if the password
|
||||
# does not check.
|
||||
@throw 'INVALID_CREDENTIAL' unless user and user.checkPassword password
|
||||
|
||||
# Stores the user identifier in the session.
|
||||
session.set 'user_id', user.get 'id'
|
||||
|
||||
# Returns the user.
|
||||
@getUserPublicProperties user
|
||||
|
||||
# Signs a user in with a token.
|
||||
exports.signInWithToken = (session, req) ->
|
||||
{token} = req.params
|
||||
@throw 'INVALID_PARAMS' unless token?
|
||||
|
||||
@throw 'ALREADY_AUTHENTICATED' if session.has('user_id')
|
||||
|
||||
# Gets the token.
|
||||
token = $waitPromise @xo.tokens.first token
|
||||
@throw 'INVALID_CREDENTIAL' unless token?
|
||||
|
||||
# Stores the user and the token identifiers in the session.
|
||||
user_id = token.get('user_id')
|
||||
session.set 'token_id', token.get('id')
|
||||
session.set 'user_id', user_id
|
||||
|
||||
# Returns the user.
|
||||
user = $waitPromise @xo.users.first user_id
|
||||
@getUserPublicProperties user
|
||||
|
||||
# Gets the the currently signed in user.
|
||||
exports.getUser = (session) ->
|
||||
id = session.get 'user_id'
|
||||
|
||||
# If the user is not signed in, returns null.
|
||||
return null unless id?
|
||||
|
||||
# Returns the user.
|
||||
user = $waitPromise @xo.users.first id
|
||||
@getUserPublicProperties user
|
33
src/api/token.coffee
Normal file
33
src/api/token.coffee
Normal file
@ -0,0 +1,33 @@
|
||||
{$waitPromise} = require '../fibers-utils'
|
||||
|
||||
#=====================================================================
|
||||
|
||||
# Creates a new token.
|
||||
#
|
||||
# TODO: Token permission.
|
||||
exports.create = (session) ->
|
||||
userId = session.get 'user_id'
|
||||
|
||||
# The user MUST be signed in and not with a token
|
||||
@throw 'UNAUTHORIZED' if not userId? or session.has 'token_id'
|
||||
|
||||
# Creates the token.
|
||||
token = $waitPromise @xo.tokens.generate userId
|
||||
|
||||
# Returns its identifier.
|
||||
token.id
|
||||
|
||||
# Deletes a token.
|
||||
exports.delete = (session, req) ->
|
||||
{token: tokenId} = req.params
|
||||
@throw 'INVALID_PARAMS' unless token?
|
||||
|
||||
# Gets the token.
|
||||
token = $waitPromise @xo.tokens.first tokenId
|
||||
@throw 'NO_SUCH_OBJECT' unless token?
|
||||
|
||||
# Deletes the token.
|
||||
$waitPromise @xo.tokens.remove tokenId
|
||||
|
||||
# Returns true.
|
||||
true
|
59
src/api/xapi.coffee
Normal file
59
src/api/xapi.coffee
Normal file
@ -0,0 +1,59 @@
|
||||
$_ = require 'underscore'
|
||||
|
||||
#=====================================================================
|
||||
|
||||
# Definitions of the methods to generate.
|
||||
defs = {
|
||||
pause: {}
|
||||
|
||||
# TODO: If XS tools are unavailable, do a hard reboot.
|
||||
reboot: 'clean_reboot'
|
||||
|
||||
# TODO: If XS tools are unavailable, do a hard shutdown.
|
||||
shutdown: 'clean_shutdown'
|
||||
|
||||
start: [
|
||||
false # Start paused?
|
||||
false # Skip the pre-boot checks?
|
||||
]
|
||||
|
||||
unpause: {}
|
||||
}
|
||||
|
||||
#=====================================================================
|
||||
|
||||
# We are generating methods in the `xapi.vm` namespace.
|
||||
vm = exports.vm = {}
|
||||
|
||||
$_.each defs, (def, name) ->
|
||||
method = name
|
||||
params = []
|
||||
|
||||
if $_.isString def
|
||||
method = def
|
||||
else if $_.isArray def
|
||||
params = def
|
||||
else if $_.isObject def
|
||||
method = def.method if def.method?
|
||||
params = def.params if def.params?
|
||||
|
||||
vm[name] = (session, req) ->
|
||||
|
||||
# This method expect to the VM's UUID.
|
||||
{id} = req.params.id
|
||||
@throw 'INVALID_PARAMS' unless id?
|
||||
|
||||
# The current session MUST have the `write`
|
||||
# permission.
|
||||
@checkPermission session, 'write'
|
||||
|
||||
# Retrieves the VM with this UUID.
|
||||
vm = @xo.xobjs.get id
|
||||
@throw 'NO_SUCH_OBJECT' unless vm?
|
||||
|
||||
# Gets the corresponding connection.
|
||||
xapi = @xo.xapis[vm.$pool]
|
||||
xapi.call.apply xapi, ["VM.#{method}", vm.$ref].concat params
|
||||
|
||||
# Returns true.
|
||||
true
|
Loading…
Reference in New Issue
Block a user