CustomVariables: add load from db support
This commit is contained in:
parent
fbc56df500
commit
91dde44441
|
@ -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 {
|
||||
|
|
|
@ -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)) {
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue