ObjectsTableService: replace HostServiceTable

This removes duplicate logic and shows services created in config
branches
This commit is contained in:
Thomas Gelf 2021-12-13 14:11:35 +01:00
parent b70b19ad32
commit 27abbb59a0
3 changed files with 137 additions and 228 deletions

View File

@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Controllers;
use gipfl\Web\Widget\Hint;
use Icinga\Module\Director\Monitoring;
use Icinga\Module\Director\Web\Table\ObjectsTableService;
use ipl\Html\Html;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Url;
@ -24,7 +25,6 @@ use Icinga\Module\Director\Web\Controller\ObjectController;
use Icinga\Module\Director\Web\SelfService;
use Icinga\Module\Director\Web\Table\IcingaHostAppliedForServiceTable;
use Icinga\Module\Director\Web\Table\IcingaHostAppliedServicesTable;
use Icinga\Module\Director\Web\Table\IcingaHostServiceTable;
use Icinga\Module\Director\Web\Table\IcingaServiceSetServiceTable;
class HostController extends ObjectController
@ -210,7 +210,7 @@ class HostController extends ObjectController
return;
}
$content = $this->content();
$table = IcingaHostServiceTable::load($host)
$table = (new ObjectsTableService($this->db()))->setAuth($this->Auth())->setHost($host)
->setTitle($this->translate('Individual Service objects'));
if ($branch->isBranch()) {
$table->setBranchUuid($branch->getUuid());
@ -224,7 +224,10 @@ class HostController extends ObjectController
$parents = IcingaTemplateRepository::instanceByObject($this->object)
->getTemplatesFor($this->object, true);
foreach ($parents as $parent) {
$table = IcingaHostServiceTable::load($parent)->setInheritedBy($host);
$table = (new ObjectsTableService($this->db()))
->setAuth($this->Auth())
->setHost($parent)
->setInheritedBy($host);
if (count($table)) {
$content->add(
$table->setTitle(sprintf(
@ -279,7 +282,10 @@ class HostController extends ObjectController
$this->addSingleTab($this->translate('Configuration (read-only)'));
$this->addTitle($this->translate('Services on %s'), $host->getObjectName());
$content = $this->content();
$table = IcingaHostServiceTable::load($host)
$table = (new ObjectsTableService($db))
->setAuth($this->Auth())
->setHost($host)
->setReadonly()
->highlightService($service)
->setTitle($this->translate('Individual Service objects'));
@ -292,8 +298,9 @@ class HostController extends ObjectController
$parents = IcingaTemplateRepository::instanceByObject($this->object)
->getTemplatesFor($this->object, true);
foreach ($parents as $parent) {
$table = IcingaHostServiceTable::load($parent)
$table = (new ObjectsTableService($db))
->setReadonly()
->setHost($parent)
->highlightService($service)
->setInheritedBy($host);
if (count($table)) {

View File

@ -1,192 +0,0 @@
<?php
namespace Icinga\Module\Director\Web\Table;
use ipl\Html\Html;
use Icinga\Module\Director\Objects\IcingaHost;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use Ramsey\Uuid\UuidInterface;
class IcingaHostServiceTable extends ZfQueryBasedTable
{
protected $title;
/** @var IcingaHost */
protected $host;
/** @var IcingaHost */
protected $inheritedBy;
/** @var bool */
protected $readonly = false;
/** @var string|null */
protected $highlightedService;
/** @var ?UuidInterface */
protected $branchUuid;
protected $searchColumns = [
'service',
];
/**
* @param IcingaHost $host
* @return static
*/
public static function load(IcingaHost $host)
{
$table = new static($host->getConnection());
$table->setHost($host);
$table->getAttributes()->set('data-base-target', '_self');
return $table;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function setBranchUuid(UuidInterface $uuid)
{
$this->branchUuid = $uuid;
return $this;
}
public function setHost(IcingaHost $host)
{
$this->host = $host;
return $this;
}
public function setInheritedBy(IcingaHost $host)
{
$this->inheritedBy = $host;
return $this;
}
/**
* Show no related links
*
* @param bool $readonly
* @return $this
*/
public function setReadonly($readonly = true)
{
$this->readonly = (bool) $readonly;
return $this;
}
public function highlightService($service)
{
$this->highlightedService = $service;
return $this;
}
public function renderRow($row)
{
$classes = [];
if ($row->blacklisted === 'y') {
$classes[] = 'strike-links';
}
if ($row->disabled === 'y') {
$classes[] = 'disabled';
}
$attributes = empty($classes) ? null : ['class' => $classes];
return $this::row([
$this->getServiceLink($row)
], $attributes);
}
protected function getServiceLink($row)
{
if ($this->readonly) {
if ($this->highlightedService === $row->service) {
return Html::tag('span', ['class' => 'icon-right-big'], $row->service);
} else {
return $row->service;
}
}
if ($target = $this->inheritedBy) {
$params = array(
'name' => $target->object_name,
'service' => $row->service,
'inheritedFrom' => $row->host,
);
return Link::create(
$row->service,
'director/host/inheritedservice',
$params
);
}
if ($row->object_type === 'apply') {
$params['id'] = $row->id;
} else {
$params = array('name' => $row->service);
if ($row->host !== null) {
$params['host'] = $row->host;
}
}
return Link::create(
$row->service,
'director/service/edit',
$params
);
}
public function getColumnsToBeRendered()
{
return [
$this->title ?: $this->translate('Servicename'),
];
}
/**
* @return \Zend_Db_Select
*/
public function prepareQuery()
{
$db = $this->db();
$query = $db->select()->from(
['s' => 'icinga_service'],
[
'id' => 's.id',
'host_id' => 's.host_id',
'host' => 'h.object_name',
'service' => 's.object_name',
'disabled' => 's.disabled',
'object_type' => 's.object_type',
'blacklisted' => "CASE WHEN hsb.service_id IS NULL THEN 'n' ELSE 'y' END"
]
)->joinLeft(
['h' => 'icinga_host'],
'h.id = s.host_id',
[]
)->joinLeft(
['hsb' => 'icinga_host_service_blacklist'],
$db->quoteInto(
's.id = hsb.service_id AND hsb.host_id = ?',
$this->inheritedBy === null
? $this->host->get('id')
: $this->inheritedBy->get('id')
),
[]
)->where(
's.host_id = ?',
$this->host->get('id')
)->order('s.object_name');
return $query;
}
}

View File

@ -3,26 +3,41 @@
namespace Icinga\Module\Director\Web\Table;
use Icinga\Module\Director\Db\DbUtil;
use Icinga\Module\Director\Objects\IcingaHost;
use ipl\Html\Html;
use gipfl\IcingaWeb2\Table\Extension\MultiSelect;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Url;
use Ramsey\Uuid\Uuid;
class ObjectsTableService extends ObjectsTable
{
use MultiSelect;
/** @var IcingaHost */
protected $host;
protected $type = 'service';
protected $title;
/** @var IcingaHost */
protected $inheritedBy;
/** @var bool */
protected $readonly = false;
/** @var string|null */
protected $highlightedService;
protected $columns = [
'object_name' => 'o.object_name',
'disabled' => 'o.disabled',
'host' => 'h.object_name',
'host_id' => 'h.id',
'host_object_type' => 'h.object_type',
'host_disabled' => 'h.disabled',
'id' => 'o.id',
'uuid' => 'o.uuid',
'uuid' => 'o.uuid',
'blacklisted' => "CASE WHEN hsb.service_id IS NULL THEN 'n' ELSE 'y' END",
];
@ -40,50 +55,79 @@ class ObjectsTableService extends ObjectsTable
);
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function setHost(IcingaHost $host)
{
$this->host = $host;
$this->getAttributes()->set('data-base-target', '_self');
return $this;
}
public function setInheritedBy(IcingaHost $host)
{
$this->inheritedBy = $host;
return $this;
}
/**
* Show no related links
*
* @param bool $readonly
* @return $this
*/
public function setReadonly($readonly = true)
{
$this->readonly = (bool) $readonly;
return $this;
}
public function highlightService($service)
{
$this->highlightedService = $service;
return $this;
}
public function getColumnsToBeRendered()
{
if ($this->title) {
return [$this->title];
}
if ($this->host) {
return [$this->translate('Servicename')];
}
return [
'host' => 'Host',
'object_name' => 'Service Name'
'host' => $this->translate('Host'),
'object_name' => $this->translate('Service Name'),
];
}
public function renderRow($row)
{
$params = [
'uuid' => Uuid::fromBytes(DbUtil::binaryResult($row->uuid))->toString(),
];
if ($row->host !== null) {
$params['host'] = $row->host;
}
$url = Url::fromPath('director/service/edit', $params);
/*
if ($this->branchUuid) {
$url = Url::fromPath('director/service/edit', [
'uuid' => Uuid::fromBytes(DbUtil::binaryResult($row->uuid))->toString(),
'host' => $row->host,
]);
} else {
$url = Url::fromPath('director/service/edit', [
'name' => $row->object_name,
'host' => $row->host,
'id' => $row->id,
]);
}
*/
$caption = $row->host === null
? Html::tag('span', ['class' => 'error'], '- none -')
: $row->host;
$hostField = static::td(Link::create($caption, $url));
$hostField = static::td($caption);
if ($row->host === null) {
$hostField->getAttributes()->add('class', 'error');
}
$tr = static::tr([
$hostField,
static::td($row->object_name)
]);
if ($this->host) {
$tr = static::tr([
static::td($this->getServiceLink($row))
]);
} else {
$tr = static::tr([
$hostField,
static::td($this->getServiceLink($row))
]);
}
$attributes = $tr->getAttributes();
$classes = $this->getRowClasses($row);
@ -98,6 +142,48 @@ class ObjectsTableService extends ObjectsTable
return $tr;
}
protected function getInheritedServiceLink($row, $target)
{
$params = [
'name' => $target->object_name,
'service' => $row->object_name,
'inheritedFrom' => $row->host,
];
return Link::create(
$row->object_name,
'director/host/inheritedservice',
$params
);
}
protected function getServiceLink($row)
{
if ($this->readonly) {
if ($this->highlightedService === $row->object_name) {
return Html::tag('span', ['class' => 'icon-right-big'], $row->object_name);
} else {
return $row->object_name;
}
}
$params = [
'uuid' => Uuid::fromBytes(DbUtil::binaryResult($row->uuid))->toString(),
];
if ($row->host !== null) {
$params['host'] = $row->host;
}
if ($target = $this->inheritedBy) {
return $this->getInheritedServiceLink($row, $target);
}
return Link::create(
$row->object_name,
'director/service/edit',
$params
);
}
public function prepareQuery()
{
$query = parent::prepareQuery();
@ -118,6 +204,14 @@ class ObjectsTableService extends ObjectsTable
[]
)->where('o.service_set_id IS NULL')
->order('o.object_name')->order('h.object_name');
if ($this->host) {
if ($this->branchUuid) {
$subQuery->where('COALESCE(h.object_name, bo.host) = ?', $this->host->getObjectName());
} else {
$subQuery->where('h.id = ?', $this->host->get('id'));
}
}
}
return $query;