From 8046510b1f7e4da1edc6733e23b86f9a24b51dff Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 15 Feb 2019 01:29:43 +0100 Subject: [PATCH] ServiceapplyrulesController and related changes fixes #1755 --- .../ServiceapplyrulesController.php | 39 +++++++++++++++++++ doc/70-REST-API.md | 9 +++++ doc/82-Changelog.md | 1 + library/Director/Objects/IcingaObject.php | 6 ++- .../Director/RestApi/IcingaObjectsHandler.php | 16 ++++++-- .../Director/Web/Table/ApplyRulesTable.php | 5 +++ 6 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 application/controllers/ServiceapplyrulesController.php diff --git a/application/controllers/ServiceapplyrulesController.php b/application/controllers/ServiceapplyrulesController.php new file mode 100644 index 00000000..c3a7f2b1 --- /dev/null +++ b/application/controllers/ServiceapplyrulesController.php @@ -0,0 +1,39 @@ +getRequest(); + if (! $request->isApiRequest()) { + throw new NotFoundError('Not found'); + } + + $table = ApplyRulesTable::create('service', $this->db()); +/* + $query = $this->db()->getDbAdapter() + ->select() + ->from('icinga_service') + ->where('object_type = ?', 'apply'); + $rules = IcingaService::loadAll($this->db(), $query); +*/ + + $handler = (new IcingaObjectsHandler( + $request, + $this->getResponse(), + $this->db() + ))->setTable($table); + + $handler->dispatch(); + } +} diff --git a/doc/70-REST-API.md b/doc/70-REST-API.md index 356166ae..2ab0d933 100644 --- a/doc/70-REST-API.md +++ b/doc/70-REST-API.md @@ -487,6 +487,15 @@ Content-Type: application/json } ``` +### Service Apply Rules + +Please note that Service Apply Rule names are not unique in Icinga 2. They are +not real objects, they are creating other objects in a loop. This makes it +impossible to distinct them by name. Therefore, a dedicated REST API endpoint +`director/serviceapplyrules` ships all Service Apply Rules combined with their +internal ID. This ID can then be used to modify or delete a Rule via +`director/service`. + ### Agent Tickets The Director is very helpful when it goes to manage your Icinga Agents. In diff --git a/doc/82-Changelog.md b/doc/82-Changelog.md index 15589e57..bc71a78d 100644 --- a/doc/82-Changelog.md +++ b/doc/82-Changelog.md @@ -25,6 +25,7 @@ before switching to a new version. * FIX: Services from a Set assigned to a single Host can be blacklisted (#1616) * FEATURE: Add TimePeriod support to Configuration Baskets (#1735) * FEATURE: RO users could want to see where a configured service originated (#1785) +* FEATURE: introduce director/serviceapplyrules REST API endpoint (#1755) ### REST API * FIX: Self Service API now ships optional Service User parameter (#1297) diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php index d8470721..6f2b0301 100644 --- a/library/Director/Objects/IcingaObject.php +++ b/library/Director/Objects/IcingaObject.php @@ -2773,6 +2773,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer * @param bool $skipDefaults * @param array|null $chosenProperties * @param bool $resolveIds + * @param bool $keepId * @return object * @throws NotFoundError */ @@ -2780,7 +2781,8 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer $resolved = false, $skipDefaults = false, array $chosenProperties = null, - $resolveIds = true + $resolveIds = true, + $keepId = false ) { $props = array(); @@ -2799,7 +2801,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer foreach ($p as $k => $v) { // Do not ship ids for IcingaObjects: if ($resolveIds) { - if ($k === 'id' && $this->hasProperty('object_name')) { + if ($k === 'id' && $keepId === false && $this->hasProperty('object_name')) { continue; } diff --git a/library/Director/RestApi/IcingaObjectsHandler.php b/library/Director/RestApi/IcingaObjectsHandler.php index 548273b1..5af1c1ea 100644 --- a/library/Director/RestApi/IcingaObjectsHandler.php +++ b/library/Director/RestApi/IcingaObjectsHandler.php @@ -7,6 +7,7 @@ use Icinga\Application\Benchmark; use Icinga\Exception\ProgrammingError; use Icinga\Module\Director\Db\Cache\PrefetchCache; use Icinga\Module\Director\Objects\IcingaObject; +use Icinga\Module\Director\Web\Table\ApplyRulesTable; use Icinga\Module\Director\Web\Table\ObjectsTable; use Zend_Db_Select as ZfSelect; @@ -25,7 +26,11 @@ class IcingaObjectsHandler extends RequestHandler } } - public function setTable(ObjectsTable $table) + /** + * @param ObjectsTable|ApplyRulesTable $table + * @return $this + */ + public function setTable($table) { $this->table = $table; return $this; @@ -63,12 +68,13 @@ class IcingaObjectsHandler extends RequestHandler ->columns('*') ->reset(ZfSelect::LIMIT_COUNT) ->reset(ZfSelect::LIMIT_OFFSET); - + $type = $table->getType(); + $serviceApply = $type === 'service' && $table instanceof ApplyRulesTable; echo '{ "objects": [ '; $cnt = 0; $objects = []; - $dummy = IcingaObject::createByType($table->getType(), [], $connection); + $dummy = IcingaObject::createByType($type, [], $connection); $dummy->prefetchAllRelatedTypes(); Benchmark::measure('Pre-fetching related objects'); @@ -100,7 +106,9 @@ class IcingaObjectsHandler extends RequestHandler $objects[] = json_encode($object->toPlainObject( $resolved, $withNull, - $properties + $properties, + true, + $serviceApply ), JSON_PRETTY_PRINT); if ($first) { Benchmark::measure('Got first row'); diff --git a/library/Director/Web/Table/ApplyRulesTable.php b/library/Director/Web/Table/ApplyRulesTable.php index 6792a6ba..01dc7cf9 100644 --- a/library/Director/Web/Table/ApplyRulesTable.php +++ b/library/Director/Web/Table/ApplyRulesTable.php @@ -38,6 +38,11 @@ class ApplyRulesTable extends ZfQueryBasedTable return $this; } + public function getType() + { + return $this->type; + } + public function getColumnsToBeRendered() { return ['Name', 'assign where'/*, 'Actions'*/];