CustomVariable*(Legacy): Add toLegacyConfigString

refs #12919
This commit is contained in:
Markus Frosch 2016-10-14 11:53:04 +02:00
parent ffab574355
commit 1592894fd7
7 changed files with 64 additions and 0 deletions

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\CustomVariable;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
class CustomVariableArray extends CustomVariable
{
@ -79,4 +80,9 @@ class CustomVariableArray extends CustomVariable
$this->value[$key] = clone($value);
}
}
public function toLegacyConfigString()
{
return c1::renderArray($this->value);
}
}

View File

@ -45,4 +45,9 @@ class CustomVariableBoolean extends CustomVariable
{
return $this->value ? 'true' : 'false';
}
public function toLegacyConfigString()
{
return $this->toConfigString();
}
}

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\CustomVariable;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
use Countable;
class CustomVariableDictionary extends CustomVariable implements Countable
@ -112,4 +113,9 @@ class CustomVariableDictionary extends CustomVariable implements Countable
{
return c::renderDictionary($this->value);
}
public function toLegacyConfigString()
{
return c1::renderDictionary($this->value);
}
}

View File

@ -44,4 +44,9 @@ class CustomVariableNull extends CustomVariable
{
return 'null';
}
public function toLegacyConfigString()
{
return $this->toConfigString();
}
}

View File

@ -65,4 +65,9 @@ class CustomVariableNumber extends CustomVariable
return sprintf(self::PRECISION, $this->value);
}
}
public function toLegacyConfigString()
{
return $this->toConfigString();
}
}

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\CustomVariable;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
class CustomVariableString extends CustomVariable
{
@ -44,4 +45,9 @@ class CustomVariableString extends CustomVariable
return c::renderString($this->getValue());
}
}
public function toLegacyConfigString()
{
return c1::renderString($this->getValue());
}
}

View File

@ -4,7 +4,9 @@ namespace Icinga\Module\Director\CustomVariable;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer;
use Icinga\Module\Director\Objects\IcingaHostVar;
use Icinga\Module\Director\Objects\IcingaObject;
use Countable;
use Exception;
@ -284,6 +286,35 @@ class CustomVariables implements Iterator, Countable, IcingaConfigRenderer
return $out;
}
public function toLegacyConfigString()
{
$out = '';
ksort($this->vars);
foreach ($this->vars as $key => $var) {
/** @var CustomVariable $var */
// TODO: ctype_alnum + underscore?
$value = null;
switch ($type = $var->getType()) {
case 'String':
# TODO: Make Prefetchable
$value = $var->toLegacyConfigString();
break;
default:
$out .= sprintf("# Unsupported var: %s (%s)\n", $key, $type);
}
if ($value !== null) {
$out .= c1::renderKeyValue('_' . $key, $value);
}
}
if ($out !== '') {
$out = "\n".$out;
}
return $out;
}
/**
* @param string $key
* @param CustomVariable $var