summaryrefslogtreecommitdiffstats
path: root/library/Director/PropertyModifier
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:17:47 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:17:47 +0000
commit5419d4428c86c488a43124f85e5407d7cbae6541 (patch)
tree772c4221a20fd7d1b3e7e67c6e21755a50e80fd7 /library/Director/PropertyModifier
parentAdding upstream version 1.10.2. (diff)
downloadicingaweb2-module-director-upstream.tar.xz
icingaweb2-module-director-upstream.zip
Adding upstream version 1.11.1.upstream/1.11.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/PropertyModifier')
-rw-r--r--library/Director/PropertyModifier/PropertyModifierArrayFilter.php2
-rw-r--r--library/Director/PropertyModifier/PropertyModifierExtractFromDN.php2
-rw-r--r--library/Director/PropertyModifier/PropertyModifierGetHostByName.php3
-rw-r--r--library/Director/PropertyModifier/PropertyModifierJsonDecode.php25
-rw-r--r--library/Director/PropertyModifier/PropertyModifierMap.php32
-rw-r--r--library/Director/PropertyModifier/PropertyModifierRegexReplace.php40
-rw-r--r--library/Director/PropertyModifier/PropertyModifierRejectOrSelect.php2
-rw-r--r--library/Director/PropertyModifier/PropertyModifierSplit.php2
8 files changed, 76 insertions, 32 deletions
diff --git a/library/Director/PropertyModifier/PropertyModifierArrayFilter.php b/library/Director/PropertyModifier/PropertyModifierArrayFilter.php
index 0b52987..a8fcbf7 100644
--- a/library/Director/PropertyModifier/PropertyModifierArrayFilter.php
+++ b/library/Director/PropertyModifier/PropertyModifierArrayFilter.php
@@ -116,7 +116,7 @@ class PropertyModifierArrayFilter extends PropertyModifierHook
default:
throw new ConfigurationError(
'%s is not a valid value for an ArrayFilter filter_method',
- var_export($method, 1)
+ var_export($method, true)
);
}
diff --git a/library/Director/PropertyModifier/PropertyModifierExtractFromDN.php b/library/Director/PropertyModifier/PropertyModifierExtractFromDN.php
index c79c5b2..6b0651d 100644
--- a/library/Director/PropertyModifier/PropertyModifierExtractFromDN.php
+++ b/library/Director/PropertyModifier/PropertyModifierExtractFromDN.php
@@ -74,7 +74,7 @@ class PropertyModifierExtractFromDN extends PropertyModifierHook
default:
throw new InvalidPropertyException(
'DN part extraction failed for %s',
- var_export($value, 1)
+ var_export($value, true)
);
}
}
diff --git a/library/Director/PropertyModifier/PropertyModifierGetHostByName.php b/library/Director/PropertyModifier/PropertyModifierGetHostByName.php
index 36884e8..d7921de 100644
--- a/library/Director/PropertyModifier/PropertyModifierGetHostByName.php
+++ b/library/Director/PropertyModifier/PropertyModifierGetHostByName.php
@@ -34,7 +34,8 @@ class PropertyModifierGetHostByName extends PropertyModifierHook
}
$host = gethostbyname($value);
- if (strlen(@inet_pton($host)) !== 4) {
+ $inAddr = inet_pton($host);
+ if (! $inAddr || strlen($inAddr) !== 4) {
switch ($this->getSetting('on_failure')) {
case 'null':
return null;
diff --git a/library/Director/PropertyModifier/PropertyModifierJsonDecode.php b/library/Director/PropertyModifier/PropertyModifierJsonDecode.php
index f6b9af8..4ab119a 100644
--- a/library/Director/PropertyModifier/PropertyModifierJsonDecode.php
+++ b/library/Director/PropertyModifier/PropertyModifierJsonDecode.php
@@ -2,8 +2,9 @@
namespace Icinga\Module\Director\PropertyModifier;
+use Exception;
+use gipfl\Json\JsonString;
use Icinga\Exception\InvalidPropertyException;
-use Icinga\Module\Director\Exception\JsonException;
use Icinga\Module\Director\Hook\PropertyModifierHook;
use Icinga\Module\Director\Web\Form\QuickForm;
@@ -38,16 +39,22 @@ class PropertyModifierJsonDecode extends PropertyModifierHook
/**
* @param $value
* @return mixed|null
- * @throws InvalidPropertyException
+ * @throws InvalidPropertyException|\gipfl\Json\JsonDecodeException
*/
public function transform($value)
{
if (null === $value) {
- return $value;
+ return null;
}
-
- $decoded = @json_decode($value);
- if ($decoded === null && JSON_ERROR_NONE !== json_last_error()) {
+ try {
+ if (is_string($value)) {
+ $decoded = JsonString::decode($value);
+ } else {
+ throw new InvalidPropertyException(
+ 'JSON decode expects a string, got ' . gettype($value)
+ );
+ }
+ } catch (Exception $e) {
switch ($this->getSetting('on_failure')) {
case 'null':
return null;
@@ -55,11 +62,7 @@ class PropertyModifierJsonDecode extends PropertyModifierHook
return $value;
case 'fail':
default:
- throw new InvalidPropertyException(
- 'JSON decoding failed with "%s" for %s',
- JsonException::getJsonErrorMessage(json_last_error()),
- substr($value, 0, 128)
- );
+ throw $e;
}
}
diff --git a/library/Director/PropertyModifier/PropertyModifierMap.php b/library/Director/PropertyModifier/PropertyModifierMap.php
index a6cb422..e411c54 100644
--- a/library/Director/PropertyModifier/PropertyModifierMap.php
+++ b/library/Director/PropertyModifier/PropertyModifierMap.php
@@ -4,6 +4,7 @@ namespace Icinga\Module\Director\PropertyModifier;
use Icinga\Exception\InvalidPropertyException;
use Icinga\Module\Director\Hook\PropertyModifierHook;
+use Icinga\Module\Director\Import\SyncUtils;
use Icinga\Module\Director\Web\Form\QuickForm;
class PropertyModifierMap extends PropertyModifierHook
@@ -30,15 +31,37 @@ class PropertyModifierMap extends PropertyModifierHook
. ' or interrupt the import process'
),
'multiOptions' => $form->optionalEnum(array(
- 'null' => $form->translate('Set null'),
- 'keep' => $form->translate('Return lookup key unmodified'),
- 'fail' => $form->translate('Let the import fail'),
+ 'null' => $form->translate('Set null'),
+ 'keep' => $form->translate('Return lookup key unmodified'),
+ 'custom' => $form->translate('Return custom default value'),
+ 'fail' => $form->translate('Let the import fail'),
)),
+ 'class' => 'autosubmit',
));
+ $method = $form->getSetting('on_missing');
+ if ($method == 'custom') {
+ $form->addElement('text', 'custom_value', array(
+ 'label' => $form->translate('Default value'),
+ 'required' => true,
+ 'description' => $form->translate(
+ 'This value will be evaluated, and variables like ${some_column}'
+ . ' will be filled accordingly. A typical use-case is generating'
+ . ' unique service identifiers via ${host}!${service} in case your'
+ . ' data source doesn\'t allow you to ship such. The chosen "property"'
+ . ' has no effect here and will be ignored.'
+ )
+ ));
+ }
+
// TODO: ignore case
}
+ public function requiresRow()
+ {
+ return true;
+ }
+
public function transform($value)
{
$this->loadCache();
@@ -53,6 +76,9 @@ class PropertyModifierMap extends PropertyModifierHook
case 'keep':
return $value;
+ case 'custom':
+ return SyncUtils::fillVariables($this->getSetting('custom_value'), $this->getRow());
+
case 'fail':
default:
throw new InvalidPropertyException(
diff --git a/library/Director/PropertyModifier/PropertyModifierRegexReplace.php b/library/Director/PropertyModifier/PropertyModifierRegexReplace.php
index 59cb245..eeae69e 100644
--- a/library/Director/PropertyModifier/PropertyModifierRegexReplace.php
+++ b/library/Director/PropertyModifier/PropertyModifierRegexReplace.php
@@ -9,25 +9,36 @@ class PropertyModifierRegexReplace extends PropertyModifierHook
{
public static function addSettingsFormFields(QuickForm $form)
{
- $form->addElement('text', 'pattern', array(
- 'label' => 'Regex pattern',
+ $form->addElement('text', 'pattern', [
+ 'label' => $form->translate('Regex pattern'),
'description' => $form->translate(
'The pattern you want to search for. This can be a regular expression like /^www\d+\./'
),
'required' => true,
- ));
+ ]);
- $form->addElement('text', 'replacement', array(
- 'label' => 'Replacement',
+ $form->addElement('text', 'replacement', [
+ 'label' => $form->translate('Replacement'),
'description' => $form->translate(
- 'The string that should be used as a preplacement'
+ 'The string that should be used as a replacement'
),
- ));
+ ]);
+ $form->addElement('select', 'when_not_matched', [
+ 'label' => $form->translate('When not matched'),
+ 'description' => $form->translate(
+ "What should happen, if the given pattern doesn't match"
+ ),
+ 'value' => 'keep',
+ 'multiOptions' => [
+ 'keep' => $form->translate('Keep the given string'),
+ 'set_null' => $form->translate('Set the value to NULL')
+ ]
+ ]);
}
public function getName()
{
- return 'Regular expression based replacement';
+ return mt('director', 'Regular expression based replacement');
}
public function transform($value)
@@ -36,10 +47,13 @@ class PropertyModifierRegexReplace extends PropertyModifierHook
return null;
}
- return preg_replace(
- $this->getSetting('pattern'),
- $this->getSetting('replacement'),
- $value
- );
+ $result = preg_replace($this->getSetting('pattern'), $this->getSetting('replacement'), $value);
+ if ($result === $value && $this->getSetting('when_not_matched', 'keep') === 'set_null') {
+ if (!preg_match($this->getSetting('pattern'), $value)) {
+ return null;
+ }
+ }
+
+ return $result;
}
}
diff --git a/library/Director/PropertyModifier/PropertyModifierRejectOrSelect.php b/library/Director/PropertyModifier/PropertyModifierRejectOrSelect.php
index 1485d5d..04c49c5 100644
--- a/library/Director/PropertyModifier/PropertyModifierRejectOrSelect.php
+++ b/library/Director/PropertyModifier/PropertyModifierRejectOrSelect.php
@@ -128,7 +128,7 @@ class PropertyModifierRejectOrSelect extends PropertyModifierHook
default:
throw new ConfigurationError(
'%s is not a valid value for an ArrayFilter filter_method',
- var_export($method, 1)
+ var_export($method, true)
);
}
diff --git a/library/Director/PropertyModifier/PropertyModifierSplit.php b/library/Director/PropertyModifier/PropertyModifierSplit.php
index 4a6fef6..3eec77d 100644
--- a/library/Director/PropertyModifier/PropertyModifierSplit.php
+++ b/library/Director/PropertyModifier/PropertyModifierSplit.php
@@ -33,7 +33,7 @@ class PropertyModifierSplit extends PropertyModifierHook
public function transform($value)
{
- if (! strlen(trim($value))) {
+ if ($value === null || ! strlen(trim($value))) {
if ($this->getSetting('when_empty', 'empty_array') === 'empty_array') {
return array();
} else {