Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Display a correct status when removing a downtime #8104

Merged
merged 1 commit into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
julianbrost marked this conversation as resolved.
Show resolved Hide resolved

#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;
Al2Klimov marked this conversation as resolved.
Show resolved Hide resolved
};

}

#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 @@ -493,7 +493,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());
julianbrost marked this conversation as resolved.
Show resolved Hide resolved
}
}

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

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

Al2Klimov marked this conversation as resolved.
Show resolved Hide resolved
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
5 changes: 2 additions & 3 deletions lib/icinga/downtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,8 @@ 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;
BOOST_THROW_EXCEPTION(invalid_downtime_removal_error("Cannot remove downtime '" + downtime->GetName() +
"'. It is owned by scheduled downtime object '" + config_owner + "'"));
}

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