PropertyModifierGetHostByName: new modifier

This commit is contained in:
Thomas Gelf 2016-03-08 22:10:50 +01:00
parent 66c8d7628f
commit 5b853879b0
2 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,50 @@
<?php
namespace Icinga\Module\Director\PropertyModifier;
use Icinga\Exception\InvalidPropertyException;
use Icinga\Module\Director\Hook\PropertyModifierHook;
use Icinga\Module\Director\Web\Form\QuickForm;
class PropertyModifierGetHostByName extends PropertyModifierHook
{
public static function addSettingsFormFields(QuickForm $form)
{
$form->addElement('select', 'on_failure', array(
'label' => 'On failure',
'description' => $form->translate('What should we do if the host (DNS) lookup fails?'),
'multiOptions' => $form->optionalEnum(array(
'null' => $form->translate('Set no value (null)'),
'keep' => $form->translate('Keep the property (hostname) as is'),
'fail' => $form->translate('Let the whole import run fail'),
)),
'required' => true,
));
}
public function getName()
{
return 'Get host by name (DNS lookup)';
}
public function transform($value)
{
$host = gethostbyname($value);
if (strlen(@inet_pton($host)) !== 4) {
switch($this->getSetting('on_failure')) {
case 'null':
return null;
case 'keep':
return $value;
case 'fail':
default:
throw new InvalidPropertyException(
'Host lookup failed for "%s"',
$value
);
}
}
return $host;
}
}

View File

@ -22,3 +22,4 @@ $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\Pro
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierStripDomain');
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierSubstring');
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierUppercase');
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierGetHostByName');