mirror of https://github.com/Icinga/icinga2.git
parent
eadb309dab
commit
e047e06fc8
|
@ -352,29 +352,34 @@ Example:
|
|||
|
||||
-------------------------------------------------------------------------------
|
||||
type Pizza {
|
||||
number radius,
|
||||
%require "radius",
|
||||
%attribute number "radius",
|
||||
|
||||
dictionary ingredients {
|
||||
string *,
|
||||
%attribute dictionary "ingredients" {
|
||||
%validator "native::ValidateIngredients",
|
||||
|
||||
dictionary * {
|
||||
number quantity,
|
||||
string name
|
||||
%attribute string "*",
|
||||
|
||||
%attribute dictionary "*" {
|
||||
%attribute number "quantity",
|
||||
%attribute string "name"
|
||||
}
|
||||
},
|
||||
|
||||
any custom::*
|
||||
%attribute any "custom::*"
|
||||
}
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
The Pizza definition provides the following validation rules:
|
||||
|
||||
* Pizza objects may contain an attribute "radius" which has to be a number.
|
||||
* Pizza objects must contain an attribute "radius" which has to be a number.
|
||||
* Pizza objects may contain an attribute "ingredients" which has to be a
|
||||
dictionary.
|
||||
* Elements in the ingredients dictionary can be either a string or a dictionary.
|
||||
* If they're a dictionary they may contain attributes "quantity" (of type
|
||||
number) and "name" (of type string).
|
||||
* The script function "native::ValidateIngredients" is run to perform further
|
||||
validation of the ingredients dictionary.
|
||||
* Pizza objects may contain attribute matching the pattern "custom::*" of any
|
||||
type.
|
||||
|
||||
|
|
|
@ -58,7 +58,29 @@ type Host {
|
|||
%attribute string "*"
|
||||
},
|
||||
%attribute dictionary "services" {
|
||||
%attribute any "*" /* TODO: more specific validation rules */
|
||||
%validator "native::ValidateServiceDictionary",
|
||||
|
||||
%attribute string "*",
|
||||
%attribute dictionary "*" {
|
||||
%attribute string "service",
|
||||
|
||||
%attribute dictionary "macros" {
|
||||
%attribute string "*"
|
||||
},
|
||||
|
||||
%attribute number "check_interval",
|
||||
%attribute number "retry_interval",
|
||||
|
||||
%attribute dictionary "servicegroups" {
|
||||
%attribute string "*"
|
||||
},
|
||||
%attribute dictionary "checkers" {
|
||||
%attribute string "*"
|
||||
},
|
||||
%attribute dictionary "dependencies" {
|
||||
%attribute string "*"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/* service attributes */
|
||||
|
|
|
@ -507,7 +507,17 @@ bool Utility::Glob(const String& pathSpec, const function<void (const String&)>&
|
|||
*/
|
||||
void Utility::WaitUntil(const function<bool (void)>& predicate)
|
||||
{
|
||||
while (!predicate())
|
||||
Application::GetInstance()->ProcessEvents();
|
||||
while (!predicate()) {
|
||||
Application::Ptr instance = Application::GetInstance();
|
||||
|
||||
/* Waiting for a predicate requires an application instance.
|
||||
* This means we cannot do certain asynchronous things
|
||||
* (like spawning a process) until the application instance
|
||||
* has been initialized. */
|
||||
if (!instance)
|
||||
throw_exception(runtime_error("Waiting for predicate failed: Application instance is not initialized."));
|
||||
|
||||
instance->ProcessEvents();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,148 +1,168 @@
|
|||
/******************************************************************************
|
||||
* Icinga 2 *
|
||||
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU General Public License *
|
||||
* as published by the Free Software Foundation; either version 2 *
|
||||
* of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the Free Software Foundation *
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||
******************************************************************************/
|
||||
|
||||
#include "i2-config.h"
|
||||
|
||||
using namespace icinga;
|
||||
|
||||
ConfigType::ConfigType(const String& name, const DebugInfo& debuginfo)
|
||||
: m_Name(name), m_RuleList(boost::make_shared<TypeRuleList>()), m_DebugInfo(debuginfo)
|
||||
{ }
|
||||
|
||||
String ConfigType::GetName(void) const
|
||||
{
|
||||
return m_Name;
|
||||
}
|
||||
|
||||
String ConfigType::GetParent(void) const
|
||||
{
|
||||
return m_Parent;
|
||||
}
|
||||
|
||||
void ConfigType::SetParent(const String& parent)
|
||||
{
|
||||
m_Parent = parent;
|
||||
}
|
||||
|
||||
TypeRuleList::Ptr ConfigType::GetRuleList(void) const
|
||||
{
|
||||
return m_RuleList;
|
||||
}
|
||||
|
||||
DebugInfo ConfigType::GetDebugInfo(void) const
|
||||
{
|
||||
return m_DebugInfo;
|
||||
}
|
||||
|
||||
void ConfigType::ValidateItem(const ConfigItem::Ptr& item) const
|
||||
{
|
||||
Dictionary::Ptr attrs = item->Link();
|
||||
|
||||
/* Don't validate abstract items. */
|
||||
if (attrs->Get("__abstract"))
|
||||
return;
|
||||
|
||||
vector<String> locations;
|
||||
locations.push_back("Object '" + item->GetName() + "' (Type: '" + item->GetType() + "')");
|
||||
|
||||
ConfigType::Ptr parent;
|
||||
if (m_Parent.IsEmpty()) {
|
||||
if (GetName() != "DynamicObject")
|
||||
parent = ConfigCompilerContext::GetContext()->GetType("DynamicObject");
|
||||
} else {
|
||||
parent = ConfigCompilerContext::GetContext()->GetType(m_Parent);
|
||||
}
|
||||
|
||||
vector<TypeRuleList::Ptr> ruleLists;
|
||||
if (parent)
|
||||
ruleLists.push_back(parent->m_RuleList);
|
||||
|
||||
ruleLists.push_back(m_RuleList);
|
||||
|
||||
ValidateDictionary(attrs, ruleLists, locations);
|
||||
}
|
||||
|
||||
String ConfigType::LocationToString(const vector<String>& locations)
|
||||
{
|
||||
bool first = true;
|
||||
String stack;
|
||||
BOOST_FOREACH(const String& location, locations) {
|
||||
if (!first)
|
||||
stack += " -> ";
|
||||
else
|
||||
first = false;
|
||||
|
||||
stack += location;
|
||||
}
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
void ConfigType::ValidateDictionary(const Dictionary::Ptr& dictionary,
|
||||
const vector<TypeRuleList::Ptr>& ruleLists, vector<String>& locations)
|
||||
{
|
||||
BOOST_FOREACH(const TypeRuleList::Ptr& ruleList, ruleLists) {
|
||||
BOOST_FOREACH(const String& require, ruleList->GetRequires()) {
|
||||
locations.push_back("Attribute '" + require + "'");
|
||||
|
||||
Value value = dictionary->Get(require);
|
||||
|
||||
if (value.IsEmpty())
|
||||
ConfigCompilerContext::GetContext()->AddError(false, "Required attribute is missing: " + LocationToString(locations));
|
||||
|
||||
locations.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
String key;
|
||||
Value value;
|
||||
BOOST_FOREACH(tie(key, value), dictionary) {
|
||||
TypeValidationResult overallResult = ValidationUnknownField;
|
||||
vector<TypeRuleList::Ptr> subRuleLists;
|
||||
|
||||
locations.push_back("Attribute '" + key + "'");
|
||||
|
||||
BOOST_FOREACH(const TypeRuleList::Ptr& ruleList, ruleLists) {
|
||||
TypeRuleList::Ptr subRuleList;
|
||||
TypeValidationResult result = ruleList->ValidateAttribute(key, value, &subRuleList);
|
||||
|
||||
if (subRuleList)
|
||||
subRuleLists.push_back(subRuleList);
|
||||
|
||||
if (result == ValidationOK) {
|
||||
overallResult = result;
|
||||
break;
|
||||
}
|
||||
|
||||
if (result == ValidationInvalidType)
|
||||
overallResult = result;
|
||||
}
|
||||
|
||||
if (overallResult == ValidationUnknownField)
|
||||
ConfigCompilerContext::GetContext()->AddError(true, "Unknown attribute: " + LocationToString(locations));
|
||||
else if (overallResult == ValidationInvalidType)
|
||||
ConfigCompilerContext::GetContext()->AddError(false, "Invalid type for attribute: " + LocationToString(locations));
|
||||
|
||||
if (subRuleLists.size() > 0 && value.IsObjectType<Dictionary>())
|
||||
ValidateDictionary(value, subRuleLists, locations);
|
||||
|
||||
locations.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* Icinga 2 *
|
||||
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU General Public License *
|
||||
* as published by the Free Software Foundation; either version 2 *
|
||||
* of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the Free Software Foundation *
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||
******************************************************************************/
|
||||
|
||||
#include "i2-config.h"
|
||||
|
||||
using namespace icinga;
|
||||
|
||||
ConfigType::ConfigType(const String& name, const DebugInfo& debuginfo)
|
||||
: m_Name(name), m_RuleList(boost::make_shared<TypeRuleList>()), m_DebugInfo(debuginfo)
|
||||
{ }
|
||||
|
||||
String ConfigType::GetName(void) const
|
||||
{
|
||||
return m_Name;
|
||||
}
|
||||
|
||||
String ConfigType::GetParent(void) const
|
||||
{
|
||||
return m_Parent;
|
||||
}
|
||||
|
||||
void ConfigType::SetParent(const String& parent)
|
||||
{
|
||||
m_Parent = parent;
|
||||
}
|
||||
|
||||
TypeRuleList::Ptr ConfigType::GetRuleList(void) const
|
||||
{
|
||||
return m_RuleList;
|
||||
}
|
||||
|
||||
DebugInfo ConfigType::GetDebugInfo(void) const
|
||||
{
|
||||
return m_DebugInfo;
|
||||
}
|
||||
|
||||
void ConfigType::ValidateItem(const ConfigItem::Ptr& item) const
|
||||
{
|
||||
Dictionary::Ptr attrs = item->Link();
|
||||
|
||||
/* Don't validate abstract items. */
|
||||
if (attrs->Get("__abstract"))
|
||||
return;
|
||||
|
||||
vector<String> locations;
|
||||
locations.push_back("Object '" + item->GetName() + "' (Type: '" + item->GetType() + "')");
|
||||
|
||||
ConfigType::Ptr parent;
|
||||
if (m_Parent.IsEmpty()) {
|
||||
if (GetName() != "DynamicObject")
|
||||
parent = ConfigCompilerContext::GetContext()->GetType("DynamicObject");
|
||||
} else {
|
||||
parent = ConfigCompilerContext::GetContext()->GetType(m_Parent);
|
||||
}
|
||||
|
||||
vector<TypeRuleList::Ptr> ruleLists;
|
||||
if (parent)
|
||||
ruleLists.push_back(parent->m_RuleList);
|
||||
|
||||
ruleLists.push_back(m_RuleList);
|
||||
|
||||
ValidateDictionary(attrs, ruleLists, locations);
|
||||
}
|
||||
|
||||
String ConfigType::LocationToString(const vector<String>& locations)
|
||||
{
|
||||
bool first = true;
|
||||
String stack;
|
||||
BOOST_FOREACH(const String& location, locations) {
|
||||
if (!first)
|
||||
stack += " -> ";
|
||||
else
|
||||
first = false;
|
||||
|
||||
stack += location;
|
||||
}
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
void ConfigType::ValidateDictionary(const Dictionary::Ptr& dictionary,
|
||||
const vector<TypeRuleList::Ptr>& ruleLists, vector<String>& locations)
|
||||
{
|
||||
BOOST_FOREACH(const TypeRuleList::Ptr& ruleList, ruleLists) {
|
||||
BOOST_FOREACH(const String& require, ruleList->GetRequires()) {
|
||||
locations.push_back("Attribute '" + require + "'");
|
||||
|
||||
Value value = dictionary->Get(require);
|
||||
|
||||
if (value.IsEmpty()) {
|
||||
ConfigCompilerContext::GetContext()->AddError(false,
|
||||
"Required attribute is missing: " + LocationToString(locations));
|
||||
}
|
||||
|
||||
locations.pop_back();
|
||||
}
|
||||
|
||||
String validator = ruleList->GetValidator();
|
||||
|
||||
if (!validator.IsEmpty()) {
|
||||
ScriptFunction::Ptr func = ScriptFunction::GetByName(validator);
|
||||
|
||||
if (!func)
|
||||
throw_exception(invalid_argument("Validator function '" + validator + "' does not exist."));
|
||||
|
||||
vector<Value> arguments;
|
||||
arguments.push_back(LocationToString(locations));
|
||||
arguments.push_back(dictionary);
|
||||
|
||||
ScriptTask::Ptr task = boost::make_shared<ScriptTask>(func, arguments);
|
||||
task->Start();
|
||||
task->Wait();
|
||||
task->GetResult();
|
||||
}
|
||||
}
|
||||
|
||||
String key;
|
||||
Value value;
|
||||
BOOST_FOREACH(tie(key, value), dictionary) {
|
||||
TypeValidationResult overallResult = ValidationUnknownField;
|
||||
vector<TypeRuleList::Ptr> subRuleLists;
|
||||
|
||||
locations.push_back("Attribute '" + key + "'");
|
||||
|
||||
BOOST_FOREACH(const TypeRuleList::Ptr& ruleList, ruleLists) {
|
||||
TypeRuleList::Ptr subRuleList;
|
||||
TypeValidationResult result = ruleList->ValidateAttribute(key, value, &subRuleList);
|
||||
|
||||
if (subRuleList)
|
||||
subRuleLists.push_back(subRuleList);
|
||||
|
||||
if (result == ValidationOK) {
|
||||
overallResult = result;
|
||||
break;
|
||||
}
|
||||
|
||||
if (result == ValidationInvalidType)
|
||||
overallResult = result;
|
||||
}
|
||||
|
||||
if (overallResult == ValidationUnknownField)
|
||||
ConfigCompilerContext::GetContext()->AddError(true, "Unknown attribute: " + LocationToString(locations));
|
||||
else if (overallResult == ValidationInvalidType)
|
||||
ConfigCompilerContext::GetContext()->AddError(false, "Invalid type for attribute: " + LocationToString(locations));
|
||||
|
||||
if (subRuleLists.size() > 0 && value.IsObjectType<Dictionary>())
|
||||
ValidateDictionary(value, subRuleLists, locations);
|
||||
|
||||
locations.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ using namespace icinga;
|
|||
map<String, vector<Service::WeakPtr> > Host::m_ServicesCache;
|
||||
bool Host::m_ServicesCacheValid = true;
|
||||
|
||||
REGISTER_SCRIPTFUNCTION("native::ValidateHostItem", &Host::ValidateHostItem);
|
||||
REGISTER_SCRIPTFUNCTION("native::ValidateServiceDictionary", &Host::ValidateServiceDictionary);
|
||||
|
||||
static AttributeDescription hostAttributes[] = {
|
||||
{ "acknowledgement", Attribute_Replicated },
|
||||
|
@ -407,18 +407,40 @@ void Host::ValidateServicesCache(void)
|
|||
m_ServicesCacheValid = true;
|
||||
}
|
||||
|
||||
void Host::ValidateHostItem(const ScriptTask::Ptr& task, const vector<Value>& arguments)
|
||||
void Host::ValidateServiceDictionary(const ScriptTask::Ptr& task, const vector<Value>& arguments)
|
||||
{
|
||||
if (arguments.size() < 1)
|
||||
throw_exception(invalid_argument("Missing argument: Host config item must be specified."));
|
||||
throw_exception(invalid_argument("Missing argument: Location must be specified."));
|
||||
|
||||
if (arguments.size() < 2)
|
||||
throw_exception(invalid_argument("Missing argument: Attribute dictionary must be specified."));
|
||||
|
||||
ConfigItem::Ptr item = arguments[0];
|
||||
String location = arguments[0];
|
||||
Dictionary::Ptr attrs = arguments[1];
|
||||
|
||||
// TODO: validate item
|
||||
String key;
|
||||
Value value;
|
||||
BOOST_FOREACH(tie(key, value), attrs) {
|
||||
String name;
|
||||
|
||||
ConfigCompilerContext::GetContext()->AddError(false, "Hello World!");
|
||||
if (value.IsScalar()) {
|
||||
name = value;
|
||||
} else if (value.IsObjectType<Dictionary>()) {
|
||||
Dictionary::Ptr serviceDesc = value;
|
||||
|
||||
if (serviceDesc->Contains("service"))
|
||||
name = serviceDesc->Get("service");
|
||||
else
|
||||
name = key;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!ConfigItem::GetObject("Service", name)) {
|
||||
ConfigCompilerContext::GetContext()->AddError(false, "Validation failed for " +
|
||||
location + ": Service '" + name + "' not found.");
|
||||
}
|
||||
}
|
||||
|
||||
task->FinishResult(Empty);
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ public:
|
|||
set<shared_ptr<Service> > GetServices(void) const;
|
||||
static void InvalidateServicesCache(void);
|
||||
|
||||
static void ValidateHostItem(const ScriptTask::Ptr& task,
|
||||
static void ValidateServiceDictionary(const ScriptTask::Ptr& task,
|
||||
const std::vector<icinga::Value>& arguments);
|
||||
|
||||
protected:
|
||||
|
|
Loading…
Reference in New Issue