Servicesset: add controller, form and table

This commit is contained in:
Thomas Gelf 2016-10-13 09:13:21 +00:00
parent 6a45d9507d
commit 696e63b4ec
6 changed files with 208 additions and 4 deletions

View File

@ -0,0 +1,42 @@
<?php
namespace Icinga\Module\Director\Controllers;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaServiceSet;
use Icinga\Module\Director\Web\Controller\ObjectController;
class ServicesetController extends ObjectController
{
protected $host;
public function init()
{
if ($host = $this->params->get('host')) {
$this->host = IcingaHost::load($host, $this->db());
}
parent::init();
}
protected function loadObject()
{
if ($this->object === null) {
if ($name = $this->params->get('name')) {
$params = array('object_name' => $name);
$db = $this->db();
if ($this->host) {
$this->view->host = $this->host;
$params['host_id'] = $this->host->id;
}
$this->object = IcingaServiceSet::load($params, $db);
} else {
parent::loadObject();
}
}
return $this->object;
}
}

View File

@ -0,0 +1,71 @@
<?php
namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Object\IcingaHost;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
class IcingaServiceSetForm extends DirectorObjectForm
{
protected $host;
public function setup()
{
$this->addImportsElement();
$this->addElement('text', 'object_name', array(
'label' => $this->translate('Service set name'),
'description' => $this->translate(
'A short name identifying this set of ser'
),
'required' => true,
));
$this->addElement('textarea', 'description', array(
'label' => $this->translate('Description'),
'description' => $this->translate(
'A meaningful description explaining your users what to expect'
. ' when assigning this set of services'
),
'rows' => '3',
'required' => ! $this->isTemplate(),
));
if ($this->host === null) {
$this->addHidden('object_type', 'object');
$this->addElement('multiselect', 'service', array(
'label' => $this->translate('Services'),
'description' => $this->translate(
'Services in this set'
),
'rows' => '5',
'multiOptions' => $this->enumServices(),
'required' => true,
));
} else {
$this->addHidden('object_type', 'object');
$this->addHidden('host_id', $this->host->id);
}
$this->setButtons();
}
public function setHost(IcingaHost $host)
{
$this->host = $host;
return $this;
}
protected function enumServices()
{
$db = $this->db->getDbAdapter();
$query = $db->select()
->from('icinga_service', 'object_name')
->where('object_type = ?', 'template');
$names = $db->fetchCol($query);
return array_combine($names, $names);
}
}

View File

@ -0,0 +1,62 @@
<?php
namespace Icinga\Module\Director\Tables;
use Icinga\Module\Director\Web\Table\IcingaObjectTable;
class IcingaServiceSetTable extends IcingaObjectTable
{
public function getColumns()
{
return array(
'id' => 'sset.id',
'name' => 'sset.object_name',
'object_type' => 'sset.object_type',
'description' => 'sset.description',
'host_name' => 'h.object_name',
);
}
public function getTitles()
{
$view = $this->view();
return array(
'name' => $view->translate('Service set'),
);
}
protected function getActionUrl($row)
{
// TODO: Remove once we got a separate apply table
if ($row->object_type === 'apply') {
$params['id'] = $row->id;
} else {
$params = array('name' => $row->name);
if ($row->host_name) {
$params['host'] = $row->host_name;
}
}
return $this->url('director/serviceset', $params);
}
protected function getUnfilteredQuery()
{
$db = $this->connection()->getConnection();
$query = $db->select()->from(
array('sset' => 'icinga_service_set'),
array()
)->joinLeft(
array('h' => 'icinga_host'),
'h.id = sset.host_id',
array()
)->order('sset.object_name');
return $query;
}
public function getBaseQuery()
{
return $this->getUnfilteredQuery();
}
}

View File

@ -2071,6 +2071,8 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$type = 'timePeriod';
} elseif ($type === 'servicegroup') {
$type = 'serviceGroup';
} elseif ($type === 'service_set') {
$type = 'serviceSet';
} elseif ($type === 'apiuser') {
$type = 'apiUser';
}

View File

@ -26,7 +26,7 @@ abstract class ObjectController extends ActionController
{
parent::init();
$type = $this->getType();
$type = strtolower($this->getType());
if ($object = $this->loadObject()) {
$this->beforeTabs();
@ -61,7 +61,6 @@ abstract class ObjectController extends ActionController
'label' => $this->translate('Fields')
));
}
} else {
$this->beforeTabs();
$this->getTabs()->add('add', array(
@ -301,8 +300,8 @@ abstract class ObjectController extends ActionController
{
// Strip final 's' and upcase an eventual 'group'
return preg_replace(
array('/group$/', '/period$/', '/argument$/', '/apiuser$/'),
array('Group', 'Period', 'Argument', 'ApiUser'),
array('/group$/', '/period$/', '/argument$/', '/apiuser$/', '/set$/'),
array('Group', 'Period', 'Argument', 'ApiUser', 'Set'),
$this->getRequest()->getControllerName()
);
}

View File

@ -67,6 +67,13 @@ abstract class ObjectsController extends ActionController
));
}
if ($object->supportsSets() || $object->isGroup() /** Bullshit, need base object, wrong on users */) {
$tabs->add('sets', array(
'url' => sprintf('director/%ss/sets', $type),
'label' => $this->translate('Sets')
));
}
$tabs->add('tree', array(
'url' => sprintf('director/%ss/templatetree', $type),
'label' => $this->translate('Tree'),
@ -246,6 +253,27 @@ abstract class ObjectsController extends ActionController
$this->setViewScript('objects/tree');
}
public function setsAction()
{
$this->view->title = $this->translate('Service sets');
$this->view->table = $this
->loadTable('IcingaServiceSet')
->setConnection($this->db());
$this->view->addLink = $this->view->qlink(
$this->translate('Add'),
'director/serviceset/add',
null,
array(
'class' => 'icon-plus',
'data-base-target' => '_next'
)
);
$this->getTabs()->activate('sets');
$this->setViewScript('objects/table');
}
protected function dummyObject()
{
if ($this->dummy === null) {