HostController: code cleanup, little fixes, tabs

This commit is contained in:
Thomas Gelf 2017-07-24 10:52:36 +02:00
parent 5660be3076
commit c295218a5c
2 changed files with 65 additions and 74 deletions

View File

@ -8,6 +8,7 @@ use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Db\AppliedServiceSetLoader; use Icinga\Module\Director\Db\AppliedServiceSetLoader;
use Icinga\Module\Director\Forms\IcingaForgetApiKeyForm; use Icinga\Module\Director\Forms\IcingaForgetApiKeyForm;
use Icinga\Module\Director\Forms\IcingaGenerateApiKeyForm; use Icinga\Module\Director\Forms\IcingaGenerateApiKeyForm;
use Icinga\Module\Director\Forms\IcingaServiceForm;
use Icinga\Module\Director\IcingaConfig\AgentWizard; use Icinga\Module\Director\IcingaConfig\AgentWizard;
use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService; use Icinga\Module\Director\Objects\IcingaService;
@ -28,17 +29,15 @@ class HostController extends ObjectController
if ($this->object) { if ($this->object) {
$tabs = $this->tabs(); $tabs = $this->tabs();
$name = $this->object->getObjectName(); $name = $this->object->getObjectName();
$tabs->add('services', array( $tabs->add('services', [
'url' => 'director/host/services', 'url' => 'director/host/services',
'urlParams' => array('name' => $name), 'urlParams' => ['name' => $name],
'label' => 'Services' 'label' => 'Services'
)); ])->add('agent', [
$tabs->add('agent', array(
'url' => 'director/host/agent', 'url' => 'director/host/agent',
'urlParams' => array('name' => $name), 'urlParams' => ['name' => $name],
'label' => 'Agent' 'label' => 'Agent'
)); ]);
} }
} }
@ -49,9 +48,7 @@ class HostController extends ObjectController
protected function loadRestrictions() protected function loadRestrictions()
{ {
return array( return [$this->getHostgroupRestriction()];
$this->getHostgroupRestriction()
);
} }
protected function getHostgroupRestriction() protected function getHostgroupRestriction()
@ -59,15 +56,6 @@ class HostController extends ObjectController
return new HostgroupRestriction($this->db(), $this->Auth()); return new HostgroupRestriction($this->db(), $this->Auth());
} }
/**
* @param IcingaHost $object
* @return bool
*/
/* protected function allowsObject(IcingaObject $object)
{
return $this->getHostgroupRestriction()->allowsHost($object);
}
*/
public function editAction() public function editAction()
{ {
parent::editAction(); parent::editAction();
@ -78,11 +66,11 @@ class HostController extends ObjectController
$this->actions()->add(Link::create( $this->actions()->add(Link::create(
$this->translate('Show'), $this->translate('Show'),
'monitoring/host/show', 'monitoring/host/show',
array('host' => $host->object_name), ['host' => $host->getObjectName()],
array( [
'class' => 'icon-globe critical', 'class' => 'icon-globe critical',
'data-base-target' => '_next' 'data-base-target' => '_next'
) ]
)); ));
} }
} catch (Exception $e) { } catch (Exception $e) {
@ -93,20 +81,21 @@ class HostController extends ObjectController
public function servicesAction() public function servicesAction()
{ {
$db = $this->db(); $db = $this->db();
$host = $this->object; $host = $this->getHostObject();
$hostname = $host->getObjectName();
$this->tabs()->activate('services'); $this->tabs()->activate('services');
$this->addTitle($this->translate('Services: %s'), $host->object_name); $this->addTitle($this->translate('Services: %s'), $host->getObjectName());
$this->actions()->add(Link::create( $this->actions()->add(Link::create(
$this->translate('Add service'), $this->translate('Add service'),
'director/service/add', 'director/service/add',
['host' => $host->object_name], ['host' => $hostname],
['class' => 'icon-plus'] ['class' => 'icon-plus']
))->add(Link::create( ))->add(Link::create(
$this->translate('Add service set'), $this->translate('Add service set'),
'director/serviceset/add', 'director/serviceset/add',
['host' => $host->object_name], ['host' => $hostname],
['class' => 'icon-plus'] ['class' => 'icon-plus']
)); ));
@ -225,19 +214,18 @@ class HostController extends ObjectController
public function appliedserviceAction() public function appliedserviceAction()
{ {
$db = $this->db(); $db = $this->db();
/** @var IcingaHost $host */ $host = $this->getHostObject();
$host = $this->object;
$serviceId = $this->params->get('service_id'); $serviceId = $this->params->get('service_id');
$parent = IcingaService::loadWithAutoIncId($serviceId, $db); $parent = IcingaService::loadWithAutoIncId($serviceId, $db);
$serviceName = $parent->object_name; $serviceName = $parent->getObjectName();
$service = IcingaService::create(array( $service = IcingaService::create([
'imports' => $parent, 'imports' => $parent,
'object_type' => 'apply', 'object_type' => 'apply',
'object_name' => $serviceName, 'object_name' => $serviceName,
'host_id' => $host->id, 'host_id' => $host->id,
'vars' => $host->getOverriddenServiceVars($serviceName), 'vars' => $host->getOverriddenServiceVars($serviceName),
), $db); ], $db);
$this->addTitle( $this->addTitle(
$this->translate('Applied service: %s'), $this->translate('Applied service: %s'),
@ -245,11 +233,12 @@ class HostController extends ObjectController
); );
$this->content()->add( $this->content()->add(
$this->loadForm('IcingaService') IcingaServiceForm::load()
->setDb($db) ->setDb($db)
->setHost($host) ->setHost($host)
->setApplyGenerated($parent) ->setApplyGenerated($parent)
->setObject($service) ->setObject($service)
->handleRequest()
); );
$this->commonForServices(); $this->commonForServices();
@ -258,41 +247,37 @@ class HostController extends ObjectController
public function inheritedserviceAction() public function inheritedserviceAction()
{ {
$db = $this->db(); $db = $this->db();
$host = $this->object; $host = $this->getHostObject();
$serviceName = $this->params->get('service'); $serviceName = $this->params->get('service');
$from = IcingaHost::load($this->params->get('inheritedFrom'), $this->db()); $from = IcingaHost::load($this->params->get('inheritedFrom'), $this->db());
$parent = IcingaService::load( $parent = IcingaService::load([
array( 'object_name' => $serviceName,
'object_name' => $serviceName, 'host_id' => $from->id
'host_id' => $from->id ], $this->db());
),
$this->db()
);
// TODO: we want to eventually show the host template name, doesn't work // TODO: we want to eventually show the host template name, doesn't work
// as template resolution would break. // as template resolution would break.
// $parent->object_name = $from->object_name; // $parent->object_name = $from->object_name;
$service = IcingaService::create(array( $service = IcingaService::create([
'object_type' => 'apply', 'object_type' => 'apply',
'object_name' => $serviceName, 'object_name' => $serviceName,
'host_id' => $host->id, 'host_id' => $host->id,
'imports' => array($parent), 'imports' => [$parent],
'vars' => $host->getOverriddenServiceVars($serviceName), 'vars' => $host->getOverriddenServiceVars($serviceName),
), $db); ], $db);
$this->addTitle($this->translate('Inherited service: %s'), $serviceName); $this->addTitle($this->translate('Inherited service: %s'), $serviceName);
$form = $this->loadForm('IcingaService')
$form = IcingaServiceForm::load()
->setDb($db) ->setDb($db)
->setHost($host) ->setHost($host)
->setInheritedFrom($from->object_name) ->setInheritedFrom($from->object_name)
->setObject($service); ->setObject($service)
$form->handleRequest(); ->handleRequest();
$this->content()->add($form); $this->content()->add($form);
$this->commonForServices(); $this->commonForServices();
// TODO: figure out whether this has any effect
// $form->setResolvedImports();
} }
public function removesetAction() public function removesetAction()
@ -322,25 +307,21 @@ class HostController extends ObjectController
public function servicesetserviceAction() public function servicesetserviceAction()
{ {
$db = $this->db(); $db = $this->db();
/** @var IcingaHost $host */ $host = $this->getHostObject();
$host = $this->object;
$serviceName = $this->params->get('service'); $serviceName = $this->params->get('service');
$set = IcingaServiceSet::load($this->params->get('set'), $db); $set = IcingaServiceSet::load($this->params->get('set'), $db);
$service = IcingaService::load( $service = IcingaService::load([
array( 'object_name' => $serviceName,
'object_name' => $serviceName, 'service_set_id' => $set->get('id')
'service_set_id' => $set->get('id') ], $this->db());
), $service = IcingaService::create([
$this->db()
);
$service = IcingaService::create(array(
'object_type' => 'apply', 'object_type' => 'apply',
'object_name' => $serviceName, 'object_name' => $serviceName,
'host_id' => $host->id, 'host_id' => $host->id,
'imports' => array($service), 'imports' => [$service],
'vars' => $host->getOverriddenServiceVars($serviceName), 'vars' => $host->getOverriddenServiceVars($serviceName),
), $db); ], $db);
// $set->copyVarsToService($service); // $set->copyVarsToService($service);
$this->addTitle( $this->addTitle(
@ -350,15 +331,14 @@ class HostController extends ObjectController
$set->getObjectName() $set->getObjectName()
); );
$form = $this->loadForm('IcingaService') $form = IcingaServiceForm::load()
->setDb($db) ->setDb($db)
->setHost($host) ->setHost($host)
->setServiceSet($set) ->setServiceSet($set)
->setObject($service); ->setObject($service)
$form->handleRequest(); ->handleRequest();
$this->getTabs()->activate('services'); $this->tabs()->activate('services');
$this->content()->add($form); $this->content()->add($form);
// $form->setResolvedImports();
$this->commonForServices(); $this->commonForServices();
} }
@ -368,7 +348,7 @@ class HostController extends ObjectController
$this->actions()->add(Link::create( $this->actions()->add(Link::create(
$this->translate('back'), $this->translate('back'),
'director/host/services', 'director/host/services',
['name' => $host->object_name], ['name' => $host->getObjectName()],
['class' => 'icon-left-big'] ['class' => 'icon-left-big']
)); ));
$this->tabs()->activate('services'); $this->tabs()->activate('services');
@ -460,7 +440,6 @@ class HostController extends ObjectController
'This will invalidate the former key' 'This will invalidate the former key'
)), )),
]); ]);
} else {
} }
$c->add([ $c->add([
@ -510,6 +489,7 @@ class HostController extends ObjectController
protected function showLegacyAgentInstructions() protected function showLegacyAgentInstructions()
{ {
$host = $this->getHostObject();
$c = $this->content(); $c = $this->content();
$docBaseUrl = 'https://docs.icinga.com/icinga2/latest/doc/module/icinga2/chapter/distributed-monitoring'; $docBaseUrl = 'https://docs.icinga.com/icinga2/latest/doc/module/icinga2/chapter/distributed-monitoring';
$sectionSetup = 'distributed-monitoring-setup-satellite-client'; $sectionSetup = 'distributed-monitoring-setup-satellite-client';
@ -528,11 +508,11 @@ class HostController extends ObjectController
)); ));
$this->addTitle('Agent deployment instructions'); $this->addTitle('Agent deployment instructions');
$certname = $this->object->getObjectName(); $certname = $host->getObjectName();
try { try {
$ticket = Util::getIcingaTicket($certname, $this->api()->getTicketSalt()); $ticket = Util::getIcingaTicket($certname, $this->api()->getTicketSalt());
$wizard = new AgentWizard($this->object); $wizard = new AgentWizard($host);
$wizard->setTicketSalt($this->api()->getTicketSalt()); $wizard->setTicketSalt($this->api()->getTicketSalt());
} catch (Exception $e) { } catch (Exception $e) {
$c->add(Html::p(['class' => 'error'], sprintf( $c->add(Html::p(['class' => 'error'], sprintf(
@ -607,20 +587,19 @@ class HostController extends ObjectController
$host = $this->object; $host = $this->object;
if ($host->getResolvedProperty('has_agent') !== 'y') { if ($host->getResolvedProperty('has_agent') !== 'y') {
throw new NotFoundError('The host "%s" is not an agent', $host->object_name); throw new NotFoundError('The host "%s" is not an agent', $host->getObjectName());
} }
$this->sendJson( $this->sendJson(
$this->getResponse(), $this->getResponse(),
Util::getIcingaTicket( Util::getIcingaTicket(
$host->object_name, $host->getObjectName(),
$this->api()->getTicketSalt() $this->api()->getTicketSalt()
) )
); );
return; } else {
parent::handleApiRequest();
} }
parent::handleApiRequest();
} }
public function ticketAction() public function ticketAction()
@ -629,4 +608,12 @@ class HostController extends ObjectController
throw new NotFoundError('Not found'); throw new NotFoundError('Not found');
} }
} }
/**
* @return IcingaHost
*/
protected function getHostObject()
{
return $this->object;
}
} }

View File

@ -909,6 +909,10 @@ abstract class DirectorObjectForm extends DirectorForm
)); ));
} }
/**
* @param Db $db
* @return $this
*/
public function setDb(Db $db) public function setDb(Db $db)
{ {
if ($this->object !== null) { if ($this->object !== null) {