Add filter targets for remove-{comment,downtime} actions

Drop remove-all-{comments,downtimes}. This is provided
by using host and service filters.

fixes #10561
This commit is contained in:
Michael Friedrich 2015-11-08 12:41:47 +01:00
parent b7f923dd01
commit 1d2606cbfe
3 changed files with 93 additions and 126 deletions

View File

@ -257,7 +257,7 @@ If you're only interested in a single object you can limit the output to that ob
https://localhost:5665/v1/objects/hosts?host=localhost https://localhost:5665/v1/objects/hosts?host=localhost
The name of the URL parameter is the lower-case version of the type the query applies to. For **The name of the URL parameter is the lower-case version of the type the query applies to.** For
example, for `Host` objects the URL parameter therefore is `host`, for `Service` objects it is example, for `Host` objects the URL parameter therefore is `host`, for `Service` objects it is
`service` and so on. `service` and so on.
@ -578,30 +578,6 @@ The following example adds a comment for all `ping4` services:
] ]
} }
### <a id="icinga2-api-actions-remove-all-comments"></a> remove-all-comments
Removes all comments for services or hosts.
Send a `POST` request to the URL endpoint `/v1/actions/remove-all-comments`.
A [filter](9-icinga2-api.md#icinga2-api-filters) must be provided. The valid types for this action are `Host` and `Service`.
The following example removes all comments from all services:
$ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/actions/remove-all-comments?type=Service' | python -m json.tool
{
"results": [
{
"code": 200.0,
"status": "Successfully removed comments for object 'example2.localdomain!aws-health'."
},
{
"code": 200.0,
"status": "Successfully removed comments for object 'example.localdomain!aws-health'."
}
}
### <a id="icinga2-api-actions-remove-comment"></a> remove-comment ### <a id="icinga2-api-actions-remove-comment"></a> remove-comment
Remove the comment using its `name` attribute , returns `OK` if the Remove the comment using its `name` attribute , returns `OK` if the
@ -611,24 +587,37 @@ Icinga 2 when [adding a comment](9-icinga2-api.md#icinga2-api-actions-add-commen
Send a `POST` request to the URL endpoint `/v1/actions/remove-comment`. Send a `POST` request to the URL endpoint `/v1/actions/remove-comment`.
Parameter | Type | Description A [filter](9-icinga2-api.md#icinga2-api-filters) must be provided. The valid types for this action are `Host`, `Service` and `Comment`.
------------|---------|--------------
name | string | **Required.** Name of the comment to remove.
Does not support a target type or filters. Example for a simple filter using the `comment` URL parameter:
Example: $ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/actions/remove-comment?comment=example2.localdomain!ping4!mbmif.local-1446986367-0' | python -m json.tool
$ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/actions/remove-comment?name=example.localdomain!ping4!example.localdomain-1446824161-0' | python -m json.tool
{ {
"results": [ "results": [
{ {
"code": 200.0, "code": 200.0,
"status": "Successfully removed comment 'example.localdomain!ping4!example.localdomain-1446824161-0'." "status": "Successfully removed comment 'example2.localdomain!ping4!mbmif.local-1446986367-0'."
} }
] ]
} }
Example for removing all service comments using a service name filter for `ping4`:
$ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/actions/remove-comment?filter=service.name==%22ping4%22&type=Service' | python -m json.tool
{
"results": [
{
"code": 200.0,
"status": "Successfully removed all comments for object 'example2.localdomain!ping4'."
},
{
"code": 200.0,
"status": "Successfully removed all comments for object 'example.localdomain!ping4'."
}
]
}
### <a id="icinga2-api-actions-schedule-downtime"></a> schedule-downtime ### <a id="icinga2-api-actions-schedule-downtime"></a> schedule-downtime
Schedule a downtime for hosts and services. Schedule a downtime for hosts and services.
@ -665,32 +654,6 @@ Example:
] ]
} }
### <a id="icinga2-api-actions-remove-all-downtimes"></a> remove-all-downtimes
Removes all downtimes for services or hosts.
Send a `POST` request to the URL endpoint `/v1/actions/remove-all-downtimes`.
A [filter](9-icinga2-api.md#icinga2-api-filters) must be provided. The valid types for this action are `Host` and `Service`.
The following example removes all downtimes for all `ping4` services:
$ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/actions/remove-all-downtimes?type=Service&filter=service.name==%22ping4%22' | python -m json.tool
{
"results": [
{
"code": 200.0,
"status": "Successfully removed downtimes for object 'example2.localdomain!ping4'."
},
{
"code": 200.0,
"status": "Successfully removed downtimes for object 'example.localdomain!ping4'."
}
]
}
### <a id="icinga2-api-actions-remove-downtime"></a> remove-downtime ### <a id="icinga2-api-actions-remove-downtime"></a> remove-downtime
Remove the downtime using its `name` attribute , returns `OK` if the Remove the downtime using its `name` attribute , returns `OK` if the
@ -700,20 +663,28 @@ Icinga 2 when [scheduling a downtime](9-icinga2-api.md#icinga2-api-actions-sched
Send a `POST` request to the URL endpoint `/v1/actions/remove-downtime`. Send a `POST` request to the URL endpoint `/v1/actions/remove-downtime`.
Parameter | Type | Description A [filter](9-icinga2-api.md#icinga2-api-filters) must be provided. The valid types for this action are `Host`, `Service` and `Downtime`.
-------------|---------|--------------
name | string | **Required.** Name of the downtime to remove.
Does not support a target type or filter. Example for a simple filter using the `downtime` URL parameter:
Example: $ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/actions/remove-downtime?downtime=example.localdomain!ping4!mbmif.local-1446979168-6' | python -m json.tool
$ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/actions/remove-downtime?name=example.localdomain!ping4!example.localdomain-1446822004-1' | python -m json.tool
{ {
"results": [ "results": [
{ {
"code": 200.0, "code": 200.0,
"status": "Successfully removed downtime 'example.localdomain!ping4!example.localdomain-1446822004-1'." "status": "Successfully removed downtime 'example.localdomain!ping4!mbmif.local-1446979168-6'."
}
]
}
Example for removing all host downtimes using a host name filter for `example.localdomain`:
$ curl -k -s -u root:icinga -H 'Accept: application/json' -X POST 'https://localhost:5665/v1/actions/remove-downtime?filter=host.name==%22example.localdomain%22&type=Host' | python -m json.tool
{
"results": [
{
"code": 200.0,
"status": "Successfully removed all downtimes for object 'example.localdomain'."
} }
] ]
} }

View File

@ -40,11 +40,9 @@ REGISTER_APIACTION(delay_notification, "Service;Host", &ApiActions::DelayNotific
REGISTER_APIACTION(acknowledge_problem, "Service;Host", &ApiActions::AcknowledgeProblem); REGISTER_APIACTION(acknowledge_problem, "Service;Host", &ApiActions::AcknowledgeProblem);
REGISTER_APIACTION(remove_acknowledgement, "Service;Host", &ApiActions::RemoveAcknowledgement); REGISTER_APIACTION(remove_acknowledgement, "Service;Host", &ApiActions::RemoveAcknowledgement);
REGISTER_APIACTION(add_comment, "Service;Host", &ApiActions::AddComment); REGISTER_APIACTION(add_comment, "Service;Host", &ApiActions::AddComment);
REGISTER_APIACTION(remove_all_comments, "Service;Host", &ApiActions::RemoveAllComments); REGISTER_APIACTION(remove_comment, "Service;Host;Comment", &ApiActions::RemoveComment);
REGISTER_APIACTION(remove_comment, "", &ApiActions::RemoveComment);
REGISTER_APIACTION(schedule_downtime, "Service;Host", &ApiActions::ScheduleDowntime); REGISTER_APIACTION(schedule_downtime, "Service;Host", &ApiActions::ScheduleDowntime);
REGISTER_APIACTION(remove_all_downtimes, "Service;Host", &ApiActions::RemoveAllDowntimes); REGISTER_APIACTION(remove_downtime, "Service;Host;Downtime", &ApiActions::RemoveDowntime);
REGISTER_APIACTION(remove_downtime, "", &ApiActions::RemoveDowntime);
REGISTER_APIACTION(shutdown_process, "", &ApiActions::ShutdownProcess); REGISTER_APIACTION(shutdown_process, "", &ApiActions::ShutdownProcess);
REGISTER_APIACTION(restart_process, "", &ApiActions::RestartProcess); REGISTER_APIACTION(restart_process, "", &ApiActions::RestartProcess);
@ -250,51 +248,51 @@ Dictionary::Ptr ApiActions::AddComment(const ConfigObject::Ptr& object,
if (!params->Contains("author") || !params->Contains("comment")) if (!params->Contains("author") || !params->Contains("comment"))
return ApiActions::CreateResult(403, "Comments require author and comment."); return ApiActions::CreateResult(403, "Comments require author and comment.");
String comment_name = Comment::AddComment(checkable, CommentUser, String commentName = Comment::AddComment(checkable, CommentUser,
HttpUtility::GetLastParameter(params, "author"), HttpUtility::GetLastParameter(params, "author"),
HttpUtility::GetLastParameter(params, "comment"), 0); HttpUtility::GetLastParameter(params, "comment"), 0);
Comment::Ptr comment = Comment::GetByName(comment_name); Comment::Ptr comment = Comment::GetByName(commentName);
int legacy_id = comment->GetLegacyId();
Dictionary::Ptr additional = new Dictionary(); Dictionary::Ptr additional = new Dictionary();
additional->Set("name", comment_name); additional->Set("name", commentName);
additional->Set("legacy_id", legacy_id); additional->Set("legacy_id", comment->GetLegacyId());
return ApiActions::CreateResult(200, "Successfully added comment '" return ApiActions::CreateResult(200, "Successfully added comment '"
+ comment_name + "' for object '" + checkable->GetName() + commentName + "' for object '" + checkable->GetName()
+ "'.", additional); + "'.", additional);
} }
Dictionary::Ptr ApiActions::RemoveAllComments(const ConfigObject::Ptr& object,
const Dictionary::Ptr& params)
{
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
if (!checkable)
return ApiActions::CreateResult(404, "Cannot remove comment form non-existent object.");
checkable->RemoveAllComments();
return ApiActions::CreateResult(200, "Successfully removed comments for object '" + checkable->GetName() + "'.");
}
Dictionary::Ptr ApiActions::RemoveComment(const ConfigObject::Ptr& object, Dictionary::Ptr ApiActions::RemoveComment(const ConfigObject::Ptr& object,
const Dictionary::Ptr& params) const Dictionary::Ptr& params)
{ {
if (!params->Contains("name")) Checkable::Ptr checkable = dynamic_pointer_cast<Checkable>(object);
return ApiActions::CreateResult(403, "Parameter 'name' is required.");
String comment_name = HttpUtility::GetLastParameter(params, "name"); if (checkable) {
std::set<Comment::Ptr> comments = checkable->GetComments();
Comment::RemoveComment(comment_name); BOOST_FOREACH(const Comment::Ptr& comment, comments) {
Comment::RemoveComment(comment->GetName());
}
Comment::Ptr comment = Comment::GetByName(comment_name); return ApiActions::CreateResult(200, "Successfully removed all comments for object '" + checkable->GetName() + "'.");
}
Comment::Ptr comment = static_pointer_cast<Comment>(object);
if (!comment) if (!comment)
return ApiActions::CreateResult(200, "Successfully removed comment '" + comment_name + "'."); return ApiActions::CreateResult(404, "Cannot remove non-existent comment object.");
return ApiActions::CreateResult(403, "Could not remove comment '" + comment_name + "'."); String commentName = comment->GetName();
Comment::RemoveComment(commentName);
comment = Comment::GetByName(commentName);
if (comment)
return ApiActions::CreateResult(403, "Could not remove comment '" + commentName + "'.");
return ApiActions::CreateResult(200, "Successfully removed comment '" + commentName + "'.");
} }
Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object, Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object,
@ -316,7 +314,7 @@ Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object,
if (params->Contains("fixed")) if (params->Contains("fixed"))
fixed = HttpUtility::GetLastParameter(params, "fixed"); fixed = HttpUtility::GetLastParameter(params, "fixed");
String downtime_name = Downtime::AddDowntime(checkable, String downtimeName = Downtime::AddDowntime(checkable,
HttpUtility::GetLastParameter(params, "author"), HttpUtility::GetLastParameter(params, "author"),
HttpUtility::GetLastParameter(params, "comment"), HttpUtility::GetLastParameter(params, "comment"),
HttpUtility::GetLastParameter(params, "start_time"), HttpUtility::GetLastParameter(params, "start_time"),
@ -324,46 +322,46 @@ Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object,
HttpUtility::GetLastParameter(params, "trigger_name"), HttpUtility::GetLastParameter(params, "trigger_name"),
HttpUtility::GetLastParameter(params, "duration")); HttpUtility::GetLastParameter(params, "duration"));
Downtime::Ptr downtime = Downtime::GetByName(downtime_name); Downtime::Ptr downtime = Downtime::GetByName(downtimeName);
int legacy_id = downtime->GetLegacyId();
Dictionary::Ptr additional = new Dictionary(); Dictionary::Ptr additional = new Dictionary();
additional->Set("name", downtime_name); additional->Set("name", downtimeName);
additional->Set("legacy_id", legacy_id); additional->Set("legacy_id", downtime->GetLegacyId());
return ApiActions::CreateResult(200, "Successfully scheduled downtime '" + return ApiActions::CreateResult(200, "Successfully scheduled downtime '" +
downtime_name + "' for object '" + checkable->GetName() + "'.", additional); downtimeName + "' for object '" + checkable->GetName() + "'.", additional);
}
Dictionary::Ptr ApiActions::RemoveAllDowntimes(const ConfigObject::Ptr& object,
const Dictionary::Ptr& params)
{
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
if (!checkable)
return ApiActions::CreateResult(404, "Cannot remove downtime for non-existent object.");
checkable->RemoveAllDowntimes();
return ApiActions::CreateResult(200, "Successfully removed downtimes for object '" + checkable->GetName() + "'.");
} }
Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object, Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object,
const Dictionary::Ptr& params) const Dictionary::Ptr& params)
{ {
if (!params->Contains("name")) Checkable::Ptr checkable = dynamic_pointer_cast<Checkable>(object);
return ApiActions::CreateResult(403, "Parameter 'name' is required.");
String downtime_name = HttpUtility::GetLastParameter(params, "name"); if (checkable) {
std::set<Downtime::Ptr> downtimes = checkable->GetDowntimes();
Downtime::RemoveDowntime(downtime_name, true); BOOST_FOREACH(const Downtime::Ptr& downtime, downtimes) {
Downtime::RemoveDowntime(downtime->GetName(), true);
}
Downtime::Ptr downtime = Downtime::GetByName(downtime_name); return ApiActions::CreateResult(200, "Successfully removed all downtimes for object '" + checkable->GetName() + "'.");
}
Downtime::Ptr downtime = static_pointer_cast<Downtime>(object);
if (!downtime) if (!downtime)
return ApiActions::CreateResult(200, "Successfully removed downtime '" + downtime_name + "'."); return ApiActions::CreateResult(404, "Cannot remove non-existent downtime object.");
return ApiActions::CreateResult(403, "Could not remove downtime '" + downtime_name + "'."); String downtimeName = downtime->GetName();
Downtime::RemoveDowntime(downtimeName, true);
downtime = Downtime::GetByName(downtimeName);
if (downtime)
return ApiActions::CreateResult(403, "Could not remove downtime '" + downtimeName + "'.");
return ApiActions::CreateResult(200, "Successfully removed downtime '" + downtimeName + "'.");
} }
Dictionary::Ptr ApiActions::ShutdownProcess(const ConfigObject::Ptr& object, Dictionary::Ptr ApiActions::ShutdownProcess(const ConfigObject::Ptr& object,

View File

@ -40,10 +40,8 @@ public:
static Dictionary::Ptr AcknowledgeProblem(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr AcknowledgeProblem(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
static Dictionary::Ptr RemoveAcknowledgement(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr RemoveAcknowledgement(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
static Dictionary::Ptr AddComment(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr AddComment(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
static Dictionary::Ptr RemoveAllComments(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
static Dictionary::Ptr RemoveComment(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr RemoveComment(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
static Dictionary::Ptr ScheduleDowntime(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr ScheduleDowntime(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
static Dictionary::Ptr RemoveAllDowntimes(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
static Dictionary::Ptr RemoveDowntime(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr RemoveDowntime(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
static Dictionary::Ptr ShutdownProcess(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr ShutdownProcess(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);
static Dictionary::Ptr RestartProcess(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr RestartProcess(const ConfigObject::Ptr& object, const Dictionary::Ptr& params);