Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deployment: New traffic filter update command (#480)
New command to perform update actions on extensions: - ecctl deployment traffic-filter update <traffic-filter id> {--file <file-path> | --generate-payload}
- Loading branch information
Showing
12 changed files
with
435 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"include_by_default": false, | ||
"name": "1235", | ||
"region": "azure-eastus2", | ||
"rules": [ | ||
{ | ||
"id": "fad8132b0470479f95c3a41c5d67bd57", | ||
"source": "0.0.0.0/0" | ||
} | ||
], | ||
"type": "ip" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"id": "4e974d9476534d35b12fbdcfd0acee0a" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package cmddeploymenttrafficfilter | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
|
||
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/trafficfilterapi" | ||
"github.com/elastic/cloud-sdk-go/pkg/models" | ||
"github.com/elastic/cloud-sdk-go/pkg/util/cmdutil" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/elastic/ecctl/pkg/ecctl" | ||
) | ||
|
||
const updateExample = ` | ||
* Return the current traffic filter state as a valid update payload. | ||
ecctl deployment traffic-filter update <traffic-filter id> --generate-payload > update.json | ||
* After editing the file with your new values pass it as an argument to the --file flag. | ||
ecctl deployment traffic-filter update <traffic-filter id> --file update.json` | ||
|
||
var updateCmd = &cobra.Command{ | ||
Use: "update <traffic-filter id> {--file <file-path> | --generate-payload}", | ||
Short: "Updates a traffic-filter", | ||
Example: updateExample, | ||
PreRunE: cmdutil.MinimumNArgsAndUUID(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
genPayload, _ := cmd.Flags().GetBool("generate-payload") | ||
file, _ := cmd.Flags().GetString("file") | ||
|
||
if err := flagRequirements(genPayload, file); err != nil { | ||
return err | ||
} | ||
|
||
if genPayload { | ||
res, err := trafficfilterapi.Get(trafficfilterapi.GetParams{ | ||
API: ecctl.Get().API, | ||
ID: args[0], | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
enc := json.NewEncoder(ecctl.Get().Config.OutputDevice) | ||
enc.SetIndent("", " ") | ||
return enc.Encode( | ||
trafficfilterapi.NewUpdateRequestFromGet(res), | ||
) | ||
} | ||
|
||
var req models.TrafficFilterRulesetRequest | ||
if err := cmdutil.DecodeFile(file, &req); err != nil { | ||
return err | ||
} | ||
res, err := trafficfilterapi.Update(trafficfilterapi.UpdateParams{ | ||
API: ecctl.Get().API, | ||
ID: args[0], | ||
Req: &req, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ecctl.Get().Formatter.Format("", res) | ||
}, | ||
} | ||
|
||
func init() { | ||
initUpdateFlags() | ||
} | ||
|
||
func initUpdateFlags() { | ||
Command.AddCommand(updateCmd) | ||
updateCmd.Flags().Bool("generate-payload", false, "Outputs JSON which can be used as an argument for the --file flag.") | ||
updateCmd.Flags().String("file", "", "Path to the file containing the update JSON definition.") | ||
updateCmd.MarkFlagFilename("file", "json") | ||
} | ||
|
||
func flagRequirements(genPayload bool, file string) error { | ||
if genPayload && file != "" { | ||
return errors.New("both --file and --generate-payload are set. Only one may be used") | ||
} | ||
|
||
if !genPayload && file == "" { | ||
return errors.New("one of --file or --generate-payload must be set") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package cmddeploymenttrafficfilter | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/elastic/cloud-sdk-go/pkg/api" | ||
"github.com/elastic/cloud-sdk-go/pkg/api/mock" | ||
"github.com/elastic/cloud-sdk-go/pkg/models" | ||
|
||
"github.com/elastic/ecctl/cmd/util/testutils" | ||
) | ||
|
||
func Test_updateCmd(t *testing.T) { | ||
updatePayloadRawResp, err := ioutil.ReadFile("./testdata/update-payload.json") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var succeedPayloadResp = new(models.TrafficFilterRulesetRequest) | ||
if err := succeedPayloadResp.UnmarshalBinary(updatePayloadRawResp); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
updatePayloadJSONOutput, err := json.MarshalIndent(succeedPayloadResp, "", " ") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
updateRawResp, err := ioutil.ReadFile("./testdata/update.json") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var succeedResp = new(models.TrafficFilterRulesetResponse) | ||
if err := succeedResp.UnmarshalBinary(updateRawResp); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
updateJSONOutput, err := json.MarshalIndent(succeedResp, "", " ") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args testutils.Args | ||
want testutils.Assertion | ||
}{ | ||
{ | ||
name: "fails due to empty argument", | ||
args: testutils.Args{ | ||
Cmd: updateCmd, | ||
Args: []string{ | ||
"update", | ||
}, | ||
Cfg: testutils.MockCfg{Responses: []mock.Response{ | ||
mock.SampleInternalError(), | ||
}}, | ||
}, | ||
want: testutils.Assertion{ | ||
Err: `requires at least 1 arg(s), only received 0`, | ||
}, | ||
}, | ||
{ | ||
name: "fails due to missing flags", | ||
args: testutils.Args{ | ||
Cmd: updateCmd, | ||
Args: []string{ | ||
"update", "4e974d9476534d35b12fbdcfd0acee0a", | ||
}, | ||
Cfg: testutils.MockCfg{Responses: []mock.Response{ | ||
mock.SampleInternalError(), | ||
}}, | ||
}, | ||
want: testutils.Assertion{ | ||
Err: `one of --file or --generate-payload must be set`, | ||
}, | ||
}, | ||
{ | ||
name: "fails due to mismatching flags", | ||
args: testutils.Args{ | ||
Cmd: updateCmd, | ||
Args: []string{ | ||
"update", "4e974d9476534d35b12fbdcfd0acee0a", "--generate-payload", "--file", "testdata/update.json", | ||
}, | ||
Cfg: testutils.MockCfg{Responses: []mock.Response{ | ||
mock.SampleInternalError(), | ||
}}, | ||
}, | ||
want: testutils.Assertion{ | ||
Err: "both --file and --generate-payload are set. Only one may be used", | ||
}, | ||
}, | ||
{ | ||
name: "fails due to API error", | ||
args: testutils.Args{ | ||
Cmd: updateCmd, | ||
Args: []string{ | ||
"update", "4e974d9476534d35b12fbdcfd0acee0a", "--generate-payload", | ||
}, | ||
Cfg: testutils.MockCfg{Responses: []mock.Response{ | ||
mock.SampleInternalError(), | ||
}}, | ||
}, | ||
want: testutils.Assertion{ | ||
Err: mock.MultierrorInternalError.Error(), | ||
}, | ||
}, | ||
{ | ||
name: "succeeds to generate payload", | ||
args: testutils.Args{ | ||
Cmd: updateCmd, | ||
Args: []string{ | ||
"update", "4e974d9476534d35b12fbdcfd0acee0a", "--generate-payload", | ||
}, | ||
Cfg: testutils.MockCfg{ | ||
OutputFormat: "json", | ||
Responses: []mock.Response{ | ||
mock.New200ResponseAssertion( | ||
&mock.RequestAssertion{ | ||
Header: api.DefaultReadMockHeaders, | ||
Method: "GET", | ||
Path: "/api/v1/deployments/traffic-filter/rulesets/4e974d9476534d35b12fbdcfd0acee0a", | ||
Host: api.DefaultMockHost, | ||
Query: url.Values{ | ||
"include_associations": []string{"false"}, | ||
}, | ||
}, | ||
mock.NewByteBody(updatePayloadRawResp), | ||
), | ||
}, | ||
}, | ||
}, | ||
want: testutils.Assertion{ | ||
Stdout: string(updatePayloadJSONOutput) + "\n", | ||
}, | ||
}, | ||
{ | ||
name: "succeeds to update", | ||
args: testutils.Args{ | ||
Cmd: updateCmd, | ||
Args: []string{ | ||
"update", "4e974d9476534d35b12fbdcfd0acee0a", "--file", "./testdata/update-payload.json", | ||
}, | ||
Cfg: testutils.MockCfg{ | ||
OutputFormat: "json", | ||
Responses: []mock.Response{ | ||
mock.New200ResponseAssertion( | ||
&mock.RequestAssertion{ | ||
Header: api.DefaultWriteMockHeaders, | ||
Method: "PUT", | ||
Path: "/api/v1/deployments/traffic-filter/rulesets/4e974d9476534d35b12fbdcfd0acee0a", | ||
Body: mock.NewStringBody( | ||
`{"include_by_default":false,"name":"1235","region":"azure-eastus2","rules":[{"id":"fad8132b0470479f95c3a41c5d67bd57","source":"0.0.0.0/0"}],"type":"ip"}` + | ||
"\n"), | ||
Host: api.DefaultMockHost, | ||
}, | ||
mock.NewByteBody(updateRawResp), | ||
), | ||
}, | ||
}, | ||
}, | ||
want: testutils.Assertion{ | ||
Stdout: string(updateJSONOutput) + "\n", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
testutils.RunCmdAssertion(t, tt.args, tt.want) | ||
tt.args.Cmd.ResetFlags() | ||
defer initUpdateFlags() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.