Inspect: completely refactored

* new tree renderer
* more tables with details
* styling
* allow to show status

fixes #1003
fixes #1005
This commit is contained in:
Thomas Gelf 2017-07-13 07:52:48 +02:00
parent f10b85ac9e
commit e27bb05fb1
7 changed files with 376 additions and 93 deletions

View File

@ -12,12 +12,6 @@ class EndpointController extends ObjectController
parent::init();
if ($this->object && $this->object->hasApiUser()) {
$params['endpoint'] = $this->object->object_name;
$this->getTabs()->add('inspect', array(
'url' => 'director/inspect/types',
'urlParams' => $params,
'label' => $this->translate('Inspect')
));
}
}
}

View File

@ -2,10 +2,21 @@
namespace Icinga\Module\Director\Controllers;
use Icinga\Module\Director\Objects\IcingaEndpoint;
use Icinga\Module\Director\PlainObjectRenderer;
use Icinga\Module\Director\Web\Controller\ActionController;
use Icinga\Module\Director\Web\Table\CoreApiFieldsTable;
use Icinga\Module\Director\Web\Table\CoreApiObjectsTable;
use Icinga\Module\Director\Web\Table\CoreApiPrototypesTable;
use Icinga\Module\Director\Web\Tabs\ObjectTabs;
use Icinga\Module\Director\Web\Tree\InspectTreeRenderer;
use ipl\Html\Html;
use ipl\Html\Link;
class InspectController extends ActionController
{
private $endpoint;
protected function checkDirectorPermissions()
{
$this->assertPermission('director/inspect');
@ -13,118 +24,97 @@ class InspectController extends ActionController
public function typesAction()
{
$api = $this->api();
$params = array('name' => $this->view->endpoint);
$this->getTabs()->add('modify', array(
'url' => 'director/endpoint',
'urlParams' => $params,
'label' => $this->translate('Endpoint')
))->add('render', array(
'url' => 'director/endpoint/render',
'urlParams' => $params,
'label' => $this->translate('Preview'),
))->add('history', array(
'url' => 'director/endpoint/history',
'urlParams' => $params,
'label' => $this->translate('History')
))->add('inspect', array(
'url' => $this->getRequest()->getUrl(),
'label' => $this->translate('Inspect')
))->activate('inspect');
$object = $this->endpoint();
$name = $object->getObjectName();
$this->tabs(
new ObjectTabs('endpoint', $this->Auth(), $object)
)->activate('inspect');
$this->view->title = sprintf(
$this->translate('Icinga2 Objects: %s'),
$this->view->endpoint
$this->addTitle($this->translate('Icinga 2 - Objects: %s'), $name);
$this->actions()->add(
Link::create(
$this->translate('Status'),
'director/inspect/status',
['endpoint' => $name],
[
'class' => 'icon-eye',
'data-base-target' => '_next'
]
)
);
$this->content()->add(
new InspectTreeRenderer($object)
);
$types = $api->getTypes();
$rootNodes = array();
foreach ($types as $name => $type) {
if (property_exists($type, 'base')) {
$base = $type->base;
if (! property_exists($types[$base], 'children')) {
$types[$base]->children = array();
}
$types[$base]->children[$name] = $type;
} else {
$rootNodes[$name] = $type;
}
}
$this->view->types = $rootNodes;
}
public function typeAction()
{
$typeName = $this->params->get('name');
$this->singleTab($this->translate('Inspect - object list'));
$this->view->title = sprintf(
$api = $this->endpoint()->api();
$typeName = $this->params->get('type');
$this->addSingleTab($this->translate('Inspect - object list'));
$this->addTitle(
$this->translate('Object type "%s"'),
$typeName
);
$this->view->type = $type = $this->api()->getType($typeName);
$c = $this->content();
$type = $api->getType($typeName);
if ($type->abstract) {
return;
$c->add($this->translate('This is an abstract object type.'));
}
if (! empty($type->fields)) {
$this->view->objects = $this->api()->listObjects(
$typeName,
$type->plural_name
);
if (! $type->abstract) {
$objects = $api->listObjects($typeName, $type->plural_name);
$c->add(Html::p(sprintf($this->translate('%d objects found'), count($objects))));
$c->add(new CoreApiObjectsTable($objects, $this->endpoint(), $type));
}
if (count((array) $type->fields)) {
$c->add([
Html::h2($this->translate('Type attributes')),
new CoreApiFieldsTable($type->fields, $this->url())
]);
}
if (count($type->prototype_keys)) {
$c->add([
Html::h2($this->translate('Prototypes (methods)')),
new CoreApiPrototypesTable($type->prototype_keys, $type->name)
]);
}
}
public function objectAction()
{
$this->singleTab($this->translate('Object Inspection'));
$this->view->object = $this->api()->getObject(
$this->params->get('name'),
$this->params->get('plural')
);
}
public function commandsAction()
{
$db = $this->db();
echo '<pre>';
foreach ($this->api()->setDb($db)->getCheckCommandObjects() as $object) {
if (! $object::exists($object->object_name, $db)) {
// var_dump($object->store());
echo $object;
}
}
echo '</pre>';
exit;
}
public function zonesAction()
{
$db = $this->db();
echo '<pre>';
foreach ($this->api()->setDb($db)->getZoneObjects() as $zone) {
if (! $zone::exists($zone->object_name, $db)) {
// var_dump($zone->store());
echo $zone;
}
}
echo '</pre>';
exit;
$name = $this->params->get('name');
$pType = $this->params->get('plural');
$this->addSingleTab($this->translate('Object Inspection'));
$object = $this->endpoint()->api()->getObject($name, $pType);
$this->addTitle('%s "%s"', $pType, $name);
$this->content()->add(Html::pre(
PlainObjectRenderer::render($object)
));
}
public function statusAction()
{
$this->view->status = $status = $this->api()->getStatus();
print_r($status);
exit;
$this->addSingleTab($this->translate('Status'));
$this->addTitle($this->translate('Icinga 2 API - Status'));
$this->content()->add(
Html::pre(PlainObjectRenderer::render($this->endpoint()->api()->getStatus()))
);
}
protected function api($endpointName = null)
protected function endpoint()
{
$this->view->endpoint = $this->params->get('endpoint');
if ($this->view->endpoint === null) {
$this->view->endpoint = $this->db()->getDeploymentEndpointName();
if ($this->endpoint === null) {
if ($name = $this->params->get('endpoint')) {
$this->endpoint = IcingaEndpoint::load($name, $this->db());
} else {
$this->endpoint = $this->db()->getDeploymentEndpointName();
}
}
return parent::api($this->view->endpoint);
return $this->endpoint;
}
}

View File

@ -0,0 +1,97 @@
<?php
namespace Icinga\Module\Director\Web\Table;
use ipl\Html\BaseElement;
use ipl\Html\Link;
use ipl\Html\Table;
use ipl\Translation\TranslationHelper;
use ipl\Web\Url;
class CoreApiFieldsTable extends Table
{
use TranslationHelper;
protected $defaultAttributes = [
'class' => ['common-table'/*, 'table-row-selectable'*/],
//'data-base-target' => '_next',
];
protected $fields;
/** @var Url */
protected $url;
public function __construct($fields, Url $url)
{
$this->url = $url;
$this->fields = $fields;
}
public function assemble()
{
$this->header();
$body = $this->body();
foreach ($this->fields as $name => $field) {
$tr = $this::tr([
$this::td($name),
$this::td(Link::create(
$field->type,
$this->url->with('type', $field->type)
)),
$this::td($field->id)
// $this::td($field->array_rank),
// $this::td($this->renderKeyValue($field->attributes))
]);
$this->addAttributeColumns($tr, $field->attributes);
$body->add($tr);
}
}
protected function addAttributeColumns(BaseElement $tr, $attrs)
{
$tr->add([
$this->makeBooleanColumn($attrs->state),
$this->makeBooleanColumn($attrs->config),
$this->makeBooleanColumn($attrs->required),
$this->makeBooleanColumn($attrs->no_user_modify),
$this->makeBooleanColumn($attrs->no_user_view),
$this->makeBooleanColumn($attrs->navigation),
]);
}
protected function makeBooleanColumn($value)
{
return $this::td($value ? $this::strong('true') : 'false');
}
public function getColumnsToBeRendered()
{
return [
$this->translate('Name'),
$this->translate('Type'),
$this->translate('Id'),
// $this->translate('Array Rank'),
// $this->translate('Attributes')
$this->translate('State'),
$this->translate('Config'),
$this->translate('Required'),
$this->translate('Protected'),
$this->translate('Hidden'),
$this->translate('Nav'),
];
}
protected function renderKeyValue($values)
{
$parts = [];
foreach ((array) $values as $key => $value) {
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}
$parts[] = "$key: $value";
}
return implode(', ', $parts);
}
}

View File

@ -0,0 +1,57 @@
<?php
namespace Icinga\Module\Director\Web\Table;
use Icinga\Module\Director\Objects\IcingaEndpoint;
use ipl\Html\Link;
use ipl\Html\Table;
use ipl\Translation\TranslationHelper;
class CoreApiObjectsTable extends Table
{
use TranslationHelper;
protected $defaultAttributes = [
'class' => ['common-table', 'table-row-selectable'],
'data-base-target' => '_next',
];
/** @var IcingaEndpoint */
protected $endpoint;
protected $objects;
protected $type;
public function __construct($objects, IcingaEndpoint $endpoint, $type)
{
$this->objects = $objects;
$this->endpoint = $endpoint;
$this->type = $type;
}
public function assemble()
{
$this->header();
$body = $this->body();
foreach ($this->objects as $name) {
$body->add($this::tr($this::td(Link::create(
str_replace('!', ': ', $name),
'director/inspect/object',
[
'name' => $name,
'type' => $this->type->name,
'plural' => $this->type->plural_name,
'endpoint' => $this->endpoint->getObjectName()
]
))));
}
}
public function getColumnsToBeRendered()
{
return [
$this->translate('Name'),
];
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace Icinga\Module\Director\Web\Table;
use ipl\Html\Table;
use ipl\Translation\TranslationHelper;
class CoreApiPrototypesTable extends Table
{
use TranslationHelper;
protected $defaultAttributes = ['class' => ['common-table']];
protected $prototypes;
protected $typeName;
public function __construct($prototypes, $typeName)
{
$this->prototypes = $prototypes;
$this->typeName = $typeName;
}
public function assemble()
{
$this->header();
$body = $this->body();
$type = $this->typeName;
foreach ($this->prototypes as $name) {
$body->add($this::tr($this::td("$type.$name()")));
}
}
public function getColumnsToBeRendered()
{
return [
$this->translate('Name'),
];
}
}

View File

@ -101,6 +101,14 @@ class ObjectTabs extends Tabs
'label' => $this->translate('Members')
]);
}
if ($object->getShortTableName() === 'endpoint') {
$this->add('inspect', [
'url' => 'director/inspect/types',
'urlParams' => ['endpoint' => $object->getObjectName()],
'label' => $this->translate('Inspect')
]);
}
}
protected function hasFields()

View File

@ -0,0 +1,97 @@
<?php
namespace Icinga\Module\Director\Web\Tree;
use Icinga\Module\Director\Objects\IcingaEndpoint;
use ipl\Html\BaseElement;
use ipl\Html\Html;
use ipl\Html\Link;
use ipl\Translation\TranslationHelper;
class InspectTreeRenderer extends BaseElement
{
use TranslationHelper;
protected $tag = 'ul';
protected $defaultAttributes = [
'class' => 'tree',
'data-base-target' => '_next',
];
protected $tree;
/** @var IcingaEndpoint */
protected $endpoint;
public function __construct(IcingaEndpoint $endpoint)
{
$this->endpoint = $endpoint;
}
protected function getNodes()
{
$rootNodes = array();
$types = $this->endpoint->api()->getTypes();
foreach ($types as $name => $type) {
if (property_exists($type, 'base')) {
$base = $type->base;
if (! property_exists($types[$base], 'children')) {
$types[$base]->children = array();
}
$types[$base]->children[$name] = $type;
} else {
$rootNodes[$name] = $type;
}
}
return $rootNodes;
}
public function assemble()
{
$this->add($this->renderNodes($this->getNodes()));
}
protected function renderNodes($nodes, $showLinks = false, $level = 0)
{
$result = [];
foreach ($nodes as $child) {
$result[] = $this->renderNode($child, $showLinks, $level + 1);
}
if ($level === 0) {
return $result;
} else {
return Html::tag('ul', null, $result);
}
}
protected function renderNode($node, $forceLinks = false, $level = 0)
{
$name = $node->name;
$showLinks = $forceLinks || $name === 'ConfigObject';
$hasChildren = property_exists($node, 'children');
$li = Html::tag('li');
if (! $hasChildren) {
$li->attributes()->add('class', 'collapsed');
}
if ($hasChildren) {
$li->add(Html::tag('span', ['class' => 'handle']));
}
$class = $node->abstract ? 'icon-sitemap' : 'icon-doc-text';
$li->add(Link::create($name, 'director/inspect/type', [
'endpoint' => $this->endpoint->getObjectName(),
'type' => $name
], ['class' => $class]));
if ($hasChildren) {
$li->add($this->renderNodes($node->children, $showLinks, $level + 1));
}
return $li;
}
}