diff --git a/cmd/scw/testdata/test-all-usage-vpc-gw-pat-rule-edit-usage.golden b/cmd/scw/testdata/test-all-usage-vpc-gw-pat-rule-edit-usage.golden new file mode 100644 index 0000000000..ce3880cd08 --- /dev/null +++ b/cmd/scw/testdata/test-all-usage-vpc-gw-pat-rule-edit-usage.golden @@ -0,0 +1,21 @@ +🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲 +πŸŸ₯πŸŸ₯πŸŸ₯ STDERR️️ πŸŸ₯πŸŸ₯πŸŸ₯️ +This command starts your default editor to edit a marshaled version of your resource +Default editor will be taken from $VISUAL, then $EDITOR or an editor based on your system + +USAGE: + scw vpc-gw pat-rule edit [arg=value ...] + +ARGS: + gateway-id ID of the PAT rules' Public Gateway + [mode=yaml] marshaling used when editing data (yaml | json) + [zone=fr-par-1] Zone to target. If none is passed will use default zone from the config + +FLAGS: + -h, --help help for edit + +GLOBAL FLAGS: + -c, --config string The path to the config file + -D, --debug Enable debug mode + -o, --output string Output format: json or human, see 'scw help output' for more info (default "human") + -p, --profile string The config profile to use diff --git a/cmd/scw/testdata/test-all-usage-vpc-gw-pat-rule-usage#01.golden b/cmd/scw/testdata/test-all-usage-vpc-gw-pat-rule-usage#01.golden index e280ed5d60..e86e3c415b 100644 --- a/cmd/scw/testdata/test-all-usage-vpc-gw-pat-rule-usage#01.golden +++ b/cmd/scw/testdata/test-all-usage-vpc-gw-pat-rule-usage#01.golden @@ -8,6 +8,7 @@ USAGE: AVAILABLE COMMANDS: create-v2 Create a PAT rule delete-v2 Delete a PAT rule + edit Edit all PAT rules of a Public Gateway get-v2 Get a PAT rule list-v2 List PAT rules set-v2 Set all PAT rules diff --git a/cmd/scw/testdata/test-all-usage-vpc-gw-pat-rule-usage.golden b/cmd/scw/testdata/test-all-usage-vpc-gw-pat-rule-usage.golden index e280ed5d60..e86e3c415b 100644 --- a/cmd/scw/testdata/test-all-usage-vpc-gw-pat-rule-usage.golden +++ b/cmd/scw/testdata/test-all-usage-vpc-gw-pat-rule-usage.golden @@ -8,6 +8,7 @@ USAGE: AVAILABLE COMMANDS: create-v2 Create a PAT rule delete-v2 Delete a PAT rule + edit Edit all PAT rules of a Public Gateway get-v2 Get a PAT rule list-v2 List PAT rules set-v2 Set all PAT rules diff --git a/docs/commands/vpc-gw.md b/docs/commands/vpc-gw.md index 351ff5e8c9..e3a15b131d 100644 --- a/docs/commands/vpc-gw.md +++ b/docs/commands/vpc-gw.md @@ -41,6 +41,7 @@ This API allows you to manage your Public Gateways. - [PAT rules management](#pat-rules-management) - [Create a PAT rule](#create-a-pat-rule) - [Delete a PAT rule](#delete-a-pat-rule) + - [Edit all PAT rules of a Public Gateway](#edit-all-pat-rules-of-a-public-gateway) - [Get a PAT rule](#get-a-pat-rule) - [List PAT rules](#list-pat-rules) - [Set all PAT rules](#set-all-pat-rules) @@ -807,6 +808,28 @@ scw vpc-gw pat-rule delete [arg=value ...] +### Edit all PAT rules of a Public Gateway + +This command starts your default editor to edit a marshaled version of your resource +Default editor will be taken from $VISUAL, then $EDITOR or an editor based on your system + +**Usage:** + +``` +scw vpc-gw pat-rule edit [arg=value ...] +``` + + +**Args:** + +| Name | | Description | +|------|---|-------------| +| gateway-id | Required | ID of the PAT rules' Public Gateway | +| mode | Default: `yaml`
One of: `yaml`, `json` | marshaling used when editing data | +| zone | Default: `fr-par-1` | Zone to target. If none is passed will use default zone from the config | + + + ### Get a PAT rule Get a PAT rule, specified by its PAT rule ID. The response object gives full details of the PAT rule, including the Public Gateway it belongs to and the configuration settings in terms of public / private ports, private IP and protocol. diff --git a/internal/namespaces/vpcgw/v2/custom.go b/internal/namespaces/vpcgw/v2/custom.go index e69d18518a..0339834ed1 100644 --- a/internal/namespaces/vpcgw/v2/custom.go +++ b/internal/namespaces/vpcgw/v2/custom.go @@ -33,5 +33,9 @@ func GetCommands() *core.Commands { cmds.MustFind("vpc-gw", "gateway-network", "create").Override(gatewayNetworkCreateBuilder) cmds.MustFind("vpc-gw", "gateway-network", "delete").Override(gatewayNetworkDeleteBuilder) + cmds.Merge(core.NewCommands( + vpcgwPATRulesEditCommand(), + )) + return cmds } diff --git a/internal/namespaces/vpcgw/v2/custom_gateway.go b/internal/namespaces/vpcgw/v2/custom_gateway.go index 09c6eaaf2e..0688ca80cf 100644 --- a/internal/namespaces/vpcgw/v2/custom_gateway.go +++ b/internal/namespaces/vpcgw/v2/custom_gateway.go @@ -48,8 +48,8 @@ func gatewayMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) vpcgtw := tmp(i.(vpcgw.Gateway)) opt.Sections = []*human.MarshalSection{ { - FieldName: "IP", - Title: "IP", + FieldName: "IPv4", + Title: "IPv4", }, { FieldName: "GatewayNetworks", diff --git a/internal/namespaces/vpcgw/v2/custom_pat_rule.go b/internal/namespaces/vpcgw/v2/custom_pat_rule.go new file mode 100644 index 0000000000..253e056e16 --- /dev/null +++ b/internal/namespaces/vpcgw/v2/custom_pat_rule.go @@ -0,0 +1,83 @@ +package vpcgw + +import ( + "context" + "fmt" + "reflect" + + "github.com/scaleway/scaleway-cli/v2/core" + "github.com/scaleway/scaleway-cli/v2/internal/editor" + "github.com/scaleway/scaleway-sdk-go/api/vpcgw/v2" + "github.com/scaleway/scaleway-sdk-go/scw" +) + +var vpcgwPATRulesEditYamlExample = `pat_rules: +- public_port: 2222 + private_ip: 192.168.1.1 + private_port: 22 + protocol: tcp +` + +type vpcgwPATRulesEditArgs struct { + Zone scw.Zone + GatewayID string + Mode editor.MarshalMode +} + +func vpcgwPATRulesEditCommand() *core.Command { + return &core.Command{ + Short: "Edit all PAT rules of a Public Gateway", + Long: editor.LongDescription, + Namespace: "vpc-gw", + Resource: "pat-rule", + Verb: "edit", + ArgsType: reflect.TypeOf(vpcgwPATRulesEditArgs{}), + ArgSpecs: core.ArgSpecs{ + { + Name: "gateway-id", + Short: "ID of the PAT rules' Public Gateway", + Required: true, + Positional: true, + }, + editor.MarshalModeArgSpec(), + core.ZoneArgSpec(), + }, + Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) { + args := argsI.(*vpcgwPATRulesEditArgs) + + client := core.ExtractClient(ctx) + api := vpcgw.NewAPI(client) + + setRequest := &vpcgw.SetPatRulesRequest{ + Zone: args.Zone, + GatewayID: args.GatewayID, + } + + rules, err := api.ListPatRules(&vpcgw.ListPatRulesRequest{ + Zone: args.Zone, + GatewayIDs: []string{args.GatewayID}, + }, scw.WithContext(ctx)) + if err != nil { + return nil, fmt.Errorf("failed to list PAT rules: %w", err) + } + + editedSetRequest, err := editor.UpdateResourceEditor(rules, setRequest, &editor.Config{ + PutRequest: true, + MarshalMode: args.Mode, + Template: vpcgwPATRulesEditYamlExample, + }) + if err != nil { + return nil, err + } + + setRequest = editedSetRequest.(*vpcgw.SetPatRulesRequest) + + resp, err := api.SetPatRules(setRequest, scw.WithContext(ctx)) + if err != nil { + return nil, fmt.Errorf("failed to set PAT rules: %w", err) + } + + return resp.PatRules, nil + }, + } +}