summaryrefslogtreecommitdiffstats
path: root/library/Director/Job/SyncJob.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:43:12 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:43:12 +0000
commitcd989f9c3aff968e19a3aeabc4eb9085787a6673 (patch)
treefbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/Job/SyncJob.php
parentInitial commit. (diff)
downloadicingaweb2-module-director-cd989f9c3aff968e19a3aeabc4eb9085787a6673.tar.xz
icingaweb2-module-director-cd989f9c3aff968e19a3aeabc4eb9085787a6673.zip
Adding upstream version 1.10.2.upstream/1.10.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/Job/SyncJob.php')
-rw-r--r--library/Director/Job/SyncJob.php128
1 files changed, 128 insertions, 0 deletions
diff --git a/library/Director/Job/SyncJob.php b/library/Director/Job/SyncJob.php
new file mode 100644
index 0000000..0a5aa37
--- /dev/null
+++ b/library/Director/Job/SyncJob.php
@@ -0,0 +1,128 @@
+<?php
+
+namespace Icinga\Module\Director\Job;
+
+use Icinga\Module\Director\Hook\JobHook;
+use Icinga\Module\Director\Web\Form\DirectorObjectForm;
+use Icinga\Module\Director\Web\Form\QuickForm;
+use Icinga\Module\Director\Objects\SyncRule;
+
+class SyncJob extends JobHook
+{
+ protected $rule;
+
+ /**
+ * @throws \Icinga\Exception\NotFoundError
+ * @throws \Icinga\Module\Director\Exception\DuplicateKeyException
+ */
+ public function run()
+ {
+ $db = $this->db();
+ $id = $this->getSetting('rule_id');
+ if ($id === '__ALL__') {
+ foreach (SyncRule::loadAll($db) as $rule) {
+ $this->runForRule($rule);
+ }
+ } else {
+ $this->runForRule(SyncRule::loadWithAutoIncId((int) $id, $db));
+ }
+ }
+
+ /**
+ * @return array
+ * @throws \Icinga\Exception\NotFoundError
+ */
+ public function exportSettings()
+ {
+ $settings = [
+ 'apply_changes' => $this->getSetting('apply_changes') === 'y'
+ ];
+ $id = $this->getSetting('rule_id');
+ if ($id !== '__ALL__') {
+ $settings['rule'] = SyncRule::loadWithAutoIncId((int) $id, $this->db())
+ ->get('rule_name');
+ }
+
+ return $settings;
+ }
+
+ /**
+ * @param SyncRule $rule
+ * @throws \Icinga\Module\Director\Exception\DuplicateKeyException
+ */
+ protected function runForRule(SyncRule $rule)
+ {
+ if ($this->getSetting('apply_changes') === 'y') {
+ $rule->applyChanges();
+ } else {
+ $rule->checkForChanges();
+ }
+ }
+
+ public static function getDescription(QuickForm $form)
+ {
+ return $form->translate(
+ 'The "Sync" job allows to run sync actions at regular intervals'
+ );
+ }
+
+ /**
+ * @param QuickForm $form
+ * @return DirectorObjectForm|QuickForm
+ * @throws \Zend_Form_Exception
+ */
+ public static function addSettingsFormFields(QuickForm $form)
+ {
+ /** @var DirectorObjectForm $form */
+ $rules = self::enumSyncRules($form);
+
+ $form->addElement('select', 'rule_id', array(
+ 'label' => $form->translate('Synchronization rule'),
+ 'description' => $form->translate(
+ 'Please choose your synchronization rule that should be executed.'
+ . ' You could create different schedules for different rules or also'
+ . ' opt for running all of them at once.'
+ ),
+ 'required' => true,
+ 'class' => 'autosubmit',
+ 'multiOptions' => $rules
+ ));
+
+ $form->addElement('select', 'apply_changes', array(
+ 'label' => $form->translate('Apply changes'),
+ 'description' => $form->translate(
+ 'You could immediately apply eventual changes or just learn about them.'
+ . ' In case you do not want them to be applied immediately, defining a'
+ . ' job still makes sense. You will be made aware of available changes'
+ . ' in your Director GUI.'
+ ),
+ 'value' => 'n',
+ 'multiOptions' => array(
+ 'y' => $form->translate('Yes'),
+ 'n' => $form->translate('No'),
+ )
+ ));
+
+ if ((string) $form->getSentOrObjectValue('job_name') !== '') {
+ if (($ruleId = $form->getSentValue('rule_id')) && array_key_exists($ruleId, $rules)) {
+ $name = sprintf('Sync job: %s', $rules[$ruleId]);
+ $form->getElement('job_name')->setValue($name);
+ ///$form->getObject()->set('job_name', $name);
+ }
+ }
+
+ return $form;
+ }
+
+ protected static function enumSyncRules(QuickForm $form)
+ {
+ /** @var DirectorObjectForm $form */
+ $db = $form->getDb();
+ $query = $db->select()->from('sync_rule', array('id', 'rule_name'))->order('rule_name');
+ $res = $db->fetchPairs($query);
+ return array(
+ null => $form->translate('- please choose -'),
+ '__ALL__' => $form->translate('Run all rules at once')
+ ) + $res;
+ }
+}