From a93bcee68afd4692e24cb80b5779572b7a23403b Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 17 Dec 2015 10:49:10 +0100 Subject: [PATCH] endpoint/inspect: add inspection for our peers --- .../controllers/EndpointController.php | 28 +++++++++++++++++ application/controllers/InspectController.php | 23 +++++++++++++- .../views/scripts/inspect/object.phtml | 2 +- application/views/scripts/inspect/type.phtml | 11 +++++-- application/views/scripts/inspect/types.phtml | 30 ++++++++++++++----- 5 files changed, 82 insertions(+), 12 deletions(-) diff --git a/application/controllers/EndpointController.php b/application/controllers/EndpointController.php index e8a4fb01..23308348 100644 --- a/application/controllers/EndpointController.php +++ b/application/controllers/EndpointController.php @@ -6,4 +6,32 @@ use Icinga\Module\Director\Web\Controller\ObjectController; class EndpointController extends ObjectController { + public function init() + { + parent::init(); + if ($this->isPeer()) { + $params['endpoint'] = $this->object->object_name; + + $this->getTabs()->add('inspect', array( + 'url' => 'director/inspect/types', + 'urlParams' => $params, + 'label' => $this->translate('Inspect') + )); + } + } + + protected function isPeer() + { + if (! $this->object) return false; + + $apiconfig = $this->Config()->getSection('api'); + $host = $apiconfig->get('address'); + $port = $apiconfig->get('port'); + + if ($host === $this->object->host + && $port === $this->object->port + ) { + return true; + } + } } diff --git a/application/controllers/InspectController.php b/application/controllers/InspectController.php index e779a161..02248aa2 100644 --- a/application/controllers/InspectController.php +++ b/application/controllers/InspectController.php @@ -2,13 +2,19 @@ namespace Icinga\Module\Director\Controllers; +use Icinga\Module\Director\Core\CoreApi; +use Icinga\Module\Director\Core\RestApiClient; +use Icinga\Module\Director\Objects\IcingaEndpoint; use Icinga\Module\Director\Web\Controller\ActionController; class InspectController extends ActionController { public function typesAction() { - $this->view->title = $this->translate('Icinga2 object types'); + $this->view->title = sprintf( + $this->translate('Icinga2 Objects: %s'), + $this->view->endpoint + ); $api = $this->api(); $types = $api->getTypes(); $rootNodes = array(); @@ -30,6 +36,10 @@ class InspectController extends ActionController public function typeAction() { $typeName = $this->params->get('name'); + $this->view->title = sprintf( + $this->translate('Object type "%s"'), + $typeName + ); $this->view->type = $type = $this->api()->getType($typeName); if ($type->abstract) { return; @@ -84,4 +94,15 @@ class InspectController extends ActionController $this->view->status = $status = $this->api()->getStatus(); print_r($status); exit; } + + protected function api() + { + $this->view->endpoint = $this->params->get('endpoint'); + $endpoint = IcingaEndpoint::load($this->view->endpoint, $this->db()); + $apiconfig = $this->Config()->getSection('api'); + $client = new RestApiClient($endpoint->host, $endpoint->port); + $client->setCredentials($apiconfig->get('username'), $apiconfig->get('password')); + $api = new CoreApi($client); + return $api; + } } diff --git a/application/views/scripts/inspect/object.phtml b/application/views/scripts/inspect/object.phtml index 97daa120..627c8a67 100644 --- a/application/views/scripts/inspect/object.phtml +++ b/application/views/scripts/inspect/object.phtml @@ -4,6 +4,6 @@
-escape(print_r($this->object, 1)) ?>
+renderPlainObject($this->object) ?>
 
diff --git a/application/views/scripts/inspect/type.phtml b/application/views/scripts/inspect/type.phtml index 48e31859..1950072a 100644 --- a/application/views/scripts/inspect/type.phtml +++ b/application/views/scripts/inspect/type.phtml @@ -4,6 +4,7 @@
+objects): ?> @@ -11,10 +12,16 @@
translate('Object name') ?>
qlink( str_replace('!', ': ', $object), 'director/inspect/object', - array('type' => $this->type->name, 'plural' => $this->type->plural_name, 'name' => $object) + array( + 'endpoint' => $this->endpoint, + 'type' => $this->type->name, + 'plural' => $this->type->plural_name, + 'name' => $object + ) ) ?>
-
escape(print_r($this->type, 1)) ?>
+ +
renderPlainObject($this->type) ?>
diff --git a/application/views/scripts/inspect/types.phtml b/application/views/scripts/inspect/types.phtml index 387f10bd..9391b5e9 100644 --- a/application/views/scripts/inspect/types.phtml +++ b/application/views/scripts/inspect/types.phtml @@ -1,20 +1,34 @@ $node) { $html .= '
  • '; - $link = $self->qlink( - $name, - 'director/inspect/type', - array('name' => $name), - array('class' => $node->abstract ? 'abstract' : 'object') - ); + + if ($showLinks || $name === 'ConfigObject') { + $link = $self->qlink( + $name, + 'director/inspect/type', + array('endpoint' => $self->endpoint, 'name' => $name), + array('class' => $node->abstract ? 'abstract' : 'object') + ); + } else { + $link = sprintf( + '%s', + $node->abstract ? 'abstract' : 'object', + $name + ); + } if (property_exists($node, 'children')) { $html .= ' '; $html .= $link; - $html .= ''; + $html .= ''; } else { $html .= $link; }