Skip to content

Commit

Permalink
Make TemplateUsageTables and other tables compatible with `director…
Browse files Browse the repository at this point in the history
… branches`

`TemplateUsageTable`, `ApplyRulesTable`, `ObjectsTableSetmembers` and `TemplatesTable` are
made compatible with `director branches`.
  • Loading branch information
raviks789 authored and Thomas-Gelf committed Sep 21, 2023
1 parent 3c7082c commit dcd3687
Show file tree
Hide file tree
Showing 13 changed files with 318 additions and 109 deletions.
2 changes: 1 addition & 1 deletion library/Director/Web/Controller/ObjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ protected function getTable()
*/
protected function getApplyRulesTable()
{
$table = new ApplyRulesTable($this->db());
$table = (new ApplyRulesTable($this->db()))->setBranch($this->getBranch());
$table->setType($this->getType())
->setBaseObjectUrl($this->getBaseObjectUrl());
$this->eventuallyFilterCommand($table);
Expand Down
4 changes: 3 additions & 1 deletion library/Director/Web/Controller/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function setmembersAction()

ObjectsTableSetMembers::create($this->getType(), $this->db(), $this->Auth())
->setBaseObjectUrl($this->getBaseObjectUrl())
->setBranch($this->getBranch())
->filterTemplate($template, $this->getInheritance())
->renderTo($this);
}
Expand All @@ -84,6 +85,7 @@ public function applyrulesAction()

ApplyRulesTable::create($type, $this->db())
->setBaseObjectUrl($this->getBaseObjectUrl())
->setBranch($this->getBranch())
->filterTemplate($template, $this->params->get('inheritance', 'direct'))
->renderTo($this);
}
Expand Down Expand Up @@ -212,7 +214,7 @@ public function usageAction()

try {
$this->content()->add(
TemplateUsageTable::forTemplate($template)
TemplateUsageTable::forTemplate($template, $this->getBranch())
);
} catch (NestingError $e) {
$this->content()->add(Hint::error($e->getMessage()));
Expand Down
105 changes: 99 additions & 6 deletions library/Director/Web/Table/ApplyRulesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Icinga\Data\Filter\Filter;
use Icinga\Exception\IcingaException;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Db\DbSelectParenthesis;
use Icinga\Module\Director\Db\DbUtil;
use Icinga\Module\Director\Db\IcingaObjectFilterHelper;
use Icinga\Module\Director\IcingaConfig\AssignRenderer;
Expand All @@ -20,6 +21,8 @@

class ApplyRulesTable extends ZfQueryBasedTable
{
use TableWithBranchSupport;

protected $searchColumns = [
'o.object_name',
'o.assign_filter',
Expand Down Expand Up @@ -94,10 +97,14 @@ public function renderRow($row)
// NOT (YET) static::td($this->createActionLinks($row))->setSeparator(' ')
]);

$classes = $this->getRowClasses($row);

if ($row->disabled === 'y') {
$tr->getAttributes()->add('class', 'disabled');
$classes[] = 'disabled';
}

$tr->getAttributes()->add('class', $classes);

return $tr;
}

Expand All @@ -117,7 +124,8 @@ public function filterTemplate(
$this->getQuery(),
$template,
'o',
$inheritance
$inheritance,
$this->branchUuid
);

return $this;
Expand Down Expand Up @@ -196,6 +204,15 @@ protected function applyRestrictions(ZfSelect $query)
return FilterRenderer::applyToQuery($filter, $query);
}

protected function getRowClasses($row)
{
// TODO: remove isset, to figure out where it is missing
if (isset($row->branch_uuid) && $row->branch_uuid !== null) {
return ['branch_modified'];
}
return [];
}


/**
* @return IcingaObject
Expand All @@ -216,6 +233,7 @@ public function prepareQuery()
'id' => 'o.id',
'uuid' => 'o.uuid',
'object_name' => 'o.object_name',
'object_type' => 'o.object_type',
'disabled' => 'o.disabled',
'assign_filter' => 'o.assign_filter',
'apply_for' => '(NULL)',
Expand All @@ -224,17 +242,92 @@ public function prepareQuery()
if ($table === 'icinga_service') {
$columns['apply_for'] = 'o.apply_for';
}

$conn = $this->connection();
$query = $this->db()->select()->from(
['o' => $table],
$columns
)->where(
"object_type = 'apply'"
)->order('o.object_name');

if ($this->type === 'service') {
$query->where('service_set_id IS NULL');
if ($this->branchUuid) {
$columns = $this->branchifyColumns($columns);
$columns['branch_uuid'] = 'bo.branch_uuid';
if ($conn->isPgsql()) {
$columns['imports'] = 'CONCAT(\'[\', ARRAY_TO_STRING(ARRAY_AGG'
. '(CONCAT(\'"\', sub_o.object_name, \'"\')), \',\'), \']\')';
} else {
$columns['imports'] = 'CONCAT(\'[\', '
. 'GROUP_CONCAT(CONCAT(\'"\', sub_o.object_name, \'"\')), \']\')';
}

$this->stripSearchColumnAliases();

$query->reset('columns');
$right = clone($query);

$query->columns($columns)
->joinLeft(
['oi' => $table . '_inheritance'],
'o.id = oi.' . $this->getType() . '_id',
[]
)->joinLeft(
['sub_o' => $table],
'sub_o.id = oi.parent_' . $this->getType() . '_id',
[]
)->group(['o.id', 'bo.uuid', 'bo.branch_uuid']);

$query->joinLeft(
['bo' => "branched_$table"],
// TODO: PgHexFunc
$this->db()->quoteInto(
'bo.uuid = o.uuid AND bo.branch_uuid = ?',
DbUtil::quoteBinaryLegacy($this->branchUuid->getBytes(), $this->db())
),
[]
)->where("(bo.branch_deleted IS NULL OR bo.branch_deleted = 'n')");

if ($this->type === 'service') {
$query->where('o.service_set_id IS NULL AND bo.service_set IS NULL');
}

$columns['imports'] = 'bo.imports';

$right->columns($columns)
->joinRight(
['bo' => "branched_$table"],
'bo.uuid = o.uuid',
[]
)
->where('o.uuid IS NULL')
->where('bo.branch_uuid = ?', $conn->quoteBinary($this->branchUuid->getBytes()));

$query = $this->db()->select()->union([
'l' => new DbSelectParenthesis($query),
'r' => new DbSelectParenthesis($right),
]);

$query = $this->db()->select()->from(['u' => $query]);
$query->order('object_name')->limit(100);
} else {
if ($this->type === 'service') {
$query->where('service_set_id IS NULL');
}
}

$query->where(
"object_type = 'apply'"
);

$this->applyRestrictions($query);

return $this->applyRestrictions($query);
}

/**
* @return Db
*/
public function connection()
{
return parent::connection();
}
}
12 changes: 9 additions & 3 deletions library/Director/Web/Table/DependencyTemplateUsageTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Icinga\Module\Director\Web\Table;

use Icinga\Module\Director\Db;

class DependencyTemplateUsageTable extends TemplateUsageTable
{
public function getTypes()
Expand All @@ -12,11 +14,15 @@ public function getTypes()
];
}

protected function getTypeSummaryDefinitions()
protected function getSummaryTables(string $templateType, Db $connection)
{
return [
'templates' => $this->getSummaryLine('template'),
'applyrules' => $this->getSummaryLine('apply'),
'templates' => TemplatesTable::create(
$templateType,
$connection
),
'applyrules' => ApplyRulesTable::create($templateType, $connection)
->setBranchUuid($this->branchUuid)
];
}
}
8 changes: 0 additions & 8 deletions library/Director/Web/Table/HostTemplateUsageTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,4 @@ public function getTypes()
'objects' => $this->translate('Objects'),
];
}

protected function getTypeSummaryDefinitions()
{
return [
'templates' => $this->getSummaryLine('template'),
'objects' => $this->getSummaryLine('object'),
];
}
}
4 changes: 4 additions & 0 deletions library/Director/Web/Table/IcingaServiceSetServiceTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ protected function getServiceLink($row)
];
$url = 'director/host/servicesetservice';
} else {
if (is_resource($row->uuid)) {
$row->uuid =stream_get_contents($row->uuid);
}

$params = [
'uuid' => Uuid::fromBytes($row->uuid)->toString(),
];
Expand Down
12 changes: 9 additions & 3 deletions library/Director/Web/Table/NotificationTemplateUsageTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Icinga\Module\Director\Web\Table;

use Icinga\Module\Director\Db;

class NotificationTemplateUsageTable extends TemplateUsageTable
{
public function getTypes()
Expand All @@ -12,11 +14,15 @@ public function getTypes()
];
}

protected function getTypeSummaryDefinitions()
protected function getSummaryTables(string $templateType, Db $connection)
{
return [
'templates' => $this->getSummaryLine('template'),
'applyrules' => $this->getSummaryLine('apply', 'o.host_id IS NULL'),
'templates' => TemplatesTable::create(
$templateType,
$connection
),
'applyrules' => ApplyRulesTable::create($templateType, $connection)
->setBranchUuid($this->branchUuid)
];
}
}
7 changes: 4 additions & 3 deletions library/Director/Web/Table/ObjectSetTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ protected function prepareQuery()
['bo' => "branched_icinga_{$type}"],
"bo.{$type}_set = bos.object_name",
[]
);
)->group(['bo.object_name', 'o.object_name']);
$query->joinLeft(
['bo' => "branched_icinga_{$type}"],
"bo.{$type}_set = bos.object_name",
[]
);
)->group(['bo.object_name', 'o.object_name']);
$this->queries = [
$query,
$right
Expand All @@ -185,8 +185,9 @@ protected function prepareQuery()
->group('object_type')
->group('assign_filter')
->group('description')
->group('service_object_name')
->group('count_services');
};
}
} else {
// Disabled for now, check for correctness:
// $query->joinLeft(
Expand Down
56 changes: 27 additions & 29 deletions library/Director/Web/Table/ObjectsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,37 +289,35 @@ protected function prepareQuery()
);

// keep the imported templates as columns
if ($this->getType() === 'host' || $this->getType() === 'service') {
$leftColumns = $columns;
$rightColumns = $columns;

if ($this->db() instanceof Zend_Db_Adapter_Pdo_Pgsql) {
$leftColumns['imports'] = 'CONCAT(\'[\', ARRAY_TO_STRING(ARRAY_AGG'
. '(CONCAT(\'"\', sub_o.object_name, \'"\')), \',\'), \']\')';
} else {
$leftColumns['imports'] = 'CONCAT(\'[\', '
. 'GROUP_CONCAT(CONCAT(\'"\', sub_o.object_name, \'"\')), \']\')';
}

$query->reset('columns');

$query->columns($leftColumns)
->joinLeft(
['oi' => $table . '_inheritance'],
'o.id = oi.' . $this->getType() . '_id',
[]
)->joinLeft(
['sub_o' => $table],
'sub_o.id = oi.parent_' . $this->getType() . '_id',
[]
)->group(['o.id', 'bo.uuid', 'bo.branch_uuid']);

$rightColumns['imports'] = 'bo.imports';

$right->reset('columns');
$right->columns($rightColumns);
$leftColumns = $columns;
$rightColumns = $columns;

if ($this->db() instanceof Zend_Db_Adapter_Pdo_Pgsql) {
$leftColumns['imports'] = 'CONCAT(\'[\', ARRAY_TO_STRING(ARRAY_AGG'
. '(CONCAT(\'"\', sub_o.object_name, \'"\')), \',\'), \']\')';
} else {
$leftColumns['imports'] = 'CONCAT(\'[\', '
. 'GROUP_CONCAT(CONCAT(\'"\', sub_o.object_name, \'"\')), \']\')';
}

$query->reset('columns');

$query->columns($leftColumns)
->joinLeft(
['oi' => $table . '_inheritance'],
'o.id = oi.' . $this->getType() . '_id',
[]
)->joinLeft(
['sub_o' => $table],
'sub_o.id = oi.parent_' . $this->getType() . '_id',
[]
)->group(['o.id', 'bo.uuid', 'bo.branch_uuid']);

$rightColumns['imports'] = 'bo.imports';

$right->reset('columns');
$right->columns($rightColumns);

$query->where("(bo.branch_deleted IS NULL OR bo.branch_deleted = 'n')");
$this->applyObjectTypeFilter($query, $right);
$right->joinRight(
Expand Down
6 changes: 6 additions & 0 deletions library/Director/Web/Table/ObjectsTableService.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,14 @@ public function prepareQuery()
'hsb.service_id = o.id AND hsb.host_id = o.host_id',
[]
)->where('o.service_set_id IS NULL')
->group(['o.id', 'h.id','hsb.service_id', 'hsb.host_id'])
->order('o.object_name')->order('h.object_name');

if ($this->branchUuid) {
$subQuery->where('bo.service_set IS NULL')
->group(['bo.uuid', 'bo.branch_uuid']);
}

if ($this->host) {
if ($this->branchUuid) {
$subQuery->where('COALESCE(h.object_name, bo.host) = ?', $this->host->getObjectName());
Expand Down
Loading

0 comments on commit dcd3687

Please sign in to comment.