mirror of https://github.com/Icinga/icinga2.git
Merge pull request #8104 from Icinga/bugfix/remove-downtime-returns-wrong-status-7408
API: Display a correct status when removing a downtime
This commit is contained in:
commit
70b438a2bf
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "base/exception.hpp"
|
||||
#include <boost/thread/tss.hpp>
|
||||
#include <utility>
|
||||
|
||||
#ifdef _WIN32
|
||||
# include "base/utility.hpp"
|
||||
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -499,7 +499,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() + "'.");
|
||||
|
@ -515,11 +521,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,
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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()) {
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue