Initial rough abstractions, basic forms

fixes #9134
This commit is contained in:
Thomas Gelf 2015-04-24 14:26:44 +02:00
parent 8e916ff66b
commit 332ec1da4b
14 changed files with 897 additions and 0 deletions

View File

@ -0,0 +1,54 @@
<?php
use Icinga\Module\Director\ActionController;
class Director_ObjectController extends ActionController
{
public function hostAction()
{
$this->view->form = $this->loadForm('icingaHost')
->setDb($this->db())
->setSuccessUrl('director/list/hosts');
if ($id = $this->params->get('id')) {
$this->view->form->loadObject($id);
$this->view->title = $this->translate('Modify Icinga Host');
} else {
$this->view->title = $this->translate('Add new Icinga Host');
}
$this->view->form->handleRequest();
$this->render('form');
}
public function commandAction()
{
$this->view->form = $this->loadForm('icingaCommand')
->setDb($this->db())
->setSuccessUrl('director/list/commands');
if ($id = $this->params->get('id')) {
$this->view->form->loadObject($id);
$this->view->title = $this->translate('Modify Icinga Command');
} else {
$this->view->title = $this->translate('Add new Icinga Command');
}
$this->view->form->handleRequest();
$this->render('form');
}
public function zoneAction()
{
$this->view->form = $this->loadForm('icingaZone')
->setDb($this->db())
->setSuccessUrl('director/list/zones');
if ($id = $this->params->get('id')) {
$this->view->title = $this->translate('Modify Icinga Zone');
$this->view->form->loadObject($id);
} else {
$this->view->title = $this->translate('Add new Icinga Zone');
}
$this->view->form->handleRequest();
$this->render('form');
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
class IcingaCommandForm extends DirectorObjectForm
{
public function setup()
{
$this->addElement('select', 'methods_execute', array(
'label' => $this->translate('Command type'),
'description' => $this->translate('Whether this should be a template'),
'multiOptions' => array(
null => '- please choose -',
'PluginCheck' => 'Plugin Check Command',
'PluginNotification' => 'Notification Plugin Command',
'IcingaCheck' => 'Icinga Check Command',
'ClusterCheck' => 'Icinga Cluster Command',
'ClusterZoneCheck' => 'Icinga Cluster Zone Check Command',
'CrlCheck' => 'Crl Check Command',
),
'class' => 'autosubmit'
));
$this->addElement('text', 'object_name', array(
'label' => $this->translate('Command name'),
'required' => true,
'description' => $this->translate('Identifier for the Icinga command you are going to create')
));
$this->addElement('text', 'timeout', array(
'label' => $this->translate('Timeout'),
'description' => $this->translate('Optional command timeout')
));
$this->addElement('select', 'zone_id', array(
'label' => $this->translate('Cluster Zone'),
'description' => $this->translate('Provide this command only to this specific Icinga cluster zone')
));
$this->addElement('select', 'object_type', array(
'label' => $this->translate('Object type'),
'required' => true,
'description' => $this->translate('Whether to create a command template or a command object'),
'multiOptions' => $this->optionalEnum(array(
'object' => $this->translate('Command object'),
'template' => $this->translate('Command template'),
))
));
$this->addElement('submit', $this->translate('Store'));
}
}

View File

@ -0,0 +1,95 @@
<?php
namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
class IcingaHostForm extends DirectorObjectForm
{
public function setup()
{
$isTemplate = isset($_POST['object_type']) && $_POST['object_type'] === 'template';
$this->addElement('select', 'object_type', array(
'label' => $this->translate('Object type'),
'description' => $this->translate('Whether this should be a template'),
'multiOptions' => array(
null => '- please choose -',
'object' => 'Host object',
'template' => 'Host template',
),
'class' => 'autosubmit'
));
if ($isTemplate) {
$this->addElement('text', 'object_name', array(
'label' => $this->translate('Host template name'),
'required' => true,
'description' => $this->translate('Name for the Icinga host template you are going to create')
));
} else {
$this->addElement('text', 'object_name', array(
'label' => $this->translate('Hostname'),
'required' => true,
'description' => $this->translate('Hostname for the Icinga host you are going to create')
));
}
$this->addElement('text', 'address', array(
'label' => $this->translate('Host address'),
'description' => $this->translate('Host address. Usually an IPv4 address, but may be any kind of address your check plugin is able to deal with')
));
$this->addElement('text', 'address6', array(
'label' => $this->translate('IPv6 address'),
'description' => $this->translate('Usually your hosts main IPv6 address')
));
$this->addElement('select', 'check_command_id', array(
'label' => $this->translate('Check command'),
'description' => $this->translate('Check command definition')
));
$this->optionalBoolean(
'enable_notifications',
$this->translate('Send notifications'),
$this->translate('Whether to send notifications for this host')
);
$this->optionalBoolean(
'enable_active_checks',
$this->translate('Execute active checks'),
$this->translate('Whether to actively check this host')
);
$this->optionalBoolean(
'enable_passive_checks',
$this->translate('Accept passive checks'),
$this->translate('Whether to accept passive check results for this host')
);
$this->optionalBoolean(
'enable_event_handler',
$this->translate('Enable event handler'),
$this->translate('Whether to enable event handlers this host')
);
$this->optionalBoolean(
'enable_perfdata',
$this->translate('Process performance data'),
$this->translate('Whether to process performance data provided by this host')
);
$this->optionalBoolean(
'volatile',
$this->translate('Volatile'),
$this->translate('Whether this check is volatile.')
);
$this->addElement('select', 'zone_id', array(
'label' => $this->translate('Cluster Zone'),
'description' => $this->translate('Check this host in this specific Icinga cluster zone')
));
$this->addElement('submit', $this->translate('Store'));
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
class IcingaZoneForm extends DirectorObjectForm
{
public function setup()
{
$this->addElement('select', 'object_type', array(
'label' => $this->translate('Object type'),
'description' => $this->translate('Whether this should be a template'),
'multiOptions' => $this->optionalEnum(array(
'object' => $this->translate('Zone object'),
'template' => $this->translate('Zone template'),
)),
));
$this->addElement('text', 'object_name', array(
'label' => $this->translate('Zone (template) name'),
'required' => true,
'description' => $this->translate('Name for the Icinga zone (templat) you are going to create')
));
$this->addElement('select', 'parent_zone_id', array(
'label' => $this->translate('Parent Zone'),
'description' => $this->translate('Chose an (optional) parent zone')
));
$this->addElement('submit', $this->translate('Store'));
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace Icinga\Module\Director;
use Icinga\Application\Icinga;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Web\Form\FormLoader;
use Icinga\Module\Director\Web\Table\TableLoader;
use Icinga\Web\Controller;
use Icinga\Web\Widget;
abstract class ActionController extends Controller
{
protected $db;
protected $forcedMonitoring = false;
public function init()
{
$m = Icinga::app()->getModuleManager();
if (! $m->hasLoaded('monitoring') && $m->hasInstalled('monitoring')) {
$m->loadModule('monitoring');
}
}
public function loadForm($name)
{
return FormLoader::load($name, $this->Module());
}
public function loadTable($name)
{
return TableLoader::load($name, $this->Module());
}
protected function setIcingaTabs()
{
$this->view->tabs = Widget::create('tabs')->add('services', array(
'label' => $this->translate('Services'),
'url' => 'director/list/services')
)->add('hosts', array(
'label' => $this->translate('Hosts'),
'url' => 'director/list/hosts')
);
return $this->view->tabs;
}
protected function db()
{
if ($this->db === null) {
$this->db = Db::fromResourceName($this->Config()->get('db', 'resource'));
}
return $this->db;
}
}

44
library/Director/Db.php Normal file
View File

@ -0,0 +1,44 @@
<?php
namespace Icinga\Module\Director;
use Icinga\Data\Db\DbConnection;
class Db extends DbConnection
{
protected $modules = array();
protected function db()
{
return $this->getDbAdapter();
}
public function enumCheckcommands()
{
$select = $this->db()->select()->from('icinga_command', array(
'id',
'object_name',
))->where('object_type IN (?)', array('object', 'external_object'))
->where('methods_execute IN (?)', array('PluginCheck', 'IcingaCheck'))
->order('object_name ASC');
return $this->db()->fetchPairs($select);
}
public function enumZones()
{
$select = $this->db()->select()->from('icinga_zone', array(
'id',
'object_name',
))->where('object_type', 'object')->order('object_name ASC');
return $this->db()->fetchPairs($select);
}
public function enumHosts()
{
$select = $this->db()->select()->from('icinga_host', array(
'id',
'object_name',
))->where('object_type', 'object')->order('object_name ASC');
return $this->db()->fetchPairs($select);
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Data\Db\DbObject;
class IcingaCommand extends DbObject
{
protected $table = 'icinga_command';
protected $keyName = 'id';
protected $autoincKeyName = 'id';
protected $defaultProperties = array(
'id' => null,
'object_name' => null,
'methods_execute' => null,
'command' => null,
'timeout' => null,
'zone_id' => null,
'object_type' => null,
);
}

View File

@ -0,0 +1,43 @@
<?php
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Data\Db\DbObject;
class IcingaHost extends DbObject
{
protected $table = 'icinga_host';
protected $keyName = 'id';
protected $autoincKeyName = 'id';
protected $defaultProperties = array(
'id' => null,
'object_name' => null,
'address' => null,
'address6' => null,
'check_command_id' => null,
'max_check_attempts' => null,
'check_period_id' => null,
'check_interval' => null,
'retry_interval' => null,
'enable_notifications' => null,
'enable_active_checks' => null,
'enable_passive_checks' => null,
'enable_event_handler' => null,
'enable_flapping' => null,
'enable_perfdata' => null,
'event_command_id' => null,
'flapping_threshold' => null,
'volatile' => null,
'zone_id' => null,
'command_endpoint_id' => null,
'notes' => null,
'notes_url' => null,
'action_url' => null,
'icon_image' => null,
'icon_image_alt' => null,
'object_type' => null,
);
}

View File

@ -0,0 +1,21 @@
<?php
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Data\Db\DbObject;
class IcingaZone extends DbObject
{
protected $table = 'icinga_zone';
protected $keyName = 'id';
protected $autoincKeyName = 'id';
protected $defaultProperties = array(
'id' => null,
'object_name' => null,
'object_type' => null,
'parent_zone_id' => null,
);
}

View File

@ -0,0 +1,53 @@
<?php
namespace Icinga\Module\Director\Web\Form;
class CsrfToken
{
/**
* Check whether the given token is valid
*
* @param string $token Token
*
* @return bool
*/
public static function isValid($token)
{
if (strpos($token, '|') === false) {
return false;
}
list($seed, $token) = explode('|', $elementValue);
if (!is_numeric($seed)) {
return false;
}
return $token === hash('sha256', self::getSessionId() . $seed);
}
/**
* Create a new token
*
* @return string
*/
public static function generate()
{
$seed = mt_rand();
list ($seed, $token) = hash('sha256', self::getSessionId() . $seed);
return sprintf('%s|%s', $seed, $token);
}
/**
* Get current session id
*
* TODO: we should do this through our App or Session object
*
* @return string
*/
protected static function getSessionId()
{
return session_id();
}
}

View File

@ -0,0 +1,126 @@
<?php
namespace Icinga\Module\Director\Web\Form;
abstract class DirectorObjectForm extends QuickForm
{
protected $db;
protected $object;
private $objectName;
private $className;
public function onSuccess()
{
if ($this->object) {
$this->object->setProperties($this->getValues())->store();
$this->redirectOnSuccess(
sprintf(
$this->translate('The Icinga %s has successfully been stored'),
$this->translate($this->getObjectName())
)
);
} else {
$class = $this->getObjectClassname();
$class::create($this->getValues())->store($this->db);
$this->redirectOnSuccess(
sprintf(
$this->translate('A new Icinga %s has successfully been created'),
$this->translate($this->getObjectName())
)
);
}
}
protected function optionalEnum($enum)
{
return array(
null => $this->translate('- please choose -')
) + $enum;
}
protected function optionalBoolean($key, $label, $description)
{
return $this->addElement('select', $key, array(
'label' => $label,
'description' => $description,
'multiOptions' => $this->selectBoolean()
));
}
protected function selectBoolean()
{
return array(
null => $this->translate('- not set -'),
'y' => $this->translate('Yes'),
'n' => $this->translate('No'),
);
}
public function hasElement($name)
{
return $this->getElement($name) !== null;
}
public function getObject()
{
return $this->object;
}
protected function getObjectClassname()
{
if ($this->className === null) {
return 'Icinga\\Module\\Director\\Objects\\'
. substr(join('', array_slice(explode('\\', get_class($this)), -1)), 0, -4);
}
return $this->className;
}
protected function getObjectname()
{
if ($this->objectName === null) {
return substr(join('', array_slice(explode('\\', get_class($this)), -1)), 6, -4);
}
return $this->objectName;
}
public function loadObject($id)
{
$class = $this->getObjectClassname();
$this->object = $class::load($id, $this->db);
$this->addHidden('id');
$this->setDefaults($this->object->getProperties());
return $this;
}
public function setDb($db)
{
$this->db = $db;
if ($this->hasElement('parent_zone_id')) {
$this->getElement('parent_zone_id')
->setMultiOptions($this->optionalEnum($db->enumZones()));
}
if ($this->hasElement('zone_id')) {
$this->getElement('zone_id')
->setMultiOptions($this->optionalEnum($db->enumZones()));
}
if ($this->hasElement('check_command_id')) {
$this->getElement('check_command_id')
->setMultiOptions($this->optionalEnum($db->enumCheckCommands()));
}
return $this;
}
private function dummyForTranslation()
{
$this->translate('Host');
$this->translate('Service');
$this->translate('Zone');
$this->translate('Command');
// ... TBC
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace Icinga\Module\Director\Web\Form;
use Icinga\Application\Icinga;
use Icinga\Application\Modules\Module;
use Icinga\Exception\ProgrammingError;
class FormLoader
{
public static function load($name, Module $module = null)
{
if ($module === null) {
$basedir = Icinga::app()->getApplicationDir('forms');
$ns = '\\Icinga\\Web\\Forms\\';
} else {
$basedir = $module->getFormDir();
$ns = '\\Icinga\\Module\\' . ucfirst($module->getName()) . '\\Forms\\';
}
if (preg_match('~^[a-z0-9/]+$~i', $name)) {
$parts = preg_split('~/~', $name);
$class = ucfirst(array_pop($parts)) . 'Form';
$file = sprintf('%s/%s/%s.php', rtrim($basedir, '/'), implode('/', $parts), $class);
if (file_exists($file)) {
require_once($file);
$class = $ns . $class;
return new $class();
}
}
throw new ProgrammingError(sprintf('Cannot load %s (%s), no such form', $name, $file));
}
}

View File

@ -0,0 +1,232 @@
<?php
namespace Icinga\Module\Director\Web\Form;
use Icinga\Application\Icinga;
use Icinga\Web\Notification;
use Icinga\Web\Request;
use Icinga\Web\Url;
use Zend_Form;
/**
* QuickForm wants to be a base class for simple forms
*/
abstract class QuickForm extends Zend_Form
{
const ID = '__FORM_NAME';
const CSRF = '__FORM_CSRF';
/**
* The name of this form
*/
protected $formName;
/**
* Whether the form has been sent
*/
protected $hasBeenSent;
/**
* Whether the form has been sent
*/
protected $hasBeenSubmitted;
/**
* The submit caption, element - still tbd
*/
// protected $submit;
/**
* Our request
*/
protected $request;
protected $successUrl;
protected $successMessage;
public function __construct($options = null)
{
parent::__construct($options);
$this->setMethod('post');
$this->setAction(Url::fromRequest());
$this->createIdElement();
$this->regenerateCsrfToken();
$this->setup();
}
protected function createIdElement()
{
$this->detectName();
$this->addHidden(self::ID, $this->getName());
$this->getElement(self::ID)->setIgnore(true);
}
public function regenerateCsrfToken()
{
if (! $element = $this->getElement(self::CSRF)) {
$this->addHidden(self::CSRF);
$element = $this->getElement(self::CSRF);
}
$element->setValue(CsrfToken::generate())->setIgnore(true);
return $this;
}
public function removeCsrfToken()
{
$this->removeElement(self::CSRF);
return $this;
}
public function addHidden($name, $value = null)
{
$this->addElement('hidden', $name);
$this->getElement($name)->setDecorators(array('ViewHelper'));
if ($value !== null) {
$this->setDefault($name, $value);
}
return $this;
}
public function setSuccessUrl($url)
{
$this->successUrl = $url;
return $this;
}
public function setup()
{
}
public function setAction($action)
{
if (! $action instanceof Url) {
$action = Url::fromPath($action);
}
return parent::setAction((string) $action);
}
public function hasBeenSubmitted()
{
return $this->hasBeenSent();
}
public function handleRequest(Request $request = null)
{
if ($request !== null) {
$this->request = $request;
}
if ($this->hasBeenSent()) {
$post = $this->getRequest()->getPost();
if ($this->hasBeenSubmitted()) {
if ($this->isValid($post)) {
$this->onSuccess();
} else {
$this->onFailure();
}
} else {
if ($this->isValidPartial($post)) {
// Nothing to do, just want to see the errors
}
}
} else {
// Well...
}
return $this;
}
public function translate($string)
{
// TODO: A module should use it's own domain
return t($string);
}
public function onSuccess()
{
$this->redirectOnSuccess();
}
public function setSuccessMessage($message)
{
$this->successMessage = $message;
return $this;
}
public function getSuccessMessage($message = null)
{
if ($message !== null) {
return $message;
}
if ($this->successMessage === null) {
return t('Form has successfully been sent');
}
return $this->successMessage;
}
public function redirectOnSuccess($message = null)
{
$url = $this->successUrl ?: $this->getAction();
$this->notifySuccess($this->getSuccessMessage($message));
$this->redirectAndExit($url);
}
public function onFailure()
{
}
public function notifySuccess($message = null)
{
if ($message === null) {
$message = t('Form has successfully been sent');
}
Notification::success($message);
return $this;
}
public function notifyError($message)
{
Notification::error($message);
return $this;
}
protected function redirectAndExit($url)
{
Icinga::app()->getFrontController()->getResponse()->redirectAndExit($url);
}
public function getRequest()
{
if ($this->request === null) {
$this->request = Icinga::app()->getFrontController()->getRequest();
}
return $this->request;
}
public function hasBeenSent()
{
if ($this->hasBeenSent === null) {
$req = $this->getRequest();
if ($req->isPost()) {
$post = $req->getPost();
$this->hasBeenSent = array_key_exists(self::ID, $post) &&
$post[self::ID] === $this->getName();
} else {
$this->hasBeenSent === false;
}
}
return $this->hasBeenSent;
}
protected function detectName()
{
if ($this->formName !== null) {
$this->setName($this->formName);
} else {
$this->setName(get_class($this));
}
}
}

30
public/css/module.less Normal file
View File

@ -0,0 +1,30 @@
/* BEGIN Forms */
form dt label {
width: auto;
}
dd {
margin: 0;
}
/* END of Forms */
table.simple {
width: 100%;
table-layout: auto !important;
th {
background: @colorPetrol;
color: white;
text-align: left;
padding: 0.5em 1em;
}
td {
padding: 0.3em 1em;
}
th.actions, td.actions {
width: 6em;
text-align: right;
}
}