summaryrefslogtreecommitdiffstats
path: root/library/Director/Objects/IcingaNotification.php
diff options
context:
space:
mode:
Diffstat (limited to 'library/Director/Objects/IcingaNotification.php')
-rw-r--r--library/Director/Objects/IcingaNotification.php125
1 files changed, 55 insertions, 70 deletions
diff --git a/library/Director/Objects/IcingaNotification.php b/library/Director/Objects/IcingaNotification.php
index 9c5d08d..4768704 100644
--- a/library/Director/Objects/IcingaNotification.php
+++ b/library/Director/Objects/IcingaNotification.php
@@ -2,9 +2,8 @@
namespace Icinga\Module\Director\Objects;
-use Icinga\Module\Director\Db;
+use Icinga\Module\Director\CustomVariable\CustomVariables;
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
-use Icinga\Module\Director\Exception\DuplicateKeyException;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use RuntimeException;
@@ -29,6 +28,8 @@ class IcingaNotification extends IcingaObject implements ExportInterface
'notification_interval' => null,
'period_id' => null,
'zone_id' => null,
+ 'users_var' => null,
+ 'user_groups_var' => null,
'assign_filter' => null,
];
@@ -85,92 +86,76 @@ class IcingaNotification extends IcingaObject implements ExportInterface
* @codingStandardsIgnoreStart
* @return string
*/
- protected function renderTimes_end()
+ protected function renderUsers_var()
{
// @codingStandardsIgnoreEnd
- return c::renderKeyValue('times.end', c::renderInterval($this->get('times_end')));
- }
-
- public function getUniqueIdentifier()
- {
- return $this->getObjectName();
+ return '';
}
/**
- * @return \stdClass
- * @deprecated please use \Icinga\Module\Director\Data\Exporter
- * @throws \Icinga\Exception\NotFoundError
+ * @codingStandardsIgnoreStart
+ * @return string
*/
- public function export()
+ protected function renderUser_groups_var()
{
- // TODO: ksort in toPlainObject?
- $props = (array) $this->toPlainObject();
- $props['fields'] = $this->loadFieldReferences();
- ksort($props);
-
- return (object) $props;
+ // @codingStandardsIgnoreEnd
+ return '';
}
- /**
- * @param $plain
- * @param Db $db
- * @param bool $replace
- * @return static
- * @throws DuplicateKeyException
- * @throws \Icinga\Exception\NotFoundError
- */
- public static function import($plain, Db $db, $replace = false)
+ protected function renderUserVarsSuffixFor($property)
{
- $properties = (array) $plain;
- $name = $properties['object_name'];
- $key = $name;
-
- if ($replace && static::exists($key, $db)) {
- $object = static::load($key, $db);
- } elseif (static::exists($key, $db)) {
- throw new DuplicateKeyException(
- 'Notification "%s" already exists',
- $name
- );
- } else {
- $object = static::create([], $db);
+ $varName = $this->getResolvedProperty("{$property}_var");
+ if ($varName === null) {
+ return '';
+ }
+
+ $varSuffix = CustomVariables::renderKeySuffix($varName);
+ $indent = ' ';
+ $objectType = $this->get('apply_to');
+ if ($objectType === 'service') {
+ return "{$indent}if (service.vars$varSuffix) {\n"
+ . c::renderKeyOperatorValue($property, '+=', "service.vars$varSuffix", $indent . ' ')
+ . "$indent} else {\n"
+ . $this->getHostSnippet($indent . ' ')
+ . "$indent if (host.vars$varSuffix) { "
+ . c::renderKeyOperatorValue($property, '+=', "host.vars$varSuffix }", '')
+ . "$indent}\n";
+ } elseif ($objectType === 'host') {
+ return $this->getHostSnippet()
+ . "{$indent}if (host.vars$varSuffix) { "
+ . c::renderKeyOperatorValue($property, '+=', "host.vars$varSuffix }");
}
- // $object->newFields = $properties['fields'];
- unset($properties['fields']);
- $object->setProperties($properties);
+ return '';
+ }
+
+ protected function getHostSnippet($indent = ' ')
+ {
+ return "{$indent}if (! host) {\n"
+ . "$indent var host = get_host(host_name)\n"
+ . "$indent}\n";
+ }
- return $object;
+ protected function renderSuffix()
+ {
+ return $this->renderUserVarsSuffixFor('users')
+ . $this->renderUserVarsSuffixFor('user_groups')
+ . parent::renderSuffix();
}
/**
- * @deprecated please use \Icinga\Module\Director\Data\FieldReferenceLoader
- * @return array
+ * @codingStandardsIgnoreStart
+ * @return string
*/
- protected function loadFieldReferences()
+ protected function renderTimes_end()
{
- $db = $this->getDb();
-
- $res = $db->fetchAll(
- $db->select()->from([
- 'nf' => 'icinga_notification_field'
- ], [
- 'nf.datafield_id',
- 'nf.is_required',
- 'nf.var_filter',
- ])->join(['df' => 'director_datafield'], 'df.id = nf.datafield_id', [])
- ->where('notification_id = ?', $this->get('id'))
- ->order('varname ASC')
- );
-
- if (empty($res)) {
- return [];
- } else {
- foreach ($res as $field) {
- $field->datafield_id = (int) $field->datafield_id;
- }
- return $res;
- }
+ // @codingStandardsIgnoreEnd
+ return c::renderKeyValue('times.end', c::renderInterval($this->get('times_end')));
+ }
+
+ public function getUniqueIdentifier()
+ {
+ return $this->getObjectName();
}
/**