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