|
| 1 | +package vpc |
| 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/vpc/v2" |
| 11 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 12 | +) |
| 13 | + |
| 14 | +var vpcACLEditYamlExample = `default_policy: drop |
| 15 | +is_ipv6: false |
| 16 | +rules: [ |
| 17 | +- protocol: TCP |
| 18 | + src_port_low: 0 |
| 19 | + src_port_high: 0 |
| 20 | + dst_port_low: 80 |
| 21 | + dst_port_high: 80 |
| 22 | + source: 0.0.0.0/0 |
| 23 | + destination: 0.0.0.0/0 |
| 24 | + description: Allow HTTP traffic from any source |
| 25 | + action: accept |
| 26 | +- protocol: TCP |
| 27 | + src_port_low: 0 |
| 28 | + src_port_high: 0 |
| 29 | + dst_port_low: 443 |
| 30 | + dst_port_high: 443 |
| 31 | + source: 0.0.0.0/0 |
| 32 | + destination: 0.0.0.0/0 |
| 33 | + description: Allow HTTPS traffic from any source |
| 34 | + action: accept |
| 35 | +] |
| 36 | +` |
| 37 | + |
| 38 | +type vpcACLEditArgs struct { |
| 39 | + Region scw.Region |
| 40 | + VpcID string |
| 41 | + IsIPv6 bool |
| 42 | + DefaultPolicy vpc.Action |
| 43 | + Mode editor.MarshalMode |
| 44 | +} |
| 45 | + |
| 46 | +func vpcACLEditCommand() *core.Command { |
| 47 | + return &core.Command{ |
| 48 | + Short: "Edit all ACL rules of a VPC", |
| 49 | + Long: editor.LongDescription, |
| 50 | + Namespace: "vpc", |
| 51 | + Resource: "rule", |
| 52 | + Verb: "edit", |
| 53 | + ArgsType: reflect.TypeOf(vpcACLEditArgs{}), |
| 54 | + ArgSpecs: core.ArgSpecs{ |
| 55 | + { |
| 56 | + Name: "vpc-id", |
| 57 | + Short: "ID of the Network ACL's VPC", |
| 58 | + Required: true, |
| 59 | + Positional: false, |
| 60 | + }, |
| 61 | + { |
| 62 | + Name: "is-ipv6", |
| 63 | + Short: "Defines whether this set of ACL rules is for IPv6 (false = IPv4). Each Network ACL can have rules for only one IP type", |
| 64 | + Required: false, |
| 65 | + Positional: false, |
| 66 | + }, |
| 67 | + { |
| 68 | + Name: "default-policy", |
| 69 | + Short: "Action to take for packets which do not match any rules", |
| 70 | + Required: false, |
| 71 | + Positional: false, |
| 72 | + }, |
| 73 | + editor.MarshalModeArgSpec(), |
| 74 | + core.RegionArgSpec(), |
| 75 | + }, |
| 76 | + Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) { |
| 77 | + args := argsI.(*vpcACLEditArgs) |
| 78 | + |
| 79 | + client := core.ExtractClient(ctx) |
| 80 | + api := vpc.NewAPI(client) |
| 81 | + |
| 82 | + setRequest := &vpc.SetACLRequest{ |
| 83 | + Region: args.Region, |
| 84 | + VpcID: args.VpcID, |
| 85 | + IsIPv6: args.IsIPv6, |
| 86 | + DefaultPolicy: args.DefaultPolicy, |
| 87 | + } |
| 88 | + |
| 89 | + rules, err := api.GetACL(&vpc.GetACLRequest{ |
| 90 | + Region: args.Region, |
| 91 | + VpcID: args.VpcID, |
| 92 | + IsIPv6: args.IsIPv6, |
| 93 | + }, scw.WithContext(ctx)) |
| 94 | + if err != nil { |
| 95 | + return nil, fmt.Errorf("failed to list ACL rules: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + editedSetRequest, err := editor.UpdateResourceEditor(rules, setRequest, &editor.Config{ |
| 99 | + PutRequest: true, |
| 100 | + MarshalMode: args.Mode, |
| 101 | + Template: vpcACLEditYamlExample, |
| 102 | + }) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + |
| 107 | + setRequest = editedSetRequest.(*vpc.SetACLRequest) |
| 108 | + |
| 109 | + resp, err := api.SetACL(setRequest, scw.WithContext(ctx)) |
| 110 | + if err != nil { |
| 111 | + return nil, fmt.Errorf("failed to set ACL rules: %w", err) |
| 112 | + } |
| 113 | + |
| 114 | + return resp.Rules, nil |
| 115 | + }, |
| 116 | + } |
| 117 | +} |
0 commit comments