summaryrefslogtreecommitdiffstats
path: root/library/Director/Hook
diff options
context:
space:
mode:
Diffstat (limited to 'library/Director/Hook')
-rw-r--r--library/Director/Hook/BranchSupportHook.php39
-rw-r--r--library/Director/Hook/DataTypeHook.php59
-rw-r--r--library/Director/Hook/DeploymentHook.php65
-rw-r--r--library/Director/Hook/HostFieldHook.php19
-rw-r--r--library/Director/Hook/IcingaObjectFormHook.php22
-rw-r--r--library/Director/Hook/ImportSourceHook.php136
-rw-r--r--library/Director/Hook/JobHook.php86
-rw-r--r--library/Director/Hook/PropertyModifierHook.php258
-rw-r--r--library/Director/Hook/ServiceFieldHook.php19
-rw-r--r--library/Director/Hook/ShipConfigFilesHook.php11
10 files changed, 714 insertions, 0 deletions
diff --git a/library/Director/Hook/BranchSupportHook.php b/library/Director/Hook/BranchSupportHook.php
new file mode 100644
index 0000000..6615cbe
--- /dev/null
+++ b/library/Director/Hook/BranchSupportHook.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Icinga\Module\Director\Hook;
+
+use Icinga\Authentication\Auth;
+use Icinga\Module\Director\Dashboard\Dashlet\Dashlet;
+use Icinga\Module\Director\Db;
+use Icinga\Module\Director\Db\Branch\Branch;
+use Icinga\Module\Director\Db\Branch\BranchSTore;
+use Icinga\Web\Request;
+use ipl\Html\ValidHtml;
+
+abstract class BranchSupportHook
+{
+ /**
+ * @param Request $request
+ * @param BranchSTore $store
+ * @param Auth $auth
+ * @return Branch
+ */
+ abstract public function getBranchForRequest(Request $request, BranchStore $store, Auth $auth);
+
+ /**
+ * @param Branch $branch
+ * @param Auth $auth
+ * @param ?string $label
+ * @return ?ValidHtml
+ */
+ abstract public function linkToBranch(Branch $branch, Auth $auth, $label = null);
+
+ /**
+ * @param Db $db
+ * @return Dashlet[]
+ */
+ public function loadDashlets(Db $db)
+ {
+ return [];
+ }
+}
diff --git a/library/Director/Hook/DataTypeHook.php b/library/Director/Hook/DataTypeHook.php
new file mode 100644
index 0000000..1eb58d6
--- /dev/null
+++ b/library/Director/Hook/DataTypeHook.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace Icinga\Module\Director\Hook;
+
+use Icinga\Module\Director\Web\Form\DirectorObjectForm;
+use Icinga\Module\Director\Web\Form\QuickForm;
+
+abstract class DataTypeHook
+{
+ protected $settings = array();
+
+ public function getName()
+ {
+ $parts = explode('\\', get_class($this));
+ $class = preg_replace('/DataType/', '', array_pop($parts));
+
+ if (array_shift($parts) === 'Icinga' && array_shift($parts) === 'Module') {
+ $module = array_shift($parts);
+ if ($module !== 'Director') {
+ return sprintf('%s (%s)', $class, $module);
+ }
+ }
+
+ return $class;
+ }
+
+ public static function getFormat()
+ {
+ return 'string';
+ }
+
+ /**
+ * @param $name
+ * @param QuickForm|DirectorObjectForm $form
+ *
+ * @return \Zend_Form_Element
+ */
+ abstract public function getFormElement($name, QuickForm $form);
+
+ public static function addSettingsFormFields(QuickForm $form)
+ {
+ return $form;
+ }
+
+ public function setSettings($settings)
+ {
+ $this->settings = $settings;
+ return $this;
+ }
+
+ public function getSetting($name, $default = null)
+ {
+ if (array_key_exists($name, $this->settings)) {
+ return $this->settings[$name];
+ } else {
+ return $default;
+ }
+ }
+}
diff --git a/library/Director/Hook/DeploymentHook.php b/library/Director/Hook/DeploymentHook.php
new file mode 100644
index 0000000..c8a834b
--- /dev/null
+++ b/library/Director/Hook/DeploymentHook.php
@@ -0,0 +1,65 @@
+<?php
+
+namespace Icinga\Module\Director\Hook;
+
+use Icinga\Module\Director\Objects\DirectorDeploymentLog;
+
+abstract class DeploymentHook
+{
+ /**
+ * Please override this method if you want to change the behaviour
+ * of the deploy (stop it by throwing an exception for some reason)
+ *
+ * @param DirectorDeploymentLog $deployment
+ */
+ public function beforeDeploy(DirectorDeploymentLog $deployment)
+ {
+ }
+
+ /**
+ * Please override this method if you want to trigger custom actions
+ * on a successful dump of the Icinga configuration
+ *
+ * @param DirectorDeploymentLog $deployment
+ */
+ public function onSuccessfulDump(DirectorDeploymentLog $deployment)
+ {
+ }
+
+ /**
+ * There is a typo in this method name, do not use this.
+ *
+ * @deprecated Please use onSuccessfulDump
+ * @param DirectorDeploymentLog $deployment
+ */
+ public function onSuccessfullDump(DirectorDeploymentLog $deployment)
+ {
+ }
+
+ /**
+ * Compatibility helper
+ *
+ * The initial version of this hook had a typo in the onSuccessfulDump method
+ * That's why we call this hook, which then calls both the correct and the
+ * erroneous method to make sure that we do not break existing implementations.
+ *
+ * @param DirectorDeploymentLog $deploymentLog
+ */
+ final public function triggerSuccessfulDump(DirectorDeploymentLog $deploymentLog)
+ {
+ $this->onSuccessfulDump($deploymentLog);
+ $this->onSuccessfullDump($deploymentLog);
+ }
+
+ /**
+ * Please override this method if you want to trigger custom actions
+ * once success (or failure) information have been collected for a deployed
+ * stage. startup_succeeded will then be filled, and startup_log might be
+ * available
+ *
+ * @param DirectorDeploymentLog $deployment
+ */
+ public function onCollect(DirectorDeploymentLog $deployment)
+ {
+ }
+}
diff --git a/library/Director/Hook/HostFieldHook.php b/library/Director/Hook/HostFieldHook.php
new file mode 100644
index 0000000..c0199d0
--- /dev/null
+++ b/library/Director/Hook/HostFieldHook.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace Icinga\Module\Director\Hook;
+
+use Icinga\Module\Director\Field\FieldSpec;
+use Icinga\Module\Director\Objects\IcingaHost;
+
+abstract class HostFieldHook
+{
+ public function wants(IcingaHost $host)
+ {
+ return true;
+ }
+
+ /**
+ * @return FieldSpec
+ */
+ abstract public function getFieldSpec(IcingaHost $host);
+}
diff --git a/library/Director/Hook/IcingaObjectFormHook.php b/library/Director/Hook/IcingaObjectFormHook.php
new file mode 100644
index 0000000..1d20ee1
--- /dev/null
+++ b/library/Director/Hook/IcingaObjectFormHook.php
@@ -0,0 +1,22 @@
+<?php
+
+namespace Icinga\Module\Director\Hook;
+
+use Icinga\Module\Director\Web\Form\DirectorObjectForm;
+use Icinga\Web\Hook;
+
+abstract class IcingaObjectFormHook
+{
+ protected $settings = [];
+
+ abstract public function onSetup(DirectorObjectForm $form);
+
+ public static function callOnSetup(DirectorObjectForm $form)
+ {
+ /** @var static[] $implementations */
+ $implementations = Hook::all('director/IcingaObjectForm');
+ foreach ($implementations as $implementation) {
+ $implementation->onSetup($form);
+ }
+ }
+}
diff --git a/library/Director/Hook/ImportSourceHook.php b/library/Director/Hook/ImportSourceHook.php
new file mode 100644
index 0000000..dba1c87
--- /dev/null
+++ b/library/Director/Hook/ImportSourceHook.php
@@ -0,0 +1,136 @@
+<?php
+
+namespace Icinga\Module\Director\Hook;
+
+use Icinga\Module\Director\Objects\ImportSource;
+use Icinga\Module\Director\Web\Form\QuickForm;
+use Icinga\Module\Director\Db;
+use Icinga\Exception\ConfigurationError;
+
+abstract class ImportSourceHook
+{
+ protected $settings = [];
+
+ public function getName()
+ {
+ $parts = explode('\\', get_class($this));
+ $class = preg_replace('/ImportSource/', '', array_pop($parts));
+
+ if (array_shift($parts) === 'Icinga' && array_shift($parts) === 'Module') {
+ $module = array_shift($parts);
+ if ($module !== 'Director') {
+ if ($class === '') {
+ return sprintf('%s module', $module);
+ }
+ return sprintf('%s (%s)', $class, $module);
+ }
+ }
+
+ return $class;
+ }
+
+ public static function forImportSource(ImportSource $source)
+ {
+ $db = $source->getDb();
+ $settings = $db->fetchPairs(
+ $db->select()->from(
+ 'import_source_setting',
+ ['setting_name', 'setting_value']
+ )->where('source_id = ?', $source->get('id'))
+ );
+
+ $className = $source->get('provider_class');
+ if (! class_exists($className)) {
+ throw new ConfigurationError(
+ 'Cannot load import provider class %s',
+ $className
+ );
+ }
+
+ /** @var ImportSourceHook $obj */
+ $obj = new $className;
+ $obj->setSettings($settings);
+ return $obj;
+ }
+
+ public static function loadByName($name, Db $db)
+ {
+ $db = $db->getDbAdapter();
+ $source = $db->fetchRow(
+ $db->select()->from(
+ 'import_source',
+ array('id', 'provider_class')
+ )->where('source_name = ?', $name)
+ );
+
+ $settings = $db->fetchPairs(
+ $db->select()->from(
+ 'import_source_setting',
+ array('setting_name', 'setting_value')
+ )->where('source_id = ?', $source->id)
+ );
+
+ if (! class_exists($source->provider_class)) {
+ throw new ConfigurationError(
+ 'Cannot load import provider class %s',
+ $source->provider_class
+ );
+ }
+ /** @var ImportSourceHook $obj */
+ $obj = new $source->provider_class;
+ $obj->setSettings($settings);
+
+ return $obj;
+ }
+
+ public function setSettings($settings)
+ {
+ $this->settings = $settings;
+ return $this;
+ }
+
+ public function getSetting($name, $default = null)
+ {
+ if (array_key_exists($name, $this->settings)) {
+ return $this->settings[$name];
+ } else {
+ return $default;
+ }
+ }
+
+ /**
+ * Returns an array containing importable objects
+ *
+ * @return array
+ */
+ abstract public function fetchData();
+
+ /**
+ * Returns a list of all available columns
+ *
+ * @return array
+ */
+ abstract public function listColumns();
+
+ /**
+ * Override this method in case you want to suggest a default
+ * key column
+ *
+ * @return string|null Default key column
+ */
+ public static function getDefaultKeyColumnName()
+ {
+ return null;
+ }
+
+ /**
+ * Override this method if you want to extend the settings form
+ *
+ * @param QuickForm $form QuickForm that should be extended
+ * @return QuickForm
+ */
+ public static function addSettingsFormFields(QuickForm $form)
+ {
+ return $form;
+ }
+}
diff --git a/library/Director/Hook/JobHook.php b/library/Director/Hook/JobHook.php
new file mode 100644
index 0000000..d9a81a9
--- /dev/null
+++ b/library/Director/Hook/JobHook.php
@@ -0,0 +1,86 @@
+<?php
+
+namespace Icinga\Module\Director\Hook;
+
+use Icinga\Module\Director\Db;
+use Icinga\Module\Director\Objects\DirectorJob;
+use Icinga\Module\Director\Web\Form\QuickForm;
+
+abstract class JobHook
+{
+ /** @var Db */
+ private $db;
+
+ /** @var DirectorJob */
+ private $jobDefinition;
+
+ public static function getDescription(QuickForm $form)
+ {
+ return false;
+ }
+
+ abstract public function run();
+
+ public function isPending()
+ {
+ // TODO: Can be overridden, double-check whether this is necessary
+ }
+
+ public function setDefinition(DirectorJob $definition)
+ {
+ $this->jobDefinition = $definition;
+ return $this;
+ }
+
+ protected function getSetting($key, $default = null)
+ {
+ return $this->jobDefinition->getSetting($key, $default);
+ }
+
+ public function getName()
+ {
+ $parts = explode('\\', get_class($this));
+ $class = preg_replace('/Job$/', '', array_pop($parts));
+
+ if (array_shift($parts) === 'Icinga' && array_shift($parts) === 'Module') {
+ $module = array_shift($parts);
+ if ($module !== 'Director') {
+ return sprintf('%s (%s)', $class, $module);
+ }
+ }
+
+ return $class;
+ }
+
+ public function exportSettings()
+ {
+ return $this->jobDefinition->getSettings();
+ }
+
+ public static function getSuggestedRunInterval(QuickForm $form)
+ {
+ return 900;
+ }
+
+ /**
+ * Override this method if you want to extend the settings form
+ *
+ * @param QuickForm $form QuickForm that should be extended
+ * @return QuickForm
+ */
+ public static function addSettingsFormFields(QuickForm $form)
+ {
+ return $form;
+ }
+
+ public function setDb(Db $db)
+ {
+ $this->db = $db;
+ return $this;
+ }
+
+ protected function db()
+ {
+ return $this->db;
+ }
+}
diff --git a/library/Director/Hook/PropertyModifierHook.php b/library/Director/Hook/PropertyModifierHook.php
new file mode 100644
index 0000000..5d8736d
--- /dev/null
+++ b/library/Director/Hook/PropertyModifierHook.php
@@ -0,0 +1,258 @@
+<?php
+
+namespace Icinga\Module\Director\Hook;
+
+use Icinga\Module\Director\Web\Form\QuickForm;
+use Icinga\Module\Director\Db;
+
+abstract class PropertyModifierHook
+{
+ /** @var array */
+ protected $settings = [];
+
+ /** @var string */
+ private $targetProperty;
+
+ /** @var string */
+ private $propertyName;
+
+ /** @var Db */
+ private $db;
+
+ /** @var bool */
+ private $rejected = false;
+
+ /** @var \stdClass */
+ private $row;
+
+ /**
+ * Methode to transform the given value
+ *
+ * Your custom property modifier needs to implement this method.
+ *
+ * @return mixed $value
+ */
+ abstract public function transform($value);
+
+ public function getName()
+ {
+ $parts = explode('\\', get_class($this));
+ $class = preg_replace('/^PropertyModifier/', '', array_pop($parts)); // right?
+
+ if (array_shift($parts) === 'Icinga' && array_shift($parts) === 'Module') {
+ $module = array_shift($parts);
+ if ($module !== 'Director') {
+ return sprintf('%s (%s)', $class, $module);
+ }
+ }
+
+ return $class;
+ }
+
+ /**
+ * Whether this PropertyModifier wants to deal with array on it's own
+ *
+ * When true, the whole array value will be passed to transform(), otherwise
+ * transform() will be called for every single array member
+ *
+ * @return bool
+ */
+ public function hasArraySupport()
+ {
+ return false;
+ }
+
+ /**
+ * This creates one cloned row for every entry of the result array
+ *
+ * When set to true and given that the property modifier returns an Array,
+ * the current row will be cloned for every entry of that array. The modified
+ * property will then be replace each time accordingly. An empty Array
+ * completely removes the corrent row.
+ *
+ * @return bool
+ */
+ public function expandsRows()
+ {
+ return false;
+ }
+
+ /**
+ * Reject this whole row
+ *
+ * Allows your property modifier to reject specific rows
+ *
+ * @param bool $reject
+ * @return $this
+ */
+ public function rejectRow($reject = true)
+ {
+ $this->rejected = (bool) $reject;
+
+ return $this;
+ }
+
+ /**
+ * Whether this PropertyModifier wants access to the current row
+ *
+ * When true, the your modifier can access the current row via $this->getRow()
+ *
+ * @return bool
+ */
+ public function requiresRow()
+ {
+ return false;
+ }
+
+ /**
+ * Whether this modifier wants to reject the current row
+ *
+ * @return bool
+ */
+ public function rejectsRow()
+ {
+ return $this->rejected;
+ }
+
+ /**
+ * Get the current row
+ *
+ * Will be null when requiresRow was not null. Please do not modify the
+ * row. It might work right now, as we pass in an object reference for
+ * performance reasons. However, modifying row properties is not supported,
+ * and the outcome of such operation might change without pre-announcement
+ * in any future version.
+ *
+ * @return \stdClass|null
+ */
+ public function getRow()
+ {
+ return $this->row;
+ }
+
+ /**
+ * Sets the current row
+ *
+ * Please see requiresRow/getRow for related details. This method is called
+ * by the Import implementation, you should never need to call this on your
+ * own - apart from writing tests of course.
+ *
+ * @param \stdClass $row
+ * @return $this
+ */
+ public function setRow($row)
+ {
+ $this->row = $row;
+ return $this;
+ }
+
+ /**
+ * @return string
+ */
+ public function getPropertyName()
+ {
+ return $this->propertyName;
+ }
+
+ /**
+ * @param string $propertyName
+ * @return $this
+ */
+ public function setPropertyName($propertyName)
+ {
+ $this->propertyName = $propertyName;
+ return $this;
+ }
+
+ /**
+ * The desired target property. Modifiers might want to have their outcome
+ * written to another property of the current row.
+ *
+ * @param $property
+ * @return $this
+ */
+ public function setTargetProperty($property)
+ {
+ $this->targetProperty = $property;
+ return $this;
+ }
+
+ /**
+ * Whether the result of transform() should be written to a new property
+ *
+ * The Import implementation deals with this
+ *
+ * @return bool
+ */
+ public function hasTargetProperty()
+ {
+ return $this->targetProperty !== null;
+ }
+
+ /**
+ * Get the configured target property
+ *
+ * @return string
+ */
+ public function getTargetProperty($default = null)
+ {
+ if ($this->targetProperty === null) {
+ return $default;
+ }
+
+ return $this->targetProperty;
+ }
+
+ public function setDb(Db $db)
+ {
+ $this->db = $db;
+ return $this;
+ }
+
+ public function getDb()
+ {
+ return $this->db;
+ }
+
+ public function setSettings(array $settings)
+ {
+ $this->settings = $settings;
+ return $this;
+ }
+
+ public function getSetting($name, $default = null)
+ {
+ if (array_key_exists($name, $this->settings)) {
+ return $this->settings[$name];
+ } else {
+ return $default;
+ }
+ }
+
+ public function setSetting($name, $value)
+ {
+ $this->settings[$name] = $value;
+ return $this;
+ }
+
+ public function exportSettings()
+ {
+ return (object) $this->settings;
+ }
+
+ public function getSettings()
+ {
+ return $this->settings;
+ }
+
+ /**
+ * Override this method if you want to extend the settings form
+ *
+ * @param QuickForm $form QuickForm that should be extended
+ * @return QuickForm
+ */
+ public static function addSettingsFormFields(QuickForm $form)
+ {
+ return $form;
+ }
+}
diff --git a/library/Director/Hook/ServiceFieldHook.php b/library/Director/Hook/ServiceFieldHook.php
new file mode 100644
index 0000000..bca0836
--- /dev/null
+++ b/library/Director/Hook/ServiceFieldHook.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace Icinga\Module\Director\Hook;
+
+use Icinga\Module\Director\Field\FieldSpec;
+use Icinga\Module\Director\Objects\IcingaService;
+
+abstract class ServiceFieldHook
+{
+ public function wants(IcingaService $service)
+ {
+ return true;
+ }
+
+ /**
+ * @return FieldSpec
+ */
+ abstract public function getFieldSpec(IcingaService $service);
+}
diff --git a/library/Director/Hook/ShipConfigFilesHook.php b/library/Director/Hook/ShipConfigFilesHook.php
new file mode 100644
index 0000000..0026c59
--- /dev/null
+++ b/library/Director/Hook/ShipConfigFilesHook.php
@@ -0,0 +1,11 @@
+<?php
+
+namespace Icinga\Module\Director\Hook;
+
+abstract class ShipConfigFilesHook
+{
+ public function fetchFiles()
+ {
+ return array();
+ }
+}