Skip to content

Commit

Permalink
API: Display a correct status when removing a scheduled downtime
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Dec 1, 2020
1 parent 04704a4 commit 3ea1289
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 19 deletions.
17 changes: 17 additions & 0 deletions lib/base/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "base/exception.hpp"
#include <boost/thread/tss.hpp>
#include <utility>

#ifdef _WIN32
# include "base/utility.hpp"
Expand Down Expand Up @@ -425,3 +426,19 @@ std::string icinga::to_string(const ContextTraceErrorInfo& e)
msgbuf << "[Context] = " << e.value();
return msgbuf.str();
}

invalid_downtime_removal_error::invalid_downtime_removal_error(String message)
: m_Message(std::move(message))
{ }

invalid_downtime_removal_error::invalid_downtime_removal_error(const char *message)
: m_Message(message)
{ }

invalid_downtime_removal_error::~invalid_downtime_removal_error() noexcept
{ }

const char *invalid_downtime_removal_error::what() const noexcept
{
return m_Message.CStr();
}
13 changes: 13 additions & 0 deletions lib/base/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ std::string to_string(const errinfo_getaddrinfo_error& e);
struct errinfo_message_;
typedef boost::error_info<struct errinfo_message_, std::string> errinfo_message;

class invalid_downtime_removal_error : virtual public std::exception, virtual public boost::exception {
public:
explicit invalid_downtime_removal_error(String message);
explicit invalid_downtime_removal_error(const char* message);

~invalid_downtime_removal_error() noexcept override;

const char *what() const noexcept final;

private:
String m_Message;
};

}

#endif /* EXCEPTION_H */
20 changes: 16 additions & 4 deletions lib/icinga/apiactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,13 @@ Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object,
downtime->SetRemovedBy(author);
}

Downtime::RemoveDowntime(downtime->GetName(), true);
try {
Downtime::RemoveDowntime(downtime->GetName(), true);
} catch (const invalid_downtime_removal_error& error) {
Log(LogWarning, "ApiActions") << error.what();

return ApiActions::CreateResult(400, error.what());
}
}

return ApiActions::CreateResult(200, "Successfully removed all downtimes for object '" + checkable->GetName() + "'.");
Expand All @@ -504,11 +510,17 @@ Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object,
downtime->SetRemovedBy(author);
}

String downtimeName = downtime->GetName();
try {
String downtimeName = downtime->GetName();

Downtime::RemoveDowntime(downtimeName, true);
Downtime::RemoveDowntime(downtimeName, true);

return ApiActions::CreateResult(200, "Successfully removed downtime '" + downtimeName + "'.");
return ApiActions::CreateResult(200, "Successfully removed downtime '" + downtimeName + "'.");
} catch (const invalid_downtime_removal_error& error) {
Log(LogWarning, "ApiActions") << error.what();

return ApiActions::CreateResult(400, error.what());
}
}

Dictionary::Ptr ApiActions::ShutdownProcess(const ConfigObject::Ptr& object,
Expand Down
7 changes: 4 additions & 3 deletions lib/icinga/downtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,10 @@ void Downtime::RemoveDowntime(const String& id, bool cancelled, bool expired, co
String config_owner = downtime->GetConfigOwner();

if (!config_owner.IsEmpty() && !expired) {
Log(LogWarning, "Downtime")
<< "Cannot remove downtime '" << downtime->GetName() << "'. It is owned by scheduled downtime object '" << config_owner << "'";
return;
String errorMessage = "Cannot remove downtime '" + downtime->GetName() +
"'. It is owned by scheduled downtime object '" + config_owner + "'";

BOOST_THROW_EXCEPTION(invalid_downtime_removal_error(errorMessage));
}

downtime->SetWasCancelled(cancelled);
Expand Down
46 changes: 34 additions & 12 deletions lib/icinga/externalcommandprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -970,10 +970,16 @@ void ExternalCommandProcessor::ScheduleSvcDowntime(double, const std::vector<Str
void ExternalCommandProcessor::DelSvcDowntime(double, const std::vector<String>& arguments)
{
int id = Convert::ToLong(arguments[0]);
Log(LogNotice, "ExternalCommandProcessor")
<< "Removing downtime ID " << arguments[0];
String rid = Downtime::GetDowntimeIDFromLegacyID(id);
Downtime::RemoveDowntime(rid, true);

try {
Downtime::RemoveDowntime(rid, true);

Log(LogNotice, "ExternalCommandProcessor")
<< "Removed downtime ID " << arguments[0];
} catch (const invalid_downtime_removal_error& error) {
Log(LogWarning, "ExternalCommandProcessor") << error.what();
}
}

void ExternalCommandProcessor::ScheduleHostDowntime(double, const std::vector<String>& arguments)
Expand Down Expand Up @@ -1072,10 +1078,16 @@ void ExternalCommandProcessor::ScheduleAndPropagateTriggeredHostDowntime(double,
void ExternalCommandProcessor::DelHostDowntime(double, const std::vector<String>& arguments)
{
int id = Convert::ToLong(arguments[0]);
Log(LogNotice, "ExternalCommandProcessor")
<< "Removing downtime ID " << arguments[0];
String rid = Downtime::GetDowntimeIDFromLegacyID(id);
Downtime::RemoveDowntime(rid, true);

try {
Downtime::RemoveDowntime(rid, true);

Log(LogNotice, "ExternalCommandProcessor")
<< "Removed downtime ID " << arguments[0];
} catch (const invalid_downtime_removal_error& error) {
Log(LogWarning, "ExternalCommandProcessor") << error.what();
}
}

void ExternalCommandProcessor::DelDowntimeByHostName(double, const std::vector<String>& arguments)
Expand All @@ -1102,10 +1114,15 @@ void ExternalCommandProcessor::DelDowntimeByHostName(double, const std::vector<S
<< ("Ignoring additional parameters for host '" + arguments[0] + "' downtime deletion.");

for (const Downtime::Ptr& downtime : host->GetDowntimes()) {
Log(LogNotice, "ExternalCommandProcessor")
<< "Removing downtime '" << downtime->GetName() << "'.";
try {
String downtimeName = downtime->GetName();
Downtime::RemoveDowntime(downtimeName, true);

Downtime::RemoveDowntime(downtime->GetName(), true);
Log(LogNotice, "ExternalCommandProcessor")
<< "Removed downtime '" << downtimeName << "'.";
} catch (const invalid_downtime_removal_error& error) {
Log(LogWarning, "ExternalCommandProcessor") << error.what();
}
}

for (const Service::Ptr& service : host->GetServices()) {
Expand All @@ -1119,10 +1136,15 @@ void ExternalCommandProcessor::DelDowntimeByHostName(double, const std::vector<S
if (!commentString.IsEmpty() && downtime->GetComment() != commentString)
continue;

Log(LogNotice, "ExternalCommandProcessor")
<< "Removing downtime '" << downtime->GetName() << "'.";
try {
String downtimeName = downtime->GetName();
Downtime::RemoveDowntime(downtimeName, true);

Downtime::RemoveDowntime(downtime->GetName(), true);
Log(LogNotice, "ExternalCommandProcessor")
<< "Removed downtime '" << downtimeName << "'.";
} catch (const invalid_downtime_removal_error& error) {
Log(LogWarning, "ExternalCommandProcessor") << error.what();
}
}
}
}
Expand Down

0 comments on commit 3ea1289

Please sign in to comment.