Various updates.

This commit is contained in:
Julien Fontanet 2013-03-26 13:57:56 +01:00
parent ce1f58956a
commit 1065fbc422
8 changed files with 158 additions and 61 deletions

View File

@ -60,7 +60,7 @@ final class Application extends Base
$users = $this->_di->get('users'); $users = $this->_di->get('users');
// Checks the user exists. // Checks the user exists.
$user = $users->getBy('name', $name, false); $user = $users->first(array('name' => $name), false);
if (!$user) if (!$user)
{ {
return array(1, 'invalid credential'); return array(1, 'invalid credential');
@ -107,7 +107,7 @@ final class Application extends Base
$tokens = $this->_di->get('tokens'); $tokens = $this->_di->get('tokens');
// Checks the token exists. // Checks the token exists.
$token = $tokens->get($token, false); $token = $tokens->first($token, false);
if (!$token) if (!$token)
{ {
return array(1, 'invalid token'); return array(1, 'invalid token');
@ -137,7 +137,7 @@ final class Application extends Base
return array(0, 'not authenticated'); return array(0, 'not authenticated');
} }
$user = $this->_di->get('users')->get($c->uid); $user = $this->_di->get('users')->first($c->uid);
$c->respond($id, array( $c->respond($id, array(
'id' => $user->id, 'id' => $user->id,
@ -175,7 +175,7 @@ final class Application extends Base
{ {
$token = uniqid('', true); $token = uniqid('', true);
} }
} while ($tokens->get($token, false)); } while ($tokens->exists($token));
// Registers it. // Registers it.
$tokens->create(array( $tokens->create(array(
@ -198,18 +198,18 @@ final class Application extends Base
{ {
return -32602; // Invalid params. return -32602; // Invalid params.
} }
$token = $params[0]; $token_id = $params[0];
$tokens = $this->_di->get('tokens'); $tokens = $this->_di->get('tokens');
// Checks the token exists. // Checks the token exists.
if (!$tokens->get($token, false)) if (!$tokens->exists($token_id))
{ {
return array(0, 'invalid token'); return array(0, 'invalid token');
} }
// Deletes it. // Deletes it.
$tokens->delete($token); $tokens->delete($token_id);
// Returns success. // Returns success.
$c->respond($id, true); $c->respond($id, true);
@ -263,7 +263,7 @@ final class Application extends Base
$users = $this->_di->get('users'); $users = $this->_di->get('users');
// Checks if the user name is already used. // Checks if the user name is already used.
if ($users->getBy('name', $name, false)) if ($users->exists(array('name' => $name)))
{ {
return array(4, 'user name already taken'); return array(4, 'user name already taken');
} }
@ -302,7 +302,7 @@ final class Application extends Base
// Checks user exists and is not the current user. // Checks user exists and is not the current user.
if (($uid === $c->uid) if (($uid === $c->uid)
|| !$users->get($uid, false)) || !$users->exists($uid))
{ {
return array(1, 'invalid user'); return array(1, 'invalid user');
} }
@ -333,7 +333,7 @@ final class Application extends Base
} }
$users = $this->_di->get('users'); $users = $this->_di->get('users');
$user = $users->get($c->uid); $user = $users->first($c->uid);
// Checks the old password matches. // Checks the old password matches.
if (!password_verify($old, $user->password)) if (!password_verify($old, $user->password))
@ -400,34 +400,36 @@ final class Application extends Base
// Checks user exists and is not the current user. // Checks user exists and is not the current user.
if (($uid === $c->uid) if (($uid === $c->uid)
|| !($user = $users->get($uid, false))) || !($user = $users->first($uid, false)))
{ {
return array(1, 'invalid user'); return array(1, 'invalid user');
} }
foreach ($properties as $field => $value) foreach ($properties as $field => $value)
{ {
switch ($field) if ('name' === $field)
{ {
case 'name':
if (!$user->checkAndSet('name', $value)) if (!$user->checkAndSet('name', $value))
{ {
return array(3, 'invalid user name'); return array(3, 'invalid user name');
} }
break; }
case 'password': elseif ('password' === $field)
{
if (!$user->checkAndSet('password', $value)) if (!$user->checkAndSet('password', $value))
{ {
return array(4, 'invalid password'); return array(4, 'invalid password');
} }
break; }
case 'permission': elseif ('permission' === $field)
{
if (!$user->checkAndSet('permission', $value)) if (!$user->checkAndSet('permission', $value))
{ {
return array(5, 'invalid permission '.$value); return array(5, 'invalid permission '.$value);
} }
break; }
default: else
{
return array(2, 'invalid property'); return array(2, 'invalid property');
} }
} }
@ -446,6 +448,67 @@ final class Application extends Base
$c->respond($id, $this->_di->get('vms')->getArray()); $c->respond($id, $this->_di->get('vms')->getArray());
} }
/**
*
*/
function api_xo_getStats($id, array $params, Client $c)
{
$mgr_vms = $this->_di->get('vms');
$mgr_metrics = $this->_di->get('vms_metrics');
$memory = 0;
$vcpus = 0;
$running_vms = $mgr_vms->get(array(
'power_state' => 'Running',
'is_control_domain' => false,
));
foreach ($running_vms as $vm)
{
$metrics = $mgr_metrics->first($vm->metrics);
$memory += $metrics->memory_actual;
$vcpus += $metrics->VCPUs_number;
}
// @todo Replace with inequality filter when Rekodi implements it.
$srs = $this->_di->get('srs')->getArray(array(
'shared' => true,
));
$n_srs = 0;
foreach ($srs as $sr)
{
if (-1 != $sr['physical_size'])
{
++$n_srs;
}
}
/**
* - Nombre de serveurs connectés.
* - Nombre de running VMs.
* - Total RAM/CPUs allouées aux running VMS.
* - Nombre de SR.
* - Nombre de VMs (ni templates ni snapshots).
*/
$stats = array(
'hosts' => $mgr_vms->count(array(
'is_control_domain' => true,
)),
'vms' => $mgr_vms->count(array(
'is_a_snapshot' => false,
'is_a_template' => false,
'is_control_domain' => false,
)),
'running_vms' => count($running_vms),
'memory' => $memory,
'vcpus' => $vcpus,
'srs' => $n_srs,
);
$c->respond($id, $stats);
}
/** /**
* *
*/ */
@ -703,7 +766,7 @@ final class Application extends Base
*/ */
private function _checkPermission($uid, $permission, $object = null) private function _checkPermission($uid, $permission, $object = null)
{ {
$user = $this->_di->get('users')->get($uid); $user = $this->_di->get('users')->first($uid);
return ($user->permission >= $permission); return ($user->permission >= $permission);
} }

View File

@ -88,7 +88,7 @@ abstract class BeanAbstract extends \Rekodi\Bean
*/ */
final function checkAndSet($field, $value) final function checkAndSet($field, $value)
{ {
if (!self::check($field, $value)) if (!static::check($field, $value))
{ {
return false; return false;
} }

View File

@ -33,4 +33,6 @@ final class SR extends BeanAbstract
} }
SR::init(array( SR::init(array(
'id', 'id',
'physical_size',
)); ));

View File

@ -117,4 +117,5 @@ User::init(array(
'id', 'id',
'name', 'name',
'password', 'password',
'permission',
)); ));

View File

@ -35,6 +35,6 @@ VMMetrics::init(array(
'id', 'id',
'memory_actual', 'memory_actual',
'VCPUs_utilisation' => true, 'VCPUs_number',
'start_time', 'start_time',
)); ));

View File

@ -107,46 +107,78 @@ abstract class ManagerAbstract
} }
/** /**
* @param string|array $filter Either the id of the entry to get or a
* filter it must match.
* *
* @return boolean
*/ */
function getBy($field, $value, $default = 'fatal error') function exists($filter)
{ {
$beans = $this->_database->get( if (!is_array($filter))
$this->_table,
array($field => $value)
);
if ($beans)
{ {
$class = $this->_bean; $filter = array('id' => $filter);
return new $class($beans[0], true);
} }
if (func_num_args() >= 3) // @todo Handle limit in Rekodi.
return $this->_database->count(
$this->_table,
$filter
);
}
/**
* @param string|array $filter Either the id of the entry to get or a
* filter it must match.
* @param mixed $default Value returned if no entry has been found.
*
* @return Bean|mixed
*/
function first($filter, $default = 'fatal error')
{
if (!is_array($filter))
{
$filter = array('id' => $filter);
}
$entries = $this->get($filter);
if ($entries)
{
return $entries[0];
}
if (func_num_args() >= 2)
{ {
return $default; return $default;
} }
foreach ($filter as $field => &$value)
{
$value = $field.'='.var_export($value, true);
}
trigger_error( trigger_error(
'no such '.$this->_table.' ('.$field.' = '.$value.')', 'no such '.$this->_table.' ('.implode(', ', $filter).')',
E_USER_ERROR E_USER_ERROR
); );
} }
/** /**
* @param string $id * @param array $filter Filter the entries must match.
* @param mixed $default
* *
* @return Bean * @return Bean[]
*/ */
function get($id, $default = 'fatal error') function get(array $filter)
{ {
if (func_num_args() === 1) $entries = $this->_database->get(
$this->_table,
$filter
);
$class = $this->_bean;
foreach ($entries as &$entry)
{ {
return $this->getBy('id', $id); $entry = new $class($entry, true);
} }
return $this->getBy('id', $id, $default); return $entries;
} }
/** /**

View File

@ -37,7 +37,7 @@ final class SRs extends XCPAbstract
parent::__construct( parent::__construct(
$manager, $manager,
'srs', 'srs',
'\Bean\SRs' '\Bean\SR'
); );
} }
} }

View File

@ -34,20 +34,19 @@ abstract class XCPAbstract extends ManagerAbstract
*/ */
function batchImport(array $objects) function batchImport(array $objects)
{ {
foreach ($objects as $id => $properties) foreach ($objects as $ref => $properties)
{ {
$properties['id'] = $id; $properties['id'] = $ref;
unset($properties['uuid']);
$n = $this->_database->update( $n = $this->_database->update(
$this->_table, $this->_table,
array('id' => $id), array('id' => $ref),
$properties $properties
); );
if (1 === $n) if (1 === $n)
{ {
echo $this->_table.': updated ('.$id.')', PHP_EOL; echo $this->_table.': updated ('.$ref.')', PHP_EOL;
} }
elseif (0 === $n) elseif (0 === $n)
{ {
@ -56,7 +55,7 @@ abstract class XCPAbstract extends ManagerAbstract
array($properties) array($properties)
); );
echo $this->_table.': new ('.$id.')', PHP_EOL; echo $this->_table.': new ('.$ref.')', PHP_EOL;
} }
else else
{ {