Skip to content

feat(vpc-gw): add edit mode for PAT rules #4798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 <gateway-id ...> [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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions docs/commands/vpc-gw.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -807,6 +808,28 @@ scw vpc-gw pat-rule delete <pat-rule-id ...> [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 <gateway-id ...> [arg=value ...]
```


**Args:**

| Name | | Description |
|------|---|-------------|
| gateway-id | Required | ID of the PAT rules' Public Gateway |
| mode | Default: `yaml`<br />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.
Expand Down
4 changes: 4 additions & 0 deletions internal/namespaces/vpcgw/v2/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions internal/namespaces/vpcgw/v2/custom_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
83 changes: 83 additions & 0 deletions internal/namespaces/vpcgw/v2/custom_pat_rule.go
Original file line number Diff line number Diff line change
@@ -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
},
}
}
Loading