Notifications: should be subject to apply rules

This commit is contained in:
Thomas Gelf 2016-05-19 15:06:05 +02:00
parent a8808d53c0
commit 0769b720b8
3 changed files with 167 additions and 0 deletions

View File

@ -3,7 +3,83 @@
namespace Icinga\Module\Director\Controllers;
use Icinga\Module\Director\Web\Controller\ObjectController;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaNotification;
use Icinga\Module\Director\Objects\IcingaService;
class NotificationController extends ObjectController
{
public function init()
{
parent::init();
if ($this->object && $this->object->object_type === 'apply') {
$this->getTabs()->add('assign', array(
'url' => 'director/notification/assign',
'urlParams' => $this->object->getUrlParams(),
'label' => 'Assign'
));
if ($host = $this->params->get('host')) {
foreach ($this->getTabs()->getTabs() as $tab) {
$tab->getUrl()->setParam('host', $host);
}
}
if ($service = $this->params->get('service')) {
foreach ($this->getTabs()->getTabs() as $tab) {
$tab->getUrl()->setParam('service', $service);
}
}
}
}
public function assignAction()
{
$this->getTabs()->activate('assign');
$this->view->form = $form = $this->loadForm('icingaNotificationAssignment');
$form
->setIcingaObject($this->object)
->setDb($this->db());
if ($id = $this->params->get('rule_id')) {
$this->view->actionLinks = $this->view->qlink(
$this->translate('back'),
$this->getRequest()->getUrl()->without('rule_id'),
null,
array('class' => 'icon-left-big')
);
$form->loadObject($id);
}
$form->handleRequest();
$this->view->table = $this->loadTable('icingaObjectAssignment')
->setObject($this->object);
$this->view->title = 'Assign notification';
$this->render('object/fields', null, true); // TODO: render table
}
protected function loadObject()
{
if ($this->object === null) {
if ($name = $this->params->get('name')) {
$params = array('object_name' => $name);
$db = $this->db();
if ($hostname = $this->params->get('host')) {
$this->view->host = IcingaHost::load($hostname, $db);
$params['host_id'] = $this->view->host->id;
}
if ($service = $this->params->get('service')) {
$this->view->service = IcingaService::load($service, $db);
$params['service_id'] = $this->view->service->id;
}
$this->object = IcingaNotification::load($params, $db);
} else {
parent::loadObject();
}
}
return $this->object;
}
}

View File

@ -27,6 +27,7 @@ class IcingaNotificationForm extends DirectorObjectForm
->addIntervalElement()
->addPeriodElement()
->addTimesElements()
->addAssignmentElements()
->addDisabledElement()
->addCommandElements()
->addEventFilterElements()
@ -34,6 +35,22 @@ class IcingaNotificationForm extends DirectorObjectForm
->setButtons();
}
protected function addAssignmentElements()
{
if (!$this->object || !$this->object->isApplyRule()) {
return $this;
}
$sub = new AssignListSubForm();
$sub->setObject($this->getObject());
$sub->setup();
$sub->setOrder(30);
$this->addSubForm($sub, 'assignlist');
return $this;
}
protected function addUsersElement()
{
$users = $this->enumUsers();

View File

@ -19,6 +19,11 @@ class IcingaNotificationTable extends IcingaObjectTable
);
}
protected function listTableClasses()
{
return array_merge(array('assignment-table'), parent::listTableClasses());
}
protected function getActionUrl($row)
{
return $this->url('director/notification', array('id' => $row->id));
@ -32,6 +37,75 @@ class IcingaNotificationTable extends IcingaObjectTable
);
}
protected function renderRow($row)
{
$v = $this->view();
$extra = $this->appliedOnes($row->id);
$htm = " <tr" . $this->getRowClassesString($row) . ">\n";
$htm .= '<td>' . $v->qlink($row->notification, $this->getActionUrl($row));
if (empty($extra)) {
$htm .= ' ' . $v->qlink(
'Create apply-rule',
'director/notification/add',
array('apply' => $row->notification),
array('class' => 'icon-plus')
);
} else {
$htm .= '. Related apply rules: <ul class="apply-rules">';
foreach ($extra as $id => $notification) {
$htm .= '<li>'
. $v->qlink($notification, 'director/notification', array('id' => $id))
. '</li>';
}
$htm .= '</ul>';
$htm .= $v->qlink(
'Add more',
'director/notification/add',
array('apply' => $row->notification),
array('class' => 'icon-plus')
);
}
$htm .= '</td>';
return $htm . " </tr>\n";
}
protected function appliedOnes($id)
{
if ($this->connection()->isPgsql()) {
$nameCol = "s.object_name || COALESCE(': ' || ARRAY_TO_STRING(ARRAY_AGG("
. "a.assign_type || ' where ' || a.filter_string"
. " ORDER BY a.assign_type, a.filter_string), ', '), '')";
} else {
$nameCol = "s.object_name || COALESCE(': ' || GROUP_CONCAT("
. "a.assign_type || ' where ' || a.filter_string"
. " ORDER BY a.assign_type, a.filter_string SEPARATOR ', '"
. "), '')";
}
$db = $this->connection()->getConnection();
$query = $db->select()->from(
array('s' => 'icinga_notification'),
array(
'id' => 's.id',
'objectname' => $nameCol,
)
)->join(
array('i' => 'icinga_notification_inheritance'),
'i.notification_id = s.id',
array()
)->where('i.parent_notification_id = ?', $id)
->where('s.object_type = ?', 'apply');
$query->joinLeft(
array('a' => 'icinga_notification_assignment'),
'a.notification_id = s.id',
array()
)->group('s.id');
return $db->fetchPairs($query);
}
public function getUnfilteredQuery()
{
$db = $this->connection()->getConnection();