From 20bb6dfc7f467e6173146457e47957f7808ba55c Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 5 Oct 2021 23:32:01 +0200 Subject: [PATCH] ServiceController: branch-related refactoring --- application/controllers/ServiceController.php | 188 +++++++++++------- 1 file changed, 116 insertions(+), 72 deletions(-) diff --git a/application/controllers/ServiceController.php b/application/controllers/ServiceController.php index 8f570a04..be488410 100644 --- a/application/controllers/ServiceController.php +++ b/application/controllers/ServiceController.php @@ -3,10 +3,11 @@ namespace Icinga\Module\Director\Controllers; use Exception; +use Icinga\Module\Director\Data\Db\DbObjectStore; +use Icinga\Module\Director\Db\Branch\UuidLookup; use Icinga\Module\Director\Forms\IcingaServiceForm; use Icinga\Module\Director\Monitoring; use Icinga\Module\Director\Web\Controller\ObjectController; -use Icinga\Module\Director\Objects\IcingaServiceSet; use Icinga\Module\Director\Objects\IcingaService; use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Web\Form\DirectorObjectForm; @@ -37,58 +38,59 @@ class ServiceController extends ObjectController public function init() { - if ($host = $this->params->get('host')) { - $this->host = IcingaHost::load($host, $this->db()); - } elseif ($hostId = $this->params->get('host_id')) { - $this->host = IcingaHost::loadWithAutoIncId($hostId, $this->db()); - } elseif ($set = $this->params->get('set')) { - $this->set = IcingaServiceSet::load(['object_name' => $set], $this->db()); - } elseif ($apply = $this->params->get('apply')) { - $this->apply = IcingaService::load( - ['object_name' => $apply, 'object_type' => 'template'], - $this->db() - ); - } parent::init(); + $this->loadOptionalHost(); + $this->loadOptionalSet(); + $this->addOptionalHostTabs(); + $this->addOptionalSetTabs(); + } - if ($this->host) { - $hostname = $this->host->getObjectName(); - $tabs = new Tabs(); - $tabs->add('host', [ - 'url' => 'director/host', - 'urlParams' => ['name' => $hostname], - 'label' => $this->translate('Host'), - ])->add('services', [ - 'url' => 'director/host/services', - 'urlParams' => ['name' => $hostname], - 'label' => $this->translate('Services'), - ]); - - $this->addParamToTabs('host', $hostname); - $this->controls()->prependTabs($tabs); - } - - if ($this->object) { - if (! $this->set && $this->object->get('service_set_id')) { - $this->set = $this->object->getRelated('service_set'); + protected function loadOptionalHost() + { + $host = $this->params->get('host', $this->params->get('host_id')); + if ($host === null && $this->object) { + if ($host = $this->object->get('host_id')) { + $host = (int) $host; + } else { + $host = $this->object->get('host'); + var_dump($host); + exit; } } - if ($this->set) { - $setName = $this->set->getObjectName(); - $tabs = new Tabs(); - $tabs->add('set', [ - 'url' => 'director/serviceset', - 'urlParams' => ['name' => $setName], - 'label' => $this->translate('ServiceSet'), - ])->add('services', [ - 'url' => 'director/serviceset/services', - 'urlParams' => ['name' => $setName], - 'label' => $this->translate('Services'), - ]); + if ($host !== null) { + $key = UuidLookup::findUuidForKey($host, 'icinga_host', $this->db(), $this->getBranch()); + $this->host = $this->loadSpecificObject('icinga_host', $key); + } + } - $this->addParamToTabs('serviceset', $setName); - $this->controls()->prependTabs($tabs); + protected function loadOptionalSet() + { + $key = $this->params->get('set', $this->params->get('service_set_id')); + if ($key === null && $this->object && $key = $this->object->get('service_set_id')) { + $key = (int) $key; + } + + if ($key !== null) { + $uuid = UuidLookup::findUuidForKey($key, 'icinga_service_set', $this->db(), $this->getBranch()); + $this->set = $this->loadSpecificObject('icinga_service_set', $uuid); + } + } + + /** + * TODO: Is this still a thing? It's unused. + * + * @throws \Icinga\Exception\NotFoundError + */ + protected function loadOptionalApplyRule() + { + if ($apply = $this->params->get('apply')) { + // Hint: doesn't work, DbObjectStore::load does no longer exist + $store = new DbObjectStore($this->db(), $this->getBranch()); + $this->apply = $store->load('service', [ + 'object_name' => $apply, + 'object_type' => 'template' + ]); } } @@ -106,6 +108,7 @@ class ServiceController extends ObjectController { parent::addAction(); if ($this->host) { + // TODO: use setTitle. And figure out, where we use this old route. $this->view->title = $this->host->object_name . ': ' . $this->view->title; } elseif ($this->set) { $this->view->title = sprintf( @@ -220,33 +223,74 @@ class ServiceController extends ObjectController $this->content()->add($table); } - protected function loadObject() + protected function getLegacyKey() { - if ($this->object === null) { - if ($id = $this->params->get('id')) { - $this->object = IcingaService::loadWithAutoIncId( - (int) $id, - $this->db() - ); - } elseif ($name = $this->params->get('name')) { - $params = array('object_name' => $name); - $db = $this->db(); - - if ($this->host) { - // $this->view->host = $this->host; - $params['host_id'] = $this->host->id; - } - - if ($this->set) { - // $this->view->set = $this->set; - $params['service_set_id'] = $this->set->id; - } - $this->object = IcingaService::load($params, $db); - } else { - parent::loadObject(); - } + if ($key = $this->params->get('id')) { + $key = (int) $key; + } else { + $key = $this->params->get('name'); } - return $this->object; + if ($key === null) { + throw new \InvalidArgumentException('uuid, name or id required'); + } + + return $key; + } + + protected function loadObject() + { + if ($this->params->has('uuid')) { + parent::loadObject(); + return; + } + + $key = $this->getLegacyKey(); + $uuid = UuidLookup::findServiceUuid($this->db(), $this->getBranch(), 'object', $key, $this->host, $this->set); + $this->params->set('uuid', $uuid->toString()); + parent::loadObject(); + } + + protected function addOptionalHostTabs() + { + if ($this->host === null) { + return; + } + $hostname = $this->host->getObjectName(); + $tabs = new Tabs(); + $urlParams = ['uuid' => $this->host->getUniqueId()->toString()]; + $tabs->add('host', [ + 'url' => 'director/host', + 'urlParams' => $urlParams, + 'label' => $this->translate('Host'), + ])->add('services', [ + 'url' => 'director/host/services', + 'urlParams' => $urlParams, + 'label' => $this->translate('Services'), + ]); + + $this->addParamToTabs('host', $hostname); + $this->controls()->prependTabs($tabs); + } + + protected function addOptionalSetTabs() + { + if ($this->set === null) { + return; + } + $setName = $this->set->getObjectName(); + $tabs = new Tabs(); + $tabs->add('set', [ + 'url' => 'director/serviceset', + 'urlParams' => ['name' => $setName], + 'label' => $this->translate('ServiceSet'), + ])->add('services', [ + 'url' => 'director/serviceset/services', + 'urlParams' => ['name' => $setName], + 'label' => $this->translate('Services'), + ]); + + $this->addParamToTabs('serviceset', $setName); + $this->controls()->prependTabs($tabs); } }