mirror of https://github.com/Icinga/icinga2.git
API: Display a correct status code when removing a scheduled downtime
This commit is contained in:
parent
6f33c2f90c
commit
dd02e3b6d8
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include "base/exception.hpp"
|
#include "base/exception.hpp"
|
||||||
#include <boost/thread/tss.hpp>
|
#include <boost/thread/tss.hpp>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
# include "base/utility.hpp"
|
# include "base/utility.hpp"
|
||||||
|
@ -425,3 +426,19 @@ std::string icinga::to_string(const ContextTraceErrorInfo& e)
|
||||||
msgbuf << "[Context] = " << e.value();
|
msgbuf << "[Context] = " << e.value();
|
||||||
return msgbuf.str();
|
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_;
|
struct errinfo_message_;
|
||||||
typedef boost::error_info<struct errinfo_message_, std::string> 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 */
|
#endif /* EXCEPTION_H */
|
||||||
|
|
|
@ -493,7 +493,13 @@ Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object,
|
||||||
downtime->SetRemovedBy(author);
|
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() + "'.");
|
return ApiActions::CreateResult(200, "Successfully removed all downtimes for object '" + checkable->GetName() + "'.");
|
||||||
|
@ -509,11 +515,17 @@ Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object,
|
||||||
downtime->SetRemovedBy(author);
|
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,
|
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();
|
String config_owner = downtime->GetConfigOwner();
|
||||||
|
|
||||||
if (!config_owner.IsEmpty() && !expired) {
|
if (!config_owner.IsEmpty() && !expired) {
|
||||||
Log(LogWarning, "Downtime")
|
BOOST_THROW_EXCEPTION(invalid_downtime_removal_error("Cannot remove downtime '" + downtime->GetName() +
|
||||||
<< "Cannot remove downtime '" << downtime->GetName() << "'. It is owned by scheduled downtime object '" << config_owner << "'";
|
"'. It is owned by scheduled downtime object '" + config_owner + "'"));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
downtime->SetWasCancelled(cancelled);
|
downtime->SetWasCancelled(cancelled);
|
||||||
|
|
|
@ -970,10 +970,16 @@ void ExternalCommandProcessor::ScheduleSvcDowntime(double, const std::vector<Str
|
||||||
void ExternalCommandProcessor::DelSvcDowntime(double, const std::vector<String>& arguments)
|
void ExternalCommandProcessor::DelSvcDowntime(double, const std::vector<String>& arguments)
|
||||||
{
|
{
|
||||||
int id = Convert::ToLong(arguments[0]);
|
int id = Convert::ToLong(arguments[0]);
|
||||||
Log(LogNotice, "ExternalCommandProcessor")
|
|
||||||
<< "Removing downtime ID " << arguments[0];
|
|
||||||
String rid = Downtime::GetDowntimeIDFromLegacyID(id);
|
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)
|
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)
|
void ExternalCommandProcessor::DelHostDowntime(double, const std::vector<String>& arguments)
|
||||||
{
|
{
|
||||||
int id = Convert::ToLong(arguments[0]);
|
int id = Convert::ToLong(arguments[0]);
|
||||||
Log(LogNotice, "ExternalCommandProcessor")
|
|
||||||
<< "Removing downtime ID " << arguments[0];
|
|
||||||
String rid = Downtime::GetDowntimeIDFromLegacyID(id);
|
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)
|
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.");
|
<< ("Ignoring additional parameters for host '" + arguments[0] + "' downtime deletion.");
|
||||||
|
|
||||||
for (const Downtime::Ptr& downtime : host->GetDowntimes()) {
|
for (const Downtime::Ptr& downtime : host->GetDowntimes()) {
|
||||||
Log(LogNotice, "ExternalCommandProcessor")
|
try {
|
||||||
<< "Removing downtime '" << downtime->GetName() << "'.";
|
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()) {
|
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)
|
if (!commentString.IsEmpty() && downtime->GetComment() != commentString)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
Log(LogNotice, "ExternalCommandProcessor")
|
try {
|
||||||
<< "Removing downtime '" << downtime->GetName() << "'.";
|
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