Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hosts deployed from director as url parameters in CubeLinks #2438

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions library/Director/ProvidedHook/CubeLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function prepareActionLinks(Cube $cube, View $view)

$title = $view->translate('Modify a host');
$description = sprintf(
$view->translate('This allows you to modify properties for "%s"'),
$view->translate('This allows you to modify properties for "%s" (deployed from director)'),
$hosts[0]
);
} else {
Expand All @@ -49,9 +49,9 @@ public function prepareActionLinks(Cube $cube, View $view)

$url = 'director/hosts/edit?' . $filter->toQueryString();

$title = sprintf($view->translate('Modify %d hosts'), $count);
$title = sprintf($view->translate('Modify %d hosts'), count($hosts));
$description = $view->translate(
'This allows you to modify properties for all chosen hosts at once'
'This allows you to modify properties for all chosen hosts (deployed from director) at once'
);
}

Expand Down
48 changes: 14 additions & 34 deletions library/Director/ProvidedHook/IcingaDbCubeLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace Icinga\Module\Director\ProvidedHook;

use gipfl\Web\Widget\Hint;
use Icinga\Application\Config;
use Icinga\Data\Filter\Filter;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Cube\BaseCube;
use Icinga\Module\Cube\Hook\IcingadbHook;
use Icinga\Module\Cube\HostCube;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Web\View;

class IcingaDbCubeLinks extends IcingadbHook
Expand All @@ -22,62 +23,41 @@ class IcingaDbCubeLinks extends IcingadbHook
*/
public function prepareActionLinks(BaseCube $cube, View $view)
{
if ( ! $cube instanceof HostCube) {
if (! $cube instanceof HostCube) {
return;
}

$directorHosts = array_keys(IcingaObject::loadAllByType('Host', $this->directorDb()));

$hosts = [];
foreach ($cube->getQuery()->getHostNames($cube->getSlices()) as $host) {
$hosts[] = $host;
}

$count = count($hosts);
$chosenHosts = [];
if ($count === 1) {
$url = 'director/host/edit';
if (in_array($hosts[0], $directorHosts)) {
$chosenHosts[] = $hosts[0];
$params = array('name' => $hosts[0]);
$title = $view->translate('Modify a host');
$description = sprintf(
$view->translate('This allows you to modify properties for "%s" (deployed from director)'),
$chosenHosts[0]
);
}

$params = array('name' => $hosts[0]);
$title = $view->translate('Modify a host');
$description = sprintf(
$view->translate('This allows you to modify properties for "%s" (deployed from director)'),
$hosts[0]
);
} else {
$params = null;

$filter = Filter::matchAny();
foreach ($hosts as $host) {
if (in_array($host, $directorHosts)) {
$chosenHosts[] = $host;
if (IcingaHost::exists(['object_name' => $host], $this->directorDb())) {
$filter->addFilter(
Filter::matchAny(Filter::expression('name', '=', $host))
);
}
}

$url = 'director/hosts/edit?' . $filter->toQueryString();

if (count($chosenHosts) == 1) {
$title = $view->translate('Modify a host');
$description = sprintf(
$view->translate('This allows you to modify properties for "%s" (deployed from director)'),
$chosenHosts[0]
);
} else {
$title = sprintf($view->translate('Modify %d hosts'), count($chosenHosts));
$description = $view->translate(
'This allows you to modify properties for all chosen hosts (deployed from director) at once'
);
}
}

if (! (count($chosenHosts) > 0)) {
return;
$title = sprintf($view->translate('Modify %d hosts'), count($hosts));
$description = $view->translate(
'This allows you to modify properties for all chosen hosts (deployed from director) at once'
);
}

$this->addActionLink(
Expand Down
101 changes: 72 additions & 29 deletions library/Director/Web/Controller/ObjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Icinga\Module\Director\Web\Controller;

use Exception;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use gipfl\Web\Widget\Hint;
use Icinga\Data\Filter\FilterChain;
use Icinga\Data\Filter\FilterExpression;
use Icinga\Exception\NotFoundError;
use Icinga\Data\Filter\Filter;
use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry;
use Icinga\Module\Director\Forms\IcingaMultiEditForm;
use Icinga\Module\Director\Objects\IcingaCommand;
use Icinga\Module\Director\Objects\IcingaHost;
Expand Down Expand Up @@ -189,24 +192,31 @@ public function commonForEdit()
}

$objects = $this->loadMultiObjectsFromParams();
$formName = 'icinga' . $type;
$form = IcingaMultiEditForm::load()
->setObjects($objects)
->pickElementsFrom($this->loadForm($formName), $this->multiEdit);
if ($type === 'Service') {
$form->setListUrl('director/services');
} elseif ($type === 'Host') {
$form->setListUrl('director/hosts');
}

$form->handleRequest();
if ($objects) {
$formName = 'icinga' . $type;
$form = IcingaMultiEditForm::load()
->setObjects($objects)
->pickElementsFrom($this->loadForm($formName), $this->multiEdit);
if ($type === 'Service') {
$form->setListUrl('director/services');
} elseif ($type === 'Host') {
$form->setListUrl('director/hosts');
}

$this
->addSingleTab($this->translate('Multiple objects'))
->addTitle(
$this->translate('Modify %d objects'),
count($objects)
)->content()->add($form);
$form->handleRequest();

$this
->addSingleTab($this->translate('Multiple objects'))
->addTitle(
$this->translate('Modify %d objects'),
count($objects)
)->content()->add($form);
} else {
throw new Exception(
'No objects found.'
);
}
}

/**
Expand Down Expand Up @@ -370,8 +380,9 @@ protected function loadMultiObjectsFromParams()
{
$filter = Filter::fromQueryString($this->params->toString());
$type = $this->getType();
$objects = array();
$db = $this->db();
$nameCol = [];
$idCol = [];
/** @var $filter FilterChain */
foreach ($filter->filters() as $sub) {
/** @var $sub FilterChain */
Expand All @@ -380,24 +391,56 @@ protected function loadMultiObjectsFromParams()
$col = $ex->getColumn();
if ($ex->isExpression()) {
if ($col === 'name') {
$name = $ex->getExpression();
if ($type === 'service') {
$key = [
'object_type' => 'template',
'object_name' => $name
];
} else {
$key = $name;
}
$objects[$name] = IcingaObject::loadByType($type, $key, $db);
$nameCol[] = $ex->getExpression();
} elseif ($col === 'id') {
$name = $ex->getExpression();
$objects[$name] = IcingaObject::loadByType($type, ['id' => $name], $db);
$idCol[] = $ex->getExpression();
}
}
}
}

$query = $db->select()
->from(DbObjectTypeRegistry::tableNameByType($type), ['*']);

$objectsWihIDKey = [];
if (count($idCol) > 0 ) {
$query->where('id', $idCol);
$objectsWihIDKey = IcingaObject::loadAllByType($type, $db, $query, 'id');
}

$objectsWihNameKey = [];
if (count($nameCol) > 0) {
if ($type === 'service') {
$query ->where('object_type', 'template');
}

$query->where('object_name', $nameCol);

$objectsWihNameKey = IcingaObject::loadAllByType($type, $db, $query, 'object_name');
}

$objects = array_merge($objectsWihIDKey, $objectsWihNameKey);

$missingObjects = array_diff(array_merge($idCol, $nameCol), array_keys($objects));

if (count($missingObjects) > 0) {
if (count($missingObjects) === count(array_merge($idCol, $nameCol))) {
throw new Exception(
'These objects have not been found.'
);
} else {
$this->content()->prepend(
Hint::warning(
$this->translate(
sprintf(
'All these object(s) have not been found %s: ',
implode(', ', $missingObjects))
)
)
);
}
}

return $objects;
}

Expand Down