From 91dde444413310c065af6a24fbd0ee11e4cc74ae Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 15 Jun 2015 16:35:18 +0200 Subject: [PATCH] CustomVariables: add load from db support --- .../CustomVariable/CustomVariable.php | 19 ++++++++++++++++ .../CustomVariable/CustomVariables.php | 22 +++++++++++++++++++ library/Director/Objects/IcingaObject.php | 17 +++++++++++++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/library/Director/CustomVariable/CustomVariable.php b/library/Director/CustomVariable/CustomVariable.php index 6d15c560..6af8cba1 100644 --- a/library/Director/CustomVariable/CustomVariable.php +++ b/library/Director/CustomVariable/CustomVariable.php @@ -109,6 +109,25 @@ abstract class CustomVariable } } + public static function fromDbRow($row) + { + switch($row->format) { + case 'string': + return new CustomVariableString($row->varname, $row->varvalue); + case 'json': + return self::create($row->varname, json_decode($row->varvalue)); + case 'expression': + throw new ProgrammingError( + 'Icinga code expressions are not yet supported' + ); + default: + throw new ProgrammingError( + '%s is not a supported custom variable format', + $row->format + ); + } + } + public function __toString() { try { diff --git a/library/Director/CustomVariable/CustomVariables.php b/library/Director/CustomVariable/CustomVariables.php index a5053191..53d53dab 100644 --- a/library/Director/CustomVariable/CustomVariables.php +++ b/library/Director/CustomVariable/CustomVariables.php @@ -3,6 +3,7 @@ namespace Icinga\Module\Director\CustomVariable; use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c; +use Icinga\Module\Director\Objects\IcingaObject; class CustomVariables { @@ -38,6 +39,27 @@ class CustomVariables return $this; } + public static function loadForStoredObject(IcingaObject $object) + { + $db = $object->getDb(); + + $query = $db->select()->from( + array('v' => $object->getVarsTableName()), + array( + 'v.varname', + 'v.varvalue', + 'v.format', + ) + )->where(sprintf('v.%s = ?', $object->getVarsIdColumn()), $object->getId()); + + $vars = new CustomVariables; + foreach ($db->fetchAll($query) as $row) { + $vars->vars[$row->varname] = CustomVariable::fromDbRow($row); + } + + return $vars; + } + public function get($key) { if (array_key_exists($key, $this->vars)) { diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php index 0f8aad9b..fc804adc 100644 --- a/library/Director/Objects/IcingaObject.php +++ b/library/Director/Objects/IcingaObject.php @@ -41,12 +41,27 @@ abstract class IcingaObject extends DbObject { $this->assertCustomVarsSupport(); if ($this->vars === null) { - $this->vars = new CustomVariables(); + if ($this->hasBeenLoadedFromDb()) { + $this->vars = CustomVariables::loadForStoredObject($this); + } else { + $this->vars = new CustomVariables(); + } } return $this->vars; } + public function getVarsTableName() + { + return $this->getTableName() . '_var'; + } + + public function getVarsIdColumn() + { + // strlen('icinga_') = 7 + return substr($this->getTableName(), 7) . '_id'; + } + public function isTemplate() { return $this->hasProperty('object_type')