Skip to content

Commit bf049ac

Browse files
Implement comment delete command (#466)
* implement comment delete command * Update cmd/comment/delete.go Co-authored-by: Marc Lopez Rubio <marc5.12@outlook.com> * Update cmd/comment/delete.go Co-authored-by: Marc Lopez Rubio <marc5.12@outlook.com> * address pr comments * make linter happy Co-authored-by: Marc Lopez Rubio <marc5.12@outlook.com>
1 parent 882c0b0 commit bf049ac

File tree

7 files changed

+314
-0
lines changed

7 files changed

+314
-0
lines changed

cmd/comment/delete.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package cmdcomment
19+
20+
import (
21+
"fmt"
22+
23+
"github.com/elastic/cloud-sdk-go/pkg/api/commentapi"
24+
"github.com/spf13/cobra"
25+
26+
cmdutil "github.com/elastic/ecctl/cmd/util"
27+
"github.com/elastic/ecctl/pkg/ecctl"
28+
)
29+
30+
var deleteCmd = &cobra.Command{
31+
Use: "delete <comment id> --resource-type <resource-type> --resource-id <resource-id>",
32+
Short: cmdutil.AdminReqDescription("Deletes a resource comment"),
33+
PreRunE: cobra.ExactValidArgs(1),
34+
RunE: func(cmd *cobra.Command, args []string) error {
35+
resourceType, _ := cmd.Flags().GetString("resource-type")
36+
resourceID, _ := cmd.Flags().GetString("resource-id")
37+
version, _ := cmd.Flags().GetString("version")
38+
39+
err := commentapi.Delete(commentapi.DeleteParams{
40+
API: ecctl.Get().API,
41+
Region: ecctl.Get().Config.Region,
42+
CommentID: args[0],
43+
ResourceID: resourceID,
44+
ResourceType: resourceType,
45+
Version: version,
46+
})
47+
48+
if err != nil {
49+
return err
50+
}
51+
fmt.Fprintln(ecctl.Get().Config.OutputDevice, "comment deleted successfully")
52+
return nil
53+
},
54+
}
55+
56+
func init() {
57+
initDeleteFlags()
58+
}
59+
60+
func initDeleteFlags() {
61+
Command.AddCommand(deleteCmd)
62+
63+
deleteCmd.Flags().String("resource-type", "", "The kind of resource that a comment belongs to. "+
64+
"Should be one of [elasticsearch, kibana, apm, appsearch, enterprise_search, allocator, constructor, runner, proxy].")
65+
deleteCmd.Flags().String("resource-id", "", "ID of the resource that the comment belongs to.")
66+
deleteCmd.Flags().String("version", "", "If specified then checks for conflicts against the version stored in the persistent store.")
67+
68+
deleteCmd.MarkFlagRequired("resource-type")
69+
deleteCmd.MarkFlagRequired("resource-id")
70+
}

cmd/comment/delete_test.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package cmdcomment
19+
20+
import (
21+
"testing"
22+
23+
"github.com/elastic/cloud-sdk-go/pkg/api"
24+
"github.com/elastic/cloud-sdk-go/pkg/api/mock"
25+
26+
"github.com/elastic/ecctl/cmd/util/testutils"
27+
)
28+
29+
func Test_deleteCmd(t *testing.T) {
30+
tests := []struct {
31+
name string
32+
args testutils.Args
33+
want testutils.Assertion
34+
}{
35+
{
36+
name: "fails due to missing comment id",
37+
args: testutils.Args{
38+
Cmd: deleteCmd,
39+
Args: []string{"delete"},
40+
Cfg: testutils.MockCfg{Responses: []mock.Response{
41+
mock.SampleInternalError(),
42+
}},
43+
},
44+
want: testutils.Assertion{
45+
Err: `accepts 1 arg(s), received 0`,
46+
},
47+
},
48+
{
49+
name: "fails due to missing resource-id",
50+
args: testutils.Args{
51+
Cmd: deleteCmd,
52+
Args: []string{"delete", "some-message-id", "--resource-type", "allocator"},
53+
Cfg: testutils.MockCfg{Responses: []mock.Response{
54+
mock.SampleInternalError(),
55+
}},
56+
},
57+
want: testutils.Assertion{
58+
Err: `required flag(s) "resource-id" not set`,
59+
},
60+
},
61+
{
62+
name: "fails due to missing resource-type",
63+
args: testutils.Args{
64+
Cmd: deleteCmd,
65+
Args: []string{"delete", "some-message-id", "--resource-id", "i-i123"},
66+
Cfg: testutils.MockCfg{Responses: []mock.Response{
67+
mock.SampleInternalError(),
68+
}},
69+
},
70+
want: testutils.Assertion{
71+
Err: `required flag(s) "resource-type" not set`,
72+
},
73+
},
74+
{
75+
name: "fails due to API error",
76+
args: testutils.Args{
77+
Cmd: deleteCmd,
78+
Args: []string{
79+
"delete", "some-message", "--resource-type", "allocator", "--resource-id", "i-123",
80+
},
81+
Cfg: testutils.MockCfg{Responses: []mock.Response{
82+
mock.SampleInternalError(),
83+
}},
84+
},
85+
want: testutils.Assertion{
86+
Err: mock.MultierrorInternalError.Error(),
87+
},
88+
},
89+
{
90+
name: "succeeds",
91+
args: testutils.Args{
92+
Cmd: deleteCmd,
93+
Args: []string{
94+
"delete", "some-message-id", "--resource-type", "allocator", "--resource-id", "i-123",
95+
},
96+
Cfg: testutils.MockCfg{
97+
OutputFormat: "text",
98+
Responses: []mock.Response{
99+
mock.New200ResponseAssertion(
100+
&mock.RequestAssertion{
101+
Header: api.DefaultWriteMockHeaders,
102+
Method: "DELETE",
103+
Path: "/api/v1/regions/ece-region/comments/allocator/i-123/some-message-id",
104+
Host: api.DefaultMockHost,
105+
},
106+
mock.NewStringBody(`{}`),
107+
),
108+
},
109+
},
110+
},
111+
want: testutils.Assertion{
112+
Stdout: "comment deleted successfully\n",
113+
},
114+
},
115+
{
116+
name: "succeeds with version",
117+
args: testutils.Args{
118+
Cmd: deleteCmd,
119+
Args: []string{
120+
"delete", "some-message-id", "--resource-type", "allocator", "--resource-id", "i-123", "--version", "v1",
121+
},
122+
Cfg: testutils.MockCfg{
123+
OutputFormat: "text",
124+
Responses: []mock.Response{
125+
mock.New200ResponseAssertion(
126+
&mock.RequestAssertion{
127+
Header: api.DefaultWriteMockHeaders,
128+
Method: "DELETE",
129+
Path: "/api/v1/regions/ece-region/comments/allocator/i-123/some-message-id",
130+
Host: api.DefaultMockHost,
131+
Query: map[string][]string{"version": {"v1"}},
132+
},
133+
mock.NewStringBody(`{}`),
134+
),
135+
},
136+
},
137+
},
138+
want: testutils.Assertion{
139+
Stdout: "comment deleted successfully\n",
140+
},
141+
},
142+
}
143+
for _, tt := range tests {
144+
t.Run(tt.name, func(t *testing.T) {
145+
testutils.RunCmdAssertion(t, tt.args, tt.want)
146+
tt.args.Cmd.ResetFlags()
147+
defer initDeleteFlags()
148+
})
149+
}
150+
}

docs/ecctl-command-reference-index.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ include::ecctl_auth_key_list.adoc[]
77
include::ecctl_auth_key_show.adoc[]
88
include::ecctl_comment.adoc[]
99
include::ecctl_comment_create.adoc[]
10+
include::ecctl_comment_delete.adoc[]
1011
include::ecctl_comment_list.adoc[]
1112
include::ecctl_comment_update.adoc[]
1213
include::ecctl_deployment.adoc[]

docs/ecctl_comment.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ ecctl comment [flags]
4343

4444
* xref:ecctl[ecctl] - Elastic Cloud Control
4545
* xref:ecctl_comment_create[ecctl comment create] - Creates a new resource comment {ece-icon} (Available for ECE only)
46+
* xref:ecctl_comment_delete[ecctl comment delete] - Deletes a resource comment {ece-icon} (Available for ECE only)
4647
* xref:ecctl_comment_list[ecctl comment list] - Lists all resource comments {ece-icon} (Available for ECE only)
4748
* xref:ecctl_comment_update[ecctl comment update] - Updates an existing resource comment

docs/ecctl_comment.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ ecctl comment [flags]
3939

4040
* [ecctl](ecctl.md) - Elastic Cloud Control
4141
* [ecctl comment create](ecctl_comment_create.md) - Creates a new resource comment (Available for ECE only)
42+
* [ecctl comment delete](ecctl_comment_delete.md) - Deletes a resource comment (Available for ECE only)
4243
* [ecctl comment list](ecctl_comment_list.md) - Lists all resource comments (Available for ECE only)
4344
* [ecctl comment update](ecctl_comment_update.md) - Updates an existing resource comment
4445

docs/ecctl_comment_delete.adoc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[#ecctl_comment_delete]
2+
== ecctl comment delete
3+
4+
Deletes a resource comment {ece-icon} (Available for ECE only)
5+
6+
----
7+
ecctl comment delete <comment id> --resource-type <resource-type> --resource-id <resource-id> [flags]
8+
----
9+
10+
[float]
11+
=== Options
12+
13+
----
14+
-h, --help help for delete
15+
--resource-id string ID of the resource that the comment belongs to.
16+
--resource-type string The kind of resource that a comment belongs to. Should be one of [elasticsearch, kibana, apm, appsearch, enterprise_search, allocator, constructor, runner, proxy].
17+
--version string If specified then checks for conflicts against the version stored in the persistent store.
18+
----
19+
20+
[float]
21+
=== Options inherited from parent commands
22+
23+
----
24+
--api-key string API key to use to authenticate (If empty will look for EC_API_KEY environment variable)
25+
--config string Config name, used to have multiple configs in $HOME/.ecctl/<env> (default "config")
26+
--force Do not ask for confirmation
27+
--format string Formats the output using a Go template
28+
--host string Base URL to use
29+
--insecure Skips all TLS validation
30+
--message string A message to set on cluster operation
31+
--output string Output format [text|json] (default "text")
32+
--pass string Password to use to authenticate (If empty will look for EC_PASS environment variable)
33+
--pprof Enables pprofing and saves the profile to pprof-20060102150405
34+
-q, --quiet Suppresses the configuration file used for the run, if any
35+
--region string Elasticsearch Service region
36+
--timeout duration Timeout to use on all HTTP calls (default 30s)
37+
--trace Enables tracing saves the trace to trace-20060102150405
38+
--user string Username to use to authenticate (If empty will look for EC_USER environment variable)
39+
--verbose Enable verbose mode
40+
--verbose-credentials When set, Authorization headers on the request/response trail will be displayed as plain text
41+
--verbose-file string When set, the verbose request/response trail will be written to the defined file
42+
----
43+
44+
[float]
45+
=== SEE ALSO
46+
47+
* xref:ecctl_comment[ecctl comment] - Manages Comments

docs/ecctl_comment_delete.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## ecctl comment delete
2+
3+
Deletes a resource comment (Available for ECE only)
4+
5+
```
6+
ecctl comment delete <comment id> --resource-type <resource-type> --resource-id <resource-id> [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-h, --help help for delete
13+
--resource-id string ID of the resource that the comment belongs to.
14+
--resource-type string The kind of resource that a comment belongs to. Should be one of [elasticsearch, kibana, apm, appsearch, enterprise_search, allocator, constructor, runner, proxy].
15+
--version string If specified then checks for conflicts against the version stored in the persistent store.
16+
```
17+
18+
### Options inherited from parent commands
19+
20+
```
21+
--api-key string API key to use to authenticate (If empty will look for EC_API_KEY environment variable)
22+
--config string Config name, used to have multiple configs in $HOME/.ecctl/<env> (default "config")
23+
--force Do not ask for confirmation
24+
--format string Formats the output using a Go template
25+
--host string Base URL to use
26+
--insecure Skips all TLS validation
27+
--message string A message to set on cluster operation
28+
--output string Output format [text|json] (default "text")
29+
--pass string Password to use to authenticate (If empty will look for EC_PASS environment variable)
30+
--pprof Enables pprofing and saves the profile to pprof-20060102150405
31+
-q, --quiet Suppresses the configuration file used for the run, if any
32+
--region string Elasticsearch Service region
33+
--timeout duration Timeout to use on all HTTP calls (default 30s)
34+
--trace Enables tracing saves the trace to trace-20060102150405
35+
--user string Username to use to authenticate (If empty will look for EC_USER environment variable)
36+
--verbose Enable verbose mode
37+
--verbose-credentials When set, Authorization headers on the request/response trail will be displayed as plain text
38+
--verbose-file string When set, the verbose request/response trail will be written to the defined file
39+
```
40+
41+
### SEE ALSO
42+
43+
* [ecctl comment](ecctl_comment.md) - Manages Comments
44+

0 commit comments

Comments
 (0)