From f311dfb775696bbf77e833cbd17a22f8dffa6436 Mon Sep 17 00:00:00 2001
From: "Alexander A. Klimov" <alexander.klimov@icinga.com>
Date: Wed, 18 Mar 2020 16:31:35 +0100
Subject: [PATCH] Apply rules: import default templates first

... to allow to override the attributes they set.

refs #7914
---
 lib/icinga/dependency-apply.cpp        | 4 ++--
 lib/icinga/notification-apply.cpp      | 4 ++--
 lib/icinga/scheduleddowntime-apply.cpp | 4 ++--
 lib/icinga/service-apply.cpp           | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/lib/icinga/dependency-apply.cpp b/lib/icinga/dependency-apply.cpp
index 0f8eddc5b..9830a1dac 100644
--- a/lib/icinga/dependency-apply.cpp
+++ b/lib/icinga/dependency-apply.cpp
@@ -35,6 +35,8 @@ bool Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, cons
 	builder.SetScope(frame.Locals->ShallowClone());
 	builder.SetIgnoreOnError(rule.GetIgnoreOnError());
 
+	builder.AddExpression(new ImportDefaultTemplatesExpression());
+
 	Host::Ptr host;
 	Service::Ptr service;
 	tie(host, service) = GetHostService(checkable);
@@ -52,8 +54,6 @@ bool Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, cons
 
 	builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "package"), OpSetLiteral, MakeLiteral(rule.GetPackage()), di));
 
-	builder.AddExpression(new ImportDefaultTemplatesExpression());
-
 	builder.AddExpression(new OwnedExpression(rule.GetExpression()));
 
 	ConfigItem::Ptr dependencyItem = builder.Compile();
diff --git a/lib/icinga/notification-apply.cpp b/lib/icinga/notification-apply.cpp
index 713bacea7..5fbc22190 100644
--- a/lib/icinga/notification-apply.cpp
+++ b/lib/icinga/notification-apply.cpp
@@ -35,6 +35,8 @@ bool Notification::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, co
 	builder.SetScope(frame.Locals->ShallowClone());
 	builder.SetIgnoreOnError(rule.GetIgnoreOnError());
 
+	builder.AddExpression(new ImportDefaultTemplatesExpression());
+
 	Host::Ptr host;
 	Service::Ptr service;
 	tie(host, service) = GetHostService(checkable);
@@ -53,8 +55,6 @@ bool Notification::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, co
 
 	builder.AddExpression(new OwnedExpression(rule.GetExpression()));
 
-	builder.AddExpression(new ImportDefaultTemplatesExpression());
-
 	ConfigItem::Ptr notificationItem = builder.Compile();
 	notificationItem->Register();
 
diff --git a/lib/icinga/scheduleddowntime-apply.cpp b/lib/icinga/scheduleddowntime-apply.cpp
index 770688cd4..36b33cfe4 100644
--- a/lib/icinga/scheduleddowntime-apply.cpp
+++ b/lib/icinga/scheduleddowntime-apply.cpp
@@ -34,6 +34,8 @@ bool ScheduledDowntime::EvaluateApplyRuleInstance(const Checkable::Ptr& checkabl
 	builder.SetScope(frame.Locals->ShallowClone());
 	builder.SetIgnoreOnError(rule.GetIgnoreOnError());
 
+	builder.AddExpression(new ImportDefaultTemplatesExpression());
+
 	Host::Ptr host;
 	Service::Ptr service;
 	tie(host, service) = GetHostService(checkable);
@@ -52,8 +54,6 @@ bool ScheduledDowntime::EvaluateApplyRuleInstance(const Checkable::Ptr& checkabl
 
 	builder.AddExpression(new OwnedExpression(rule.GetExpression()));
 
-	builder.AddExpression(new ImportDefaultTemplatesExpression());
-
 	ConfigItem::Ptr downtimeItem = builder.Compile();
 	downtimeItem->Register();
 
diff --git a/lib/icinga/service-apply.cpp b/lib/icinga/service-apply.cpp
index 6479aac52..89de8d767 100644
--- a/lib/icinga/service-apply.cpp
+++ b/lib/icinga/service-apply.cpp
@@ -34,6 +34,8 @@ bool Service::EvaluateApplyRuleInstance(const Host::Ptr& host, const String& nam
 	builder.SetScope(frame.Locals->ShallowClone());
 	builder.SetIgnoreOnError(rule.GetIgnoreOnError());
 
+	builder.AddExpression(new ImportDefaultTemplatesExpression());
+
 	builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di));
 
 	builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "name"), OpSetLiteral, MakeLiteral(name), di));
@@ -47,8 +49,6 @@ bool Service::EvaluateApplyRuleInstance(const Host::Ptr& host, const String& nam
 
 	builder.AddExpression(new OwnedExpression(rule.GetExpression()));
 
-	builder.AddExpression(new ImportDefaultTemplatesExpression());
-
 	ConfigItem::Ptr serviceItem = builder.Compile();
 	serviceItem->Register();