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.
This commit is contained in:
Julian Brost 2021-01-07 17:48:01 +01:00
parent 0276c0b052
commit 88e5744d54
5 changed files with 17 additions and 22 deletions

View File

@ -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 },

View File

@ -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)

View File

@ -45,7 +45,7 @@ public:
static int GetNextDowntimeID();
static String AddDowntime(const intrusive_ptr<Checkable>& checkable, const String& author,
static Ptr AddDowntime(const intrusive_ptr<Checkable>& 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(),

View File

@ -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]));
}
}

View File

@ -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() << "'.";
}
}
}