Skip to content

Commit 882c0b0

Browse files
implements comment update command (#467)
* implements comment update command * address pr comments
1 parent 44509f5 commit 882c0b0

File tree

9 files changed

+470
-3
lines changed

9 files changed

+470
-3
lines changed

cmd/comment/update.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
"github.com/elastic/cloud-sdk-go/pkg/api/commentapi"
22+
"github.com/spf13/cobra"
23+
24+
"github.com/elastic/ecctl/pkg/ecctl"
25+
)
26+
27+
var updateCmd = &cobra.Command{
28+
Use: "update <comment id> <message> --resource-type <resource-type> --resource-id <resource-id>",
29+
Short: "Updates an existing resource comment",
30+
PreRunE: cobra.ExactArgs(2),
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
resourceType, _ := cmd.Flags().GetString("resource-type")
33+
resourceID, _ := cmd.Flags().GetString("resource-id")
34+
version, _ := cmd.Flags().GetString("version")
35+
36+
res, err := commentapi.Update(commentapi.UpdateParams{
37+
API: ecctl.Get().API,
38+
Region: ecctl.Get().Config.Region,
39+
CommentID: args[0],
40+
Message: args[1],
41+
ResourceID: resourceID,
42+
ResourceType: resourceType,
43+
Version: version,
44+
})
45+
46+
if err != nil {
47+
return err
48+
}
49+
50+
return ecctl.Get().Formatter.Format("comment/create", res)
51+
},
52+
}
53+
54+
func init() {
55+
initUpdateFlags()
56+
}
57+
58+
func initUpdateFlags() {
59+
Command.AddCommand(updateCmd)
60+
61+
updateCmd.Flags().String("resource-type", "", "The kind of Resource that a Comment belongs to. Should be one of [elasticsearch, kibana, apm, appsearch, enterprise_search, allocator, constructor, runner, proxy].")
62+
updateCmd.Flags().String("resource-id", "", "Id of the Resource that a Comment belongs to.")
63+
updateCmd.Flags().String("version", "", "If specified then checks for conflicts against the version stored in the persistent store.")
64+
65+
updateCmd.MarkFlagRequired("resource-type")
66+
updateCmd.MarkFlagRequired("resource-id")
67+
}

cmd/comment/update_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_updateCmd(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 arguments",
37+
args: testutils.Args{
38+
Cmd: updateCmd,
39+
Args: []string{"update"},
40+
Cfg: testutils.MockCfg{Responses: []mock.Response{
41+
mock.SampleInternalError(),
42+
}},
43+
},
44+
want: testutils.Assertion{
45+
Err: `accepts 2 arg(s), received 0`,
46+
},
47+
},
48+
{
49+
name: "fails due to missing resource-id",
50+
args: testutils.Args{
51+
Cmd: updateCmd,
52+
Args: []string{"update", "comment-id", "some-message", "--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: updateCmd,
65+
Args: []string{"update", "comment-id", "some-message", "--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: updateCmd,
78+
Args: []string{
79+
"update", "comment-id", "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: updateCmd,
93+
Args: []string{
94+
"update", "comment-id", "some-message", "--resource-type", "allocator", "--resource-id", "i-123",
95+
},
96+
Cfg: testutils.MockCfg{
97+
Responses: []mock.Response{
98+
mock.New200ResponseAssertion(
99+
&mock.RequestAssertion{
100+
Header: api.DefaultWriteMockHeaders,
101+
Method: "PUT",
102+
Path: "/api/v1/regions/ece-region/comments/allocator/i-123/comment-id",
103+
Host: api.DefaultMockHost,
104+
Body: mock.NewStringBody(`{"message":"some-message"}` + "\n"),
105+
},
106+
mock.NewStringBody(`{"id":"random-generated-id"}`),
107+
),
108+
},
109+
},
110+
},
111+
want: testutils.Assertion{
112+
Stdout: `"random-generated-id"` + "\n",
113+
},
114+
},
115+
{
116+
name: "succeeds with version",
117+
args: testutils.Args{
118+
Cmd: updateCmd,
119+
Args: []string{
120+
"update", "comment-id", "some-message", "--resource-type", "allocator", "--resource-id", "i-123", "--version", "v1",
121+
},
122+
Cfg: testutils.MockCfg{
123+
Responses: []mock.Response{
124+
mock.New200ResponseAssertion(
125+
&mock.RequestAssertion{
126+
Header: api.DefaultWriteMockHeaders,
127+
Method: "PUT",
128+
Path: "/api/v1/regions/ece-region/comments/allocator/i-123/comment-id",
129+
Host: api.DefaultMockHost,
130+
Body: mock.NewStringBody(`{"message":"some-message"}` + "\n"),
131+
Query: map[string][]string{"version": {"v1"}},
132+
},
133+
mock.NewStringBody(`{"id":"random-generated-id"}`),
134+
),
135+
},
136+
},
137+
},
138+
want: testutils.Assertion{
139+
Stdout: `"random-generated-id"` + "\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 initUpdateFlags()
148+
})
149+
}
150+
}

docs/ecctl-command-reference-index.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ include::ecctl_auth_key_show.adoc[]
88
include::ecctl_comment.adoc[]
99
include::ecctl_comment_create.adoc[]
1010
include::ecctl_comment_list.adoc[]
11+
include::ecctl_comment_update.adoc[]
1112
include::ecctl_deployment.adoc[]
1213
include::ecctl_deployment_create.adoc[]
1314
include::ecctl_deployment_delete.adoc[]

docs/ecctl_comment.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ ecctl comment [flags]
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)
4646
* xref:ecctl_comment_list[ecctl comment list] - Lists all resource comments {ece-icon} (Available for ECE only)
47+
* 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
@@ -40,4 +40,5 @@ ecctl comment [flags]
4040
* [ecctl](ecctl.md) - Elastic Cloud Control
4141
* [ecctl comment create](ecctl_comment_create.md) - Creates a new resource comment (Available for ECE only)
4242
* [ecctl comment list](ecctl_comment_list.md) - Lists all resource comments (Available for ECE only)
43+
* [ecctl comment update](ecctl_comment_update.md) - Updates an existing resource comment
4344

docs/ecctl_comment_update.adoc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[#ecctl_comment_update]
2+
== ecctl comment update
3+
4+
Updates an existing resource comment
5+
6+
----
7+
ecctl comment update <comment id> <message> --resource-type <resource-type> --resource-id <resource-id> [flags]
8+
----
9+
10+
[float]
11+
=== Options
12+
13+
----
14+
-h, --help help for update
15+
--resource-id string Id of the Resource that a 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_update.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## ecctl comment update
2+
3+
Updates an existing resource comment
4+
5+
```
6+
ecctl comment update <comment id> <message> --resource-type <resource-type> --resource-id <resource-id> [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-h, --help help for update
13+
--resource-id string Id of the Resource that a 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+

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.13
44

55
require (
66
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
7-
github.com/elastic/cloud-sdk-go v1.2.1-0.20210318081215-b2e2902a5ce0
7+
github.com/elastic/cloud-sdk-go v1.2.1-0.20210318095149-ef9cbed03e4d
88
github.com/go-openapi/runtime v0.19.26
99
github.com/go-openapi/strfmt v0.20.0
1010
github.com/pkg/errors v0.9.1

0 commit comments

Comments
 (0)