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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -103,11 +103,31 @@ void ConfigType::ValidateDictionary(const Dictionary::Ptr& dictionary,
|
|||
|
||||
Value value = dictionary->Get(require);
|
||||
|
||||
if (value.IsEmpty())
|
||||
ConfigCompilerContext::GetContext()->AddError(false, "Required attribute is missing: " + LocationToString(locations));
|
||||
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;
|
||||
|
|
|
@ -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