[icinga-checkins] icinga.org: icinga2/master: Write a validator for method names

git at icinga.org git at icinga.org
Wed Feb 6 13:22:43 CET 2013


Module: icinga2
Branch: master
Commit: 671eb0110feb9d1ab054df26acaccb315944839e
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=671eb0110feb9d1ab054df26acaccb315944839e

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Wed Feb  6 13:10:14 2013 +0100

Write a validator for method names

Fixes #3637

---

 itl/types.conf            |    2 ++
 lib/config/configtype.cpp |   24 ++++++++++++++++++++++++
 lib/config/configtype.h   |    3 +++
 3 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/itl/types.conf b/itl/types.conf
index e83a925..e5b1bba 100644
--- a/itl/types.conf
+++ b/itl/types.conf
@@ -31,6 +31,8 @@ type DynamicObject {
 	%attribute string "__type",
 
 	%attribute dictionary "methods" {
+		%validator "native::ValidateMethods",
+
 		%attribute string "*",
 	},
 
diff --git a/lib/config/configtype.cpp b/lib/config/configtype.cpp
index 52e57d6..55c4087 100644
--- a/lib/config/configtype.cpp
+++ b/lib/config/configtype.cpp
@@ -21,6 +21,8 @@
 
 using namespace icinga;
 
+REGISTER_SCRIPTFUNCTION("native::ValidateMethods", &ConfigType::ValidateMethods);
+
 ConfigType::ConfigType(const String& name, const DebugInfo& debuginfo)
 	: m_Name(name), m_RuleList(boost::make_shared<TypeRuleList>()), m_DebugInfo(debuginfo)
 { }
@@ -166,3 +168,25 @@ void ConfigType::ValidateDictionary(const Dictionary::Ptr& dictionary,
 	}
 }
 
+void ConfigType::ValidateMethods(const ScriptTask::Ptr& task,
+    const vector<Value>& arguments)
+{
+	if (arguments.size() < 1)
+		BOOST_THROW_EXCEPTION(invalid_argument("Missing argument: Location must be specified."));
+
+	if (arguments.size() < 2)
+		BOOST_THROW_EXCEPTION(invalid_argument("Missing argument: Attribute dictionary must be specified."));
+
+	String location = arguments[0];
+	Dictionary::Ptr attrs = arguments[1];
+
+	String key;
+	BOOST_FOREACH(tie(key, tuples::ignore), attrs) {
+		if (!ScriptFunction::GetByName(key)) {
+			ConfigCompilerContext::GetContext()->AddError(false, "Validation failed for " +
+			    location + ": Method '" + key + "' not found.");
+		}
+	}
+
+	task->FinishResult(Empty);
+}
diff --git a/lib/config/configtype.h b/lib/config/configtype.h
index 4251a5f..b72aeaa 100644
--- a/lib/config/configtype.h
+++ b/lib/config/configtype.h
@@ -46,6 +46,9 @@ public:
 

 	void ValidateItem(const ConfigItem::Ptr& object) const;

 

+	static void ValidateMethods(const ScriptTask::Ptr& task,

+	    const vector<Value>& arguments);

+

 private:

 	String m_Name; /**< The type name. */

 	String m_Parent; /**< The parent type. */






More information about the icinga-checkins mailing list