Skip to content

Commit a01959c

Browse files
authored
deployment: Add list command (#30)
Adds the deployment list command and an associated text template. Signed-off-by: Marc Lopez <marc5.12@outlook.com>
1 parent df2d729 commit a01959c

File tree

10 files changed

+416
-0
lines changed

10 files changed

+416
-0
lines changed

cmd/deployment/list.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 cmddeployment
19+
20+
import (
21+
"github.com/spf13/cobra"
22+
23+
"github.com/elastic/ecctl/pkg/deployment"
24+
"github.com/elastic/ecctl/pkg/ecctl"
25+
)
26+
27+
var listCmd = &cobra.Command{
28+
Use: "list",
29+
Short: "Lists the platform's deployments",
30+
PreRunE: cobra.NoArgs,
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
res, err := deployment.List(deployment.ListParams{
33+
API: ecctl.Get().API,
34+
})
35+
if err != nil {
36+
return err
37+
}
38+
39+
return ecctl.Get().Formatter.Format("deployment/list", res)
40+
},
41+
}
42+
43+
func init() {
44+
Command.AddCommand(listCmd)
45+
}

docs/ecctl_deployment.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ ecctl deployment [flags]
4343
* [ecctl deployment apm](ecctl_deployment_apm.md) - Manages APM deployments
4444
* [ecctl deployment elasticsearch](ecctl_deployment_elasticsearch.md) - Manages Elasticsearch clusters
4545
* [ecctl deployment kibana](ecctl_deployment_kibana.md) - Manages Kibana clusters
46+
* [ecctl deployment list](ecctl_deployment_list.md) - Lists the platform's deployments
4647
* [ecctl deployment note](ecctl_deployment_note.md) - Manages a deployment's notes
4748
* [ecctl deployment show](ecctl_deployment_show.md) - Shows the specified deployment resources
4849

docs/ecctl_deployment_list.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## ecctl deployment list
2+
3+
Lists the platform's deployments
4+
5+
### Synopsis
6+
7+
Lists the platform's deployments
8+
9+
```
10+
ecctl deployment list [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help help for list
17+
```
18+
19+
### Options inherited from parent commands
20+
21+
```
22+
--apikey string API key to use to authenticate (If empty will look for EC_APIKEY environment variable)
23+
--config string Config name, used to have multiple configs in $HOME/.ecctl/<env> (default "config")
24+
--force Do not ask for confirmation
25+
--format string Formats the output using a Go template
26+
--host string Base URL to use (default "https://api.elastic-cloud.com")
27+
--insecure Skips all TLS validation
28+
--message string A message to set on cluster operation
29+
--output string Output format [text|json] (default "text")
30+
--pass string Password to use to authenticate (If empty will look for EC_PASS environment variable)
31+
--pprof Enables pprofing and saves the profile to pprof-20060102150405
32+
-q, --quiet Suppresses the configuration file used for the run, if any
33+
--region string Elastic Cloud region
34+
--timeout duration Timeout to use on all HTTP calls (default 30s)
35+
--trace Enables tracing saves the trace to trace-20060102150405
36+
--user string Username to use to authenticate (If empty will look for EC_USER environment variable)
37+
--verbose Enable verbose mode
38+
```
39+
40+
### SEE ALSO
41+
42+
* [ecctl deployment](ecctl_deployment.md) - Manages deployments
43+

pkg/deployment/list.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 deployment
19+
20+
import (
21+
"github.com/elastic/cloud-sdk-go/pkg/api"
22+
"github.com/elastic/cloud-sdk-go/pkg/client/deployments"
23+
"github.com/elastic/cloud-sdk-go/pkg/models"
24+
25+
"github.com/elastic/ecctl/pkg/util"
26+
)
27+
28+
// ListParams is consumed by List.
29+
type ListParams struct {
30+
*api.API
31+
}
32+
33+
// Validate ensures the parameters are usable.
34+
func (params ListParams) Validate() error {
35+
if params.API == nil {
36+
return util.ErrAPIReq
37+
}
38+
39+
return nil
40+
}
41+
42+
// List returns the platform deployemnts
43+
func List(params ListParams) (*models.DeploymentsListResponse, error) {
44+
if err := params.Validate(); err != nil {
45+
return nil, err
46+
}
47+
48+
res, err := params.V1API.Deployments.ListDeployments(
49+
deployments.NewListDeploymentsParams(),
50+
params.AuthWriter,
51+
)
52+
if err != nil {
53+
return nil, api.UnwrapError(err)
54+
}
55+
56+
return res.Payload, nil
57+
}

pkg/deployment/list_test.go

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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 deployment
19+
20+
import (
21+
"errors"
22+
"reflect"
23+
"testing"
24+
25+
"github.com/elastic/cloud-sdk-go/pkg/api"
26+
"github.com/elastic/cloud-sdk-go/pkg/api/mock"
27+
"github.com/elastic/cloud-sdk-go/pkg/models"
28+
"github.com/elastic/cloud-sdk-go/pkg/util/ec"
29+
30+
"github.com/elastic/ecctl/pkg/util"
31+
)
32+
33+
var deploymentList = `{
34+
"deployments": [
35+
{
36+
"id": "709551ecd21143adbb7a37bbf36c4321",
37+
"name": "admin-console-elasticsearch",
38+
"resources": [
39+
{
40+
"cloud_id": "admin-console-elasticsearch:MTkyLjE2OC40NC4xMC5pcC5lcy5pbzo5MjQzJDcwOTU1MWVjZDIxMTQzYWRiYjdhMzdiYmYzNmM0MzIxJA==",
41+
"id": "709551ecd21143adbb7a37bbf36c4321",
42+
"kind": "elasticsearch",
43+
"ref_id": "elasticsearch",
44+
"region": "ece-region"
45+
}
46+
]
47+
},
48+
{
49+
"id": "83c2027d92c44aeba34378f44cf08137",
50+
"name": "logging-and-metrics",
51+
"resources": [
52+
{
53+
"cloud_id": "logging-and-metrics:MTkyLjE2OC40NC4xMC5pcC5lcy5pbzo5MjQzJDgzYzIwMjdkOTJjNDRhZWJhMzQzNzhmNDRjZjA4MTM3JGQxNDBiNmUzNWIwNDQ1YTlhZmQ0NDIwZjZjMWIzYjIx",
54+
"id": "83c2027d92c44aeba34378f44cf08137",
55+
"kind": "elasticsearch",
56+
"ref_id": "elasticsearch",
57+
"region": "ece-region"
58+
},
59+
{
60+
"elasticsearch_cluster_ref_id": "elasticsearch",
61+
"id": "d140b6e35b0445a9afd4420f6c1b3b21",
62+
"kind": "kibana",
63+
"ref_id": "kibana",
64+
"region": "ece-region"
65+
}
66+
]
67+
},
68+
{
69+
"id": "89a54cee4a2847c291f520ac00760886",
70+
"name": "security-cluster",
71+
"resources": [
72+
{
73+
"cloud_id": "security-cluster:MTkyLjE2OC40NC4xMC5pcC5lcy5pbzo5MjQzJDg5YTU0Y2VlNGEyODQ3YzI5MWY1MjBhYzAwNzYwODg2JA==",
74+
"id": "89a54cee4a2847c291f520ac00760886",
75+
"kind": "elasticsearch",
76+
"ref_id": "elasticsearch",
77+
"region": "ece-region"
78+
}
79+
]
80+
}
81+
]
82+
}
83+
`
84+
85+
func TestList(t *testing.T) {
86+
type args struct {
87+
params ListParams
88+
}
89+
tests := []struct {
90+
name string
91+
args args
92+
want *models.DeploymentsListResponse
93+
err error
94+
}{
95+
{
96+
name: "fails on parameter validation",
97+
err: util.ErrAPIReq,
98+
},
99+
{
100+
name: "fails on API error",
101+
args: args{params: ListParams{
102+
API: api.NewMock(mock.New500Response(mock.NewStringBody("error"))),
103+
}},
104+
err: errors.New("unknown error (status 500)"),
105+
},
106+
{
107+
name: "Succeeds",
108+
args: args{params: ListParams{
109+
API: api.NewMock(mock.New200Response(mock.NewStringBody(deploymentList))),
110+
}},
111+
want: &models.DeploymentsListResponse{Deployments: []*models.DeploymentsListingData{
112+
{
113+
ID: ec.String("709551ecd21143adbb7a37bbf36c4321"),
114+
Name: ec.String("admin-console-elasticsearch"),
115+
Resources: []*models.DeploymentResource{
116+
{
117+
CloudID: "admin-console-elasticsearch:MTkyLjE2OC40NC4xMC5pcC5lcy5pbzo5MjQzJDcwOTU1MWVjZDIxMTQzYWRiYjdhMzdiYmYzNmM0MzIxJA==",
118+
ID: ec.String("709551ecd21143adbb7a37bbf36c4321"),
119+
Kind: ec.String("elasticsearch"),
120+
RefID: ec.String("elasticsearch"),
121+
Region: ec.String("ece-region"),
122+
},
123+
},
124+
},
125+
{
126+
ID: ec.String("83c2027d92c44aeba34378f44cf08137"),
127+
Name: ec.String("logging-and-metrics"),
128+
Resources: []*models.DeploymentResource{
129+
{
130+
CloudID: "logging-and-metrics:MTkyLjE2OC40NC4xMC5pcC5lcy5pbzo5MjQzJDgzYzIwMjdkOTJjNDRhZWJhMzQzNzhmNDRjZjA4MTM3JGQxNDBiNmUzNWIwNDQ1YTlhZmQ0NDIwZjZjMWIzYjIx",
131+
ID: ec.String("83c2027d92c44aeba34378f44cf08137"),
132+
Kind: ec.String("elasticsearch"),
133+
RefID: ec.String("elasticsearch"),
134+
Region: ec.String("ece-region"),
135+
},
136+
{
137+
ElasticsearchClusterRefID: "elasticsearch",
138+
ID: ec.String("d140b6e35b0445a9afd4420f6c1b3b21"),
139+
Kind: ec.String("kibana"),
140+
RefID: ec.String("kibana"),
141+
Region: ec.String("ece-region"),
142+
},
143+
},
144+
},
145+
{
146+
ID: ec.String("89a54cee4a2847c291f520ac00760886"),
147+
Name: ec.String("security-cluster"),
148+
Resources: []*models.DeploymentResource{
149+
{
150+
CloudID: "security-cluster:MTkyLjE2OC40NC4xMC5pcC5lcy5pbzo5MjQzJDg5YTU0Y2VlNGEyODQ3YzI5MWY1MjBhYzAwNzYwODg2JA==",
151+
ID: ec.String("89a54cee4a2847c291f520ac00760886"),
152+
Kind: ec.String("elasticsearch"),
153+
RefID: ec.String("elasticsearch"),
154+
Region: ec.String("ece-region"),
155+
},
156+
},
157+
},
158+
}},
159+
},
160+
}
161+
for _, tt := range tests {
162+
t.Run(tt.name, func(t *testing.T) {
163+
got, err := List(tt.args.params)
164+
if !reflect.DeepEqual(err, tt.err) {
165+
t.Errorf("List() error = %v, wantErr %v", err, tt.err)
166+
return
167+
}
168+
if !reflect.DeepEqual(got, tt.want) {
169+
t.Errorf("List() = %+v, want %+v", got, tt.want)
170+
}
171+
})
172+
}
173+
}

pkg/formatter/templates/bindata.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// text/allocator/showmetadata.gotmpl
2525
// text/apm/list.gotmpl
2626
// text/apm/planhistory.gotmpl
27+
// text/deployment/list.gotmpl
2728
// text/deployment/notelist.gotmpl
2829
// text/deployment-template/list.gotmpl
2930
// text/elasticsearch/list.gotmpl
@@ -261,6 +262,26 @@ func textApmPlanhistoryGotmpl() (*asset, error) {
261262
return a, nil
262263
}
263264

265+
var _textDeploymentListGotmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x8c\x91\x51\x6e\x82\x40\x10\x86\xdf\x3d\xc5\x64\xe3\xab\x7b\x80\x26\x3e\x6c\x85\xa4\xc4\x4a\x8c\x7a\x81\x81\x1d\x5a\x52\x58\x28\x2c\x4d\x1b\xc2\xdd\x1b\xd8\x1d\x2b\x6a\x13\x9f\x18\xbe\x99\x7f\xfe\x7f\xa0\xef\x57\xa0\x29\xcb\x0d\x81\xa8\xbe\xa8\x69\x72\x4d\x02\x86\xa1\xef\xa1\x41\xf3\x46\x20\x03\xaa\x8b\xea\xa7\x24\x63\x5b\xc7\xe9\x9b\xd2\xce\xd2\x89\xca\xba\x40\x4b\x20\x87\x61\x31\x62\xa3\x7d\x9f\x0b\xde\xab\x29\xc3\xae\xb0\xe3\xda\xc5\xe8\x27\xa2\xc0\x59\x58\x4c\xa6\x41\x11\xab\x5d\x28\x2e\x88\x08\x5f\xd5\xf1\x14\x6d\x8e\xa1\x3a\x6c\x5e\x66\x9d\x6d\xf4\xac\x62\x35\x43\x6a\xbf\xbb\x7a\xdf\x9f\x85\x93\xe1\xdd\x4b\xa6\xce\xf2\x23\x4f\xd0\x20\x3c\xad\x41\xac\x78\x7c\x89\x75\x79\x43\xea\x96\xb0\x49\xdf\xe7\xdc\x2f\x3e\x50\x5b\x75\x4d\x4a\xad\xc7\x79\x06\xf4\xd9\x61\x01\x72\x9b\x1b\x0d\xc2\x99\xf8\xcf\xca\x96\x6b\x90\x51\x30\x21\x32\xfa\x1f\x21\xd6\x25\xab\xc6\x4c\x8f\x49\x7c\xd2\x3f\x21\x47\xbf\x27\xe7\xea\xdc\xe2\x7f\x22\x63\x2c\xc9\xad\xb0\x98\x4c\x85\x8c\x82\xcb\x09\x3e\x64\x36\xe3\x82\xde\x20\x8e\x70\xe5\xea\x9e\xbf\x01\x00\x00\xff\xff\x78\x70\x0c\xba\x84\x02\x00\x00")
266+
267+
func textDeploymentListGotmplBytes() ([]byte, error) {
268+
return bindataRead(
269+
_textDeploymentListGotmpl,
270+
"text/deployment/list.gotmpl",
271+
)
272+
}
273+
274+
func textDeploymentListGotmpl() (*asset, error) {
275+
bytes, err := textDeploymentListGotmplBytes()
276+
if err != nil {
277+
return nil, err
278+
}
279+
280+
info := bindataFileInfo{name: "text/deployment/list.gotmpl", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
281+
a := &asset{bytes: bytes, info: info}
282+
return a, nil
283+
}
284+
264285
var _textDeploymentNotelistGotmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x64\x8e\xc1\x8e\x83\x20\x14\x45\xf7\x7e\x05\x61\x3f\xfc\xc3\x64\x24\x13\x17\x38\xc9\x88\x1f\x40\xcb\xd5\x98\x28\x1a\xc0\xa6\x09\xe1\xdf\x1b\xa4\xb8\xb0\x2b\x5e\xce\xe3\x9d\x7b\x43\xf8\x22\x1a\xc3\x64\x40\xe8\xfa\x80\xb5\x93\x06\x25\x31\x86\x40\xac\x32\x23\x08\x6b\x57\x0f\x97\x09\x9e\xb8\xef\x1e\x12\xcb\x36\x2b\x0f\xc2\x62\xac\x12\x36\xfa\xbd\x2f\x43\x31\x6a\x0c\x6a\x9f\x7d\x12\x56\x29\x89\xca\x46\xf0\xac\xf7\xea\x96\xff\xd2\x9f\x3f\x21\x78\x2b\x49\x53\x5f\x37\x7d\xc7\xff\xaf\x4c\xf0\xae\xfb\xfe\xe5\xa7\xf2\xd2\x32\xf5\x61\x72\x5a\xe0\xbc\x5a\xb6\x72\x7b\x9c\xb2\xa6\xce\x8e\x53\xc6\x7a\x07\xfb\x49\x05\x9c\x53\x23\x4a\x02\x8c\x3e\xa6\xfc\xbe\x02\x00\x00\xff\xff\x1a\x65\x9c\x45\x31\x01\x00\x00")
265286

266287
func textDeploymentNotelistGotmplBytes() ([]byte, error) {
@@ -740,6 +761,7 @@ var _bindata = map[string]func() (*asset, error){
740761
"text/allocator/showmetadata.gotmpl": textAllocatorShowmetadataGotmpl,
741762
"text/apm/list.gotmpl": textApmListGotmpl,
742763
"text/apm/planhistory.gotmpl": textApmPlanhistoryGotmpl,
764+
"text/deployment/list.gotmpl": textDeploymentListGotmpl,
743765
"text/deployment/notelist.gotmpl": textDeploymentNotelistGotmpl,
744766
"text/deployment-template/list.gotmpl": textDeploymentTemplateListGotmpl,
745767
"text/elasticsearch/list.gotmpl": textElasticsearchListGotmpl,
@@ -817,6 +839,7 @@ var _bintree = &bintree{nil, map[string]*bintree{
817839
"planhistory.gotmpl": &bintree{textApmPlanhistoryGotmpl, map[string]*bintree{}},
818840
}},
819841
"deployment": &bintree{nil, map[string]*bintree{
842+
"list.gotmpl": &bintree{textDeploymentListGotmpl, map[string]*bintree{}},
820843
"notelist.gotmpl": &bintree{textDeploymentNotelistGotmpl, map[string]*bintree{}},
821844
}},
822845
"deployment-template": &bintree{nil, map[string]*bintree{
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{{- define "override" }}{{ range .Deployments }}{{ executeTemplate .}}
2+
{{ end }}{{ end }}{{ define "default" }}
3+
{{- "ID" }}{{tab}}{{ "NAME"}}{{tab}}{{"ELASTICSEARCH"}}{{tab}}{{"KIBANA"}}{{tab}}{{"APM"}}{{tab}}{{"APPSEARCH"}}
4+
{{- range .Deployments }}
5+
{{- $kibana := "-"}}
6+
{{- $apm := "-"}}
7+
{{- $appsearch := "-"}}
8+
{{- range .Resources}}
9+
{{- if equal .Kind "kibana" }}{{ $kibana = .ID }}{{end}}
10+
{{- if equal .Kind "apm" }}{{ $apm = .ID }}{{end}}
11+
{{- if equal .Kind "appsearch" }}{{ $appsearch = .ID }}{{end}}
12+
{{- end}}
13+
{{ .ID }}{{tab}}{{ .Name }}{{ tab }}{{.ID}}{{tab}}{{ $kibana }}{{ tab }}{{ $apm }}{{ tab }}{{ $appsearch }}
14+
{{- end}}
15+
{{end}}

0 commit comments

Comments
 (0)