Skip to content

Commit

Permalink
RestApi: introduce RestApiParams, use Exporter
Browse files Browse the repository at this point in the history
fixes #2568
  • Loading branch information
Thomas-Gelf committed Jul 20, 2022
1 parent cb355f9 commit ab4b580
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 36 deletions.
27 changes: 10 additions & 17 deletions library/Director/RestApi/IcingaObjectHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
use Icinga\Exception\NotFoundError;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Core\CoreApi;
use Icinga\Module\Director\Data\Exporter;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Exception\DuplicateKeyException;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Util;

class IcingaObjectHandler extends RequestHandler
{
Expand All @@ -19,9 +20,13 @@ class IcingaObjectHandler extends RequestHandler
/** @var CoreApi */
protected $api;

/** @var Db */
protected $connection;

public function setObject(IcingaObject $object)
{
$this->object = $object;
$this->connection = $object->getConnection();
return $this;
}

Expand Down Expand Up @@ -151,22 +156,10 @@ protected function handleApiRequest()
break;

case 'GET':
$params = $this->request->getUrl()->getParams();
$this->requireObject();
$properties = $params->shift('properties');
if (strlen($properties)) {
$properties = preg_split('/\s*,\s*/', $properties, -1, PREG_SPLIT_NO_EMPTY);
} else {
$properties = null;
}

$this->sendJson(
$this->requireObject()->toPlainObject(
$params->shift('resolved'),
! $params->shift('withNull'),
$properties
)
);
$object = $this->requireObject();
$exporter = new Exporter($this->connection);
RestApiParams::applyParamsToExporter($exporter, $this->request, $object->getShortTableName());
$this->sendJson($exporter->export($object));
break;

default:
Expand Down
29 changes: 10 additions & 19 deletions library/Director/RestApi/IcingaObjectsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Icinga\Module\Director\RestApi;

use Exception;
use gipfl\Json\JsonString;
use Icinga\Application\Benchmark;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Data\Exporter;
use Icinga\Module\Director\Db\Cache\PrefetchCache;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Table\ApplyRulesTable;
Expand Down Expand Up @@ -61,14 +63,18 @@ protected function streamJsonResult()
Benchmark::measure('Ready to stream JSON result');
$db = $connection->getDbAdapter();
$table = $this->getTable();
$exporter = new Exporter($connection);
$type = $table->getType();
RestApiParams::applyParamsToExporter($exporter, $this->request, $type);
$query = $table
->getQuery()
->reset(ZfSelect::COLUMNS)
->columns('*')
->reset(ZfSelect::LIMIT_COUNT)
->reset(ZfSelect::LIMIT_OFFSET);
$type = $table->getType();
$serviceApply = $type === 'service' && $table instanceof ApplyRulesTable;
if ($type === 'service' && $table instanceof ApplyRulesTable) {
$exporter->showIds();
}
echo '{ "objects": [ ';
$cnt = 0;
$objects = [];
Expand All @@ -84,15 +90,6 @@ protected function streamJsonResult()
if (! ob_get_level()) {
ob_start();
}
$params = $this->request->getUrl()->getParams();
$resolved = (bool) $params->get('resolved', false);
$withNull = ! $params->shift('withNull');
$properties = $params->shift('properties');
if ($properties !== null && strlen($properties)) {
$properties = preg_split('/\s*,\s*/', $properties, -1, PREG_SPLIT_NO_EMPTY);
} else {
$properties = null;
}

$first = true;
$flushes = 0;
Expand All @@ -102,13 +99,7 @@ protected function streamJsonResult()
Benchmark::measure('Fetching first row');
}
$object = $dummy::fromDbRow($row, $connection);
$objects[] = json_encode($object->toPlainObject(
$resolved,
$withNull,
$properties,
true,
$serviceApply
), JSON_PRETTY_PRINT);
$objects[] = JsonString::encode($exporter->export($object), JSON_PRETTY_PRINT);
if ($first) {
Benchmark::measure('Got first row');
$first = false;
Expand All @@ -134,7 +125,7 @@ protected function streamJsonResult()
echo implode(', ', $objects);
}

if ($params->get('benchmark')) {
if ($this->request->getUrl()->getParams()->get('benchmark')) {
echo "],\n";
Benchmark::measure('All done');
echo '"benchmark_string": ' . json_encode(Benchmark::renderToText());
Expand Down
30 changes: 30 additions & 0 deletions library/Director/RestApi/RestApiParams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Icinga\Module\Director\RestApi;

use Icinga\Module\Director\Data\Exporter;
use Icinga\Web\Request;
use InvalidArgumentException;

class RestApiParams
{
public static function applyParamsToExporter(Exporter $exporter, Request $request, $shortObjectType = null)
{
$params = $request->getUrl()->getParams();
$resolved = (bool) $params->get('resolved', false);
$withNull = $params->shift('withNull');
$withServices = (bool) $params->get('withServices');
if ($withServices) {
if ($shortObjectType !== 'host') {
throw new InvalidArgumentException('withServices is available for Hosts only');
}
$exporter->enableHostServices();
}
$properties = $params->shift('properties');
if ($properties !== null && strlen($properties)) {
$exporter->filterProperties(preg_split('/\s*,\s*/', $properties, -1, PREG_SPLIT_NO_EMPTY));
}
$exporter->resolveObjects($resolved);
$exporter->showDefaults($withNull);
}
}

0 comments on commit ab4b580

Please sign in to comment.