mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-13 16:54:27 +02:00
parent
eadb309dab
commit
e047e06fc8
@ -352,29 +352,34 @@ Example:
|
|||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
type Pizza {
|
type Pizza {
|
||||||
number radius,
|
%require "radius",
|
||||||
|
%attribute number "radius",
|
||||||
|
|
||||||
dictionary ingredients {
|
%attribute dictionary "ingredients" {
|
||||||
string *,
|
%validator "native::ValidateIngredients",
|
||||||
|
|
||||||
dictionary * {
|
%attribute string "*",
|
||||||
number quantity,
|
|
||||||
string name
|
%attribute dictionary "*" {
|
||||||
|
%attribute number "quantity",
|
||||||
|
%attribute string "name"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
any custom::*
|
%attribute any "custom::*"
|
||||||
}
|
}
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
The Pizza definition provides the following validation rules:
|
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
|
* Pizza objects may contain an attribute "ingredients" which has to be a
|
||||||
dictionary.
|
dictionary.
|
||||||
* Elements in the ingredients dictionary can be either a string or 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
|
* If they're a dictionary they may contain attributes "quantity" (of type
|
||||||
number) and "name" (of type string).
|
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
|
* Pizza objects may contain attribute matching the pattern "custom::*" of any
|
||||||
type.
|
type.
|
||||||
|
|
||||||
|
@ -58,7 +58,29 @@ type Host {
|
|||||||
%attribute string "*"
|
%attribute string "*"
|
||||||
},
|
},
|
||||||
%attribute dictionary "services" {
|
%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 */
|
/* 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)
|
void Utility::WaitUntil(const function<bool (void)>& predicate)
|
||||||
{
|
{
|
||||||
while (!predicate())
|
while (!predicate()) {
|
||||||
Application::GetInstance()->ProcessEvents();
|
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);
|
Value value = dictionary->Get(require);
|
||||||
|
|
||||||
if (value.IsEmpty())
|
if (value.IsEmpty()) {
|
||||||
ConfigCompilerContext::GetContext()->AddError(false, "Required attribute is missing: " + LocationToString(locations));
|
ConfigCompilerContext::GetContext()->AddError(false,
|
||||||
|
"Required attribute is missing: " + LocationToString(locations));
|
||||||
|
}
|
||||||
|
|
||||||
locations.pop_back();
|
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;
|
String key;
|
||||||
|
@ -24,7 +24,7 @@ using namespace icinga;
|
|||||||
map<String, vector<Service::WeakPtr> > Host::m_ServicesCache;
|
map<String, vector<Service::WeakPtr> > Host::m_ServicesCache;
|
||||||
bool Host::m_ServicesCacheValid = true;
|
bool Host::m_ServicesCacheValid = true;
|
||||||
|
|
||||||
REGISTER_SCRIPTFUNCTION("native::ValidateHostItem", &Host::ValidateHostItem);
|
REGISTER_SCRIPTFUNCTION("native::ValidateServiceDictionary", &Host::ValidateServiceDictionary);
|
||||||
|
|
||||||
static AttributeDescription hostAttributes[] = {
|
static AttributeDescription hostAttributes[] = {
|
||||||
{ "acknowledgement", Attribute_Replicated },
|
{ "acknowledgement", Attribute_Replicated },
|
||||||
@ -407,18 +407,40 @@ void Host::ValidateServicesCache(void)
|
|||||||
m_ServicesCacheValid = true;
|
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)
|
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)
|
if (arguments.size() < 2)
|
||||||
throw_exception(invalid_argument("Missing argument: Attribute dictionary must be specified."));
|
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];
|
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;
|
set<shared_ptr<Service> > GetServices(void) const;
|
||||||
static void InvalidateServicesCache(void);
|
static void InvalidateServicesCache(void);
|
||||||
|
|
||||||
static void ValidateHostItem(const ScriptTask::Ptr& task,
|
static void ValidateServiceDictionary(const ScriptTask::Ptr& task,
|
||||||
const std::vector<icinga::Value>& arguments);
|
const std::vector<icinga::Value>& arguments);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user