Skip to content

Commit c8febba

Browse files
yfodilremyleone
andauthored
feat(vpc-gw): add edit mode for PAT rules (#4798)
Co-authored-by: Rémy Léone <[email protected]>
1 parent 26f88c9 commit c8febba

File tree

7 files changed

+135
-2
lines changed

7 files changed

+135
-2
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
3+
This command starts your default editor to edit a marshaled version of your resource
4+
Default editor will be taken from $VISUAL, then $EDITOR or an editor based on your system
5+
6+
USAGE:
7+
scw vpc-gw pat-rule edit <gateway-id ...> [arg=value ...]
8+
9+
ARGS:
10+
gateway-id ID of the PAT rules' Public Gateway
11+
[mode=yaml] marshaling used when editing data (yaml | json)
12+
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config
13+
14+
FLAGS:
15+
-h, --help help for edit
16+
17+
GLOBAL FLAGS:
18+
-c, --config string The path to the config file
19+
-D, --debug Enable debug mode
20+
-o, --output string Output format: json or human, see 'scw help output' for more info (default "human")
21+
-p, --profile string The config profile to use

cmd/scw/testdata/test-all-usage-vpc-gw-pat-rule-usage#01.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ USAGE:
88
AVAILABLE COMMANDS:
99
create-v2 Create a PAT rule
1010
delete-v2 Delete a PAT rule
11+
edit Edit all PAT rules of a Public Gateway
1112
get-v2 Get a PAT rule
1213
list-v2 List PAT rules
1314
set-v2 Set all PAT rules

cmd/scw/testdata/test-all-usage-vpc-gw-pat-rule-usage.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ USAGE:
88
AVAILABLE COMMANDS:
99
create-v2 Create a PAT rule
1010
delete-v2 Delete a PAT rule
11+
edit Edit all PAT rules of a Public Gateway
1112
get-v2 Get a PAT rule
1213
list-v2 List PAT rules
1314
set-v2 Set all PAT rules

docs/commands/vpc-gw.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ This API allows you to manage your Public Gateways.
4141
- [PAT rules management](#pat-rules-management)
4242
- [Create a PAT rule](#create-a-pat-rule)
4343
- [Delete a PAT rule](#delete-a-pat-rule)
44+
- [Edit all PAT rules of a Public Gateway](#edit-all-pat-rules-of-a-public-gateway)
4445
- [Get a PAT rule](#get-a-pat-rule)
4546
- [List PAT rules](#list-pat-rules)
4647
- [Set all PAT rules](#set-all-pat-rules)
@@ -807,6 +808,28 @@ scw vpc-gw pat-rule delete <pat-rule-id ...> [arg=value ...]
807808

808809

809810

811+
### Edit all PAT rules of a Public Gateway
812+
813+
This command starts your default editor to edit a marshaled version of your resource
814+
Default editor will be taken from $VISUAL, then $EDITOR or an editor based on your system
815+
816+
**Usage:**
817+
818+
```
819+
scw vpc-gw pat-rule edit <gateway-id ...> [arg=value ...]
820+
```
821+
822+
823+
**Args:**
824+
825+
| Name | | Description |
826+
|------|---|-------------|
827+
| gateway-id | Required | ID of the PAT rules' Public Gateway |
828+
| mode | Default: `yaml`<br />One of: `yaml`, `json` | marshaling used when editing data |
829+
| zone | Default: `fr-par-1` | Zone to target. If none is passed will use default zone from the config |
830+
831+
832+
810833
### Get a PAT rule
811834

812835
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.

internal/namespaces/vpcgw/v2/custom.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,9 @@ func GetCommands() *core.Commands {
3333
cmds.MustFind("vpc-gw", "gateway-network", "create").Override(gatewayNetworkCreateBuilder)
3434
cmds.MustFind("vpc-gw", "gateway-network", "delete").Override(gatewayNetworkDeleteBuilder)
3535

36+
cmds.Merge(core.NewCommands(
37+
vpcgwPATRulesEditCommand(),
38+
))
39+
3640
return cmds
3741
}

internal/namespaces/vpcgw/v2/custom_gateway.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ func gatewayMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error)
4848
vpcgtw := tmp(i.(vpcgw.Gateway))
4949
opt.Sections = []*human.MarshalSection{
5050
{
51-
FieldName: "IP",
52-
Title: "IP",
51+
FieldName: "IPv4",
52+
Title: "IPv4",
5353
},
5454
{
5555
FieldName: "GatewayNetworks",
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package vpcgw
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
8+
"github.com/scaleway/scaleway-cli/v2/core"
9+
"github.com/scaleway/scaleway-cli/v2/internal/editor"
10+
"github.com/scaleway/scaleway-sdk-go/api/vpcgw/v2"
11+
"github.com/scaleway/scaleway-sdk-go/scw"
12+
)
13+
14+
var vpcgwPATRulesEditYamlExample = `pat_rules:
15+
- public_port: 2222
16+
private_ip: 192.168.1.1
17+
private_port: 22
18+
protocol: tcp
19+
`
20+
21+
type vpcgwPATRulesEditArgs struct {
22+
Zone scw.Zone
23+
GatewayID string
24+
Mode editor.MarshalMode
25+
}
26+
27+
func vpcgwPATRulesEditCommand() *core.Command {
28+
return &core.Command{
29+
Short: "Edit all PAT rules of a Public Gateway",
30+
Long: editor.LongDescription,
31+
Namespace: "vpc-gw",
32+
Resource: "pat-rule",
33+
Verb: "edit",
34+
ArgsType: reflect.TypeOf(vpcgwPATRulesEditArgs{}),
35+
ArgSpecs: core.ArgSpecs{
36+
{
37+
Name: "gateway-id",
38+
Short: "ID of the PAT rules' Public Gateway",
39+
Required: true,
40+
Positional: true,
41+
},
42+
editor.MarshalModeArgSpec(),
43+
core.ZoneArgSpec(),
44+
},
45+
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
46+
args := argsI.(*vpcgwPATRulesEditArgs)
47+
48+
client := core.ExtractClient(ctx)
49+
api := vpcgw.NewAPI(client)
50+
51+
setRequest := &vpcgw.SetPatRulesRequest{
52+
Zone: args.Zone,
53+
GatewayID: args.GatewayID,
54+
}
55+
56+
rules, err := api.ListPatRules(&vpcgw.ListPatRulesRequest{
57+
Zone: args.Zone,
58+
GatewayIDs: []string{args.GatewayID},
59+
}, scw.WithContext(ctx))
60+
if err != nil {
61+
return nil, fmt.Errorf("failed to list PAT rules: %w", err)
62+
}
63+
64+
editedSetRequest, err := editor.UpdateResourceEditor(rules, setRequest, &editor.Config{
65+
PutRequest: true,
66+
MarshalMode: args.Mode,
67+
Template: vpcgwPATRulesEditYamlExample,
68+
})
69+
if err != nil {
70+
return nil, err
71+
}
72+
73+
setRequest = editedSetRequest.(*vpcgw.SetPatRulesRequest)
74+
75+
resp, err := api.SetPatRules(setRequest, scw.WithContext(ctx))
76+
if err != nil {
77+
return nil, fmt.Errorf("failed to set PAT rules: %w", err)
78+
}
79+
80+
return resp.PatRules, nil
81+
},
82+
}
83+
}

0 commit comments

Comments
 (0)