From d84efecb62f2052f279ca128eb7f44e0368e42dc Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 21 Nov 2019 15:53:40 +0100 Subject: [PATCH 1/3] /v1/actions/remove-comment: let users specify themselves --- lib/icinga/apiactions.cpp | 11 +++++++++++ lib/icinga/comment.ti | 2 ++ lib/icingadb/icingadb-objects.cpp | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/icinga/apiactions.cpp b/lib/icinga/apiactions.cpp index 757b37eca..3f0d2346f 100644 --- a/lib/icinga/apiactions.cpp +++ b/lib/icinga/apiactions.cpp @@ -280,12 +280,18 @@ Dictionary::Ptr ApiActions::AddComment(const ConfigObject::Ptr& object, Dictionary::Ptr ApiActions::RemoveComment(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) { + auto author (HttpUtility::GetLastParameter(params, "author")); Checkable::Ptr checkable = dynamic_pointer_cast(object); if (checkable) { std::set comments = checkable->GetComments(); for (const Comment::Ptr& comment : comments) { + { + ObjectLock oLock (comment); + comment->SetRemovedBy(author); + } + Comment::RemoveComment(comment->GetName()); } @@ -297,6 +303,11 @@ Dictionary::Ptr ApiActions::RemoveComment(const ConfigObject::Ptr& object, if (!comment) return ApiActions::CreateResult(404, "Cannot remove non-existent comment object."); + { + ObjectLock oLock (comment); + comment->SetRemovedBy(author); + } + String commentName = comment->GetName(); Comment::RemoveComment(commentName); diff --git a/lib/icinga/comment.ti b/lib/icinga/comment.ti index 4bf667f31..5a5bf5054 100644 --- a/lib/icinga/comment.ti +++ b/lib/icinga/comment.ti @@ -73,6 +73,8 @@ class Comment : ConfigObject < CommentNameComposer [config] bool persistent; [config] Timestamp expire_time; [state] int legacy_id; + + [no_user_view, no_user_modify] String removed_by; }; } diff --git a/lib/icingadb/icingadb-objects.cpp b/lib/icingadb/icingadb-objects.cpp index 0f8c01717..c5f430b5f 100644 --- a/lib/icingadb/icingadb-objects.cpp +++ b/lib/icingadb/icingadb-objects.cpp @@ -1485,7 +1485,7 @@ void IcingaDB::SendRemovedComment(const Comment::Ptr& comment) "comment_id", GetObjectIdentifier(comment), "environment_id", SHA1(GetEnvironment()), "entry_time", Convert::ToString(TimestampToMilliseconds(comment->GetEntryTime())), - "author", Utility::ValidateUTF8(comment->GetAuthor()), + "author", Utility::ValidateUTF8(comment->GetRemovedBy()), "comment", Utility::ValidateUTF8(comment->GetText()), "entry_type", Convert::ToString(comment->GetEntryType()), "is_persistent", Convert::ToString((unsigned short)comment->GetPersistent()), From 80bf316e7b5553264d43e1e3c845f230a1cb1f94 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 21 Nov 2019 16:28:22 +0100 Subject: [PATCH 2/3] /v1/actions/remove-acknowledgement: let users specify themselves --- lib/icinga/apiactions.cpp | 2 +- lib/icinga/checkable-comment.cpp | 10 ++++++++-- lib/icinga/checkable.hpp | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/icinga/apiactions.cpp b/lib/icinga/apiactions.cpp index 3f0d2346f..ce2c2d653 100644 --- a/lib/icinga/apiactions.cpp +++ b/lib/icinga/apiactions.cpp @@ -245,7 +245,7 @@ Dictionary::Ptr ApiActions::RemoveAcknowledgement(const ConfigObject::Ptr& objec + object->GetName() + "."); checkable->ClearAcknowledgement(); - checkable->RemoveCommentsByType(CommentAcknowledgement); + checkable->RemoveCommentsByType(CommentAcknowledgement, HttpUtility::GetLastParameter(params, "author")); return ApiActions::CreateResult(200, "Successfully removed acknowledgement for object '" + checkable->GetName() + "'."); } diff --git a/lib/icinga/checkable-comment.cpp b/lib/icinga/checkable-comment.cpp index b2184e1ff..88f5a5207 100644 --- a/lib/icinga/checkable-comment.cpp +++ b/lib/icinga/checkable-comment.cpp @@ -18,15 +18,21 @@ void Checkable::RemoveAllComments() } } -void Checkable::RemoveCommentsByType(int type) +void Checkable::RemoveCommentsByType(int type, const String& removedBy) { for (const Comment::Ptr& comment : GetComments()) { /* Do not remove persistent comments from an acknowledgement */ if (comment->GetEntryType() == CommentAcknowledgement && comment->GetPersistent()) continue; - if (comment->GetEntryType() == type) + if (comment->GetEntryType() == type) { + { + ObjectLock oLock (comment); + comment->SetRemovedBy(removedBy); + } + Comment::RemoveComment(comment->GetName()); + } } } diff --git a/lib/icinga/checkable.hpp b/lib/icinga/checkable.hpp index 498cb1d8f..a5d34725b 100644 --- a/lib/icinga/checkable.hpp +++ b/lib/icinga/checkable.hpp @@ -133,7 +133,7 @@ public: /* Comments */ void RemoveAllComments(); - void RemoveCommentsByType(int type); + void RemoveCommentsByType(int type, const String& removedBy = String()); std::set GetComments() const; void RegisterComment(const Comment::Ptr& comment); From 9f3e87711b74c5fde7d21739c92dfdf8ffa4096a Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Tue, 26 Nov 2019 14:58:02 +0100 Subject: [PATCH 3/3] Update docs --- doc/12-icinga2-api.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/doc/12-icinga2-api.md b/doc/12-icinga2-api.md index 616dd945f..94813e937 100644 --- a/doc/12-icinga2-api.md +++ b/doc/12-icinga2-api.md @@ -1204,7 +1204,11 @@ been removed the next notification will be sent again. Send a `POST` request to the URL endpoint `/v1/actions/remove-acknowledgement`. -A [filter](12-icinga2-api.md#icinga2-api-filters) must be provided. The valid types for this action are `Host` and `Service`. + Parameter | Type | Description + ----------|--------|-------------- + author | String | **Optional.** Name of the removal requestor. + +In addition to these parameters a [filter](12-icinga2-api.md#icinga2-api-filters) must be provided. The valid types for this action are `Host` and `Service`. The example removes all service acknowledgements: @@ -1272,7 +1276,11 @@ Icinga 2 when [adding a comment](12-icinga2-api.md#icinga2-api-actions-add-comme Send a `POST` request to the URL endpoint `/v1/actions/remove-comment`. -A [filter](12-icinga2-api.md#icinga2-api-filters) must be provided. The valid types for this action are `Host`, `Service` and `Comment`. + Parameter | Type | Description + ----------|--------|-------------- + author | String | **Optional.** Name of the removal requestor. + +In addition to these parameters a [filter](12-icinga2-api.md#icinga2-api-filters) must be provided. The valid types for this action are `Host`, `Service` and `Comment`. Example for a simple filter using the `comment` URL parameter: