From: Gunnar Beutner Date: Wed, 12 Nov 2014 18:08:36 +0000 (+0100) Subject: Implement the __get_object function X-Git-Tag: v2.2.0~45 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e5bd573756f698aca06cd1a648cb814a1ab2fb9b;p=icinga2 Implement the __get_object function --- diff --git a/lib/base/scriptutils.cpp b/lib/base/scriptutils.cpp index 9ad8200ef..bc74a5a70 100644 --- a/lib/base/scriptutils.cpp +++ b/lib/base/scriptutils.cpp @@ -24,6 +24,7 @@ #include "base/json.hpp" #include "base/logger.hpp" #include "base/objectlock.hpp" +#include "base/dynamictype.hpp" #include #include #include @@ -42,6 +43,7 @@ REGISTER_SCRIPTFUNCTION(exit, &ScriptUtils::Exit); REGISTER_SCRIPTFUNCTION(typeof, &ScriptUtils::TypeOf); REGISTER_SCRIPTFUNCTION(keys, &ScriptUtils::Keys); REGISTER_SCRIPTFUNCTION(random, &Utility::Random); +REGISTER_SCRIPTFUNCTION(__get_object, &ScriptUtils::GetObject); bool ScriptUtils::Regex(const String& pattern, const String& text) { @@ -210,3 +212,13 @@ Array::Ptr ScriptUtils::Keys(const Dictionary::Ptr& dict) return result; } +DynamicObject::Ptr ScriptUtils::GetObject(const String& type, const String& name) +{ + DynamicType::Ptr dtype = DynamicType::GetByName(type); + + if (!dtype) + BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid type name")); + + return dtype->GetObject(name); +} + diff --git a/lib/base/scriptutils.hpp b/lib/base/scriptutils.hpp index 1ba25e0a4..1a158f8cb 100644 --- a/lib/base/scriptutils.hpp +++ b/lib/base/scriptutils.hpp @@ -25,6 +25,7 @@ #include "base/array.hpp" #include "base/dictionary.hpp" #include "base/type.hpp" +#include "base/dynamicobject.hpp" namespace icinga { @@ -44,6 +45,7 @@ public: static void Exit(int code); static Type::Ptr TypeOf(const Value& value); static Array::Ptr Keys(const Dictionary::Ptr& dict); + static DynamicObject::Ptr GetObject(const String& type, const String& name); private: ScriptUtils(void);