Cleanup IcingaDbCubeLinks with getObjectsFilter provided by IcingaDbCube.

This commit is contained in:
raviks789 2022-03-02 15:20:07 +01:00 committed by Thomas Gelf
parent 83cc12944d
commit e134f80093
2 changed files with 33 additions and 66 deletions

View File

@ -2,81 +2,58 @@
namespace Icinga\Module\Director\ProvidedHook; namespace Icinga\Module\Director\ProvidedHook;
use Icinga\Application\Config;
use Icinga\Data\Filter\Filter; use Icinga\Data\Filter\Filter;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Icinga\Module\Cube\BaseCube; use Icinga\Module\Cube\Hook\IcingaDbActionsHook;
use Icinga\Module\Cube\Hook\IcingadbHook; use Icinga\Module\Cube\IcingaDb\IcingaDbCube;
use Icinga\Module\Cube\HostCube; use Icinga\Module\Cube\IcingaDb\IcingaDbHostStatusCube;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Web\View;
class IcingaDbCubeLinks extends IcingadbHook class IcingaDbCubeLinks extends IcingaDbActionsHook
{ {
/** /**
* @inheritDoc * @inheritDoc
* @param BaseCube $cube * @param IcingaDbCube $cube
* @param View $view
* @throws ProgrammingError * @throws ProgrammingError
*/ */
public function prepareActionLinks(BaseCube $cube, View $view) public function createActionLinks(IcingaDbCube $cube)
{ {
if (! $cube instanceof HostCube) { if (! $cube instanceof IcingaDbHostStatusCube) {
return; return;
} }
$directorHosts = array_keys(IcingaObject::loadAllByType('Host', $this->directorDb())); $filterChain = $cube->getObjectsFilter();
$hosts = []; if ($filterChain->count() === 1) {
foreach ($cube->getQuery()->getHostNames($cube->getSlices()) as $host) { $url = 'director/host/edit?';
$hosts[] = $host; $params = ['name' => $filterChain->getIterator()->current()->getValue()];
}
$count = count($hosts); $title = t('Modify a host');
$chosenHosts = []; $description = sprintf(
if ($count === 1) { t('This allows you to modify properties for "%s"'),
$url = 'director/host/edit'; $filterChain->getIterator()->current()->getValue()
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]
);
}
} else { } else {
$params = null; $params = null;
$filter = Filter::matchAny(); $urlFilter = Filter::matchAny();
foreach ($hosts as $host) { foreach ($filterChain as $filter) {
if (in_array($host, $directorHosts)) { $urlFilter->addFilter(
$chosenHosts[] = $host; Filter::matchAny(
$filter->addFilter( Filter::expression(
Filter::matchAny(Filter::expression('name', '=', $host)) 'name',
); '=',
} $filter->getValue()
} )
)
$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)) { $url = 'director/hosts/edit?' . $urlFilter->toQueryString();
return;
$title = sprintf(t('Modify %d hosts'), $filterChain->count());
$description = t(
'This allows you to modify properties for all chosen hosts at once'
);
} }
$this->addActionLink( $this->addActionLink(
@ -86,14 +63,4 @@ class IcingaDbCubeLinks extends IcingadbHook
'wrench' 'wrench'
); );
} }
protected function directorDb()
{
$resourceName = Config::module('director')->get('db', 'resource');
if (! $resourceName) {
return false;
}
return Db::fromResourceName($resourceName);
}
} }

View File

@ -64,7 +64,7 @@ use Icinga\Module\Director\ProvidedHook\IcingaDbCubeLinks;
$this->provideHook('monitoring/HostActions'); $this->provideHook('monitoring/HostActions');
$this->provideHook('monitoring/ServiceActions'); $this->provideHook('monitoring/ServiceActions');
$this->provideHook('cube/Actions', CubeLinks::class); $this->provideHook('cube/Actions', CubeLinks::class);
$this->provideHook('cube/Icingadb', IcingaDbCubeLinks::class); $this->provideHook('cube/IcingaDbActions', IcingaDbCubeLinks::class);
$directorHooks = [ $directorHooks = [
'director/DataType' => [ 'director/DataType' => [