From 88e5744d54f79ab6a84260bd4a8d2cc0ffd43465 Mon Sep 17 00:00:00 2001 From: Julian Brost Date: Thu, 7 Jan 2021 17:48:01 +0100 Subject: [PATCH] AddDowntime: return Downtime::Ptr instead of String containing the name At numerous places in the code, something like this is performed: String name = Downtime::AddDowntime(...); Downtime::Ptr downtime = Downtime::GetByName(name); However, `downtime` can be a `nullptr` after this as it is possible that the downtime is deleted in between. This commit changes the return type of `Downtime::AddDowntime` to return a Downtime::Ptr instead of the full name of the downtime. `AddDowntime` performs the very same `GetByName()` operation internally, but handles the `nullptr` case correctly and throws an exception. --- lib/icinga/apiactions.cpp | 20 ++++++++------------ lib/icinga/downtime.cpp | 4 ++-- lib/icinga/downtime.hpp | 2 +- lib/icinga/externalcommandprocessor.cpp | 4 ++-- lib/icinga/scheduleddowntime.cpp | 9 ++++----- 5 files changed, 17 insertions(+), 22 deletions(-) diff --git a/lib/icinga/apiactions.cpp b/lib/icinga/apiactions.cpp index a0a168854..1abbd4cdd 100644 --- a/lib/icinga/apiactions.cpp +++ b/lib/icinga/apiactions.cpp @@ -381,10 +381,9 @@ Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object, } } - String downtimeName = Downtime::AddDowntime(checkable, author, comment, startTime, endTime, + Downtime::Ptr downtime = Downtime::AddDowntime(checkable, author, comment, startTime, endTime, fixed, triggerName, duration); - - Downtime::Ptr downtime = Downtime::GetByName(downtimeName); + String downtimeName = downtime->GetName(); Dictionary::Ptr additional = new Dictionary({ { "name", downtimeName }, @@ -404,10 +403,9 @@ Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object, Log(LogNotice, "ApiActions") << "Creating downtime for service " << hostService->GetName() << " on host " << host->GetName(); - String serviceDowntimeName = Downtime::AddDowntime(hostService, author, comment, startTime, endTime, + Downtime::Ptr serviceDowntime = Downtime::AddDowntime(hostService, author, comment, startTime, endTime, fixed, triggerName, duration); - - Downtime::Ptr serviceDowntime = Downtime::GetByName(serviceDowntimeName); + String serviceDowntimeName = serviceDowntime->GetName(); serviceDowntimes.push_back(new Dictionary({ { "name", serviceDowntimeName }, @@ -435,14 +433,13 @@ Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object, Log(LogNotice, "ApiActions") << "Scheduling downtime for child object " << child->GetName(); - String childDowntimeName = Downtime::AddDowntime(child, author, comment, startTime, endTime, + Downtime::Ptr childDowntime = Downtime::AddDowntime(child, author, comment, startTime, endTime, fixed, triggerName, duration); + String childDowntimeName = childDowntime->GetName(); Log(LogNotice, "ApiActions") << "Add child downtime '" << childDowntimeName << "'."; - Downtime::Ptr childDowntime = Downtime::GetByName(childDowntimeName); - Dictionary::Ptr childAdditional = new Dictionary({ { "name", childDowntimeName }, { "legacy_id", childDowntime->GetLegacyId() } @@ -460,10 +457,9 @@ Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object, Log(LogNotice, "ApiActions") << "Creating downtime for service " << hostService->GetName() << " on child host " << host->GetName(); - String serviceDowntimeName = Downtime::AddDowntime(hostService, author, comment, startTime, endTime, + Downtime::Ptr serviceDowntime = Downtime::AddDowntime(hostService, author, comment, startTime, endTime, fixed, triggerName, duration); - - Downtime::Ptr serviceDowntime = Downtime::GetByName(serviceDowntimeName); + String serviceDowntimeName = serviceDowntime->GetName(); childServiceDowntimes.push_back(new Dictionary({ { "name", serviceDowntimeName }, diff --git a/lib/icinga/downtime.cpp b/lib/icinga/downtime.cpp index 2bdec256c..9c618b786 100644 --- a/lib/icinga/downtime.cpp +++ b/lib/icinga/downtime.cpp @@ -206,7 +206,7 @@ int Downtime::GetNextDowntimeID() return l_NextDowntimeID; } -String Downtime::AddDowntime(const Checkable::Ptr& checkable, const String& author, +Downtime::Ptr Downtime::AddDowntime(const Checkable::Ptr& checkable, const String& author, const String& comment, double startTime, double endTime, bool fixed, const String& triggeredBy, double duration, const String& scheduledDowntime, const String& scheduledBy, @@ -302,7 +302,7 @@ String Downtime::AddDowntime(const Checkable::Ptr& checkable, const String& auth << "' and '" << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S", endTime) << "', author: '" << author << "', " << (fixed ? "fixed" : "flexible with " + Convert::ToString(duration) + "s duration"); - return fullName; + return downtime; } void Downtime::RemoveDowntime(const String& id, bool cancelled, bool expired, const MessageOrigin::Ptr& origin) diff --git a/lib/icinga/downtime.hpp b/lib/icinga/downtime.hpp index c33d9017d..6aac86148 100644 --- a/lib/icinga/downtime.hpp +++ b/lib/icinga/downtime.hpp @@ -45,7 +45,7 @@ public: static int GetNextDowntimeID(); - static String AddDowntime(const intrusive_ptr& checkable, const String& author, + static Ptr AddDowntime(const intrusive_ptr& checkable, const String& author, const String& comment, double startTime, double endTime, bool fixed, const String& triggeredBy, double duration, const String& scheduledDowntime = String(), const String& scheduledBy = String(), const String& id = String(), diff --git a/lib/icinga/externalcommandprocessor.cpp b/lib/icinga/externalcommandprocessor.cpp index e974a0175..4f23fe002 100644 --- a/lib/icinga/externalcommandprocessor.cpp +++ b/lib/icinga/externalcommandprocessor.cpp @@ -1049,7 +1049,7 @@ void ExternalCommandProcessor::ScheduleAndPropagateTriggeredHostDowntime(double, Log(LogNotice, "ExternalCommandProcessor") << "Creating downtime for host " << host->GetName(); - String parentDowntime = Downtime::AddDowntime(host, arguments[6], arguments[7], + Downtime::Ptr parentDowntime = Downtime::AddDowntime(host, arguments[6], arguments[7], Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]), Convert::ToBool(is_fixed), triggeredBy, Convert::ToDouble(arguments[5])); @@ -1065,7 +1065,7 @@ void ExternalCommandProcessor::ScheduleAndPropagateTriggeredHostDowntime(double, (void) Downtime::AddDowntime(child, arguments[6], arguments[7], Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]), - Convert::ToBool(is_fixed), parentDowntime, Convert::ToDouble(arguments[5])); + Convert::ToBool(is_fixed), parentDowntime->GetName(), Convert::ToDouble(arguments[5])); } } diff --git a/lib/icinga/scheduleddowntime.cpp b/lib/icinga/scheduleddowntime.cpp index 6642841f4..c18f4f045 100644 --- a/lib/icinga/scheduleddowntime.cpp +++ b/lib/icinga/scheduleddowntime.cpp @@ -253,11 +253,10 @@ void ScheduledDowntime::CreateNextDowntime() return; } - String downtimeName = Downtime::AddDowntime(GetCheckable(), GetAuthor(), GetComment(), + Downtime::Ptr downtime = Downtime::AddDowntime(GetCheckable(), GetAuthor(), GetComment(), segment.first, segment.second, GetFixed(), String(), GetDuration(), GetName(), GetName()); - - Downtime::Ptr downtime = Downtime::GetByName(downtimeName); + String downtimeName = downtime->GetName(); int childOptions = Downtime::ChildOptionsFromValue(GetChildOptions()); if (childOptions > 0) { @@ -275,11 +274,11 @@ void ScheduledDowntime::CreateNextDowntime() Log(LogNotice, "ScheduledDowntime") << "Scheduling downtime for child object " << child->GetName(); - String childDowntimeName = Downtime::AddDowntime(child, GetAuthor(), GetComment(), + Downtime::Ptr childDowntime = Downtime::AddDowntime(child, GetAuthor(), GetComment(), segment.first, segment.second, GetFixed(), triggerName, GetDuration(), GetName(), GetName()); Log(LogNotice, "ScheduledDowntime") - << "Add child downtime '" << childDowntimeName << "'."; + << "Add child downtime '" << childDowntime->GetName() << "'."; } } }