2015-04-24 15:57:01 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Objects;
|
|
|
|
|
2015-06-11 19:18:47 +02:00
|
|
|
use Icinga\Module\Director\CustomVariable\CustomVariables;
|
2015-04-24 15:57:01 +02:00
|
|
|
use Icinga\Module\Director\Data\Db\DbObject;
|
2015-08-02 13:43:16 +02:00
|
|
|
use Icinga\Module\Director\Db;
|
2015-06-16 17:58:47 +02:00
|
|
|
use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer;
|
2015-06-11 22:44:17 +02:00
|
|
|
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
|
2015-06-08 14:37:33 +02:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
|
|
|
use Exception;
|
2015-04-24 15:57:01 +02:00
|
|
|
|
2015-06-16 17:58:47 +02:00
|
|
|
abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
2015-04-24 15:57:01 +02:00
|
|
|
{
|
2015-07-02 14:31:35 +02:00
|
|
|
protected $keyName = 'object_name';
|
2015-04-24 15:57:01 +02:00
|
|
|
|
|
|
|
protected $autoincKeyName = 'id';
|
|
|
|
|
2015-06-08 13:02:09 +02:00
|
|
|
protected $supportsCustomVars = false;
|
|
|
|
|
2015-06-16 17:58:47 +02:00
|
|
|
protected $supportsGroups = false;
|
|
|
|
|
2015-07-01 15:15:49 +02:00
|
|
|
protected $supportsRanges = false;
|
|
|
|
|
2015-08-03 13:43:30 +02:00
|
|
|
protected $supportsArguments = false;
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
protected $supportsImports = false;
|
|
|
|
|
2015-07-30 09:08:27 +02:00
|
|
|
protected $supportsFields = false;
|
|
|
|
|
2015-08-28 18:21:04 +02:00
|
|
|
protected $type;
|
2015-06-08 14:37:33 +02:00
|
|
|
|
2015-11-13 23:59:26 +01:00
|
|
|
protected $booleans = array();
|
|
|
|
|
2015-06-11 19:46:36 +02:00
|
|
|
private $vars;
|
|
|
|
|
2015-06-16 17:58:47 +02:00
|
|
|
private $groups;
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
private $imports;
|
|
|
|
|
2015-07-02 14:51:59 +02:00
|
|
|
private $importedObjects;
|
|
|
|
|
2015-07-01 15:15:49 +02:00
|
|
|
private $ranges;
|
|
|
|
|
2015-08-03 13:43:30 +02:00
|
|
|
private $arguments;
|
|
|
|
|
2015-11-13 23:59:26 +01:00
|
|
|
public function propertyIsBoolean($property)
|
|
|
|
{
|
|
|
|
return array_key_exists($property, $this->booleans);
|
|
|
|
}
|
|
|
|
|
2015-06-08 13:02:09 +02:00
|
|
|
public function supportsCustomVars()
|
|
|
|
{
|
|
|
|
return $this->supportsCustomVars;
|
|
|
|
}
|
|
|
|
|
2015-06-16 17:58:47 +02:00
|
|
|
public function supportsGroups()
|
|
|
|
{
|
|
|
|
return $this->supportsGroups;
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:15:49 +02:00
|
|
|
public function supportsRanges()
|
|
|
|
{
|
|
|
|
return $this->supportsRanges;
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:43:30 +02:00
|
|
|
public function supportsArguments()
|
|
|
|
{
|
|
|
|
return $this->supportsArguments;
|
|
|
|
}
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
public function supportsImports()
|
|
|
|
{
|
|
|
|
return $this->supportsImports;
|
|
|
|
}
|
|
|
|
|
2015-07-30 09:08:27 +02:00
|
|
|
public function supportsFields()
|
|
|
|
{
|
|
|
|
return $this->supportsFields;
|
|
|
|
}
|
|
|
|
|
2015-06-24 10:13:06 +02:00
|
|
|
public function hasBeenModified()
|
|
|
|
{
|
|
|
|
if ($this->supportsCustomVars() && $this->vars !== null && $this->vars()->hasBeenModified()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-24 11:25:22 +02:00
|
|
|
if ($this->supportsGroups() && $this->groups !== null && $this->groups()->hasBeenModified()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
if ($this->supportsImports() && $this->imports !== null && $this->imports()->hasBeenModified()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:15:49 +02:00
|
|
|
if ($this->supportsRanges() && $this->ranges !== null && $this->ranges()->hasBeenModified()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-24 10:13:06 +02:00
|
|
|
return parent::hasBeenModified();
|
|
|
|
}
|
|
|
|
|
2015-08-28 18:23:13 +02:00
|
|
|
public function set($key, $value)
|
|
|
|
{
|
|
|
|
if ($key === 'groups') {
|
|
|
|
$this->groups()->set($value);
|
|
|
|
return $this;
|
|
|
|
|
|
|
|
} elseif ($key === 'imports') {
|
|
|
|
$this->imports()->set($value);
|
|
|
|
return $this;
|
|
|
|
|
2015-12-02 02:42:14 +01:00
|
|
|
} elseif ($key === 'arguments') {
|
|
|
|
if (is_object($value)) {
|
|
|
|
foreach ($value as $arg => $val) {
|
|
|
|
$this->arguments()->set($arg, $val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
|
2015-08-28 18:23:13 +02:00
|
|
|
} elseif ($key === 'vars') {
|
|
|
|
$value = (array) $value;
|
|
|
|
$unset = array();
|
|
|
|
foreach ($this->vars() as $k => $f) {
|
|
|
|
if (! array_key_exists($k, $value)) {
|
|
|
|
$unset[] = $k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($unset as $k) {
|
|
|
|
unset($this->vars()->$k);
|
|
|
|
}
|
|
|
|
foreach ($value as $k => $v) {
|
|
|
|
$this->vars()->set($k, $v);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-11-13 23:59:26 +01:00
|
|
|
if ($this->propertyIsBoolean($key) && $value !== null) {
|
|
|
|
if ($value === 'y' || $value === '1' || $value === true || $value === 1) {
|
|
|
|
return parent::set($key, 'y');
|
|
|
|
} elseif ($value === 'n' || $value === '0' || $value === false || $value === 0) {
|
|
|
|
return parent::set($key, 'n');
|
|
|
|
} else {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
'Got invalid boolean: %s',
|
|
|
|
var_export($value, 1)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 18:23:13 +02:00
|
|
|
return parent::set($key, $value);
|
|
|
|
}
|
|
|
|
|
2015-06-16 17:58:47 +02:00
|
|
|
public function groups()
|
|
|
|
{
|
|
|
|
$this->assertGroupsSupport();
|
|
|
|
if ($this->groups === null) {
|
|
|
|
if ($this->hasBeenLoadedFromDb()) {
|
|
|
|
$this->groups = IcingaObjectGroups::loadForStoredObject($this);
|
|
|
|
} else {
|
|
|
|
$this->groups = new IcingaObjectGroups($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->groups;
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:15:49 +02:00
|
|
|
public function ranges()
|
|
|
|
{
|
|
|
|
$this->assertRangesSupport();
|
|
|
|
if ($this->ranges === null) {
|
|
|
|
if ($this->hasBeenLoadedFromDb()) {
|
|
|
|
$this->ranges = IcingaTimePeriodRanges::loadForStoredObject($this);
|
|
|
|
} else {
|
|
|
|
$this->ranges = new IcingaTimePeriodRanges($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->ranges;
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:43:30 +02:00
|
|
|
public function arguments()
|
|
|
|
{
|
|
|
|
$this->assertArgumentsSupport();
|
|
|
|
if ($this->arguments === null) {
|
|
|
|
if ($this->hasBeenLoadedFromDb()) {
|
|
|
|
$this->arguments = IcingaArguments::loadForStoredObject($this);
|
|
|
|
} else {
|
|
|
|
$this->arguments = new IcingaArguments($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->arguments;
|
|
|
|
}
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
public function imports()
|
|
|
|
{
|
|
|
|
$this->assertImportsSupport();
|
|
|
|
if ($this->imports === null) {
|
|
|
|
if ($this->hasBeenLoadedFromDb()) {
|
|
|
|
$this->imports = IcingaObjectImports::loadForStoredObject($this);
|
|
|
|
} else {
|
|
|
|
$this->imports = new IcingaObjectImports($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->imports;
|
|
|
|
}
|
|
|
|
|
2015-07-02 14:51:59 +02:00
|
|
|
// TODO: what happens if imports change at runtime?
|
|
|
|
public function importedObjects()
|
|
|
|
{
|
|
|
|
$this->assertImportsSupport();
|
|
|
|
if ($this->importedObjects === null) {
|
|
|
|
$this->importedObjects = array();
|
|
|
|
foreach ($this->imports()->listImportNames() as $import) {
|
|
|
|
$this->importedObjects[$import] = self::load($import, $this->connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->importedObjects;
|
|
|
|
}
|
|
|
|
|
2015-07-30 11:39:00 +02:00
|
|
|
public function clearImportedObjects()
|
|
|
|
{
|
|
|
|
$this->importedObjects = null;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-11-06 09:49:05 +01:00
|
|
|
public function getResolvedProperty($key)
|
|
|
|
{
|
|
|
|
$properties = $this->getResolvedProperties();
|
|
|
|
if (property_exists($properties, $key)) {
|
|
|
|
return $properties->$key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-07-02 14:51:59 +02:00
|
|
|
public function getResolvedProperties()
|
|
|
|
{
|
2015-07-30 11:39:00 +02:00
|
|
|
return $this->getResolved('Properties');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInheritedProperties()
|
|
|
|
{
|
|
|
|
return $this->getInherited('Properties');
|
2015-07-02 14:51:59 +02:00
|
|
|
}
|
|
|
|
|
2015-07-31 14:52:46 +02:00
|
|
|
public function getOriginsProperties()
|
|
|
|
{
|
|
|
|
return $this->getOrigins('Properties');
|
|
|
|
}
|
|
|
|
|
2015-07-02 14:51:59 +02:00
|
|
|
public function resolveProperties()
|
|
|
|
{
|
2015-07-30 11:39:00 +02:00
|
|
|
return $this->resolve('Properties');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getResolvedFields()
|
|
|
|
{
|
|
|
|
return $this->getResolved('Fields');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInheritedFields()
|
|
|
|
{
|
|
|
|
return $this->getInherited('Fields');
|
|
|
|
}
|
|
|
|
|
2015-07-31 14:52:46 +02:00
|
|
|
public function getOriginsFields()
|
|
|
|
{
|
|
|
|
return $this->getOrigins('Fields');
|
|
|
|
}
|
|
|
|
|
2015-07-30 11:39:00 +02:00
|
|
|
public function resolveFields()
|
|
|
|
{
|
|
|
|
return $this->resolve('Fields');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getResolvedVars()
|
|
|
|
{
|
|
|
|
return $this->getResolved('Vars');
|
|
|
|
}
|
2015-07-02 14:51:59 +02:00
|
|
|
|
2015-07-30 11:39:00 +02:00
|
|
|
public function getInheritedVars()
|
|
|
|
{
|
|
|
|
return $this->getInherited('Vars');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function resolveVars()
|
|
|
|
{
|
|
|
|
return $this->resolve('Vars');
|
|
|
|
}
|
|
|
|
|
2015-07-31 14:52:46 +02:00
|
|
|
public function getOriginsVars()
|
|
|
|
{
|
|
|
|
return $this->getOrigins('Vars');
|
|
|
|
}
|
|
|
|
|
2015-07-30 11:39:00 +02:00
|
|
|
public function getVars()
|
|
|
|
{
|
|
|
|
$vars = (object) array();
|
|
|
|
foreach ($this->vars() as $key => $var) {
|
|
|
|
$vars->$key = $var->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getResolved($what)
|
|
|
|
{
|
|
|
|
$func = 'resolve' . $what;
|
|
|
|
$res = $this->$func();
|
|
|
|
return $res['_MERGED_'];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getInherited($what)
|
|
|
|
{
|
|
|
|
$func = 'resolve' . $what;
|
|
|
|
$res = $this->$func();
|
|
|
|
return $res['_INHERITED_'];
|
|
|
|
}
|
|
|
|
|
2015-07-31 14:52:46 +02:00
|
|
|
protected function getOrigins($what)
|
|
|
|
{
|
|
|
|
$func = 'resolve' . $what;
|
|
|
|
$res = $this->$func();
|
|
|
|
return $res['_ORIGINS_'];
|
|
|
|
}
|
|
|
|
|
2015-07-30 11:39:00 +02:00
|
|
|
protected function resolve($what)
|
|
|
|
{
|
|
|
|
$vals = array();
|
2015-07-31 14:52:46 +02:00
|
|
|
$vals['_MERGED_'] = (object) array();
|
2015-07-30 11:39:00 +02:00
|
|
|
$vals['_INHERITED_'] = (object) array();
|
2015-07-31 14:52:46 +02:00
|
|
|
$vals['_ORIGINS_'] = (object) array();
|
2015-07-30 11:39:00 +02:00
|
|
|
$objects = $this->importedObjects();
|
|
|
|
|
2015-07-31 14:52:46 +02:00
|
|
|
$get = 'get' . $what;
|
|
|
|
$getInherited = 'getInherited' . $what;
|
|
|
|
$getOrigins = 'getOrigins' . $what;
|
2015-07-30 11:39:00 +02:00
|
|
|
|
2015-07-02 14:51:59 +02:00
|
|
|
foreach ($objects as $name => $object) {
|
2015-07-31 14:52:46 +02:00
|
|
|
$origins = $object->$getOrigins();
|
2015-07-30 11:39:00 +02:00
|
|
|
|
2015-07-31 14:52:46 +02:00
|
|
|
foreach ($object->$getInherited() as $key => $value) {
|
|
|
|
// $vals[$name]->$key = $value;
|
|
|
|
$vals['_MERGED_']->$key = $value;
|
|
|
|
$vals['_INHERITED_']->$key = $value;
|
|
|
|
$vals['_ORIGINS_']->$key = $origins->$key;
|
2015-07-02 14:51:59 +02:00
|
|
|
}
|
2015-07-31 14:52:46 +02:00
|
|
|
|
|
|
|
foreach ($object->$get() as $key => $value) {
|
|
|
|
if ($value === null) continue;
|
|
|
|
$vals['_MERGED_']->$key = $value;
|
|
|
|
$vals['_INHERITED_']->$key = $value;
|
|
|
|
$vals['_ORIGINS_']->$key = $name;
|
2015-07-02 14:51:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 14:52:46 +02:00
|
|
|
$blacklist = array('id', 'object_type', 'object_name');
|
|
|
|
foreach ($this->$get() as $key => $value) {
|
|
|
|
if ($value === null) continue;
|
|
|
|
if (in_array($key, $blacklist)) continue;
|
|
|
|
|
|
|
|
// $vals[$this->object_name]->$key = $value;
|
|
|
|
$vals['_MERGED_']->$key = $value;
|
|
|
|
}
|
|
|
|
|
2015-07-30 11:39:00 +02:00
|
|
|
return $vals;
|
2015-07-02 14:51:59 +02:00
|
|
|
}
|
|
|
|
|
2015-06-11 19:18:47 +02:00
|
|
|
protected function assertCustomVarsSupport()
|
|
|
|
{
|
|
|
|
if (! $this->supportsCustomVars()) {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
'Objects of type "%s" have no custom vars',
|
|
|
|
$this->getType()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-06-16 17:58:47 +02:00
|
|
|
protected function assertGroupsSupport()
|
|
|
|
{
|
|
|
|
if (! $this->supportsGroups()) {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
'Objects of type "%s" have no groups',
|
|
|
|
$this->getType()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:15:49 +02:00
|
|
|
protected function assertRangesSupport()
|
|
|
|
{
|
|
|
|
if (! $this->supportsRanges()) {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
'Objects of type "%s" have no ranges',
|
|
|
|
$this->getType()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:43:30 +02:00
|
|
|
protected function assertArgumentsSupport()
|
|
|
|
{
|
|
|
|
if (! $this->supportsArguments()) {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
'Objects of type "%s" have no arguments',
|
|
|
|
$this->getType()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
protected function assertImportsSupport()
|
|
|
|
{
|
|
|
|
if (! $this->supportsImports()) {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
'Objects of type "%s" have no imports',
|
|
|
|
$this->getType()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-06-11 19:18:47 +02:00
|
|
|
public function vars()
|
|
|
|
{
|
|
|
|
$this->assertCustomVarsSupport();
|
|
|
|
if ($this->vars === null) {
|
2015-06-15 16:35:18 +02:00
|
|
|
if ($this->hasBeenLoadedFromDb()) {
|
|
|
|
$this->vars = CustomVariables::loadForStoredObject($this);
|
|
|
|
} else {
|
|
|
|
$this->vars = new CustomVariables();
|
|
|
|
}
|
2015-06-11 19:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->vars;
|
|
|
|
}
|
|
|
|
|
2015-06-15 16:35:18 +02:00
|
|
|
public function getVarsTableName()
|
|
|
|
{
|
|
|
|
return $this->getTableName() . '_var';
|
|
|
|
}
|
|
|
|
|
2015-06-16 17:58:47 +02:00
|
|
|
public function getShortTableName()
|
2015-06-15 16:35:18 +02:00
|
|
|
{
|
|
|
|
// strlen('icinga_') = 7
|
2015-06-16 17:58:47 +02:00
|
|
|
return substr($this->getTableName(), 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getVarsIdColumn()
|
|
|
|
{
|
|
|
|
return $this->getShortTableName() . '_id';
|
2015-06-15 16:35:18 +02:00
|
|
|
}
|
|
|
|
|
2015-07-30 09:09:44 +02:00
|
|
|
public function getFields()
|
|
|
|
{
|
|
|
|
$fields = (object) array();
|
|
|
|
|
|
|
|
if (! $this->supportsFields()) {
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
$db = $this->getDb();
|
|
|
|
|
|
|
|
$query = $db->select()->from(
|
|
|
|
array('df' => 'director_datafield'),
|
|
|
|
array(
|
|
|
|
'datafield_id' => 'f.datafield_id',
|
|
|
|
'is_required' => 'f.is_required',
|
2015-11-14 00:21:42 +01:00
|
|
|
'varname' => 'df.varname',
|
|
|
|
'description' => 'df.description',
|
|
|
|
'datatype' => 'df.datatype',
|
|
|
|
'format' => 'df.format',
|
2015-07-30 09:09:44 +02:00
|
|
|
)
|
|
|
|
)->join(
|
|
|
|
array('f' => $this->getTableName() . '_field'),
|
|
|
|
'df.id = f.datafield_id',
|
|
|
|
array()
|
2015-07-31 17:34:12 +02:00
|
|
|
)->where('f.' . $this->getShortTableName() . '_id = ?', (int) $this->id)
|
2015-07-30 09:09:44 +02:00
|
|
|
->order('df.caption ASC');
|
|
|
|
|
|
|
|
$res = $db->fetchAll($query);
|
|
|
|
|
|
|
|
foreach ($res as $r) {
|
|
|
|
$fields->{$r->varname} = $r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2015-06-08 13:02:09 +02:00
|
|
|
public function isTemplate()
|
|
|
|
{
|
2015-06-08 14:37:33 +02:00
|
|
|
return $this->hasProperty('object_type')
|
2015-06-08 13:02:09 +02:00
|
|
|
&& $this->object_type === 'template';
|
|
|
|
}
|
|
|
|
|
2015-12-01 14:47:10 +01:00
|
|
|
public function isExternal()
|
|
|
|
{
|
|
|
|
return $this->hasProperty('object_type')
|
|
|
|
&& $this->object_type === 'external_object';
|
|
|
|
}
|
|
|
|
|
2015-06-08 13:02:09 +02:00
|
|
|
public function isApplyRule()
|
|
|
|
{
|
2015-06-08 14:37:33 +02:00
|
|
|
return $this->hasProperty('object_type')
|
2015-06-08 13:02:09 +02:00
|
|
|
&& $this->object_type === 'apply';
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:43:30 +02:00
|
|
|
protected function storeRelatedObjects()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->storeCustomVars()
|
|
|
|
->storeGroups()
|
|
|
|
->storeImports()
|
|
|
|
->storeRanges()
|
|
|
|
->storeArguments();
|
|
|
|
}
|
|
|
|
|
2015-04-24 15:57:01 +02:00
|
|
|
public function onInsert()
|
|
|
|
{
|
|
|
|
DirectorActivityLog::logCreation($this, $this->connection);
|
2015-08-29 01:03:34 +02:00
|
|
|
$this->storeRelatedObjects();
|
2015-04-24 15:57:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onUpdate()
|
|
|
|
{
|
|
|
|
DirectorActivityLog::logModification($this, $this->connection);
|
2015-08-29 01:03:34 +02:00
|
|
|
$this->storeRelatedObjects();
|
2015-04-24 15:57:01 +02:00
|
|
|
}
|
|
|
|
|
2015-06-24 10:13:06 +02:00
|
|
|
protected function storeCustomVars()
|
|
|
|
{
|
|
|
|
if ($this->supportsCustomVars()) {
|
2015-06-24 11:25:22 +02:00
|
|
|
$this->vars !== null && $this->vars()->storeToDb($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function storeGroups()
|
|
|
|
{
|
|
|
|
if ($this->supportsGroups()) {
|
|
|
|
$this->groups !== null && $this->groups()->store();
|
2015-06-24 10:13:06 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:55:16 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:15:49 +02:00
|
|
|
protected function storeRanges()
|
|
|
|
{
|
|
|
|
if ($this->supportsRanges()) {
|
|
|
|
$this->ranges !== null && $this->ranges()->store();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:43:30 +02:00
|
|
|
protected function storeArguments()
|
|
|
|
{
|
|
|
|
if ($this->supportsArguments()) {
|
|
|
|
$this->arguments !== null && $this->arguments()->store();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:55:16 +02:00
|
|
|
protected function storeImports()
|
|
|
|
{
|
|
|
|
if ($this->supportsImports()) {
|
|
|
|
$this->imports !== null && $this->imports()->store();
|
|
|
|
}
|
|
|
|
|
2015-06-24 10:13:06 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-04-24 15:57:01 +02:00
|
|
|
public function onDelete()
|
|
|
|
{
|
|
|
|
DirectorActivityLog::logRemoval($this, $this->connection);
|
|
|
|
}
|
2015-06-08 14:37:33 +02:00
|
|
|
|
|
|
|
protected function renderImports()
|
|
|
|
{
|
|
|
|
// TODO: parent_host ORDERed by weigth...
|
2015-06-26 10:39:30 +02:00
|
|
|
if ($this->supportsImports()) {
|
|
|
|
return $this->imports()->toConfigString();
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
2015-06-08 14:37:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function renderProperties()
|
|
|
|
{
|
|
|
|
$out = '';
|
|
|
|
$blacklist = array(
|
|
|
|
'id',
|
|
|
|
'object_name',
|
|
|
|
'object_type',
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($this->properties as $key => $value) {
|
|
|
|
|
|
|
|
if ($value === null) continue;
|
|
|
|
if (in_array($key, $blacklist)) continue;
|
|
|
|
|
|
|
|
$method = 'render' . ucfirst($key);
|
|
|
|
if (method_exists($this, $method)) {
|
|
|
|
$out .= $this->$method($value);
|
|
|
|
} else {
|
2015-11-13 23:59:26 +01:00
|
|
|
if ($this->propertyIsBoolean($key)) {
|
|
|
|
if ($value !== $this->defaultProperties[$key]) {
|
|
|
|
$out .= c::renderKeyValue(
|
|
|
|
$this->booleans[$key],
|
|
|
|
c::renderBoolean($value)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$out .= c::renderKeyValue($key, c::renderString($value));
|
|
|
|
}
|
2015-06-08 14:37:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function renderBooleanProperty($key)
|
|
|
|
{
|
2015-06-11 23:02:43 +02:00
|
|
|
return c::renderKeyValue($key, c::renderBoolean($this->$key));
|
2015-06-08 14:37:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function renderSuffix()
|
|
|
|
{
|
2015-06-11 22:49:06 +02:00
|
|
|
return "}\n\n";
|
2015-06-08 14:37:33 +02:00
|
|
|
}
|
|
|
|
|
2015-06-10 15:40:45 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-06-08 14:37:33 +02:00
|
|
|
protected function renderCustomVars()
|
|
|
|
{
|
|
|
|
if ($this->supportsCustomVars()) {
|
2015-06-15 14:23:15 +02:00
|
|
|
return $this->vars()->toConfigString();
|
|
|
|
} else {
|
|
|
|
return '';
|
2015-06-08 14:37:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 17:58:47 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function renderGroups()
|
|
|
|
{
|
|
|
|
if ($this->supportsGroups()) {
|
|
|
|
return $this->groups()->toConfigString();
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:15:49 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function renderRanges()
|
|
|
|
{
|
|
|
|
if ($this->supportsRanges()) {
|
|
|
|
return $this->ranges()->toConfigString();
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:43:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function renderArguments()
|
|
|
|
{
|
|
|
|
if ($this->supportsArguments()) {
|
|
|
|
return $this->arguments()->toConfigString();
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2015-10-16 18:41:07 +02:00
|
|
|
|
2015-06-08 14:37:33 +02:00
|
|
|
protected function renderCommandProperty($commandId, $propertyName = 'check_command')
|
|
|
|
{
|
2015-06-11 07:44:22 +02:00
|
|
|
return c::renderKeyValue(
|
2015-06-08 14:37:33 +02:00
|
|
|
$propertyName,
|
2015-06-11 23:02:43 +02:00
|
|
|
c::renderString($this->connection->getCommandName($commandId))
|
2015-06-08 14:37:33 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function renderZoneProperty($zoneId, $propertyName = 'zone')
|
|
|
|
{
|
2015-08-29 01:04:28 +02:00
|
|
|
// TODO: set file info?
|
|
|
|
return '';
|
2015-06-11 07:44:22 +02:00
|
|
|
return c::renderKeyValue(
|
2015-06-08 14:37:33 +02:00
|
|
|
$propertyName,
|
2015-06-11 23:02:43 +02:00
|
|
|
c::renderString($this->connection->getZoneName($zoneId))
|
2015-06-08 14:37:33 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function renderZone_id()
|
|
|
|
{
|
|
|
|
return $this->renderZoneProperty($this->zone_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function renderObjectHeader()
|
|
|
|
{
|
|
|
|
return sprintf(
|
|
|
|
"%s %s %s {\n",
|
|
|
|
$this->getObjectTypeName(),
|
|
|
|
$this->getType(),
|
2015-06-11 07:44:22 +02:00
|
|
|
c::renderString($this->getObjectName())
|
2015-06-08 14:37:33 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toConfigString()
|
|
|
|
{
|
|
|
|
return implode(array(
|
|
|
|
$this->renderObjectHeader(),
|
|
|
|
$this->renderImports(),
|
|
|
|
$this->renderProperties(),
|
2015-07-01 15:15:49 +02:00
|
|
|
$this->renderRanges(),
|
2015-08-03 13:43:30 +02:00
|
|
|
$this->renderArguments(),
|
2015-06-16 17:58:47 +02:00
|
|
|
$this->renderGroups(),
|
2015-06-08 14:37:33 +02:00
|
|
|
$this->renderCustomVars(),
|
|
|
|
$this->renderSuffix()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-06-30 11:19:31 +02:00
|
|
|
public function isGroup()
|
|
|
|
{
|
|
|
|
return substr($this->getType(), -5) === 'Group';
|
|
|
|
}
|
|
|
|
|
2015-11-06 09:49:05 +01:00
|
|
|
public function hasCheckCommand()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-08 14:37:33 +02:00
|
|
|
protected function getType()
|
|
|
|
{
|
|
|
|
if ($this->type === null) {
|
|
|
|
$parts = explode('\\', get_class($this));
|
|
|
|
// 6 = strlen('Icinga');
|
|
|
|
$this->type = substr(end($parts), 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getObjectTypeName()
|
|
|
|
{
|
|
|
|
if ($this->isTemplate()) {
|
|
|
|
return 'template';
|
|
|
|
} elseif ($this->isApplyRule()) {
|
|
|
|
return 'apply';
|
|
|
|
} else {
|
|
|
|
return 'object';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getObjectName()
|
|
|
|
{
|
|
|
|
if ($this->hasProperty('object_name')) {
|
|
|
|
return $this->object_name;
|
|
|
|
} else {
|
|
|
|
// TODO: replace with an exception once finished
|
|
|
|
return 'ERROR: NO NAME';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 19:26:53 +02:00
|
|
|
protected static function classByType($type)
|
2015-08-02 13:43:16 +02:00
|
|
|
{
|
2015-08-29 01:12:04 +02:00
|
|
|
// allow for icinga_host and host
|
|
|
|
$type = preg_replace('/^icinga_/', '', $type);
|
|
|
|
|
2015-08-04 19:26:53 +02:00
|
|
|
if (strpos($type, 'data') === false) {
|
|
|
|
$prefix = 'Icinga';
|
|
|
|
} else {
|
|
|
|
$prefix = 'Director';
|
|
|
|
}
|
2015-08-29 01:12:04 +02:00
|
|
|
|
2015-08-04 19:26:53 +02:00
|
|
|
return 'Icinga\\Module\\Director\\Objects\\' . $prefix . ucfirst($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function createByType($type, $properties = array(), Db $db = null)
|
|
|
|
{
|
|
|
|
$class = self::classByType($type);
|
|
|
|
return $class::create($properties, $db);
|
|
|
|
}
|
2015-08-02 13:43:16 +02:00
|
|
|
|
2015-08-04 19:26:53 +02:00
|
|
|
public static function loadByType($type, $id, Db $db)
|
|
|
|
{
|
|
|
|
$class = self::classByType($type);
|
|
|
|
return $class::load($id, $db);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function loadAllByType($type, Db $db, $query = null, $keyColumn = 'object_name')
|
|
|
|
{
|
|
|
|
if ($type === 'datalistEntry') $keyColumn = 'entry_name';
|
|
|
|
$class = self::classByType($type);
|
|
|
|
return $class::loadAll($db, $query, $keyColumn);
|
2015-08-02 13:43:16 +02:00
|
|
|
}
|
|
|
|
|
2015-12-02 03:08:57 +01:00
|
|
|
public function toJson($resolved = false)
|
|
|
|
{
|
|
|
|
$props = array();
|
|
|
|
foreach ($this->getProperties() as $k => $v) {
|
|
|
|
if ($v !== null) {
|
|
|
|
$props[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->supportsGroups()) {
|
|
|
|
// TODO: resolve
|
|
|
|
$props['groups'] = $this->groups()->listGroupNames();
|
|
|
|
}
|
|
|
|
if ($this->supportsCustomVars()) {
|
|
|
|
if ($resolved) {
|
|
|
|
$props['vars'] = $this->getVars();
|
|
|
|
} else {
|
|
|
|
$props['vars'] = $this->getResolvedVars();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($this->supportsImports()) {
|
|
|
|
$props['imports'] = $this->imports()->listImportNames();
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_encode($props);
|
|
|
|
}
|
|
|
|
|
2015-06-08 14:37:33 +02:00
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $this->toConfigString();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
trigger_error($e);
|
|
|
|
$previousHandler = set_exception_handler(function () {});
|
|
|
|
restore_error_handler();
|
2015-06-15 16:36:51 +02:00
|
|
|
if ($previousHandler !== null) {
|
|
|
|
call_user_func($previousHandler, $e);
|
|
|
|
die();
|
|
|
|
} else {
|
|
|
|
die($e->getMessage());
|
|
|
|
}
|
2015-06-08 14:37:33 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-24 15:57:01 +02:00
|
|
|
}
|