Skip to content

Commit 73c3e03

Browse files
authored
deployment: New commands for traffic-filter show, list and delete (#455)
Adds support for read and delete actions for traffic filters: - ecctl deployment traffic-filter show <ruleset id> [--include-associations]: Shows information about a traffic filter ruleset. - ecctl deployment traffic-filter list [--include-associations] [--single-region <region>]: Lists the traffic filter rulesets. - ecctl deployment traffic-filter delete <ruleset id> [--ignore-associations]: Deletes a traffic filter ruleset.
1 parent 7680eff commit 73c3e03

24 files changed

+1154
-0
lines changed

cmd/deployment/command.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
cmddeploymentplan "github.com/elastic/ecctl/cmd/deployment/plan"
2626
cmddeploymentresource "github.com/elastic/ecctl/cmd/deployment/resource"
2727
cmddeploymenttemplate "github.com/elastic/ecctl/cmd/deployment/template"
28+
cmddeploymenttrafficfilter "github.com/elastic/ecctl/cmd/deployment/trafficfilter"
2829
)
2930

3031
// Command is the deployment subcommand
@@ -43,6 +44,7 @@ func init() {
4344
cmddeploymentplan.Command,
4445
cmddeploymentresource.Command,
4546
cmddeploymenttemplate.Command,
47+
cmddeploymenttrafficfilter.Command,
4648
cmdelasticsearch.Command,
4749
)
4850
}
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 cmddeploymenttrafficfilter
19+
20+
import (
21+
"github.com/spf13/cobra"
22+
)
23+
24+
// Command represents the deployment traffic-filter subcommand.
25+
var Command = &cobra.Command{
26+
Use: "traffic-filter",
27+
Short: "Manages traffic filter rulesets",
28+
PreRunE: cobra.MaximumNArgs(0),
29+
Run: func(cmd *cobra.Command, args []string) { cmd.Help() },
30+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 cmddeploymenttrafficfilter
19+
20+
import (
21+
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/trafficfilterapi"
22+
sdkcmdutil "github.com/elastic/cloud-sdk-go/pkg/util/cmdutil"
23+
"github.com/spf13/cobra"
24+
25+
"github.com/elastic/ecctl/pkg/ecctl"
26+
)
27+
28+
var deleteCmd = &cobra.Command{
29+
Use: "delete <ruleset id> [--ignore-associations]",
30+
Short: "Deletes a traffic filter ruleset",
31+
PreRunE: sdkcmdutil.MinimumNArgsAndUUID(1),
32+
RunE: func(cmd *cobra.Command, args []string) error {
33+
assoc, _ := cmd.Flags().GetBool("ignore-associations")
34+
35+
return trafficfilterapi.Delete(trafficfilterapi.DeleteParams{
36+
API: ecctl.Get().API,
37+
ID: args[0],
38+
IgnoreAssociations: assoc,
39+
})
40+
},
41+
}
42+
43+
func init() {
44+
initDeleteFlags()
45+
}
46+
47+
func initDeleteFlags() {
48+
Command.AddCommand(deleteCmd)
49+
deleteCmd.Flags().Bool("ignore-associations", false, "Optional flag to delete the ruleset even if it has associated rules")
50+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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 cmddeploymenttrafficfilter
19+
20+
import (
21+
"net/url"
22+
"testing"
23+
24+
"github.com/elastic/cloud-sdk-go/pkg/api"
25+
"github.com/elastic/cloud-sdk-go/pkg/api/mock"
26+
27+
"github.com/elastic/ecctl/cmd/util/testutils"
28+
)
29+
30+
func Test_deleteCmd(t *testing.T) {
31+
tests := []struct {
32+
name string
33+
args testutils.Args
34+
want testutils.Assertion
35+
}{
36+
{
37+
name: "fails due to empty argument",
38+
args: testutils.Args{
39+
Cmd: showCmd,
40+
Args: []string{"delete"},
41+
Cfg: testutils.MockCfg{Responses: []mock.Response{
42+
mock.SampleInternalError(),
43+
}},
44+
},
45+
want: testutils.Assertion{
46+
Err: `requires at least 1 arg(s), only received 0`,
47+
},
48+
},
49+
{
50+
name: "fails due to API error",
51+
args: testutils.Args{
52+
Cmd: showCmd,
53+
Args: []string{
54+
"delete", "11111111111111111111111111111111",
55+
},
56+
Cfg: testutils.MockCfg{Responses: []mock.Response{
57+
mock.SampleInternalError(),
58+
}},
59+
},
60+
want: testutils.Assertion{
61+
Err: mock.MultierrorInternalError.Error(),
62+
},
63+
},
64+
{
65+
name: "succeeds",
66+
args: testutils.Args{
67+
Cmd: showCmd,
68+
Args: []string{
69+
"delete", "4e974d9476534d35b12fbdcfd0acee0a",
70+
},
71+
Cfg: testutils.MockCfg{
72+
OutputFormat: "json",
73+
Responses: []mock.Response{
74+
mock.New200ResponseAssertion(
75+
&mock.RequestAssertion{
76+
Header: api.DefaultWriteMockHeaders,
77+
Method: "DELETE",
78+
Path: "/api/v1/deployments/traffic-filter/rulesets/4e974d9476534d35b12fbdcfd0acee0a",
79+
Host: api.DefaultMockHost,
80+
Query: url.Values{
81+
"ignore_associations": []string{"false"},
82+
},
83+
},
84+
mock.NewByteBody(nil),
85+
),
86+
},
87+
},
88+
},
89+
},
90+
{
91+
name: "succeeds with ignore associations",
92+
args: testutils.Args{
93+
Cmd: showCmd,
94+
Args: []string{
95+
"delete", "4e974d9476534d35b12fbdcfd0acee0a", "--ignore-associations",
96+
},
97+
Cfg: testutils.MockCfg{
98+
OutputFormat: "json",
99+
Responses: []mock.Response{
100+
mock.New200ResponseAssertion(
101+
&mock.RequestAssertion{
102+
Header: api.DefaultWriteMockHeaders,
103+
Method: "DELETE",
104+
Path: "/api/v1/deployments/traffic-filter/rulesets/4e974d9476534d35b12fbdcfd0acee0a",
105+
Host: api.DefaultMockHost,
106+
Query: url.Values{
107+
"ignore_associations": []string{"true"},
108+
},
109+
},
110+
mock.NewByteBody(nil),
111+
),
112+
},
113+
},
114+
},
115+
},
116+
}
117+
for _, tt := range tests {
118+
t.Run(tt.name, func(t *testing.T) {
119+
testutils.RunCmdAssertion(t, tt.args, tt.want)
120+
tt.args.Cmd.ResetFlags()
121+
})
122+
}
123+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 cmddeploymenttrafficfilter
19+
20+
import (
21+
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/trafficfilterapi"
22+
"github.com/spf13/cobra"
23+
24+
"github.com/elastic/ecctl/pkg/ecctl"
25+
)
26+
27+
var listCmd = &cobra.Command{
28+
Use: "list [--include-associations] [--single-region <region>]",
29+
Short: "Lists the traffic filter rulesets",
30+
PreRunE: cobra.MaximumNArgs(0),
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
region, _ := cmd.Flags().GetString("single-region")
33+
assoc, _ := cmd.Flags().GetBool("include-associations")
34+
35+
res, err := trafficfilterapi.List(trafficfilterapi.ListParams{
36+
API: ecctl.Get().API,
37+
Region: region,
38+
IncludeAssociations: assoc,
39+
})
40+
if err != nil {
41+
return err
42+
}
43+
44+
return ecctl.Get().Formatter.Format("", res)
45+
},
46+
}
47+
48+
func init() {
49+
initListFlags()
50+
}
51+
52+
func initListFlags() {
53+
Command.AddCommand(listCmd)
54+
listCmd.Flags().String("single-region", "", "Optional flag to list traffic filters from a specific region only")
55+
listCmd.Flags().Bool("include-associations", false, "Optional flag to include all associated resources")
56+
}

0 commit comments

Comments
 (0)