various: shorten long line, spacing, readability

This commit is contained in:
Thomas Gelf 2017-08-25 22:42:38 +02:00
parent cd4e90e418
commit e45962c1b9
9 changed files with 239 additions and 192 deletions

View File

@ -2,6 +2,7 @@
namespace Icinga\Module\Director\Controllers;
use Icinga\Module\Director\Forms\IcingaDependencyForm;
use Icinga\Module\Director\Web\Controller\ObjectController;
use Icinga\Module\Director\Objects\IcingaDependency;
@ -49,6 +50,7 @@ class DependencyController extends ObjectController
protected function beforeHandlingAddRequest($form)
{
/** @var IcingaDependencyForm $form */
if ($this->apply) {
$form->createApplyRuleFor($this->apply);
}

View File

@ -2,6 +2,7 @@
namespace Icinga\Module\Director\Controllers;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Web\Controller\ActionController;
use Icinga\Data\Filter\Filter;

View File

@ -36,11 +36,11 @@ class IcingaDependencyForm extends DirectorObjectForm
protected function addNameElement()
{
$this->addElement('text', 'object_name', array(
$this->addElement('text', 'object_name', [
'label' => $this->translate('Name'),
'required' => true,
'description' => $this->translate('Name for the Icinga dependency you are going to create')
));
]);
return $this;
}
@ -52,20 +52,18 @@ class IcingaDependencyForm extends DirectorObjectForm
return $this;
}
$this->addElement('select', 'apply_to', array(
$this->addElement('select', 'apply_to', [
'label' => $this->translate('Apply to'),
'description' => $this->translate(
'Whether this dependency should affect hosts or services'
),
'required' => true,
'class' => 'autosubmit',
'multiOptions' => $this->optionalEnum(
array(
'host' => $this->translate('Hosts'),
'service' => $this->translate('Services'),
)
)
));
'multiOptions' => $this->optionalEnum([
'host' => $this->translate('Hosts'),
'service' => $this->translate('Services'),
])
]);
$applyTo = $this->getSentOrObjectValue('apply_to');
@ -114,33 +112,29 @@ class IcingaDependencyForm extends DirectorObjectForm
protected function addBooleanElements()
{
$this->addBoolean('disable_checks', [
'label' => $this->translate('Disable Checks'),
'description' => $this->translate(
'Whether to disable checks when this dependency fails.'
. ' Defaults to false.'
)
], null);
$this->addBoolean(
'disable_checks',
array(
'label' => $this->translate('Disable Checks'),
'description' => $this->translate('Whether to disable checks when this dependency fails. Defaults to false.')
),
null
);
$this->addBoolean('disable_notifications', [
'label' => $this->translate('Disable Notificiations'),
'description' => $this->translate(
'Whether to disable notifications when this dependency fails.'
. ' Defaults to true.'
)
], null);
$this->addBoolean(
'disable_notifications',
array(
'label' => $this->translate('Disable Notificiations'),
'description' => $this->translate('Whether to disable notifications when this dependency fails. Defaults to true.')
),
null
);
$this->addBoolean(
'ignore_soft_states',
array(
'label' => $this->translate('Ignore Soft States'),
'description' => $this->translate('Whether to ignore soft states for the reachability calculation. Defaults to true.')
),
null
);
$this->addBoolean('ignore_soft_states', [
'label' => $this->translate('Ignore Soft States'),
'description' => $this->translate(
'Whether to ignore soft states for the reachability calculation.'
. ' Defaults to true.'
)
], null);
return $this;
}
@ -183,7 +177,8 @@ class IcingaDependencyForm extends DirectorObjectForm
);
}
// If configuring Object, allow selection of child host and/or service, otherwise apply rules will determine child object.
// If configuring Object, allow selection of child host and/or service,
// otherwise apply rules will determine child object.
if ($this->isObject()) {
$this->addElement(
'text',
@ -193,46 +188,42 @@ class IcingaDependencyForm extends DirectorObjectForm
'description' => $this->translate(
'The child host.'
),
'class' => "autosubmit director-suggest",
'data-suggestion-context' => 'hostnames',
'value' => $this->getObject()->get('child_host'),
'order' => 30,
'class' => "autosubmit director-suggest",
'required' => $this->isObject(),
'value' => $this->getObject()->get('child_host')
'data-suggestion-context' => 'hostnames',
)
);
$sent_child=$this->getSentOrObjectValue("child_host");
if (!empty($sent_child)) {
$this->addElement(
'text',
'child_service',
array(
'label' => $this->translate('Child Service'),
'description' => $this->translate(
'Optional. The child service. If omitted this dependency object is treated as host dependency.'
),
'class' => "autosubmit director-suggest",
'data-suggestion-context' => 'servicenames',
'data-suggestion-for-host' => $sent_child,
'order' => 40,
'value' => $this->getObject()->get('child_service')
)
);
$this->addElement('text', 'child_service', [
'label' => $this->translate('Child Service'),
'description' => $this->translate(
'Optional. The child service. If omitted this dependency'
. ' object is treated as host dependency.'
),
'class' => "autosubmit director-suggest",
'order' => 40,
'value' => $this->getObject()->get('child_service'),
'data-suggestion-context' => 'servicenames',
'data-suggestion-for-host' => $sent_child,
]);
}
}
$elements=array('parent_host','child_host','parent_service','child_service');
$this->addDisplayGroup($elements, 'related_objects', array(
'decorators' => array(
$elements = ['parent_host', 'child_host', 'parent_service', 'child_service'];
$this->addDisplayGroup($elements, 'related_objects', [
'decorators' => [
'FormElements',
array('HtmlTag', array('tag' => 'dl')),
['HtmlTag', ['tag' => 'dl']],
'Fieldset',
),
],
'order' => 25,
'legend' => $this->translate('Related Objects')
));
]);
return $this;
}
@ -240,9 +231,10 @@ class IcingaDependencyForm extends DirectorObjectForm
public function createApplyRuleFor(IcingaDependency $dependency)
{
$object = $this->object();
$object->imports = $dependency->object_name;
$object->object_type = 'apply';
$object->object_name = $dependency->object_name;
$object->setImports($dependency->getObjectName());
$object->set('object_type', 'apply');
$object->set('object_name', $dependency->getObjectName());
return $this;
}
}

View File

@ -4,18 +4,18 @@ namespace Icinga\Module\Director\Dashboard\Dashlet;
class DependencyObjectDashlet extends Dashlet
{
protected $icon = '';
protected $icon = 'sitemap';
protected $requiredStats = array('dependency');
public function getTitle()
{
return $this->translate('Dependencies.');
return $this->translate('Dependencies');
}
public function getSummary()
{
return $this->translate('Define object dependency relationships.')
return $this->translate('Object dependency relationships.')
. ' ' . parent::getSummary();
}

View File

@ -6,30 +6,29 @@ use Icinga\Exception\ConfigurationError;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Exception\NotFoundError;
use Icinga\Data\Filter\Filter;
use Icinga\Module\Director\Objects\HostApplyMatches;
class IcingaDependency extends IcingaObject
{
protected $table = 'icinga_dependency';
protected $defaultProperties = array(
'id' => null,
'object_name' => null,
'object_type' => null,
'disabled' => 'n',
'apply_to' => null,
'parent_host_id' => null,
'parent_service_id' => null,
'child_host_id' => null,
'child_service_id' => null,
'disable_checks' => null,
'disable_notifications' => null,
'ignore_soft_states' => null,
'period_id' => null,
'zone_id' => null,
'assign_filter' => null,
'parent_service_by_name' => null,
);
protected $defaultProperties = [
'id' => null,
'object_name' => null,
'object_type' => null,
'disabled' => 'n',
'apply_to' => null,
'parent_host_id' => null,
'parent_service_id' => null,
'child_host_id' => null,
'child_service_id' => null,
'disable_checks' => null,
'disable_notifications' => null,
'ignore_soft_states' => null,
'period_id' => null,
'zone_id' => null,
'assign_filter' => null,
'parent_service_by_name' => null,
];
protected $supportsCustomVars = false;
@ -37,24 +36,24 @@ class IcingaDependency extends IcingaObject
protected $supportsApplyRules = true;
protected $relatedSets = array(
protected $relatedSets = [
'states' => 'StateFilterSet',
);
];
protected $relations = array(
'zone' => 'IcingaZone',
protected $relations = [
'zone' => 'IcingaZone',
'parent_host' => 'IcingaHost',
'parent_service' => 'IcingaService',
'child_host' => 'IcingaHost',
'child_service' => 'IcingaService',
'period' => 'IcingaTimePeriod',
);
'child_host' => 'IcingaHost',
'child_service' => 'IcingaService',
'period' => 'IcingaTimePeriod',
];
protected $booleans = array(
'disable_checks' => 'disable_checks',
protected $booleans = [
'disable_checks' => 'disable_checks',
'disable_notifications' => 'disable_notifications',
'ignore_soft_states' => 'ignore_soft_states'
);
'ignore_soft_states' => 'ignore_soft_states'
];
/**
* Do not render internal property apply_to
@ -94,10 +93,20 @@ class IcingaDependency extends IcingaObject
protected function setKey($key)
{
// TODO: Check if this method can be removed
if (is_int($key)) {
$this->id = $key;
} elseif (is_array($key)) {
foreach (array('id', 'parent_host_id', 'parent_service_id', 'child_host_id', 'child_service_id', 'object_name') as $k) {
$keys = [
'id',
'parent_host_id',
'parent_service_id',
'child_host_id',
'child_service_id',
'object_name'
];
foreach ($keys as $k) {
if (array_key_exists($k, $key)) {
$this->set($k, $key[$k]);
}
@ -111,14 +120,20 @@ class IcingaDependency extends IcingaObject
protected function renderAssignments()
{
// TODO: this will never be reached
if ($this->hasBeenAssignedToServiceApply()) {
$tmpService= $this->getRelatedObject('child_service', $this->child_service_id);
/** @var IcingaService $tmpService */
$tmpService = $this->getRelatedObject(
'child_service',
$this->get('child_service_id')
);
// TODO: fix this, will crash:
$assigns = $tmpService->assignments()->toConfigString();
$filter = sprintf(
'%s && service.name == "%s"',
trim($assigns),
$this->child_service
$this->get('child_service')
);
return "\n " . $filter . "\n";
}
@ -126,15 +141,15 @@ class IcingaDependency extends IcingaObject
if ($this->hasBeenAssignedToHostTemplateService()) {
$filter = sprintf(
'assign where "%s" in host.templates && service.name == "%s"',
$this->child_host,
$this->child_service
$this->get('child_host'),
$this->get('child_service')
);
return "\n " . $filter . "\n";
}
if ($this->hasBeenAssignedToHostTemplate()) {
$filter = sprintf(
'assign where "%s" in host.templates',
$this->child_host
$this->get('child_host')
);
return "\n " . $filter . "\n";
}
@ -142,7 +157,7 @@ class IcingaDependency extends IcingaObject
if ($this->hasBeenAssignedToServiceTemplate()) {
$filter = sprintf(
'assign where "%s" in service.templates',
$this->child_service
$this->get('child_service')
);
return "\n " . $filter . "\n";
}
@ -153,10 +168,11 @@ class IcingaDependency extends IcingaObject
protected function hasBeenAssignedToHostTemplate()
{
try {
return $this->child_host_id && $this->getRelatedObject(
$id = $this->get('child_host_id');
return $id && $this->getRelatedObject(
'child_host',
$this->child_host_id
)->object_type === 'template';
$id
)->isTemplate();
} catch (NotFoundError $e) {
return false;
}
@ -165,10 +181,11 @@ class IcingaDependency extends IcingaObject
protected function hasBeenAssignedToServiceTemplate()
{
try {
return $this->child_service_id && $this->getRelatedObject(
$id = $this->get('child_service_id');
return $id && $this->getRelatedObject(
'child_service',
$this->child_service_id
)->object_type === 'template';
$id
)->isTemplate();
} catch (NotFoundError $e) {
return false;
}
@ -180,10 +197,11 @@ class IcingaDependency extends IcingaObject
return false;
}
try {
return $this->child_service_id && $this->getRelatedObject(
$id = $this->get('child_service_id');
return $id && $this->getRelatedObject(
'child_service',
$this->child_service_id
)->object_type === 'object';
$id
)->isObject();
} catch (NotFoundError $e) {
return false;
}
@ -192,16 +210,16 @@ class IcingaDependency extends IcingaObject
protected function hasBeenAssignedToServiceApply()
{
try {
return $this->child_service_id && $this->getRelatedObject(
$id = $this->get('child_service_id');
return $id && $this->getRelatedObject(
'child_service',
$this->child_service_id
)->object_type === 'apply';
$id
)->isApplyRule();
} catch (NotFoundError $e) {
return false;
}
}
/**
* Render child_host_id as host_name
*
@ -218,7 +236,11 @@ class IcingaDependency extends IcingaObject
return '';
}
return $this->renderRelationProperty('child_host', $this->child_host_id, 'child_host_name');
return $this->renderRelationProperty(
'child_host',
$this->get('child_host_id'),
'child_host_name'
);
}
/**
@ -233,10 +255,13 @@ class IcingaDependency extends IcingaObject
{
// @codingStandardsIgnoreEnd
return $this->renderRelationProperty('parent_host', $this->parent_host_id, 'parent_host_name');
return $this->renderRelationProperty(
'parent_host',
$this->get('parent_host_id'),
'parent_host_name'
);
}
/**
* Render child_service_id as host_name
*
@ -248,19 +273,18 @@ class IcingaDependency extends IcingaObject
public function renderChild_service_id()
{
// @codingStandardsIgnoreEnd
if ($this->hasBeenAssignedToServiceTemplate()) {
if ($this->hasBeenAssignedToServiceTemplate()
|| $this->hasBeenAssignedToHostTemplateService()
|| $this->hasBeenAssignedToServiceApply()
) {
return '';
}
if ($this->hasBeenAssignedToHostTemplateService()) {
return '';
}
if ($this->hasBeenAssignedToServiceApply()) {
return '';
}
return $this->renderRelationProperty('child_service', $this->child_service_id, 'child_service_name');
return $this->renderRelationProperty(
'child_service',
$this->get('child_service_id'),
'child_service_name'
);
}
/**
@ -273,84 +297,107 @@ class IcingaDependency extends IcingaObject
*/
public function renderParent_service_id()
{
return $this->renderRelationProperty('parent_service', $this->parent_service_id, 'parent_service_name');
return $this->renderRelationProperty(
'parent_service',
$this->get('parent_service_id'),
'parent_service_name'
);
}
//special case for parent service set as plain string for Apply rules
public function renderParent_service_by_name()
{
return "\n parent_service_name = \"" . $this->parent_service_by_name ."\"\n";
// TODO:
return c::renderKeyValue(
'parent_service_name',
$this->get('parent_service_by_name')
);
}
public function isApplyRule()
{
if ($this->hasBeenAssignedToHostTemplate()) {
if ($this->hasBeenAssignedToHostTemplate()
|| $this->hasBeenAssignedToServiceTemplate()
|| $this->hasBeenAssignedToServiceApply()
) {
return true;
}
if ($this->hasBeenAssignedToServiceTemplate()) {
return true;
}
if ($this->hasBeenAssignedToServiceApply()) {
return true;
}
return $this->hasProperty('object_type')
&& $this->object_type === 'apply';
return parent::isApplyRule();
}
protected function resolveUnresolvedRelatedProperty($name)
{
$short = substr($name, 0, -3);
/** @var IcingaObject $class */
$class = $this->getRelationClass($short);
$obj_key = $this->unresolvedRelatedProperties[$name];
$objKey = $this->unresolvedRelatedProperties[$name];
# related services need array key
if ($class == "Icinga\Module\Director\Objects\IcingaService" ) {
if ($name == "parent_service_id" && $this->object_type == 'apply' ) { //special case , parent service can be set as simple string for Apply
if ($this->properties['parent_host_id']==null) {
$this->reallySet('parent_service_by_name', $this->unresolvedRelatedProperties[$name]);
$this->reallySet('parent_service_id',null);
if ($name === 'parent_service_id' && $this->object_type === 'apply' ) {
//special case , parent service can be set as simple string for Apply
if ($this->properties['parent_host_id'] === null) {
$this->reallySet(
'parent_service_by_name',
$this->unresolvedRelatedProperties[$name]
);
$this->reallySet('parent_service_id', null);
unset($this->unresolvedRelatedProperties[$name]);
return;
}
}
$this->reallySet('parent_service_by_name',null);
$host_id_prop=str_replace("service","host",$name);
if (isset($this->properties[$host_id_prop])) {
$obj_key=array("host_id" => $this->properties[$host_id_prop], "object_name" => $this->unresolvedRelatedProperties[$name]);
$this->reallySet('parent_service_by_name', null);
$hostIdProperty = str_replace('service', 'host', $name);
if (isset($this->properties[$hostIdProperty])) {
$objKey = [
'host_id' => $this->properties[$hostIdProperty],
'object_name' => $this->unresolvedRelatedProperties[$name]
];
} else {
$obj_key=array("host_id" => null, "object_name" => $this->unresolvedRelatedProperties[$name]);
$objKey = [
'host_id' => null,
'object_name' => $this->unresolvedRelatedProperties[$name]
];
}
try {
$object = $class::load( $obj_key, $this->connection);
$class::load( $objKey, $this->connection);
} catch (NotFoundError $e) {
// Not a simple service on host
// Hunt through inherited services, use service assigned to template if found
$tmp_host=IcingaHost::loadWithAutoIncId($this->properties[$host_id_prop], $this->connection);
// Hunt through inherited services, use service assigned to
// template if found
$tmpHost = IcingaHost::loadWithAutoIncId(
$this->properties[$hostIdProperty],
$this->connection
);
//services for applicable templates
$resolver = $tmp_host->templateResolver();
//services for applicable templates
$resolver = $tmpHost->templateResolver();
foreach ($resolver->fetchResolvedParents() as $template_obj) {
$obj_key=array("host_id" => $template_obj->id, "object_name" => $this->unresolvedRelatedProperties[$name]);
try {
$object = $class::load( $obj_key, $this->connection);
$objKey = [
'host_id' => $template_obj->id,
'object_name' => $this->unresolvedRelatedProperties[$name]
];
try {
$object = $class::load( $objKey, $this->connection);
} catch (NotFoundError $e) {
continue;
}
break;
}
if (!isset($object)) { //Not an inherited service, now try apply rules
$matcher = HostApplyMatches::prepare($tmp_host);
if (!isset($object)) {
// Not an inherited service, now try apply rules
$matcher = HostApplyMatches::prepare($tmpHost);
foreach ($this->getAllApplyRules() as $rule) {
if ($matcher->matchesFilter($rule->filter)) {
if ($rule->name == $this->unresolvedRelatedProperties[$name]) {
$object=IcingaService::loadWithAutoIncId($rule->id, $this->connection);
if ($rule->name === $this->unresolvedRelatedProperties[$name]) {
$object = IcingaService::loadWithAutoIncId(
$rule->get('id'),
$this->connection
);
break;
}
}
@ -358,10 +405,7 @@ class IcingaDependency extends IcingaObject
}
}
} else {
$object = $class::load(
$obj_key,
$this->connection
);
$object = $class::load($objKey, $this->connection);
}
if (isset($object)) {
@ -374,7 +418,7 @@ class IcingaDependency extends IcingaObject
protected function getAllApplyRules()
{
$allApplyRules=$this->fetchAllApplyRules();
$allApplyRules = $this->fetchAllApplyRules();
foreach ($allApplyRules as $rule) {
$rule->filter = Filter::fromQueryString($rule->assign_filter);
}
@ -408,15 +452,18 @@ class IcingaDependency extends IcingaObject
/** @var IcingaObject $class */
$class = $this->getRelationClass($key);
$object = $class::loadWithAutoIncId($id, $this->connection);
return $object->get('object_name');
return $object->getObjectName();
} else {
// handle special case for plain string parent service on Dependency Apply rules
if ($key == 'parent_service' && $this->get('parent_service_by_name') != null) {
// handle special case for plain string parent service on Dependency
// Apply rules
if ($key === 'parent_service'
&& null !== $this->get('parent_service_by_name')
) {
return $this->get('parent_service_by_name');
}
}
return null;
}
}

View File

@ -9,7 +9,7 @@ class TemplateActionBar extends DirectorBaseActionBar
protected function assemble()
{
$type = $this->type;
$pltype = preg_replace('/cys$/', 'cies', $type . 's');
$plType = preg_replace('/cys$/', 'cies', $type . 's');
$renderTree = $this->url->getParam('render') === 'tree';
$renderParams = $renderTree ? null : ['render' => 'tree'];
$this->add(
@ -28,7 +28,7 @@ class TemplateActionBar extends DirectorBaseActionBar
)->add(
Link::create(
$renderTree ? $this->translate('Table') : $this->translate('Tree'),
"director/$pltype/templates",
"director/$plType/templates",
$renderParams,
[
'class' => 'icon-' . ($renderTree ? 'doc-text' : 'sitemap'),

View File

@ -33,6 +33,7 @@ abstract class ObjectsController extends ActionController
$this->assertPermission('director/' . $this->getPluralBaseType());
}
}
/**
* @return $this
*/
@ -127,7 +128,6 @@ abstract class ObjectsController extends ActionController
)->content()->add($form);
}
/**
* Loads the TemplatesTable or the TemplateTreeRenderer
*
@ -306,11 +306,11 @@ abstract class ObjectsController extends ActionController
protected function getPluralType()
{
return preg_replace("/cys$/", "cies", $this->getType() . 's');
return preg_replace('/cys$/', 'cies', $this->getType() . 's');
}
protected function getPluralBaseType()
{
return preg_replace("/cys$/", "cies", $this->getBaseType() . 's');
return preg_replace('/cys$/', 'cies', $this->getBaseType() . 's');
}
}

View File

@ -177,7 +177,11 @@ abstract class TemplateController extends CompatController
protected function getPluralType()
{
return preg_replace('/cys$/', 'cies', $this->template()->getShortTableName() . 's');
return preg_replace(
'/cys$/',
'cies',
$this->template()->getShortTableName() . 's'
);
}
protected function getTranslatedType()

View File

@ -18,28 +18,29 @@ class ObjectsTabs extends Tabs
$object = IcingaObject::createByType(substr($type, 0, -5));
}
$pltype=strtolower(preg_replace('/cys$/', 'cies', $type . 's'));
if ($auth->hasPermission("director/${pltype}")) {
$plType = strtolower(preg_replace('/cys$/', 'cies', $type . 's'));
if ($auth->hasPermission("director/${plType}")) {
$this->add('index', array(
'url' => sprintf('director/%s', $pltype),
'label' => $this->translate(ucfirst($pltype)),
'url' => sprintf('director/%s', $plType),
'label' => $this->translate(ucfirst($plType)),
));
}
if ($object->getShortTableName() === 'command') {
$this->add('external', array(
'url' => sprintf('director/%s', strtolower($pltype)),
'url' => sprintf('director/%s', strtolower($plType)),
'urlParams' => ['type' => 'external_object'],
'label' => $this->translate('External'),
));
}
if ($auth->hasPermission('director/admin') || (
$object->getShortTableName() === 'notification' && $auth->hasPermission('director/notifications')
)) {
$object->getShortTableName() === 'notification'
&& $auth->hasPermission('director/notifications')
)) {
if ($object->supportsApplyRules()) {
$this->add('applyrules', array(
'url' => sprintf('director/%s/applyrules', $pltype),
'url' => sprintf('director/%s/applyrules', $plType),
'label' => $this->translate('Apply')
));
}
@ -48,7 +49,7 @@ class ObjectsTabs extends Tabs
if ($auth->hasPermission('director/admin') && $type !== 'zone') {
if ($object->supportsImports()) {
$this->add('templates', array(
'url' => sprintf('director/%s/templates', $pltype),
'url' => sprintf('director/%s/templates', $plType),
'label' => $this->translate('Templates'),
));
}
@ -71,7 +72,7 @@ class ObjectsTabs extends Tabs
}
if ($object->supportsSets() && $auth->hasPermission("director/${type}_sets")) {
$this->add('sets', array(
'url' => sprintf('director/%s/sets', $pltype),
'url' => sprintf('director/%s/sets', $plType),
'label' => $this->translate('Sets')
));
}