|
| 1 | +package scaleway |
| 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/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 9 | + lbSDK "github.com/scaleway/scaleway-sdk-go/api/lb/v1" |
| 10 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 11 | +) |
| 12 | + |
| 13 | +func resourceScalewayLbACL() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + CreateContext: resourceScalewayLbACLCreate, |
| 16 | + ReadContext: resourceScalewayLbACLRead, |
| 17 | + UpdateContext: resourceScalewayLbACLUpdate, |
| 18 | + DeleteContext: resourceScalewayLbACLDelete, |
| 19 | + Importer: &schema.ResourceImporter{ |
| 20 | + StateContext: schema.ImportStatePassthroughContext, |
| 21 | + }, |
| 22 | + Timeouts: &schema.ResourceTimeout{ |
| 23 | + Default: schema.DefaultTimeout(defaultLbLbTimeout), |
| 24 | + }, |
| 25 | + SchemaVersion: 1, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "frontend_id": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + ForceNew: true, |
| 31 | + ValidateFunc: validationUUIDorUUIDWithLocality(), |
| 32 | + Description: "The frontend ID on which the ACL is applied", |
| 33 | + }, |
| 34 | + "name": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Optional: true, |
| 37 | + Computed: true, |
| 38 | + Description: "The ACL name", |
| 39 | + }, |
| 40 | + "description": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Optional: true, |
| 43 | + Description: "Description of the ACL", |
| 44 | + }, |
| 45 | + "index": { |
| 46 | + Type: schema.TypeInt, |
| 47 | + Description: "The priority of the ACL. (ACLs are applied in ascending order, 0 is the first ACL executed)", |
| 48 | + Required: true, |
| 49 | + ValidateFunc: validation.IntAtLeast(0), |
| 50 | + }, |
| 51 | + "action": { |
| 52 | + Type: schema.TypeList, |
| 53 | + Required: true, |
| 54 | + Description: "Action to undertake when an ACL filter matches", |
| 55 | + MaxItems: 1, |
| 56 | + MinItems: 1, |
| 57 | + Elem: &schema.Resource{ |
| 58 | + Schema: map[string]*schema.Schema{ |
| 59 | + "type": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Required: true, |
| 62 | + ValidateFunc: validation.StringInSlice([]string{ |
| 63 | + lbSDK.ACLActionTypeAllow.String(), |
| 64 | + lbSDK.ACLActionTypeDeny.String(), |
| 65 | + lbSDK.ACLActionTypeRedirect.String(), |
| 66 | + }, false), |
| 67 | + Description: "The action type", |
| 68 | + }, |
| 69 | + "redirect": { |
| 70 | + Type: schema.TypeList, |
| 71 | + Optional: true, |
| 72 | + Description: "Redirect parameters when using an ACL with `redirect` action", |
| 73 | + Elem: &schema.Resource{ |
| 74 | + Schema: map[string]*schema.Schema{ |
| 75 | + "type": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Optional: true, |
| 78 | + ValidateFunc: validation.StringInSlice([]string{ |
| 79 | + lbSDK.ACLActionRedirectRedirectTypeLocation.String(), |
| 80 | + lbSDK.ACLActionRedirectRedirectTypeScheme.String(), |
| 81 | + }, false), |
| 82 | + Description: "The redirect type", |
| 83 | + }, |
| 84 | + "target": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Optional: true, |
| 87 | + Description: "An URL can be used in case of a location redirect ", |
| 88 | + }, |
| 89 | + "code": { |
| 90 | + Type: schema.TypeInt, |
| 91 | + Optional: true, |
| 92 | + Description: "The HTTP redirect code to use", |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + "match": { |
| 101 | + Type: schema.TypeList, |
| 102 | + Optional: true, |
| 103 | + MaxItems: 1, |
| 104 | + MinItems: 1, |
| 105 | + Description: "The ACL match rule", |
| 106 | + Elem: &schema.Resource{ |
| 107 | + Schema: map[string]*schema.Schema{ |
| 108 | + "ip_subnet": { |
| 109 | + Type: schema.TypeList, |
| 110 | + Elem: &schema.Schema{ |
| 111 | + Type: schema.TypeString, |
| 112 | + }, |
| 113 | + Optional: true, |
| 114 | + Description: "A list of IPs or CIDR v4/v6 addresses of the client of the session to match", |
| 115 | + }, |
| 116 | + "http_filter": { |
| 117 | + Type: schema.TypeString, |
| 118 | + Optional: true, |
| 119 | + Default: lbSDK.ACLHTTPFilterACLHTTPFilterNone.String(), |
| 120 | + ValidateFunc: validation.StringInSlice([]string{ |
| 121 | + lbSDK.ACLHTTPFilterACLHTTPFilterNone.String(), |
| 122 | + lbSDK.ACLHTTPFilterPathBegin.String(), |
| 123 | + lbSDK.ACLHTTPFilterPathEnd.String(), |
| 124 | + lbSDK.ACLHTTPFilterRegex.String(), |
| 125 | + lbSDK.ACLHTTPFilterHTTPHeaderMatch.String(), |
| 126 | + }, false), |
| 127 | + Description: "The HTTP filter to match", |
| 128 | + }, |
| 129 | + "http_filter_value": { |
| 130 | + Type: schema.TypeList, |
| 131 | + Optional: true, |
| 132 | + Description: "A list of possible values to match for the given HTTP filter", |
| 133 | + Elem: &schema.Schema{ |
| 134 | + Type: schema.TypeString, |
| 135 | + }, |
| 136 | + }, |
| 137 | + "http_filter_option": { |
| 138 | + Type: schema.TypeString, |
| 139 | + Optional: true, |
| 140 | + Description: "You can use this field with http_header_match acl type to set the header name to filter", |
| 141 | + }, |
| 142 | + "invert": { |
| 143 | + Type: schema.TypeBool, |
| 144 | + Optional: true, |
| 145 | + Description: `If set to true, the condition will be of type "unless"`, |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + "created_at": { |
| 151 | + Type: schema.TypeString, |
| 152 | + Computed: true, |
| 153 | + Description: "Date and time of ACL's creation (RFC 3339 format)", |
| 154 | + }, |
| 155 | + "updated_at": { |
| 156 | + Type: schema.TypeString, |
| 157 | + Computed: true, |
| 158 | + Description: "Date and time of ACL's update (RFC 3339 format)", |
| 159 | + }, |
| 160 | + }, |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +func resourceScalewayLbACLCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 165 | + lbAPI, _, err := lbAPIWithZone(d, meta) |
| 166 | + if err != nil { |
| 167 | + return diag.FromErr(err) |
| 168 | + } |
| 169 | + |
| 170 | + frontZone, frontID, err := parseZonedID(d.Get("frontend_id").(string)) |
| 171 | + if err != nil { |
| 172 | + return diag.FromErr(err) |
| 173 | + } |
| 174 | + |
| 175 | + req := &lbSDK.ZonedAPICreateACLRequest{ |
| 176 | + Zone: frontZone, |
| 177 | + FrontendID: frontID, |
| 178 | + Name: d.Get("name").(string), |
| 179 | + Action: expandLbACLAction(d.Get("action")), |
| 180 | + Match: expandLbACLMatch(d.Get("match")), |
| 181 | + Index: int32(d.Get("index").(int)), |
| 182 | + Description: d.Get("description").(string), |
| 183 | + } |
| 184 | + |
| 185 | + res, err := lbAPI.CreateACL(req, scw.WithContext(ctx)) |
| 186 | + if err != nil { |
| 187 | + return diag.FromErr(err) |
| 188 | + } |
| 189 | + |
| 190 | + d.SetId(newZonedIDString(frontZone, res.ID)) |
| 191 | + |
| 192 | + return resourceScalewayLbACLRead(ctx, d, meta) |
| 193 | +} |
| 194 | + |
| 195 | +func resourceScalewayLbACLRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 196 | + lbAPI, zone, ID, err := lbAPIWithZoneAndID(meta, d.Id()) |
| 197 | + if err != nil { |
| 198 | + return diag.FromErr(err) |
| 199 | + } |
| 200 | + |
| 201 | + acl, err := lbAPI.GetACL(&lbSDK.ZonedAPIGetACLRequest{ |
| 202 | + Zone: zone, |
| 203 | + ACLID: ID, |
| 204 | + }, scw.WithContext(ctx)) |
| 205 | + if err != nil { |
| 206 | + if is404Error(err) { |
| 207 | + d.SetId("") |
| 208 | + return nil |
| 209 | + } |
| 210 | + return diag.FromErr(err) |
| 211 | + } |
| 212 | + |
| 213 | + _ = d.Set("frontend_id", newZonedIDString(zone, acl.Frontend.ID)) |
| 214 | + _ = d.Set("name", acl.Name) |
| 215 | + _ = d.Set("description", acl.Description) |
| 216 | + _ = d.Set("index", int(acl.Index)) |
| 217 | + _ = d.Set("created_at", flattenTime(acl.CreatedAt)) |
| 218 | + _ = d.Set("updated_at", flattenTime(acl.UpdatedAt)) |
| 219 | + _ = d.Set("action", flattenLbACLAction(acl.Action)) |
| 220 | + |
| 221 | + if acl.Match != nil { |
| 222 | + _ = d.Set("match", flattenLbACLMatch(acl.Match)) |
| 223 | + } |
| 224 | + |
| 225 | + return nil |
| 226 | +} |
| 227 | + |
| 228 | +func resourceScalewayLbACLUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 229 | + lbAPI, zone, ID, err := lbAPIWithZoneAndID(meta, d.Id()) |
| 230 | + if err != nil { |
| 231 | + return diag.FromErr(err) |
| 232 | + } |
| 233 | + |
| 234 | + req := &lbSDK.ZonedAPIUpdateACLRequest{ |
| 235 | + Zone: zone, |
| 236 | + ACLID: ID, |
| 237 | + Name: d.Get("name").(string), |
| 238 | + Action: expandLbACLAction(d.Get("action")), |
| 239 | + Index: int32(d.Get("index").(int)), |
| 240 | + Match: expandLbACLMatch(d.Get("match")), |
| 241 | + Description: expandUpdatedStringPtr(d.Get("description")), |
| 242 | + } |
| 243 | + |
| 244 | + _, err = lbAPI.UpdateACL(req, scw.WithContext(ctx)) |
| 245 | + if err != nil { |
| 246 | + return diag.FromErr(err) |
| 247 | + } |
| 248 | + |
| 249 | + return resourceScalewayLbACLRead(ctx, d, meta) |
| 250 | +} |
| 251 | + |
| 252 | +func resourceScalewayLbACLDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 253 | + lbAPI, zone, ID, err := lbAPIWithZoneAndID(meta, d.Id()) |
| 254 | + if err != nil { |
| 255 | + return diag.FromErr(err) |
| 256 | + } |
| 257 | + |
| 258 | + err = lbAPI.DeleteACL(&lbSDK.ZonedAPIDeleteACLRequest{ |
| 259 | + Zone: zone, |
| 260 | + ACLID: ID, |
| 261 | + }, scw.WithContext(ctx)) |
| 262 | + if err != nil { |
| 263 | + return diag.FromErr(err) |
| 264 | + } |
| 265 | + |
| 266 | + return nil |
| 267 | +} |
0 commit comments