|
| 1 | +package vpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + "github.com/scaleway/scaleway-sdk-go/api/vpc/v2" |
| 9 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 10 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors" |
| 11 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality" |
| 12 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional" |
| 13 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/verify" |
| 14 | +) |
| 15 | + |
| 16 | +func ResourceACL() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + CreateContext: ResourceVPCACLCreate, |
| 19 | + ReadContext: ResourceVPCACLRead, |
| 20 | + UpdateContext: ResourceVPCACLUpdate, |
| 21 | + DeleteContext: ResourceVPCACLDelete, |
| 22 | + Importer: &schema.ResourceImporter{ |
| 23 | + StateContext: schema.ImportStatePassthroughContext, |
| 24 | + }, |
| 25 | + SchemaVersion: 0, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "vpc_id": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + Description: "The VPC in which to create the ACL rule", |
| 31 | + }, |
| 32 | + "default_policy": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Required: true, |
| 35 | + Description: "The action to take for packets which do not match any rules", |
| 36 | + ValidateDiagFunc: verify.ValidateEnum[vpc.Action](), |
| 37 | + }, |
| 38 | + "is_ipv6": { |
| 39 | + Type: schema.TypeBool, |
| 40 | + Optional: true, |
| 41 | + Default: false, |
| 42 | + Description: "Defines whether this set of ACL rules is for IPv6 (false = IPv4). Each Network ACL can have rules for only one IP type", |
| 43 | + }, |
| 44 | + "rules": { |
| 45 | + Type: schema.TypeList, |
| 46 | + Required: true, |
| 47 | + Description: "The list of Network ACL rules", |
| 48 | + Elem: &schema.Resource{ |
| 49 | + Schema: map[string]*schema.Schema{ |
| 50 | + "protocol": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Optional: true, |
| 53 | + Default: "ANY", |
| 54 | + Description: "The protocol to which this rule applies. Default value: ANY", |
| 55 | + ValidateDiagFunc: verify.ValidateEnum[vpc.ACLRuleProtocol](), |
| 56 | + }, |
| 57 | + "source": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Optional: true, |
| 60 | + Description: "Source IP range to which this rule applies (CIDR notation with subnet mask)", |
| 61 | + }, |
| 62 | + "src_port_low": { |
| 63 | + Type: schema.TypeInt, |
| 64 | + Optional: true, |
| 65 | + Description: "Starting port of the source port range to which this rule applies (inclusive)", |
| 66 | + }, |
| 67 | + "src_port_high": { |
| 68 | + Type: schema.TypeInt, |
| 69 | + Optional: true, |
| 70 | + Description: "Ending port of the source port range to which this rule applies (inclusive)", |
| 71 | + }, |
| 72 | + "destination": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Optional: true, |
| 75 | + Description: "Destination IP range to which this rule applies (CIDR notation with subnet mask)", |
| 76 | + }, |
| 77 | + "dst_port_low": { |
| 78 | + Type: schema.TypeInt, |
| 79 | + Optional: true, |
| 80 | + Description: "Starting port of the destination port range to which this rule applies (inclusive)", |
| 81 | + }, |
| 82 | + "dst_port_high": { |
| 83 | + Type: schema.TypeInt, |
| 84 | + Optional: true, |
| 85 | + Description: "Ending port of the destination port range to which this rule applies (inclusive)", |
| 86 | + }, |
| 87 | + "action": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Optional: true, |
| 90 | + Description: "The policy to apply to the packet", |
| 91 | + ValidateDiagFunc: verify.ValidateEnum[vpc.Action](), |
| 92 | + }, |
| 93 | + "description": { |
| 94 | + Type: schema.TypeString, |
| 95 | + Optional: true, |
| 96 | + Description: "The rule description", |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + "region": regional.Schema(), |
| 102 | + }, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func ResourceVPCACLCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 107 | + vpcAPI, region, err := vpcAPIWithRegion(d, m) |
| 108 | + if err != nil { |
| 109 | + return diag.FromErr(err) |
| 110 | + } |
| 111 | + |
| 112 | + req := &vpc.SetACLRequest{ |
| 113 | + VpcID: locality.ExpandID(d.Get("vpc_id").(string)), |
| 114 | + IsIPv6: d.Get("is_ipv6").(bool), |
| 115 | + DefaultPolicy: vpc.Action(d.Get("default_policy").(string)), |
| 116 | + Region: region, |
| 117 | + } |
| 118 | + |
| 119 | + expandedRules, err := expandACLRules(d.Get("rules")) |
| 120 | + if err != nil { |
| 121 | + return diag.FromErr(err) |
| 122 | + } |
| 123 | + |
| 124 | + if d.Get("rules") != nil { |
| 125 | + req.Rules = expandedRules |
| 126 | + } |
| 127 | + |
| 128 | + _, err = vpcAPI.SetACL(req, scw.WithContext(ctx)) |
| 129 | + if err != nil { |
| 130 | + return diag.FromErr(err) |
| 131 | + } |
| 132 | + |
| 133 | + d.SetId(regional.NewIDString(region, regional.ExpandID(d.Get("vpc_id").(string)).ID)) |
| 134 | + |
| 135 | + return ResourceVPCACLRead(ctx, d, m) |
| 136 | +} |
| 137 | + |
| 138 | +func ResourceVPCACLRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 139 | + vpcAPI, region, ID, err := NewAPIWithRegionAndID(m, d.Id()) |
| 140 | + if err != nil { |
| 141 | + return diag.FromErr(err) |
| 142 | + } |
| 143 | + |
| 144 | + acl, err := vpcAPI.GetACL(&vpc.GetACLRequest{ |
| 145 | + VpcID: locality.ExpandID(ID), |
| 146 | + Region: region, |
| 147 | + IsIPv6: d.Get("is_ipv6").(bool), |
| 148 | + }, scw.WithContext(ctx)) |
| 149 | + if err != nil { |
| 150 | + if httperrors.Is404(err) { |
| 151 | + d.SetId("") |
| 152 | + |
| 153 | + return nil |
| 154 | + } |
| 155 | + |
| 156 | + return diag.FromErr(err) |
| 157 | + } |
| 158 | + |
| 159 | + _ = d.Set("rules", flattenACLRules(acl.Rules)) |
| 160 | + _ = d.Set("default_policy", acl.DefaultPolicy.String()) |
| 161 | + |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +func ResourceVPCACLUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 166 | + vpcAPI, region, ID, err := NewAPIWithRegionAndID(m, d.Id()) |
| 167 | + if err != nil { |
| 168 | + return diag.FromErr(err) |
| 169 | + } |
| 170 | + |
| 171 | + req := &vpc.SetACLRequest{ |
| 172 | + VpcID: locality.ExpandID(ID), |
| 173 | + IsIPv6: d.Get("is_ipv6").(bool), |
| 174 | + DefaultPolicy: vpc.Action(d.Get("default_policy").(string)), |
| 175 | + Region: region, |
| 176 | + } |
| 177 | + |
| 178 | + expandedRules, err := expandACLRules(d.Get("rules")) |
| 179 | + if err != nil { |
| 180 | + return diag.FromErr(err) |
| 181 | + } |
| 182 | + |
| 183 | + if d.Get("rules") != nil { |
| 184 | + req.Rules = expandedRules |
| 185 | + } |
| 186 | + |
| 187 | + _, err = vpcAPI.SetACL(req, scw.WithContext(ctx)) |
| 188 | + if err != nil { |
| 189 | + return diag.FromErr(err) |
| 190 | + } |
| 191 | + |
| 192 | + return ResourceVPCACLRead(ctx, d, m) |
| 193 | +} |
| 194 | + |
| 195 | +func ResourceVPCACLDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 196 | + vpcAPI, region, ID, err := NewAPIWithRegionAndID(m, d.Id()) |
| 197 | + if err != nil { |
| 198 | + return diag.FromErr(err) |
| 199 | + } |
| 200 | + |
| 201 | + _, err = vpcAPI.SetACL(&vpc.SetACLRequest{ |
| 202 | + VpcID: locality.ExpandID(ID), |
| 203 | + Region: region, |
| 204 | + DefaultPolicy: "drop", |
| 205 | + }, scw.WithContext(ctx)) |
| 206 | + if err != nil { |
| 207 | + return diag.FromErr(err) |
| 208 | + } |
| 209 | + |
| 210 | + return nil |
| 211 | +} |
0 commit comments