mirror of
https://github.com/Icinga/icinga2.git
synced 2026-02-03 20:40:17 -05:00
Merge bb7f4c2b94 into 274a0e39d5
This commit is contained in:
commit
fa0cd0a97c
13 changed files with 146 additions and 24 deletions
|
|
@ -11,9 +11,9 @@ ApplyRule::RuleMap ApplyRule::m_Rules;
|
|||
ApplyRule::TypeMap ApplyRule::m_Types;
|
||||
|
||||
ApplyRule::ApplyRule(String name, Expression::Ptr expression,
|
||||
Expression::Ptr filter, String package, String fkvar, String fvvar, Expression::Ptr fterm,
|
||||
Expression::Ptr filter, String zone, String package, String fkvar, String fvvar, Expression::Ptr fterm,
|
||||
bool ignoreOnError, DebugInfo di, Dictionary::Ptr scope)
|
||||
: m_Name(std::move(name)), m_Expression(std::move(expression)), m_Filter(std::move(filter)), m_Package(std::move(package)), m_FKVar(std::move(fkvar)),
|
||||
: m_Name(std::move(name)), m_Expression(std::move(expression)), m_Filter(std::move(filter)), m_Zone(std::move(zone)), m_Package(std::move(package)), m_FKVar(std::move(fkvar)),
|
||||
m_FVVar(std::move(fvvar)), m_FTerm(std::move(fterm)), m_IgnoreOnError(ignoreOnError), m_DebugInfo(std::move(di)), m_Scope(std::move(scope)), m_HasMatches(false)
|
||||
{ }
|
||||
|
||||
|
|
@ -32,6 +32,11 @@ Expression::Ptr ApplyRule::GetFilter() const
|
|||
return m_Filter;
|
||||
}
|
||||
|
||||
String ApplyRule::GetZone() const
|
||||
{
|
||||
return m_Zone;
|
||||
}
|
||||
|
||||
String ApplyRule::GetPackage() const
|
||||
{
|
||||
return m_Package;
|
||||
|
|
@ -58,7 +63,7 @@ Dictionary::Ptr ApplyRule::GetScope() const
|
|||
}
|
||||
|
||||
void ApplyRule::AddRule(const String& sourceType, const String& targetType, const String& name,
|
||||
const Expression::Ptr& expression, const Expression::Ptr& filter, const String& package, const String& fkvar,
|
||||
const Expression::Ptr& expression, const Expression::Ptr& filter, const String& zone, const String& package, const String& fkvar,
|
||||
const String& fvvar, const Expression::Ptr& fterm, bool ignoreOnError, const DebugInfo& di, const Dictionary::Ptr& scope)
|
||||
{
|
||||
auto actualTargetType (&targetType);
|
||||
|
|
@ -71,7 +76,7 @@ void ApplyRule::AddRule(const String& sourceType, const String& targetType, cons
|
|||
}
|
||||
}
|
||||
|
||||
ApplyRule::Ptr rule = new ApplyRule(name, expression, filter, package, fkvar, fvvar, fterm, ignoreOnError, di, scope);
|
||||
ApplyRule::Ptr rule = new ApplyRule(name, expression, filter, zone, package, fkvar, fvvar, fterm, ignoreOnError, di, scope);
|
||||
auto& rules (m_Rules[Type::GetByName(sourceType).get()]);
|
||||
|
||||
if (!AddTargetedRule(rule, *actualTargetType, rules)) {
|
||||
|
|
|
|||
|
|
@ -55,6 +55,7 @@ public:
|
|||
String GetName() const;
|
||||
Expression::Ptr GetExpression() const;
|
||||
Expression::Ptr GetFilter() const;
|
||||
String GetZone() const;
|
||||
String GetPackage() const;
|
||||
|
||||
inline const String& GetFKVar() const noexcept
|
||||
|
|
@ -77,7 +78,7 @@ public:
|
|||
bool EvaluateFilter(ScriptFrame& frame) const;
|
||||
|
||||
static void AddRule(const String& sourceType, const String& targetType, const String& name, const Expression::Ptr& expression,
|
||||
const Expression::Ptr& filter, const String& package, const String& fkvar, const String& fvvar, const Expression::Ptr& fterm,
|
||||
const Expression::Ptr& filter, const String& zone, const String& package, const String& fkvar, const String& fvvar, const Expression::Ptr& fterm,
|
||||
bool ignoreOnError, const DebugInfo& di, const Dictionary::Ptr& scope);
|
||||
static const std::vector<ApplyRule::Ptr>& GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType);
|
||||
static const std::set<ApplyRule::Ptr>& GetTargetedHostRules(const Type::Ptr& sourceType, const String& host);
|
||||
|
|
@ -97,6 +98,7 @@ private:
|
|||
String m_Name;
|
||||
Expression::Ptr m_Expression;
|
||||
Expression::Ptr m_Filter;
|
||||
String m_Zone;
|
||||
String m_Package;
|
||||
String m_FKVar;
|
||||
String m_FVVar;
|
||||
|
|
@ -117,7 +119,7 @@ private:
|
|||
static const Value * GetConst(Expression* exp, const Dictionary::Ptr& constants);
|
||||
|
||||
ApplyRule(String name, Expression::Ptr expression,
|
||||
Expression::Ptr filter, String package, String fkvar, String fvvar, Expression::Ptr fterm,
|
||||
Expression::Ptr filter, String zone, String package, String fkvar, String fvvar, Expression::Ptr fterm,
|
||||
bool ignoreOnError, DebugInfo di, Dictionary::Ptr scope);
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1221,7 +1221,7 @@ apply:
|
|||
std::unique_ptr<Expression> fterm{context->m_FTerm.top()};
|
||||
context->m_FTerm.pop();
|
||||
|
||||
$$ = new ApplyExpression(std::move(type), std::move(target), std::unique_ptr<Expression>($4), std::move(filter), context->GetPackage(), std::move(fkvar), std::move(fvvar), std::move(fterm), std::move(*$7), $8, std::unique_ptr<Expression>($10), DebugInfoRange(@2, @8));
|
||||
$$ = new ApplyExpression(std::move(type), std::move(target), std::unique_ptr<Expression>($4), std::move(filter), context->GetZone(), context->GetPackage(), std::move(fkvar), std::move(fvvar), std::move(fterm), std::move(*$7), $8, std::unique_ptr<Expression>($10), DebugInfoRange(@2, @8));
|
||||
delete $7;
|
||||
}
|
||||
;
|
||||
|
|
|
|||
|
|
@ -911,7 +911,7 @@ ExpressionResult ApplyExpression::DoEvaluate(ScriptFrame& frame, DebugHint*) con
|
|||
ExpressionResult nameres = m_Name->Evaluate(frame);
|
||||
CHECK_RESULT(nameres);
|
||||
|
||||
return VMOps::NewApply(frame, m_Type, m_Target, nameres.GetValue(), m_Filter,
|
||||
return VMOps::NewApply(frame, m_Type, m_Target, nameres.GetValue(), m_Filter, m_Zone,
|
||||
m_Package, m_FKVar, m_FVVar, m_FTerm, m_ClosedVars, m_IgnoreOnError, m_Expression, m_DebugInfo);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -823,11 +823,11 @@ class ApplyExpression final : public DebuggableExpression
|
|||
{
|
||||
public:
|
||||
ApplyExpression(String type, String target, std::unique_ptr<Expression> name,
|
||||
std::unique_ptr<Expression> filter, String package, String fkvar, String fvvar,
|
||||
std::unique_ptr<Expression> filter, String zone, String package, String fkvar, String fvvar,
|
||||
std::unique_ptr<Expression> fterm, std::map<String, std::unique_ptr<Expression> >&& closedVars, bool ignoreOnError,
|
||||
std::unique_ptr<Expression> expression, const DebugInfo& debugInfo = DebugInfo())
|
||||
: DebuggableExpression(debugInfo), m_Type(std::move(type)), m_Target(std::move(target)),
|
||||
m_Name(std::move(name)), m_Filter(filter.release()), m_Package(std::move(package)), m_FKVar(std::move(fkvar)), m_FVVar(std::move(fvvar)),
|
||||
m_Name(std::move(name)), m_Filter(filter.release()), m_Zone(std::move(zone)), m_Package(std::move(package)), m_FKVar(std::move(fkvar)), m_FVVar(std::move(fvvar)),
|
||||
m_FTerm(fterm.release()), m_IgnoreOnError(ignoreOnError), m_ClosedVars(std::move(closedVars)),
|
||||
m_Expression(expression.release())
|
||||
{ }
|
||||
|
|
@ -840,6 +840,7 @@ private:
|
|||
String m_Target;
|
||||
std::unique_ptr<Expression> m_Name;
|
||||
Expression::Ptr m_Filter;
|
||||
String m_Zone;
|
||||
String m_Package;
|
||||
String m_FKVar;
|
||||
String m_FVVar;
|
||||
|
|
|
|||
|
|
@ -115,11 +115,11 @@ public:
|
|||
return new Function(name, wrapper, argNames);
|
||||
}
|
||||
|
||||
static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const Expression::Ptr& filter,
|
||||
static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const Expression::Ptr& filter, const String& zone,
|
||||
const String& package, const String& fkvar, const String& fvvar, const Expression::Ptr& fterm, const std::map<String, std::unique_ptr<Expression> >& closedVars,
|
||||
bool ignoreOnError, const Expression::Ptr& expression, const DebugInfo& debugInfo = DebugInfo())
|
||||
{
|
||||
ApplyRule::AddRule(type, target, name, expression, filter, package, fkvar,
|
||||
ApplyRule::AddRule(type, target, name, expression, filter, zone, package, fkvar,
|
||||
fvvar, fterm, ignoreOnError, debugInfo, EvaluateClosedVars(frame, closedVars));
|
||||
|
||||
return Empty;
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ set(icinga_SOURCES
|
|||
i2-icinga.hpp icinga-itl.cpp
|
||||
apiactions.cpp apiactions.hpp
|
||||
apievents.cpp apievents.hpp
|
||||
apply-utility.cpp apply-utility.hpp
|
||||
checkable.cpp checkable.hpp checkable-ti.hpp
|
||||
checkable-check.cpp checkable-comment.cpp checkable-dependency.cpp
|
||||
checkable-downtime.cpp checkable-event.cpp checkable-flapping.cpp
|
||||
|
|
|
|||
73
lib/icinga/apply-utility.cpp
Normal file
73
lib/icinga/apply-utility.cpp
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/* Icinga 2 | (c) 2019 Icinga GmbH | GPLv2+ */
|
||||
|
||||
#include "base/debuginfo.hpp"
|
||||
#include "base/scriptframe.hpp"
|
||||
#include "base/value.hpp"
|
||||
#include "config/configcompiler.hpp"
|
||||
#include "config/expression.hpp"
|
||||
#include "icinga/apply-utility.hpp"
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
using namespace icinga;
|
||||
|
||||
/* Icinga DSL function which sets this.zone of a config object originated from an apply rule
|
||||
* to either the zone of the config object the apply rule matched or the zone of the apply rule.
|
||||
*/
|
||||
static const Value l_MakeCommonZoneDsl = ([]() -> Value {
|
||||
const char *dsl = R"EOF(
|
||||
|
||||
function(parent_object_zone, apply_rule_zone) {
|
||||
var child_zone = get_object(Zone, apply_rule_zone)
|
||||
|
||||
var common_zone = if (child_zone && child_zone.global) {
|
||||
parent_object_zone
|
||||
} else {
|
||||
apply_rule_zone
|
||||
}
|
||||
|
||||
if (common_zone != "") {
|
||||
this.zone = common_zone
|
||||
}
|
||||
}
|
||||
|
||||
)EOF";
|
||||
|
||||
ScriptFrame frame (false);
|
||||
auto expr (ConfigCompiler::CompileText("<anonymous>", dsl));
|
||||
|
||||
return std::move(expr->Evaluate(frame).GetValue());
|
||||
})();
|
||||
|
||||
/**
|
||||
* Create a DSL expression which sets this.zone of a config object originated from an apply rule
|
||||
*
|
||||
* @param parent_object_zone The zone of the config object the apply rule matched
|
||||
* @param apply_rule_zone The zone of the apply rule
|
||||
*
|
||||
* @return The newly created DSL expression or nullptr if nothing to do
|
||||
*/
|
||||
std::unique_ptr<Expression> ApplyUtility::MakeCommonZone(String parent_object_zone, String apply_rule_zone, const DebugInfo& debug_info)
|
||||
{
|
||||
if (parent_object_zone == apply_rule_zone) {
|
||||
if (parent_object_zone.IsEmpty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/* this.zone = parent_object_zone */
|
||||
return std::unique_ptr<Expression>(new SetExpression(MakeIndexer(ScopeThis, "zone"), OpSetLiteral, MakeLiteral(std::move(parent_object_zone)), debug_info));
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<Expression>> args;
|
||||
args.reserve(3);
|
||||
args.emplace_back(new GetScopeExpression(ScopeThis));
|
||||
args.emplace_back(MakeLiteral(std::move(parent_object_zone)));
|
||||
args.emplace_back(MakeLiteral(std::move(apply_rule_zone)));
|
||||
|
||||
/* l_MakeCommonZoneDsl.call(this, parent_object_zone, apply_rule_zone) */
|
||||
return std::unique_ptr<Expression>(new FunctionCallExpression(
|
||||
std::unique_ptr<Expression>(new IndexerExpression(MakeLiteral(l_MakeCommonZoneDsl), MakeLiteral("call"))),
|
||||
std::move(args),
|
||||
debug_info
|
||||
));
|
||||
}
|
||||
24
lib/icinga/apply-utility.hpp
Normal file
24
lib/icinga/apply-utility.hpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/* Icinga 2 | (c) 2019 Icinga GmbH | GPLv2+ */
|
||||
|
||||
#ifndef APPLY_UTILITY_H
|
||||
#define APPLY_UTILITY_H
|
||||
|
||||
#include "base/debuginfo.hpp"
|
||||
#include "base/string.hpp"
|
||||
#include "config/expression.hpp"
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace icinga
|
||||
{
|
||||
|
||||
namespace ApplyUtility
|
||||
{
|
||||
|
||||
std::unique_ptr<Expression> MakeCommonZone(String parent_object_zone, String apply_rule_zone, const DebugInfo& debug_info);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif /* APPLY_UTILITY_H */
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
||||
|
||||
#include "icinga/apply-utility.hpp"
|
||||
#include "icinga/dependency.hpp"
|
||||
#include "icinga/service.hpp"
|
||||
#include "config/configitembuilder.hpp"
|
||||
|
|
@ -47,10 +48,13 @@ bool Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, cons
|
|||
if (service)
|
||||
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "child_service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di));
|
||||
|
||||
String zone = checkable->GetZoneName();
|
||||
{
|
||||
auto expr (ApplyUtility::MakeCommonZone(checkable->GetZoneName(), rule.GetZone(), di));
|
||||
|
||||
if (!zone.IsEmpty())
|
||||
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "zone"), OpSetLiteral, MakeLiteral(zone), di));
|
||||
if (expr) {
|
||||
builder.AddExpression(expr.release());
|
||||
}
|
||||
}
|
||||
|
||||
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "package"), OpSetLiteral, MakeLiteral(rule.GetPackage()), di));
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
||||
|
||||
#include "icinga/apply-utility.hpp"
|
||||
#include "icinga/notification.hpp"
|
||||
#include "icinga/service.hpp"
|
||||
#include "config/configitembuilder.hpp"
|
||||
|
|
@ -46,10 +47,13 @@ bool Notification::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, co
|
|||
if (service)
|
||||
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di));
|
||||
|
||||
String zone = checkable->GetZoneName();
|
||||
{
|
||||
auto expr (ApplyUtility::MakeCommonZone(checkable->GetZoneName(), rule.GetZone(), di));
|
||||
|
||||
if (!zone.IsEmpty())
|
||||
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "zone"), OpSetLiteral, MakeLiteral(zone), di));
|
||||
if (expr) {
|
||||
builder.AddExpression(expr.release());
|
||||
}
|
||||
}
|
||||
|
||||
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "package"), OpSetLiteral, MakeLiteral(rule.GetPackage()), di));
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
||||
|
||||
#include "icinga/apply-utility.hpp"
|
||||
#include "icinga/scheduleddowntime.hpp"
|
||||
#include "icinga/service.hpp"
|
||||
#include "config/configitembuilder.hpp"
|
||||
|
|
@ -45,10 +46,13 @@ bool ScheduledDowntime::EvaluateApplyRuleInstance(const Checkable::Ptr& checkabl
|
|||
if (service)
|
||||
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di));
|
||||
|
||||
String zone = checkable->GetZoneName();
|
||||
{
|
||||
auto expr (ApplyUtility::MakeCommonZone(checkable->GetZoneName(), rule.GetZone(), di));
|
||||
|
||||
if (!zone.IsEmpty())
|
||||
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "zone"), OpSetLiteral, MakeLiteral(zone), di));
|
||||
if (expr) {
|
||||
builder.AddExpression(expr.release());
|
||||
}
|
||||
}
|
||||
|
||||
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "package"), OpSetLiteral, MakeLiteral(rule.GetPackage()), di));
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
||||
|
||||
#include "icinga/apply-utility.hpp"
|
||||
#include "icinga/service.hpp"
|
||||
#include "config/configitembuilder.hpp"
|
||||
#include "config/applyrule.hpp"
|
||||
|
|
@ -40,10 +41,13 @@ bool Service::EvaluateApplyRuleInstance(const Host::Ptr& host, const String& nam
|
|||
|
||||
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "name"), OpSetLiteral, MakeLiteral(name), di));
|
||||
|
||||
String zone = host->GetZoneName();
|
||||
{
|
||||
auto expr (ApplyUtility::MakeCommonZone(host->GetZoneName(), rule.GetZone(), di));
|
||||
|
||||
if (!zone.IsEmpty())
|
||||
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "zone"), OpSetLiteral, MakeLiteral(zone), di));
|
||||
if (expr) {
|
||||
builder.AddExpression(expr.release());
|
||||
}
|
||||
}
|
||||
|
||||
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "package"), OpSetLiteral, MakeLiteral(rule.GetPackage()), di));
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue