mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-27 15:54:03 +02:00
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()
|
public function __toString()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace Icinga\Module\Director\CustomVariable;
|
namespace Icinga\Module\Director\CustomVariable;
|
||||||
|
|
||||||
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
|
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
|
||||||
|
use Icinga\Module\Director\Objects\IcingaObject;
|
||||||
|
|
||||||
class CustomVariables
|
class CustomVariables
|
||||||
{
|
{
|
||||||
@ -38,6 +39,27 @@ class CustomVariables
|
|||||||
return $this;
|
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)
|
public function get($key)
|
||||||
{
|
{
|
||||||
if (array_key_exists($key, $this->vars)) {
|
if (array_key_exists($key, $this->vars)) {
|
||||||
|
@ -41,12 +41,27 @@ abstract class IcingaObject extends DbObject
|
|||||||
{
|
{
|
||||||
$this->assertCustomVarsSupport();
|
$this->assertCustomVarsSupport();
|
||||||
if ($this->vars === null) {
|
if ($this->vars === null) {
|
||||||
|
if ($this->hasBeenLoadedFromDb()) {
|
||||||
|
$this->vars = CustomVariables::loadForStoredObject($this);
|
||||||
|
} else {
|
||||||
$this->vars = new CustomVariables();
|
$this->vars = new CustomVariables();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $this->vars;
|
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()
|
public function isTemplate()
|
||||||
{
|
{
|
||||||
return $this->hasProperty('object_type')
|
return $this->hasProperty('object_type')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user