Merge branch 'feature/expression-support'

This commit is contained in:
Thomas Gelf 2016-10-22 05:50:01 +00:00
commit 0c5a568191
11 changed files with 174 additions and 22 deletions

View File

@ -88,12 +88,12 @@ abstract class CustomVariable implements IcingaConfigRenderer
return $this->modified;
}
public function toConfigStringPrefetchable()
public function toConfigStringPrefetchable($renderExpressions = false)
{
if (PrefetchCache::shouldBeUsed()) {
return PrefetchCache::instance()->renderVar($this);
return PrefetchCache::instance()->renderVar($this, $renderExpressions);
} else {
return $this->toConfigString();
return $this->toConfigString($renderExpressions);
}
}

View File

@ -36,8 +36,12 @@ class CustomVariableString extends CustomVariable
return $this;
}
public function toConfigString()
public function toConfigString($renderExpressions = false)
{
return c::renderString($this->getValue());
if ($renderExpressions) {
return c::renderStringWithVariables($this->getValue());
} else {
return c::renderString($this->getValue());
}
}
}

View File

@ -268,24 +268,24 @@ class CustomVariables implements Iterator, Countable, IcingaConfigRenderer
return $this->storedVars;
}
public function toConfigString()
public function toConfigString($renderExpressions = false)
{
$out = '';
ksort($this->vars);
foreach ($this->vars as $key => $var) {
// TODO: ctype_alnum + underscore?
$out .= $this->renderSingleVar($key, $var);
$out .= $this->renderSingleVar($key, $var, $renderExpressions);
}
return $out;
}
protected function renderSingleVar($key, $var)
protected function renderSingleVar($key, $var, $renderExpressions = false)
{
return c::renderKeyValue(
$this->renderKeyName($key),
$var->toConfigStringPrefetchable()
$var->toConfigStringPrefetchable($renderExpressions)
);
}

View File

@ -84,14 +84,14 @@ class PrefetchCache
}
*/
public function renderVar(CustomVariable $var)
public function renderVar(CustomVariable $var, $renderExpressions = false)
{
$checksum = $var->getChecksum();
if (null === $checksum) {
return $var->toConfigString();
return $var->toConfigString($renderExpressions);
} else {
if (! array_key_exists($checksum, $this->renderedVars)) {
$this->renderedVars[$checksum] = $var->toConfigString();
$this->renderedVars[$checksum] = $var->toConfigString($renderExpressions);
}
return $this->renderedVars[$checksum];

View File

@ -257,4 +257,22 @@ class IcingaConfigHelper
return $seconds . 's';
}
public static function renderStringWithVariables($string)
{
$string = preg_replace(
'/(?<!\$)\$([\w\.]+)\$(?!\$)/',
'" + ${1} + "',
static::renderString($string)
);
if (substr($string, 0, 5) === '"" + ') {
$string = substr($string, 5);
}
if (substr($string, -5) === ' + ""') {
$string = substr($string, 0, -5);
}
return $string;
}
}

View File

@ -1631,11 +1631,8 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
protected function renderLegacyProperties()
{
$out = '';
$blacklist = array_merge(array(
'id',
'object_name',
'object_type',
), array() /* $this->prioritizedProperties */);
$blacklist = array_merge($this->propertiesNotForRendering,
array() /* $this->prioritizedProperties */);
foreach ($this->properties as $key => $value) {
if (in_array($key, $blacklist)) {
@ -1706,7 +1703,12 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return $this->renderRelationProperty($relKey, $value);
}
return c::renderKeyValue($key, c::renderString($value));
return c::renderKeyValue(
$key,
$this->isApplyRule() ?
c::renderStringWithVariables($value) :
c::renderString($value)
);
}
protected function renderLegacyObjectProperty($key, $value)
@ -1783,7 +1785,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
protected function renderCustomVars()
{
if ($this->supportsCustomVars()) {
return $this->vars()->toConfigString();
return $this->vars()->toConfigString($this->isApplyRule());
} else {
return '';
}

View File

@ -44,6 +44,21 @@ class CustomVariablesTest extends BaseTestCase
);
}
public function testVariablesToExpression()
{
$vars = $this->newVars();
$vars->bla = 'da';
$vars->abc = '$val$';
$expected = $this->indentVarsList(array(
'vars.abc = val',
'vars.bla = "da"'
));
$this->assertEquals(
$vars->toConfigString(true),
$expected
);
}
protected function indentVarsList($vars)
{
return $this->indent . implode(

View File

@ -69,4 +69,38 @@ class IcingaConfigHelperTest extends BaseTestCase
{
return file_get_contents(__DIR__ . '/rendered/' . $name . '.out');
}
public function testRenderStringIsCorrectlyRendered()
{
$this->assertEquals(c::renderString('val1\\\val2'), '"val1\\\\\\\\val2"');
$this->assertEquals(c::renderString('"val1"'), '"\"val1\""');
$this->assertEquals(c::renderString('\$val\$'), '"\\\\$val\\\\$"');
$this->assertEquals(c::renderString('\t'), '"\\\\t"');
$this->assertEquals(c::renderString('\r'), '"\\\\r"');
$this->assertEquals(c::renderString('\n'), '"\\\\n"');
$this->assertEquals(c::renderString('\f'), '"\\\\f"');
}
public function testRenderStringWithVariables()
{
$this->assertEquals(c::renderStringWithVariables('Before $var$'), '"Before " + var');
$this->assertEquals(c::renderStringWithVariables('$var$ After'), 'var + " After"');
$this->assertEquals(c::renderStringWithVariables('$var$'), 'var');
$this->assertEquals(c::renderStringWithVariables('$$var$$'), '"$$var$$"');
$this->assertEquals(c::renderStringWithVariables('Before $$var$$ After'), '"Before $$var$$ After"');
$this->assertEquals(
c::renderStringWithVariables('Before $name$ $name$ After'),
'"Before " + name + " " + name + " After"'
);
$this->assertEquals(
c::renderStringWithVariables('Before $var1$ $var2$ After'),
'"Before " + var1 + " " + var2 + " After"'
);
$this->assertEquals(c::renderStringWithVariables('$host.vars.custom$'), 'host.vars.custom');
$this->assertEquals(c::renderStringWithVariables('$var"$'), '"$var\"$"');
$this->assertEquals(
c::renderStringWithVariables('\tI am\rrendering\nproperly\fand I $support$ "multiple" $variables$\$'),
'"\\\\tI am\\\\rrendering\\\\nproperly\\\\fand I " + support + " \"multiple\" " + variables + "\\\\$"'
);
}
}

View File

@ -13,6 +13,8 @@ class IcingaServiceTest extends BaseTestCase
protected $testServiceName = '___TEST___service';
protected $createdServices = array();
public function testUnstoredHostCanBeLazySet()
{
$service = $this->service();
@ -233,6 +235,51 @@ class IcingaServiceTest extends BaseTestCase
);
}
public function testVariablesInPropertiesAndCustomVariables()
{
if ($this->skipForMissingDb()) {
return;
}
$db = $this->getDb();
$service = $this->service('___TEST___service_$not_replaced$');
$service->object_type = 'apply';
$service->display_name = 'Service: $host.vars.replaced$';
$service->assignments = array(
'host.address="127.*"',
);
$service->{'vars.custom_var'} = '$host.vars.replaced$';
$service->store($db);
$service = IcingaService::loadWithAutoIncId($service->id, $db);
$this->assertEquals(
$this->loadRendered('service3'),
(string) $service
);
}
public function testVariablesAreNotReplacedForNonApplyObjects()
{
if ($this->skipForMissingDb()) {
return;
}
$db = $this->getDb();
$service = $this->service('___TEST___service_$not_replaced$');
$service->object_type = 'object';
$service->display_name = 'Service: $host.vars.not_replaced$';
$service->{'vars.custom_var'} = '$host.vars.not_replaced$';
$service->store($db);
$service = IcingaService::loadWithAutoIncId($service->id, $db);
$this->assertEquals(
$this->loadRendered('service4'),
(string) $service
);
}
protected function host()
{
return IcingaHost::create(array(
@ -242,10 +289,14 @@ class IcingaServiceTest extends BaseTestCase
));
}
protected function service()
protected function service($objectName = null)
{
if ($objectName === null) {
$objectName = $this->testServiceName;
}
$this->createdServices[] = $objectName;
return IcingaService::create(array(
'object_name' => $this->testServiceName,
'object_name' => $objectName,
'object_type' => 'object',
'display_name' => 'Whatever service',
'vars' => array(
@ -279,7 +330,7 @@ class IcingaServiceTest extends BaseTestCase
}
}
$kill = array($this->testServiceName);
$kill = $this->createdServices;
foreach ($kill as $name) {
if (IcingaService::exists(array($name), $db)) {
IcingaService::load($name, $db)->delete();

View File

@ -0,0 +1,16 @@
apply Service "___TEST___service_$not_replaced$" {
display_name = "Service: " + host.vars.replaced
vars.custom_var = host.vars.replaced
vars.test1 = "string"
vars.test2 = 17
vars.test3 = false
vars.test4 = {
a = [ "dict", "ionary" ]
@this = "is"
}
assign where match("127.*", host.address)
import DirectorOverrideTemplate
}

View File

@ -0,0 +1,12 @@
object Service "___TEST___service_$not_replaced$" {
display_name = "Service: $host.vars.not_replaced$"
vars.custom_var = "$host.vars.not_replaced$"
vars.test1 = "string"
vars.test2 = 17
vars.test3 = false
vars.test4 = {
a = [ "dict", "ionary" ]
@this = "is"
}
}