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
Changes from 1 commit
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
65 changes: 51 additions & 14 deletions library/Director/ProvidedHook/CubeLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Icinga\Module\Director\ProvidedHook;

use Icinga\Application\Config;
use Icinga\Data\Filter\Filter;
use Icinga\Module\Cube\Cube;
use Icinga\Module\Cube\Hook\ActionsHook;
use Icinga\Module\Cube\Ido\IdoHostStatusCube;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Web\View;

class CubeLinks extends ActionsHook
Expand All @@ -19,6 +22,8 @@ public function prepareActionLinks(Cube $cube, View $view)
return;
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a good idea, loading all Hosts is not an option. While the user might select just a bunch of them, the DB can easily contain 100k+.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nilmerg: thanks for pointing this out, I overlooked it! @raviks789: please either use the deployment location in IcingaDB as a source of truth or ask the Director about the selected hosts only. If unsure how to address this, please let me know


$cube->finalizeInnerQuery();
$query = $cube->innerQuery()
->reset('columns')
Expand All @@ -28,31 +33,53 @@ public function prepareActionLinks(Cube $cube, View $view)
$hosts = $cube->db()->fetchCol($query);

$count = count($hosts);
$chosenHosts = [];
if ($count === 1) {
$url = 'director/host/edit';
$params = array('name' => $hosts[0]);
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"'),
$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) {
$filter->addFilter(
Filter::matchAny(Filter::expression('name', '=', $host))
);
if (in_array($host, $directorHosts)) {
$chosenHosts[] = $host;
$filter->addFilter(
Filter::matchAny(Filter::expression('name', '=', $host))
);
}
}

$url = 'director/hosts/edit?' . $filter->toQueryString();
if (count($chosenHosts) == 1) {
$url = 'director/host/edit';
$params = array('name' => $chosenHosts[0]);

$title = $view->translate('Modify a host');
$description = sprintf(
$view->translate('This allows you to modify properties for "%s" (deployed from director)'),
$chosenHosts[0]
);
} else {
$url = 'director/hosts/edit?' . $filter->toQueryString();

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

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

$this->addActionLink(
Expand All @@ -62,4 +89,14 @@ public function prepareActionLinks(Cube $cube, View $view)
'wrench'
);
}

protected function directorDb()
{
$resourceName = Config::module('director')->get('db', 'resource');
if (! $resourceName) {
return false;
}

return Db::fromResourceName($resourceName);
}
}