From ce1f58956accc111aec6541ea9dea2c19fa862a8 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Mon, 25 Mar 2013 18:47:29 +0100 Subject: [PATCH] Various updates. --- composer.lock | 8 +- lib/Application.php | 104 ++++---------- lib/Bean/BeanAbstract.php | 99 ++++++++++++++ lib/Bean/SR.php | 36 +++++ lib/Bean/Token.php | 21 +-- lib/Bean/User.php | 42 ++---- lib/Bean/VIF.php | 36 +++++ lib/Bean/VM.php | 234 +++++++++++++------------------- lib/Bean/VMGuestMetrics.php | 39 ++++++ lib/Bean/VMMetrics.php | 40 ++++++ lib/DI.php | 74 ++++++++-- lib/Manager/ManagerAbstract.php | 59 +++++--- lib/Manager/SRs.php | 43 ++++++ lib/Manager/VIFs.php | 39 ++++++ lib/Manager/VMs.php | 2 +- lib/Manager/VMsGuestMetrics.php | 43 ++++++ lib/Manager/VMsMetrics.php | 43 ++++++ lib/Manager/XCPAbstract.php | 70 ++++++++++ 18 files changed, 730 insertions(+), 302 deletions(-) create mode 100644 lib/Bean/BeanAbstract.php create mode 100644 lib/Bean/SR.php create mode 100644 lib/Bean/VIF.php create mode 100644 lib/Bean/VMGuestMetrics.php create mode 100644 lib/Bean/VMMetrics.php create mode 100644 lib/Manager/SRs.php create mode 100644 lib/Manager/VIFs.php create mode 100644 lib/Manager/VMsGuestMetrics.php create mode 100644 lib/Manager/VMsMetrics.php create mode 100644 lib/Manager/XCPAbstract.php diff --git a/composer.lock b/composer.lock index 4dbe308..973c8f8 100644 --- a/composer.lock +++ b/composer.lock @@ -188,12 +188,12 @@ "source": { "type": "git", "url": "https://github.com/vatesfr/rekodi.git", - "reference": "a0122f7958e9375626d265497b1bfef62c552915" + "reference": "2ed168af2f75ad359fa78d57c6372f5d50b032f0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/vatesfr/rekodi/zipball/a0122f7958e9375626d265497b1bfef62c552915", - "reference": "a0122f7958e9375626d265497b1bfef62c552915", + "url": "https://api.github.com/repos/vatesfr/rekodi/zipball/2ed168af2f75ad359fa78d57c6372f5d50b032f0", + "reference": "2ed168af2f75ad359fa78d57c6372f5d50b032f0", "shasum": "" }, "require": { @@ -222,7 +222,7 @@ "keywords": [ "event" ], - "time": "2013-03-22 15:41:39" + "time": "2013-03-25 17:30:48" } ], "packages-dev": [ diff --git a/lib/Application.php b/lib/Application.php index 1735efc..ca0d02b 100644 --- a/lib/Application.php +++ b/lib/Application.php @@ -505,88 +505,30 @@ final class Application extends Base foreach ($events as $event) { - $_ = array_keys($event); - if (!$keys) - { - $keys = $_; - var_export($keys); echo PHP_EOL; - } - elseif ($_ !== $keys) - { - $keys = array_intersect($keys, $_); - var_export($keys); echo PHP_EOL; - } - $class = $event['class']; $ref = $event['ref']; $snapshot = $event['snapshot']; // Not present in the documentation. - echo "$class - $ref\n"; - $objects[$class][$ref] = $snapshot; + + echo "Event: $class ($ref)\n"; } - isset($objects['pool']) - and $this->updateXenPools($objects['pool']); - isset($objects['host']) - and $this->updateXenHosts($objects['host']); + isset($objects['sr']) + and $this->_di->get('srs')->batchImport($objects['sr']); + isset($objects['vif']) + and $this->_di->get('vifs')->batchImport($objects['vifs']); isset($objects['vm']) - and $this->updateXenVms($objects['vm']); + and $this->_di->get('vms')->batchImport($objects['vm']); + isset($objects['vm_guest_metrics']) + and $this->_di->get('vms_guest_metrics')->batchImport($objects['vm_guest_metrics']); + isset($objects['vm_metrics']) + and $this->_di->get('vms_metrics')->batchImport($objects['vm_metrics']); // Requeue this request. return true; } - /** - * - */ - function updateXenPools(array $pools) - { - foreach ($pools as $ref => $pool) - { - $this->_update($this->_xenPools[$ref], $pool); - } - } - - /** - * - */ - function updateXenHosts(array $hosts) - { - foreach ($hosts as $ref => $host) - { - $this->_update($this->_xenHosts[$ref], $host); - } - } - - /** - * - */ - function updateXenVms(array $vms) - { - $manager = $this->_di->get('vms'); - - foreach ($vms as $id => $properties) - { - $properties['id'] = $id; - - $vm = $manager->get($id, false); - if (!$vm) - { - $manager->create($properties); - - echo "new VM: $id\n"; - } - else - { - $vm->set($properties, true); - $manager->save($vm); - - echo "updated VM: $id\n"; - } - } - } - /** * */ @@ -606,14 +548,28 @@ final class Application extends Base //-------------------------------------- + // map(XCP class: manager) + $classes = array( + 'SR' => 'srs', + 'VIF' => 'vifs', + 'VM' => 'vms', + 'VM_guest_metrics' => 'vms_guest_metrics', + 'VM_metrics' => 'vms_metrics', + ); + foreach ($config['xcp'] as $_) { $xcp = new XCP($loop, $_['url'], $_['username'], $_['password']); - $xcp->queue( - 'VM.get_all_records', - null, - array($this, 'updateXenVms') - ); + + foreach ($classes as $class => $manager) + { + $xcp->queue( + $class.'.get_all_records', + null, + array($this->_di->get($manager), 'batchImport') + ); + } + $xcp->queue( 'event.register', array(array('host', 'pool', 'vm')) diff --git a/lib/Bean/BeanAbstract.php b/lib/Bean/BeanAbstract.php new file mode 100644 index 0000000..d804e34 --- /dev/null +++ b/lib/Bean/BeanAbstract.php @@ -0,0 +1,99 @@ +. + * + * @author Julien Fontanet + * @license http://www.gnu.org/licenses/gpl-3.0-standalone.html GPLv3 + * + * @package Xen Orchestra Server + */ + +namespace Bean; + +/** + * @todo Migrate check() and checkAndSet() to \Rekodi\Bean. + */ +abstract class BeanAbstract extends \Rekodi\Bean +{ + /** + * This function is not necessary but allow us to dynamically + * initialize our beans. + */ + static final function init($fields) + { + foreach ($fields as $key => $value) + { + if (is_bool($value)) + { + static::$_fields[$key] = $value; + } + else + { + static::$_fields[$value] = false; + } + } + } + + /** + * + */ + static function check($field, &$value) + { + return isset(static::$_fields[$field]); + } + + /** + * + */ + function __get($name) + { + $value = parent::__get($name); + if (static::$_fields[$name]) + { + return json_decode($value, true); + } + return $value; + } + + /** + * + */ + function __set($name, $value) + { + if (is_array($value) + || is_object($value)) + { + $value = json_encode($value); + } + + return parent::__set($name, $value); + } + + /** + * + */ + final function checkAndSet($field, $value) + { + if (!self::check($field, $value)) + { + return false; + } + + $this->__set($field, $value); + return true; + } +} diff --git a/lib/Bean/SR.php b/lib/Bean/SR.php new file mode 100644 index 0000000..7d6bd77 --- /dev/null +++ b/lib/Bean/SR.php @@ -0,0 +1,36 @@ +. + * + * @author Julien Fontanet + * @license http://www.gnu.org/licenses/gpl-3.0-standalone.html GPLv3 + * + * @package Xen Orchestra Server + */ + +namespace Bean; + +/** + * + */ +final class SR extends BeanAbstract +{ + protected static $_fields; +} +SR::init(array( + 'id', +)); diff --git a/lib/Bean/Token.php b/lib/Bean/Token.php index 77cc47d..7d015a8 100644 --- a/lib/Bean/Token.php +++ b/lib/Bean/Token.php @@ -27,21 +27,12 @@ namespace Bean; /** * */ -final class Token extends \Rekodi\Bean +final class Token extends BeanAbstract { - /** - * This function is not necessary but allow us to dynamically - * initialize our bean. - */ - static function init() - { - self::$_fields = array_flip(array( - 'id', - 'expiration', - 'user_id', - )); - } - protected static $_fields; } -Token::init(); +Token::init(array( + 'id', + 'expiration', + 'user_id', +)); diff --git a/lib/Bean/User.php b/lib/Bean/User.php index f69a519..123b845 100644 --- a/lib/Bean/User.php +++ b/lib/Bean/User.php @@ -25,9 +25,9 @@ namespace Bean; /** - * @todo Migrate check() and checkAndSet() to \Rekodi\Bean. + * */ -final class User extends \Rekodi\Bean +final class User extends BeanAbstract { /** * @@ -83,20 +83,6 @@ final class User extends \Rekodi\Bean : false; } - /** - * This function is not necessary but allow us to dynamically - * initialize our bean. - */ - static function init() - { - self::$_fields = array_flip(array( - 'id', - 'name', - 'password', - 'permission', - )); - } - /** * */ @@ -104,8 +90,6 @@ final class User extends \Rekodi\Bean { switch ($field) { - case 'id': - return true; case 'name': return ( is_string($value) @@ -124,23 +108,13 @@ final class User extends \Rekodi\Bean return (false !== $value); } - return false; - } - - /** - * - */ - function checkAndSet($field, $value) - { - if (!self::check($field, $value)) - { - return false; - } - - $this->__set($field, $value); - return true; + return parent::check($field, $value); } protected static $_fields; } -User::init(); +User::init(array( + 'id', + 'name', + 'password', +)); diff --git a/lib/Bean/VIF.php b/lib/Bean/VIF.php new file mode 100644 index 0000000..7a7210a --- /dev/null +++ b/lib/Bean/VIF.php @@ -0,0 +1,36 @@ +. + * + * @author Julien Fontanet + * @license http://www.gnu.org/licenses/gpl-3.0-standalone.html GPLv3 + * + * @package Xen Orchestra Server + */ + +namespace Bean; + +/** + * + */ +final class VIF extends BeanAbstract +{ + protected static $_fields; +} +VIF::init(array( + 'id', +)); diff --git a/lib/Bean/VM.php b/lib/Bean/VM.php index 089bf03..31ea9e4 100644 --- a/lib/Bean/VM.php +++ b/lib/Bean/VM.php @@ -27,147 +27,99 @@ namespace Bean; /** * */ -final class VM extends \Rekodi\Bean +final class VM extends BeanAbstract { - /** - * This function is not necessary but allow us to dynamically - * initialize our bean. - */ - static function init() - { - self::$_fields = array_flip(array( - // Identifiers. - 'id', - 'name_label', - 'resident_on', // The host this VM is currently resident on. - 'domarch', // Domain architecture if available, null otherwise. - 'domid', // Domain ID. - - // Description. - 'name_description', - 'is_a_template', - 'is_a_snapshot', - 'is_control_domain', - 'tags', - - // Technical characteristics. - 'attached_PCIs', - 'platform', - 'VBDs', // Virtual Block Devices. - 'VCPUs_at_startup', - 'VCPUs_max', - 'VCPUs_params', - 'VGPUs', - 'VIFs', // Virtual Network Interface (string[]). @todo - //'VTPMs', // Virtual Trust Platform Modules ??? - - // Event-related actions. - 'actions_after_crash', - 'actions_after_reboot', - 'actions_after_shutdown', - - // Current state. - 'guest_metrics', - 'memory_dynamic_max', - 'memory_dynamic_min', - 'memory_overhead', - 'memory_static_max', - 'memory_static_min', - 'memory_target', - 'metrics', - 'power_state', - - // Snapshot-related info. - 'shutdown_delay', - 'snapshot_info', - 'snapshot_metadata', - 'snapshot_of', - 'snapshot_time', - 'snapshots', - 'transportable_snapshot_id', - - // Operations. - 'allowed_operations', - 'current_operations', - - // Various. - 'consoles', - - // 'affinity', - // 'appliance', - // 'bios_strings', - // 'blobs', - // 'blocked_operations', - // 'children', // ??? - // 'crash_dumps', - // 'ha_always_run', - // 'ha_restart_priority', - // 'HVM_boot_params', - // 'HVM_boot_policy', - // 'HVM_shadow_multiplier', - // 'is_snapshot_from_vmpp', - // 'last_boot_CPU_flags', - // 'last_booted_record', - // 'order', - // 'other_config', - // 'parent', // ??? - // 'PCI_bus', - // 'protection_policy', - // 'PV_args', - // 'PV_bootloader', - // 'PV_bootloader_args', - // 'PV_kernel', - // 'PV_legacy_args', - // 'PV_ramdisk', - // 'recommendations', - // 'start_delay', - // 'suspend_SR', - // 'suspend_VDI', - // 'user_version', - // 'version', - // 'xenstore_data', - )); - } - - function __get($name) - { - static $json_encoded; - if (!$json_encoded) - { - $json_encoded = array_flip(array( - 'VBDs', - 'VCPUs_params', - 'VGPUs', - 'VIFs', - 'allowed_operations', - 'consoles', - 'crash_dumps', - 'current_operations', - 'platform', - 'snapshot_info', - 'tags', - )); - } - - $value = parent::__get($name); - if (isset($json_encoded[$name])) - { - return json_decode($value, true); - } - return $value; - } - - function __set($name, $value) - { - if (is_array($value) - || is_object($value)) - { - $value = json_encode($value); - } - - return parent::__set($name, $value); - } - protected static $_fields; } -VM::init(); +VM::init(array( + // Identifiers. + 'id', + 'name_label', + 'resident_on', // The host this VM is currently resident on. + 'domarch', // Domain architecture if available, null otherwise. + 'domid', // Domain ID. + + // Description. + 'name_description', + 'is_a_template', + 'is_a_snapshot', + 'is_control_domain', + 'tags' => true, + + // Technical characteristics. + 'attached_PCIs' => true, + 'platform', + 'VBDs' => true, // Virtual Block Devices. + 'VCPUs_at_startup', + 'VCPUs_max', + 'VCPUs_params' => true, + 'VGPUs' => true, + 'VIFs' => true, // Virtual Network Interface. + //'VTPMs' => true, // Virtual Trust Platform Modules ??? + + // Event-related actions. + 'actions_after_crash', + 'actions_after_reboot', + 'actions_after_shutdown', + + // Current state. + 'guest_metrics', + 'memory_dynamic_max', + 'memory_dynamic_min', + 'memory_overhead', + 'memory_static_max', + //'memory_static_min', // @deprecated + 'memory_target', + 'metrics', + 'power_state', + + // Snapshot-related info. + 'shutdown_delay', + 'snapshot_info', + 'snapshot_metadata', + 'snapshot_of', + 'snapshot_time', + 'snapshots' => true, + 'transportable_snapshot_id', + + // Operations. + 'allowed_operations' => true, + 'current_operations', + + // Various. + 'consoles' => true, + + // 'affinity', + // 'appliance', + // 'bios_strings', + // 'blobs', + // 'blocked_operations', + // 'children' => true, // ??? + // 'crash_dumps' => true, + // 'ha_always_run', // @deprecated + // 'ha_restart_priority', + // 'HVM_boot_params', + // 'HVM_boot_policy', + // 'HVM_shadow_multiplier', + // 'is_snapshot_from_vmpp', + // 'last_boot_CPU_flags', + // 'last_booted_record', + // 'order', + // 'other_config', + // 'parent', // ??? + // 'PCI_bus', // @deprecated + // 'protection_policy', + // 'PV_args', + // 'PV_bootloader', + // 'PV_bootloader_args', + // 'PV_kernel', + // 'PV_legacy_args', + // 'PV_ramdisk', + // 'recommendations', + // 'start_delay', + // 'suspend_SR', + // 'suspend_VDI', + // 'user_version', + // 'version', + // 'xenstore_data', +)); diff --git a/lib/Bean/VMGuestMetrics.php b/lib/Bean/VMGuestMetrics.php new file mode 100644 index 0000000..08be807 --- /dev/null +++ b/lib/Bean/VMGuestMetrics.php @@ -0,0 +1,39 @@ +. + * + * @author Julien Fontanet + * @license http://www.gnu.org/licenses/gpl-3.0-standalone.html GPLv3 + * + * @package Xen Orchestra Server + */ + +namespace Bean; + +/** + * + */ +final class VMGuestMetrics extends BeanAbstract +{ + protected static $_fields; +} +VMGuestMetrics::init(array( + 'id', + + 'memory' => true, + 'networks' => true, +)); diff --git a/lib/Bean/VMMetrics.php b/lib/Bean/VMMetrics.php new file mode 100644 index 0000000..48e8b69 --- /dev/null +++ b/lib/Bean/VMMetrics.php @@ -0,0 +1,40 @@ +. + * + * @author Julien Fontanet + * @license http://www.gnu.org/licenses/gpl-3.0-standalone.html GPLv3 + * + * @package Xen Orchestra Server + */ + +namespace Bean; + +/** + * + */ +final class VMMetrics extends BeanAbstract +{ + protected static $_fields; +} +VMMetrics::init(array( + 'id', + + 'memory_actual', + 'VCPUs_utilisation' => true, + 'start_time', +)); diff --git a/lib/DI.php b/lib/DI.php index 716ceba..9fac797 100644 --- a/lib/DI.php +++ b/lib/DI.php @@ -87,6 +87,42 @@ final class DI extends Base return JSONDatabase::factory($config['database.file']); } + private function _init_database_cache() + { + $database = new \Rekodi\Manager\Memory; + + $database->createTable('srs', function ($table) { + $table + ->string('id')->unique() + ->boolean('shared') + ; + }); + $database->createTable('vifs', function ($table) { + $table + ->string('id')->unique() + ; + }); + $database->createTable('vms', function ($table) { + $table + ->string('id')->unique() + ->string('power_state') + ->boolean('is_control_domain') + ; + }); + $database->createTable('vms_metrics', function ($table) { + $table + ->string('id')->unique() + ; + }); + $database->createTable('vms_guest_metrics', function ($table) { + $table + ->string('id')->unique() + ; + }); + + return $database; + } + private function _init_errorLogger() { return new ErrorLogger($this->get('logger')); @@ -126,29 +162,41 @@ final class DI extends Base return new Loop; } + //-------------------------------------- + // Managers + + private function _init_srs() + { + return new \Manager\SRs($this->get('database.cache')); + } + private function _init_tokens() { - return new \Manager\Tokens( - $this->get('database') - ); + return new \Manager\Tokens($this->get('database')); } private function _init_users() { - return new \Manager\Users( - $this->get('database') - ); + return new \Manager\Users($this->get('database')); + } + + private function _init_vifs() + { + return new \Manager\VIFs($this->get('database.cache')); } private function _init_vms() { - $database = new \Rekodi\Manager\Memory; - $database->createTable('vms', function ($table) { - $table - ->string('id')->unique() - ; - }); + return new \Manager\VMs($this->get('database.cache')); + } - return new \Manager\VMs($database); + private function _init_vmsGuestMetrics() + { + return new \Manager\VMsGuestMetrics($this->get('database.cache')); + } + + private function _init_vmsMetrics() + { + return new \Manager\VMsMetrics($this->get('database.cache')); } } diff --git a/lib/Manager/ManagerAbstract.php b/lib/Manager/ManagerAbstract.php index 6fa81b9..b75f090 100644 --- a/lib/Manager/ManagerAbstract.php +++ b/lib/Manager/ManagerAbstract.php @@ -32,11 +32,11 @@ abstract class ManagerAbstract /** * */ - protected function __construct(\Rekodi\Manager $manager, $table, $bean) + protected function __construct(\Rekodi\Manager $database, $table, $bean) { - $this->_manager = $manager; - $this->_table = $table; - $this->_bean = $bean; + $this->_database = $database; + $this->_table = $table; + $this->_bean = $bean; } /** @@ -53,30 +53,49 @@ abstract class ManagerAbstract $class = $this->_bean; $bean = new $class($properties, true); - $new_props = $this->_manager->create( + $new_props = $this->_database->create( $this->_table, array($bean->getProperties()) ); - if (count($new_props) !== 1) - { - trigger_error( - 'unexpected number of created '.$this->_table.' ('.$n.')', - E_USER_ERROR - ); - } - $bean->set($new_props[0]); $bean->markAsClean(); return $bean; } + /** + * + */ + function count(array $filter = null) + { + return $this->_database->count( + $this->_table, + $filter + ); + } + + /** + * + */ + function createOrUpdate(array $properties) + { + $bean = $this->get($properties['id'], false); + if (!$bean) + { + return $this->create($properties); + } + + $bean->set($properties, true); + $this->save($bean); + return $bean; + } + /** * */ function delete($id) { - $n = $this->_manager->delete($this->_table, array('id' => $id)); + $n = $this->_database->delete($this->_table, array('id' => $id)); if (1 !== $n) { @@ -92,7 +111,7 @@ abstract class ManagerAbstract */ function getBy($field, $value, $default = 'fatal error') { - $beans = $this->_manager->get( + $beans = $this->_database->get( $this->_table, array($field => $value) ); @@ -135,7 +154,7 @@ abstract class ManagerAbstract */ function getArray($filter = null, $fields = null) { - return $this->_manager->get( + return $this->_database->get( $this->_table, $filter, $fields @@ -154,7 +173,7 @@ abstract class ManagerAbstract return; } - $n = $this->_manager->update( + $n = $this->_database->update( $this->_table, array('id' => $bean->id), $bean->getDirty() @@ -172,15 +191,15 @@ abstract class ManagerAbstract /** * @var \Rekodi\Manager */ - private $_manager; + protected $_database; /** * @var string */ - private $_table; + protected $_table; /** * @var string */ - private $_bean; + protected $_bean; } diff --git a/lib/Manager/SRs.php b/lib/Manager/SRs.php new file mode 100644 index 0000000..f24b1a3 --- /dev/null +++ b/lib/Manager/SRs.php @@ -0,0 +1,43 @@ +. + * + * @author Julien Fontanet + * @license http://www.gnu.org/licenses/gpl-3.0-standalone.html GPLv3 + * + * @package Xen Orchestra Server + */ + +namespace Manager; + +/** + * + */ +final class SRs extends XCPAbstract +{ + /** + * + */ + function __construct(\Rekodi\Manager $manager) + { + parent::__construct( + $manager, + 'srs', + '\Bean\SRs' + ); + } +} diff --git a/lib/Manager/VIFs.php b/lib/Manager/VIFs.php new file mode 100644 index 0000000..3d3f473 --- /dev/null +++ b/lib/Manager/VIFs.php @@ -0,0 +1,39 @@ +. + * + * @author Julien Fontanet + * @license http://www.gnu.org/licenses/gpl-3.0-standalone.html GPLv3 + * + * @package Xen Orchestra Server + */ + +namespace Manager; + +/** + * + */ +final class VIFs extends XCPAbstract +{ + /** + * + */ + function __construct(\Rekodi\Manager $manager) + { + parent::__construct($manager, 'vifs', '\Bean\VIFs'); + } +} diff --git a/lib/Manager/VMs.php b/lib/Manager/VMs.php index b7b6392..849c87c 100644 --- a/lib/Manager/VMs.php +++ b/lib/Manager/VMs.php @@ -27,7 +27,7 @@ namespace Manager; /** * */ -final class VMs extends ManagerAbstract +final class VMs extends XCPAbstract { /** * diff --git a/lib/Manager/VMsGuestMetrics.php b/lib/Manager/VMsGuestMetrics.php new file mode 100644 index 0000000..ab52e2f --- /dev/null +++ b/lib/Manager/VMsGuestMetrics.php @@ -0,0 +1,43 @@ +. + * + * @author Julien Fontanet + * @license http://www.gnu.org/licenses/gpl-3.0-standalone.html GPLv3 + * + * @package Xen Orchestra Server + */ + +namespace Manager; + +/** + * + */ +final class VMsGuestMetrics extends XCPAbstract +{ + /** + * + */ + function __construct(\Rekodi\Manager $manager) + { + parent::__construct( + $manager, + 'vms_guest_metrics', + '\Bean\VMGuestMetrics' + ); + } +} diff --git a/lib/Manager/VMsMetrics.php b/lib/Manager/VMsMetrics.php new file mode 100644 index 0000000..129446c --- /dev/null +++ b/lib/Manager/VMsMetrics.php @@ -0,0 +1,43 @@ +. + * + * @author Julien Fontanet + * @license http://www.gnu.org/licenses/gpl-3.0-standalone.html GPLv3 + * + * @package Xen Orchestra Server + */ + +namespace Manager; + +/** + * + */ +final class VMsMetrics extends XCPAbstract +{ + /** + * + */ + function __construct(\Rekodi\Manager $manager) + { + parent::__construct( + $manager, + 'vms_metrics', + '\Bean\VMMetrics' + ); + } +} diff --git a/lib/Manager/XCPAbstract.php b/lib/Manager/XCPAbstract.php new file mode 100644 index 0000000..5432141 --- /dev/null +++ b/lib/Manager/XCPAbstract.php @@ -0,0 +1,70 @@ +. + * + * @author Julien Fontanet + * @license http://www.gnu.org/licenses/gpl-3.0-standalone.html GPLv3 + * + * @package Xen Orchestra Server + */ + +namespace Manager; + +/** + * + */ +abstract class XCPAbstract extends ManagerAbstract +{ + /** + * + */ + function batchImport(array $objects) + { + foreach ($objects as $id => $properties) + { + $properties['id'] = $id; + unset($properties['uuid']); + + $n = $this->_database->update( + $this->_table, + array('id' => $id), + $properties + ); + + if (1 === $n) + { + echo $this->_table.': updated ('.$id.')', PHP_EOL; + } + elseif (0 === $n) + { + $this->_database->create( + $this->_table, + array($properties) + ); + + echo $this->_table.': new ('.$id.')', PHP_EOL; + } + else + { + trigger_error( + 'unexpected number of updated '.$this->_table.' ('.$n.')', + E_USER_ERROR + ); + } + } + } +}