Skip to content

Commit e5d22b5

Browse files
authored
deployment: Adds read and delete extension subcommands (#458)
Adds support for read and delete actions for extensions: - ecctl deployment extension show <extension id> [--include-deployments]: Shows information about an extension. - ecctl deployment extension list: Lists the extensions. - ecctl deployment extension delete <extension id>: Deletes an extension.
1 parent 6a9e3e9 commit e5d22b5

22 files changed

+924
-0
lines changed

cmd/deployment/command.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/spf13/cobra"
2222

2323
cmdelasticsearch "github.com/elastic/ecctl/cmd/deployment/elasticsearch"
24+
cmddeploymentextension "github.com/elastic/ecctl/cmd/deployment/extension"
2425
cmddeploymentnote "github.com/elastic/ecctl/cmd/deployment/note"
2526
cmddeploymentplan "github.com/elastic/ecctl/cmd/deployment/plan"
2627
cmddeploymentresource "github.com/elastic/ecctl/cmd/deployment/resource"
@@ -40,6 +41,7 @@ var Command = &cobra.Command{
4041

4142
func init() {
4243
Command.AddCommand(
44+
cmddeploymentextension.Command,
4345
cmddeploymentnote.Command,
4446
cmddeploymentplan.Command,
4547
cmddeploymentresource.Command,

cmd/deployment/extension/command.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 cmddeploymentextension
19+
20+
import (
21+
"github.com/spf13/cobra"
22+
)
23+
24+
// Command represents the deployment extension subcommand.
25+
var Command = &cobra.Command{
26+
Use: "extension",
27+
Short: "Manages deployment extensions, such as custom plugins or bundles",
28+
PreRunE: cobra.MaximumNArgs(0),
29+
Run: func(cmd *cobra.Command, args []string) { cmd.Help() },
30+
}

cmd/deployment/extension/delete.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 cmddeploymentextension
19+
20+
import (
21+
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/extensionapi"
22+
"github.com/spf13/cobra"
23+
24+
"github.com/elastic/ecctl/pkg/ecctl"
25+
)
26+
27+
var deleteCmd = &cobra.Command{
28+
Use: "delete <extension id>",
29+
Short: "Deletes a deployment extension",
30+
PreRunE: cobra.MinimumNArgs(1),
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
return extensionapi.Delete(extensionapi.DeleteParams{
33+
API: ecctl.Get().API,
34+
ExtensionID: args[0],
35+
})
36+
},
37+
}
38+
39+
func init() {
40+
Command.AddCommand(deleteCmd)
41+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 cmddeploymentextension
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 empty argument",
37+
args: testutils.Args{
38+
Cmd: showCmd,
39+
Args: []string{"delete"},
40+
Cfg: testutils.MockCfg{Responses: []mock.Response{
41+
mock.SampleInternalError(),
42+
}},
43+
},
44+
want: testutils.Assertion{
45+
Err: `requires at least 1 arg(s), only received 0`,
46+
},
47+
},
48+
{
49+
name: "fails due to API error",
50+
args: testutils.Args{
51+
Cmd: showCmd,
52+
Args: []string{
53+
"delete", "2536123203",
54+
},
55+
Cfg: testutils.MockCfg{Responses: []mock.Response{
56+
mock.SampleInternalError(),
57+
}},
58+
},
59+
want: testutils.Assertion{
60+
Err: mock.MultierrorInternalError.Error(),
61+
},
62+
},
63+
{
64+
name: "succeeds",
65+
args: testutils.Args{
66+
Cmd: showCmd,
67+
Args: []string{
68+
"delete", "2536123203",
69+
},
70+
Cfg: testutils.MockCfg{
71+
OutputFormat: "json",
72+
Responses: []mock.Response{
73+
mock.New200ResponseAssertion(
74+
&mock.RequestAssertion{
75+
Header: api.DefaultWriteMockHeaders,
76+
Method: "DELETE",
77+
Path: "/api/v1/deployments/extensions/2536123203",
78+
Host: api.DefaultMockHost,
79+
},
80+
mock.NewByteBody(nil),
81+
),
82+
},
83+
},
84+
},
85+
},
86+
}
87+
for _, tt := range tests {
88+
t.Run(tt.name, func(t *testing.T) {
89+
testutils.RunCmdAssertion(t, tt.args, tt.want)
90+
})
91+
}
92+
}

cmd/deployment/extension/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 cmddeploymentextension
19+
20+
import (
21+
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/extensionapi"
22+
"github.com/spf13/cobra"
23+
24+
"github.com/elastic/ecctl/pkg/ecctl"
25+
)
26+
27+
var listCmd = &cobra.Command{
28+
Use: "list",
29+
Short: "Lists all deployment extensions",
30+
PreRunE: cobra.MaximumNArgs(0),
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
res, err := extensionapi.List(extensionapi.ListParams{
33+
API: ecctl.Get().API,
34+
})
35+
if err != nil {
36+
return err
37+
}
38+
39+
return ecctl.Get().Formatter.Format("", res)
40+
},
41+
}
42+
43+
func init() {
44+
Command.AddCommand(listCmd)
45+
}

cmd/deployment/extension/list_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 cmddeploymentextension
19+
20+
import (
21+
"encoding/json"
22+
"io/ioutil"
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+
29+
"github.com/elastic/ecctl/cmd/util/testutils"
30+
)
31+
32+
func Test_listCmd(t *testing.T) {
33+
listRawResp, err := ioutil.ReadFile("./testdata/list.json")
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
38+
var succeedResp = new(models.Extensions)
39+
if err := succeedResp.UnmarshalBinary(listRawResp); err != nil {
40+
t.Fatal(err)
41+
}
42+
43+
listJSONOutput, err := json.MarshalIndent(succeedResp, "", " ")
44+
if err != nil {
45+
t.Fatal(err)
46+
}
47+
48+
tests := []struct {
49+
name string
50+
args testutils.Args
51+
want testutils.Assertion
52+
}{
53+
{
54+
name: "fails due to API error",
55+
args: testutils.Args{
56+
Cmd: listCmd,
57+
Args: []string{"list"},
58+
Cfg: testutils.MockCfg{Responses: []mock.Response{
59+
mock.SampleInternalError(),
60+
}},
61+
},
62+
want: testutils.Assertion{
63+
Err: mock.MultierrorInternalError.Error(),
64+
},
65+
},
66+
{
67+
name: "succeeds",
68+
args: testutils.Args{
69+
Cmd: listCmd,
70+
Args: []string{"list"},
71+
Cfg: testutils.MockCfg{
72+
OutputFormat: "json",
73+
Responses: []mock.Response{
74+
mock.New200ResponseAssertion(
75+
&mock.RequestAssertion{
76+
Header: api.DefaultReadMockHeaders,
77+
Method: "GET",
78+
Path: "/api/v1/deployments/extensions",
79+
Host: api.DefaultMockHost,
80+
},
81+
mock.NewByteBody(listRawResp),
82+
),
83+
},
84+
},
85+
},
86+
want: testutils.Assertion{
87+
Stdout: string(listJSONOutput) + "\n",
88+
},
89+
},
90+
}
91+
for _, tt := range tests {
92+
t.Run(tt.name, func(t *testing.T) {
93+
testutils.RunCmdAssertion(t, tt.args, tt.want)
94+
})
95+
}
96+
}

cmd/deployment/extension/show.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 cmddeploymentextension
19+
20+
import (
21+
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/extensionapi"
22+
"github.com/spf13/cobra"
23+
24+
"github.com/elastic/ecctl/pkg/ecctl"
25+
)
26+
27+
var showCmd = &cobra.Command{
28+
Use: "show <extension id> [--include-deployments]",
29+
Short: "Shows information about a deployment extension",
30+
PreRunE: cobra.MinimumNArgs(1),
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
deps, _ := cmd.Flags().GetBool("include-deployments")
33+
34+
res, err := extensionapi.Get(extensionapi.GetParams{
35+
API: ecctl.Get().API,
36+
ExtensionID: args[0],
37+
IncludeDeployments: deps,
38+
})
39+
if err != nil {
40+
return err
41+
}
42+
43+
return ecctl.Get().Formatter.Format("", res)
44+
},
45+
}
46+
47+
func init() {
48+
initShowFlags()
49+
}
50+
51+
func initShowFlags() {
52+
Command.AddCommand(showCmd)
53+
showCmd.Flags().Bool("include-deployments", false, "Include deployments referencing this extension. Up to only 10000 deployments will be included.")
54+
}

0 commit comments

Comments
 (0)