Skip to content

Commit

Permalink
Merge branch 'feature/show-packages'
Browse files Browse the repository at this point in the history
fixes #1995
  • Loading branch information
Thomas-Gelf committed Oct 17, 2019
2 parents 1723e13 + ece50a0 commit e871ba4
Show file tree
Hide file tree
Showing 7 changed files with 380 additions and 26 deletions.
56 changes: 39 additions & 17 deletions application/controllers/InspectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Icinga\Module\Director\Controllers;

use gipfl\IcingaWeb2\Link;
use Icinga\Module\Director\Objects\IcingaEndpoint;
use Icinga\Module\Director\PlainObjectRenderer;
use Icinga\Module\Director\Web\Controller\ActionController;
Expand All @@ -10,9 +11,9 @@
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 gipfl\IcingaWeb2\Link;
use Icinga\Module\Director\Web\Widget\IcingaObjectInspection;
use Icinga\Module\Director\Web\Widget\InspectPackages;
use ipl\Html\Html;

class InspectController extends ActionController
{
Expand All @@ -24,9 +25,7 @@ protected function checkDirectorPermissions()
}

/**
* @throws \Icinga\Exception\Http\HttpNotFoundException
* @throws \Icinga\Exception\IcingaException
* @throws \Icinga\Exception\ProgrammingError
* @throws \Icinga\Exception\NotFoundError
*/
public function typesAction()
{
Expand Down Expand Up @@ -55,8 +54,7 @@ public function typesAction()
}

/**
* @throws \Icinga\Exception\IcingaException
* @throws \Icinga\Exception\ProgrammingError
* @throws \Icinga\Exception\NotFoundError
*/
public function typeAction()
{
Expand Down Expand Up @@ -95,9 +93,7 @@ public function typeAction()
}

/**
* @throws \Icinga\Exception\ConfigurationError
* @throws \Icinga\Exception\IcingaException
* @throws \Icinga\Exception\ProgrammingError
* @throws \Icinga\Exception\NotFoundError
*/
public function objectAction()
{
Expand All @@ -116,7 +112,6 @@ public function objectAction()

/**
* @param IcingaEndpoint $endpoint
* @throws \Icinga\Exception\IcingaException
*/
protected function showEndpointInformation(IcingaEndpoint $endpoint)
{
Expand All @@ -132,8 +127,6 @@ protected function showEndpointInformation(IcingaEndpoint $endpoint)
/**
* @param IcingaEndpoint $endpoint
* @return Link
* @throws \Icinga\Exception\ProgrammingError
* @throws \Icinga\Exception\IcingaException
*/
protected function linkToEndpoint(IcingaEndpoint $endpoint)
{
Expand All @@ -143,8 +136,7 @@ protected function linkToEndpoint(IcingaEndpoint $endpoint)
}

/**
* @throws \Icinga\Exception\IcingaException
* @throws \Icinga\Exception\ProgrammingError
* @throws \Icinga\Exception\NotFoundError
*/
public function statusAction()
{
Expand All @@ -157,10 +149,40 @@ public function statusAction()
));
}

/**
* @throws \Icinga\Exception\NotFoundError
*/
public function packagesAction()
{
$db = $this->db();
$endpointName = $this->params->get('endpoint');
$package = $this->params->get('package');
$stage = $this->params->get('stage');
$file = $this->params->get('file');
if ($endpointName === null) {
$endpoint = null;
} else {
$endpoint = IcingaEndpoint::load($endpointName, $db);
}
if ($endpoint === null) {
$this->addSingleTab($this->translate('Inspect Packages'));
} elseif ($file !== null) {
$this->addSingleTab($this->translate('Inspect File Content'));
} else {
$this->tabs(
new ObjectTabs('endpoint', $this->Auth(), $endpoint)
)->activate('packages');
}
$widget = new InspectPackages($this->db(), 'director/inspect/packages');
$this->addTitle($widget->getTitle($endpoint, $package, $stage, $file));
if ($file === null) {
$this->actions()->add($widget->getBreadCrumb($endpoint, $package, $stage));
}
$this->content()->add($widget->getContent($endpoint, $package, $stage, $file));
}

/**
* @return IcingaEndpoint
* @throws \Icinga\Exception\ConfigurationError
* @throws \Icinga\Exception\IcingaException
* @throws \Icinga\Exception\NotFoundError
*/
protected function endpoint()
Expand Down
1 change: 1 addition & 0 deletions doc/82-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ next (will be 1.8.0)
### User Interface
* FIX: It's now possible to set Endpoint ports > 32767 on PostgreSQL (#928)
* FEATURE: Data Fields can now be grouped into categories (#1969)
* FEATURE: Inspect is now available for Packages, Stages and Files (#1995)

1.7.2
-----
Expand Down
24 changes: 15 additions & 9 deletions library/Director/Core/CoreApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,24 +708,30 @@ public function wipeInactiveStages(Db $db)
}
}

public function listStageFiles($stage)
public function listStageFiles($stage, $packageName = null)
{
if ($packageName === null) {
$packageName = $this->getPackageName();
}
return array_keys(
$this->client()->get(sprintf(
$this->client()->get(\sprintf(
'config/stages/%s/%s',
urlencode($this->getPackageName()),
urlencode($stage)
\urlencode($packageName),
\urlencode($stage)
))->getResult('name', array('type' => 'file'))
);
}

public function getStagedFile($stage, $file)
public function getStagedFile($stage, $file, $packageName = null)
{
return $this->client()->getRaw(sprintf(
if ($packageName === null) {
$packageName = $this->getPackageName();
}
return $this->client()->getRaw(\sprintf(
'config/files/%s/%s/%s',
urlencode($this->getPackageName()),
urlencode($stage),
urlencode($file)
\urlencode($packageName),
\urlencode($stage),
\urlencode($file)
));
}

Expand Down
20 changes: 20 additions & 0 deletions library/Director/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,26 @@ public function hasDeploymentEndpoint()
return $db->fetchOne($query) > 0;
}

public function getEndpointNamesInDeploymentZone()
{
$db = $this->db();
$query = $db->select()->from(
array('z' => 'icinga_zone'),
array('object_name' => 'e.object_name')
)->join(
array('e' => 'icinga_endpoint'),
'e.zone_id = z.id',
array()
)->join(
array('au' => 'icinga_apiuser'),
'e.apiuser_id = au.id',
array()
)->where('z.object_name = ?', $this->getMasterZoneName())
->order('e.object_name ASC');

return $db->fetchCol($query) ?: [];
}

public function getDeploymentEndpointName()
{
$db = $this->db();
Expand Down
5 changes: 5 additions & 0 deletions library/Director/Web/Tabs/ObjectTabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ protected function addTabsForExistingObject()
'urlParams' => ['endpoint' => $object->getObjectName()],
'label' => $this->translate('Inspect')
]);
$this->add('packages', [
'url' => 'director/inspect/packages',
'urlParams' => ['endpoint' => $object->getObjectName()],
'label' => $this->translate('Packages')
]);
}

if ($object->getShortTableName() === 'host') {
Expand Down
174 changes: 174 additions & 0 deletions library/Director/Web/Widget/InspectPackages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

namespace Icinga\Module\Director\Web\Widget;

use gipfl\IcingaWeb2\Link;
use gipfl\Translation\TranslationHelper;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaEndpoint;
use ipl\Html\Html;
use ipl\Html\Table;

class InspectPackages
{
use TranslationHelper;

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

/** @var string */
protected $baseUrl;

public function __construct(Db $db, $baseUrl)
{
$this->db = $db;
$this->baseUrl = $baseUrl;
}

public function getContent(IcingaEndpoint $endpoint = null, $package = null, $stage = null, $file = null)
{
if ($endpoint === null) {
return $this->getRootEndpoints();
} elseif ($package === null) {
return $this->getPackages($endpoint);
} elseif ($stage === null) {
return $this->getStages($endpoint, $package);
} elseif ($file === null) {
return $this->getFiles($endpoint, $package, $stage);
} else {
return $this->getFile($endpoint, $package, $stage, $file);
}
}

public function getTitle(IcingaEndpoint $endpoint = null, $package = null, $stage = null, $file = null)
{
if ($endpoint === null) {
return $this->translate('Endpoint in your Root Zone');
} elseif ($package === null) {
return \sprintf($this->translate('Packages on Endpoint: %s'), $endpoint->getObjectName());
} elseif ($stage === null) {
return \sprintf($this->translate('Stages in Package: %s'), $package);
} elseif ($file === null) {
return \sprintf($this->translate('Files in Stage: %s'), $stage);
} else {
return \sprintf($this->translate('File Content: %s'), $file);
}
}

public function getBreadCrumb(IcingaEndpoint $endpoint = null, $package = null, $stage = null)
{
$parts = [
'endpoint' => $endpoint === null ? null : $endpoint->getObjectName(),
'package' => $package,
'stage' => $stage,
];

$params = [];
// No root zone link for now:
// $result = [Link::create($this->translate('Root Zone'), $this->baseUrl)];
$result = [Html::tag('a', ['href' => '#'], $this->translate('Root Zone'))];
foreach ($parts as $name => $value) {
if ($value === null) {
break;
}
$params[$name] = $value;
$result[] = Link::create($value, $this->baseUrl, $params);
}

return Html::tag('ul', ['class' => 'breadcrumb'], Html::wrapEach($result, 'li'));
}

protected function getRootEndpoints()
{
$table = $this->prepareTable();
foreach ($this->db->getEndpointNamesInDeploymentZone() as $name) {
$table->add(Table::row([
Link::create($name, $this->baseUrl, [
'endpoint' => $name,
])
]));
}

return $table;
}

protected function getPackages(IcingaEndpoint $endpoint)
{
$table = $this->prepareTable();
$api = $endpoint->api();
foreach ($api->getPackages() as $package) {
$table->add(Table::row([
Link::create($package->name, $this->baseUrl, [
'endpoint' => $endpoint->getObjectName(),
'package' => $package->name,
])
]));
}

return $table;
}

protected function getStages(IcingaEndpoint $endpoint, $packageName)
{
$table = $this->prepareTable();
$api = $endpoint->api();
foreach ($api->getPackages() as $package) {
if ($package->name !== $packageName) {
continue;
}
foreach ($package->stages as $stage) {
$label = [$stage];
if ($stage === $package->{'active-stage'}) {
$label[] = Html::tag('small', [' (', $this->translate('active'), ')']);
}

$table->add(Table::row([
Link::create($label, $this->baseUrl, [
'endpoint' => $endpoint->getObjectName(),
'package' => $package->name,
'stage' => $stage
])
]));
}
}

return $table;
}

protected function getFiles(IcingaEndpoint $endpoint, $package, $stage)
{
$table = $this->prepareTable();
$table->getAttributes()->set('data-base-target', '_next');
foreach ($endpoint->api()->listStageFiles($stage, $package) as $filename) {
$table->add($table->row([
Link::create($filename, $this->baseUrl, [
'endpoint' => $endpoint->getObjectName(),
'package' => $package,
'stage' => $stage,
'file' => $filename
])
]));
}

return $table;
}

protected function getFile(IcingaEndpoint $endpoint, $package, $stage, $file)
{
return Html::tag('pre', $endpoint->api()->getStagedFile($stage, $file, $package));
}

protected function prepareTable($headerCols = [])
{
$table = new Table();
$table->addAttributes([
'class' => ['common-table', 'table-row-selectable'],
'data-base-target' => '_self'
]);
if (! empty($headerCols)) {
$table->add($table::row($headerCols, null, 'th'));
}

return $table;
}
}
Loading

0 comments on commit e871ba4

Please sign in to comment.