Skip to content

Commit

Permalink
Add setmembers summyary line to TemplateUsageTable
Browse files Browse the repository at this point in the history
`'setmembers' => $this->getSummaryLine('object', 'o.service_set_id IS NOT NULL'),` is added to TemplateUsageTable
and `objects` is set to ` $this->getSummaryLine('object', 'o.service_set_id IS NULL')`. Subsequently, ObjectTableSetMembers
is added which is used in `setmembersAction` in `TemplateController`. `ObjectTableSetMembers` uses service set name or service name
as search columns.
  • Loading branch information
raviks789 committed Sep 30, 2021
1 parent 84682a8 commit 1e86760
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 14 deletions.
20 changes: 20 additions & 0 deletions library/Director/Web/Controller/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
19 changes: 8 additions & 11 deletions library/Director/Web/Table/ObjectsTableService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -43,7 +43,7 @@ public function assemble()
public function getColumnsToBeRendered()
{
return [
'host_set' => 'Host | Service Set',
'host' => 'Host',
'object_name' => 'Service Name'
];
}
Expand All @@ -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([
Expand Down Expand Up @@ -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');
}
}
152 changes: 152 additions & 0 deletions library/Director/Web/Table/ObjectsTableSetMembers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

namespace Icinga\Module\Director\Web\Table;

use Icinga\Authentication\Auth;
use Icinga\Module\Director\Db;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use gipfl\IcingaWeb2\Url;
use Icinga\Module\Director\Db\IcingaObjectFilterHelper;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Restriction\FilterByNameRestriction;

class ObjectsTableSetMembers extends ZfQueryBasedTable
{
protected $searchColumns = [
'os.object_name',
'o.object_name',
];

private $type;

/** @var IcingaObject */
protected $dummyObject;

protected $baseObjectUrl;

/** @var Auth */
private $auth;

public static function create($type, Db $db, Auth $auth)
{
$table = new static($db);
$table->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();
}
}
6 changes: 3 additions & 3 deletions library/Director/Web/Table/ServiceTemplateUsageTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ 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'),
];
}

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'),
];
}
}

0 comments on commit 1e86760

Please sign in to comment.