Skip to content

Commit 44509f5

Browse files
implements comment list command (#464)
* implements comment list command * fix imports * Update cmd/comment/list.go Co-authored-by: Marc Lopez Rubio <marc5.12@outlook.com> * address pr comments Co-authored-by: Marc Lopez Rubio <marc5.12@outlook.com>
1 parent 86d353a commit 44509f5

File tree

11 files changed

+343
-3
lines changed

11 files changed

+343
-3
lines changed

cmd/comment/list.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
cmdutil "github.com/elastic/ecctl/cmd/util"
25+
"github.com/elastic/ecctl/pkg/ecctl"
26+
)
27+
28+
var listCmd = &cobra.Command{
29+
Use: "list --resource-type <resource-type> --resource-id <resource-id>",
30+
Short: cmdutil.AdminReqDescription("Lists all resource comments"),
31+
PreRunE: cobra.MaximumNArgs(0),
32+
RunE: func(cmd *cobra.Command, args []string) error {
33+
resourceType, _ := cmd.Flags().GetString("resource-type")
34+
resourceID, _ := cmd.Flags().GetString("resource-id")
35+
36+
res, err := commentapi.List(commentapi.ListParams{
37+
API: ecctl.Get().API,
38+
Region: ecctl.Get().Config.Region,
39+
ResourceID: resourceID,
40+
ResourceType: resourceType,
41+
})
42+
43+
if err != nil {
44+
return err
45+
}
46+
47+
return ecctl.Get().Formatter.Format("comment/list", res)
48+
},
49+
}
50+
51+
func init() {
52+
initListFlags()
53+
}
54+
55+
func initListFlags() {
56+
Command.AddCommand(listCmd)
57+
58+
listCmd.Flags().String("resource-type", "", "The kind of resource that a comment belongs to. "+
59+
"Should be one of [elasticsearch, kibana, apm, appsearch, enterprise_search, allocator, constructor, runner, proxy].")
60+
listCmd.Flags().String("resource-id", "", "Id of the resource that a comment belongs to.")
61+
62+
listCmd.MarkFlagRequired("resource-type")
63+
listCmd.MarkFlagRequired("resource-id")
64+
}

cmd/comment/list_test.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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_listCmd(t *testing.T) {
30+
listJSONResponse := `{
31+
"values": [
32+
{
33+
"comment": {
34+
"id": "93420f71ca474b79aa4bc2aaa7f37e21",
35+
"message": "some message",
36+
"user_id": "root"
37+
},
38+
"metadata": {
39+
"created_time": "2021-03-17T13:05:06.958Z",
40+
"modified_time": "2021-03-17T13:05:06.958Z",
41+
"version": "8|14"
42+
}
43+
},
44+
{
45+
"comment": {
46+
"id": "e66e8336d4f3489193146a993779ce92",
47+
"message": "some message",
48+
"user_id": "root"
49+
},
50+
"metadata": {
51+
"created_time": "2021-03-17T12:21:58.202Z",
52+
"modified_time": "2021-03-17T12:21:58.202Z",
53+
"version": "9|12"
54+
}
55+
}
56+
]
57+
}
58+
`
59+
tests := []struct {
60+
name string
61+
args testutils.Args
62+
want testutils.Assertion
63+
}{
64+
{
65+
name: "fails due to arguments passed",
66+
args: testutils.Args{
67+
Cmd: listCmd,
68+
Args: []string{"list", "some-argument"},
69+
Cfg: testutils.MockCfg{Responses: []mock.Response{
70+
mock.SampleInternalError(),
71+
}},
72+
},
73+
want: testutils.Assertion{
74+
Err: `accepts at most 0 arg(s), received 1`,
75+
},
76+
},
77+
{
78+
name: "fails due to missing resource-id",
79+
args: testutils.Args{
80+
Cmd: listCmd,
81+
Args: []string{"list", "--resource-type", "allocator"},
82+
Cfg: testutils.MockCfg{Responses: []mock.Response{
83+
mock.SampleInternalError(),
84+
}},
85+
},
86+
want: testutils.Assertion{
87+
Err: `required flag(s) "resource-id" not set`,
88+
},
89+
},
90+
{
91+
name: "fails due to missing resource-type",
92+
args: testutils.Args{
93+
Cmd: listCmd,
94+
Args: []string{"list", "--resource-id", "i-i123"},
95+
Cfg: testutils.MockCfg{Responses: []mock.Response{
96+
mock.SampleInternalError(),
97+
}},
98+
},
99+
want: testutils.Assertion{
100+
Err: `required flag(s) "resource-type" not set`,
101+
},
102+
},
103+
{
104+
name: "fails due to API error",
105+
args: testutils.Args{
106+
Cmd: listCmd,
107+
Args: []string{
108+
"list", "--resource-type", "allocator", "--resource-id", "i-123",
109+
},
110+
Cfg: testutils.MockCfg{Responses: []mock.Response{
111+
mock.SampleInternalError(),
112+
}},
113+
},
114+
want: testutils.Assertion{
115+
Err: mock.MultierrorInternalError.Error(),
116+
},
117+
},
118+
{
119+
name: "succeeds",
120+
args: testutils.Args{
121+
Cmd: listCmd,
122+
Args: []string{
123+
"list", "--resource-type", "allocator", "--resource-id", "i-123",
124+
},
125+
Cfg: testutils.MockCfg{
126+
OutputFormat: "text",
127+
Responses: []mock.Response{
128+
mock.New200ResponseAssertion(
129+
&mock.RequestAssertion{
130+
Header: api.DefaultReadMockHeaders,
131+
Method: "GET",
132+
Path: "/api/v1/regions/ece-region/comments/allocator/i-123",
133+
Host: api.DefaultMockHost,
134+
},
135+
mock.NewStringBody(listJSONResponse),
136+
),
137+
},
138+
},
139+
},
140+
want: testutils.Assertion{
141+
Stdout: "COMMENT ID USER MESSAGE CREATED TIME MODIFIED TIME VERSION\n" +
142+
"93420f71ca474b79aa4bc2aaa7f37e21 root some message 2021-03-17T13:05:06.958Z 2021-03-17T13:05:06.958Z 8|14\n" +
143+
"e66e8336d4f3489193146a993779ce92 root some message 2021-03-17T12:21:58.202Z 2021-03-17T12:21:58.202Z 9|12\n",
144+
},
145+
},
146+
}
147+
for _, tt := range tests {
148+
t.Run(tt.name, func(t *testing.T) {
149+
testutils.RunCmdAssertion(t, tt.args, tt.want)
150+
tt.args.Cmd.ResetFlags()
151+
defer initListFlags()
152+
})
153+
}
154+
}

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_list.adoc[]
1011
include::ecctl_deployment.adoc[]
1112
include::ecctl_deployment_create.adoc[]
1213
include::ecctl_deployment_delete.adoc[]

docs/ecctl_comment.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ 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_list[ecctl comment list] - Lists all resource comments {ece-icon} (Available for ECE only)

docs/ecctl_comment.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ 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 list](ecctl_comment_list.md) - Lists all resource comments (Available for ECE only)
4243

docs/ecctl_comment_list.adoc

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

docs/ecctl_comment_list.md

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

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.20210317171548-61252b42f818
7+
github.com/elastic/cloud-sdk-go v1.2.1-0.20210318081215-b2e2902a5ce0
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

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
5858
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
5959
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
6060
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
61-
github.com/elastic/cloud-sdk-go v1.2.1-0.20210317171548-61252b42f818 h1:jvPviqvYNXYKn1BE6ScLMzn/ELeEPfThrMGCN7pYSkM=
62-
github.com/elastic/cloud-sdk-go v1.2.1-0.20210317171548-61252b42f818/go.mod h1:uF1RAbMMbuxRF3hGJ/9m7yx3qz/5BYYLP8I6slVK4Xg=
61+
github.com/elastic/cloud-sdk-go v1.2.1-0.20210318081215-b2e2902a5ce0 h1:UvO1JoOE0XV3w9RARftWt1UEG0E7M75M/knxmqCGWAE=
62+
github.com/elastic/cloud-sdk-go v1.2.1-0.20210318081215-b2e2902a5ce0/go.mod h1:uF1RAbMMbuxRF3hGJ/9m7yx3qz/5BYYLP8I6slVK4Xg=
6363
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
6464
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
6565
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=

0 commit comments

Comments
 (0)