From 5296616c419f6f26b968da51227187ecf414d315 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 17 Mar 2016 01:08:34 +0100 Subject: [PATCH] CoreApi: add runtime object creation sample --- library/Director/Core/CoreApi.php | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/library/Director/Core/CoreApi.php b/library/Director/Core/CoreApi.php index 004c82fb..90f06a0c 100644 --- a/library/Director/Core/CoreApi.php +++ b/library/Director/Core/CoreApi.php @@ -163,6 +163,42 @@ class CoreApi } } + public function supportsRuntimeCreationFor(IcingaObject $object) + { + $valid = array('host'); + return in_array($object->getShortTableName(), $valid); + } + + protected function assertRuntimeCreationSupportFor(IcingaObject $object) + { + if (!$this->supportsRuntimeCreationFor($object)) { + throw new IcingaException( + 'Object creation at runtime is not supported for "%s"', + $object->getShortTableName() + ); + } + } + + // Note: this is for testing purposes only, NOT production-ready + public function createObjectAtRuntime(IcingaObject $object) + { + $this->assertRuntimeCreationSupportFor($object); + + $key = $object->getShortTableName(); + + $command = sprintf( + "f = function() {\n" + . ' existing = get_%s("%s")' + . "\n if (existing) { return false }" + . "\n%s\n}\n__run_with_activation_context(f)\n", + $key, + $object->object_name, + (string) $object + ); + + return $this->runConsoleCommand($command)->getSingleResult(); + } + public function getConstants() { $constants = array();