diff --git a/library/Director/Web/Controller/TemplateController.php b/library/Director/Web/Controller/TemplateController.php index 8bb53d8bd..f7e62c32a 100644 --- a/library/Director/Web/Controller/TemplateController.php +++ b/library/Director/Web/Controller/TemplateController.php @@ -10,6 +10,7 @@ use Icinga\Module\Director\Web\Controller\Extension\DirectorDb; use Icinga\Module\Director\Web\Table\ApplyRulesTable; use Icinga\Module\Director\Web\Table\ObjectsTable; +use Icinga\Module\Director\Web\Table\ObjectsTableSetMembers; use Icinga\Module\Director\Web\Table\TemplatesTable; use Icinga\Module\Director\Web\Table\TemplateUsageTable; use Icinga\Module\Director\Web\Tabs\ObjectTabs; @@ -46,6 +47,25 @@ public function objectsAction() ->renderTo($this); } + public function setmembersAction() + { + $template = $this->requireTemplate(); + $plural = $this->getTranslatedPluralType(); + $this + ->addSingleTab($plural) + ->setAutorefreshInterval(10) + ->addTitle( + $this->translate('%s based on %s'), + $plural, + $template->getObjectName() + )->addBackToUsageLink($template); + + ObjectsTableSetMembers::create($this->getType(), $this->db(), $this->Auth()) + ->setBaseObjectUrl($this->getBaseObjectUrl()) + ->filterTemplate($template, $this->getInheritance()) + ->renderTo($this); + } + public function applyrulesAction() { $type = $this->getType(); diff --git a/library/Director/Web/Table/ObjectsTableService.php b/library/Director/Web/Table/ObjectsTableService.php index 6d46b07ac..360d9838f 100644 --- a/library/Director/Web/Table/ObjectsTableService.php +++ b/library/Director/Web/Table/ObjectsTableService.php @@ -23,7 +23,7 @@ public function getColumns() return [ 'object_name' => 'o.object_name', 'disabled' => 'o.disabled', - 'host_set' => 'CASE WHEN o.service_set_id IS NULL THEN h.object_name ELSE os.object_name END', + 'host' => 'h.object_name', 'host_object_type' => 'h.object_type', 'host_disabled' => 'h.disabled', 'id' => 'o.id', @@ -43,7 +43,7 @@ public function assemble() public function getColumnsToBeRendered() { return [ - 'host_set' => 'Host | Service Set', + 'host' => 'Host', 'object_name' => 'Service Name' ]; } @@ -52,16 +52,16 @@ public function renderRow($row) { $url = Url::fromPath('director/service/edit', [ 'name' => $row->object_name, - 'host_set' => $row->host_set, + 'host' => $row->host, 'id' => $row->id, ]); - $caption = $row->host_set === null + $caption = $row->host === null ? Html::tag('span', ['class' => 'error'], '- none -') - : $row->host_set; + : $row->host; $hostField = static::td(Link::create($caption, $url)); - if ($row->host_set === null) { + if ($row->host === null) { $hostField->getAttributes()->add('class', 'error'); } $tr = static::tr([ @@ -90,10 +90,7 @@ public function prepareQuery() ['hsb' => 'icinga_host_service_blacklist'], 'hsb.service_id = o.id AND hsb.host_id = o.host_id', [] - )->joinLeft( - ['os' => 'icinga_service_set'], - 'os.id = o.service_set_id', - [] - )->order('o.object_name')->order('h.object_name'); + )->where('o.service_set_id IS NULL') + ->order('o.object_name')->order('h.object_name'); } } diff --git a/library/Director/Web/Table/ObjectsTableSetMembers.php b/library/Director/Web/Table/ObjectsTableSetMembers.php new file mode 100644 index 000000000..30526c1d5 --- /dev/null +++ b/library/Director/Web/Table/ObjectsTableSetMembers.php @@ -0,0 +1,152 @@ +type = $type; + $table->auth = $auth; + return $table; + } + + public function getType() + { + return $this->type; + } + + public function getColumnsToBeRendered() + { + return [ + 'os.object_name' => 'Service Set', + 'o.object_name' => 'Service Name' + ]; + } + + public function setBaseObjectUrl($url) + { + $this->baseObjectUrl = $url; + + return $this; + } + + /** + * Should be triggered from renderRow, still unused. + * + * @param IcingaObject $template + * @param string $inheritance + * @return $this + * @throws \Icinga\Exception\ProgrammingError + */ + public function filterTemplate( + IcingaObject $template, + $inheritance = IcingaObjectFilterHelper::INHERIT_DIRECT + ) { + IcingaObjectFilterHelper::filterByTemplate( + $this->getQuery(), + $template, + 'o', + $inheritance + ); + + return $this; + } + + + public function renderRow($row) + { + $url = Url::fromPath('director/service/edit', [ + 'name' => $row->object_name, + 'id' => $row->id, + ]); + + return static::tr([ + static::td([ + Link::create($row->service_set, $url), + ]), + static::td($row->object_name), + ]); + } + + /** + * @return IcingaObject + */ + protected function getDummyObject() + { + if ($this->dummyObject === null) { + $type = $this->type; + $this->dummyObject = IcingaObject::createByType($type); + } + return $this->dummyObject; + } + + protected function prepareQuery() + { + $table = $this->getDummyObject()->getTableName(); + $type = $this->getType(); + + $columns = [ + 'id' => 'o.id', + 'service_set' => 'os.object_name', + 'object_name' => 'o.object_name', + 'object_type' => 'os.object_type', + 'assign_filter' => 'os.assign_filter', + 'description' => 'os.description', + ]; + + $query = $this->db()->select()->from( + ['o' => $table], + $columns + )->joinLeft( + ['os' => "icinga_${type}_set"], + "o.${type}_set_id = os.id", + [] + ); + $nameFilter = new FilterByNameRestriction( + $this->connection(), + $this->auth, + "${type}_set" + ); + $nameFilter->applyToQuery($query, 'os'); + + $query + ->where('o.object_type = ?', 'object') + ->order('os.object_name'); + + return $query; + } + + /** + * @return Db + */ + public function connection() + { + return parent::connection(); + } +} diff --git a/library/Director/Web/Table/ServiceTemplateUsageTable.php b/library/Director/Web/Table/ServiceTemplateUsageTable.php index 82f9643e7..daed10c61 100644 --- a/library/Director/Web/Table/ServiceTemplateUsageTable.php +++ b/library/Director/Web/Table/ServiceTemplateUsageTable.php @@ -10,7 +10,7 @@ public function getTypes() 'templates' => $this->translate('Templates'), 'objects' => $this->translate('Objects'), 'applyrules' => $this->translate('Apply Rules'), - // 'setmembers' => $this->translate('Set Members'), + 'setmembers' => $this->translate('Set Members'), ]; } @@ -18,10 +18,10 @@ protected function getTypeSummaryDefinitions() { return [ 'templates' => $this->getSummaryLine('template'), - 'objects' => $this->getSummaryLine('object'), + 'objects' => $this->getSummaryLine('object', 'o.service_set_id IS NULL'), 'applyrules' => $this->getSummaryLine('apply', 'o.service_set_id IS NULL'), // TODO: re-enable - // 'setmembers' => $this->getSummaryLine('apply', 'o.service_set_id IS NOT NULL'), + 'setmembers' => $this->getSummaryLine('object', 'o.service_set_id IS NOT NULL'), ]; } }