-
Notifications
You must be signed in to change notification settings - Fork 14
/
resource_l7rule.go
426 lines (357 loc) · 13.4 KB
/
resource_l7rule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package lb
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vk-cs/terraform-provider-vkcs/vkcs/internal/clients"
"github.com/vk-cs/terraform-provider-vkcs/vkcs/internal/util"
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/l7policies"
il7policies "github.com/vk-cs/terraform-provider-vkcs/vkcs/internal/services/lb/v2/l7policies"
"github.com/vk-cs/terraform-provider-vkcs/vkcs/internal/services/lb/v2/listeners"
)
func ResourceL7Rule() *schema.Resource {
return &schema.Resource{
CreateContext: resourceL7RuleCreate,
ReadContext: resourceL7RuleRead,
UpdateContext: resourceL7RuleUpdate,
DeleteContext: resourceL7RuleDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceL7RuleImport,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: "The region in which to obtain the Loadbalancer client. If omitted, the `region` argument of the provider is used. Changing this creates a new L7 Rule.",
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"COOKIE", "FILE_TYPE", "HEADER", "HOST_NAME", "PATH",
}, true),
Description: "The L7 Rule type - can either be COOKIE, FILE\\_TYPE, HEADER, HOST\\_NAME or PATH.",
},
"compare_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"CONTAINS", "STARTS_WITH", "ENDS_WITH", "EQUAL_TO", "REGEX",
}, true),
Description: "The comparison type for the L7 rule - can either be CONTAINS, STARTS\\_WITH, ENDS_WITH, EQUAL_TO or REGEX",
},
"l7policy_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The ID of the L7 Policy to query. Changing this creates a new L7 Rule.",
},
"listener_id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the Listener owning this resource.",
},
"value": {
Type: schema.TypeString,
Required: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
if len(v.(string)) == 0 {
errors = append(errors, fmt.Errorf("'value' field should not be empty"))
}
return
},
Description: "The value to use for the comparison. For example, the file type to compare.",
},
"key": {
Type: schema.TypeString,
Optional: true,
Description: "The key to use for the comparison. For example, the name of the cookie to evaluate. Valid when `type` is set to COOKIE or HEADER.",
},
"invert": {
Type: schema.TypeBool,
Default: false,
Optional: true,
Description: "When true the logic of the rule is inverted. For example, with invert true, equal to would become not equal to. Default is false.",
},
"admin_state_up": {
Type: schema.TypeBool,
Default: true,
Optional: true,
Description: "The administrative state of the L7 Rule. A valid value is true (UP) or false (DOWN).",
},
},
Description: "Manages a L7 Rule resource within VKCS.",
}
}
func resourceL7RuleCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(clients.Config)
lbClient, err := config.LoadBalancerV2Client(util.GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating VKCS loadbalancer client: %s", err)
}
// Assign some required variables for use in creation.
l7policyID := d.Get("l7policy_id").(string)
listenerID := ""
ruleType := d.Get("type").(string)
key := d.Get("key").(string)
compareType := d.Get("compare_type").(string)
adminStateUp := d.Get("admin_state_up").(bool)
// Ensure the right combination of options have been specified.
err = checkL7RuleType(ruleType, key)
if err != nil {
return diag.Errorf("Unable to create L7 Rule: %s", err)
}
createOpts := l7policies.CreateRuleOpts{
RuleType: l7policies.RuleType(ruleType),
CompareType: l7policies.CompareType(compareType),
Value: d.Get("value").(string),
Key: key,
Invert: d.Get("invert").(bool),
AdminStateUp: &adminStateUp,
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
timeout := d.Timeout(schema.TimeoutCreate)
// Get a clean copy of the parent L7 Policy.
parentL7Policy, err := il7policies.Get(lbClient, l7policyID).Extract()
if err != nil {
return diag.Errorf("Unable to get parent L7 Policy: %s", err)
}
if parentL7Policy.ListenerID != "" {
listenerID = parentL7Policy.ListenerID
} else {
// Fallback for the Neutron LBaaS extension
listenerID, err = getListenerIDForL7Policy(lbClient, l7policyID)
if err != nil {
return diag.FromErr(err)
}
}
// Get a clean copy of the parent listener.
parentListener, err := listeners.Get(lbClient, listenerID).Extract()
if err != nil {
return diag.Errorf("Unable to retrieve listener %s: %s", listenerID, err)
}
// Wait for parent L7 Policy to become active before continuing
err = waitForLBL7Policy(ctx, lbClient, parentListener, parentL7Policy, "ACTIVE", getLbPendingStatuses(), timeout)
if err != nil {
return diag.FromErr(err)
}
log.Printf("[DEBUG] Attempting to create L7 Rule")
var l7Rule *l7policies.Rule
err = retry.RetryContext(ctx, timeout, func() *retry.RetryError {
l7Rule, err = il7policies.CreateRule(lbClient, l7policyID, createOpts).Extract()
if err != nil {
return util.CheckForRetryableError(err)
}
return nil
})
if err != nil {
return diag.Errorf("Error creating L7 Rule: %s", err)
}
d.SetId(l7Rule.ID)
d.Set("listener_id", listenerID)
// Wait for L7 Rule to become active before continuing
err = waitForLBL7Rule(ctx, lbClient, parentListener, parentL7Policy, l7Rule, "ACTIVE", getLbPendingStatuses(), timeout)
if err != nil {
return diag.FromErr(err)
}
return resourceL7RuleRead(ctx, d, meta)
}
func resourceL7RuleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(clients.Config)
lbClient, err := config.LoadBalancerV2Client(util.GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating VKCS loadbalancer client: %s", err)
}
l7policyID := d.Get("l7policy_id").(string)
l7Rule, err := il7policies.GetRule(lbClient, l7policyID, d.Id()).Extract()
if err != nil {
return diag.FromErr(util.CheckDeleted(d, err, "L7 Rule"))
}
log.Printf("[DEBUG] Retrieved L7 Rule %s: %#v", d.Id(), l7Rule)
d.Set("l7policy_id", l7policyID)
d.Set("type", l7Rule.RuleType)
d.Set("compare_type", l7Rule.CompareType)
d.Set("value", l7Rule.Value)
d.Set("key", l7Rule.Key)
d.Set("invert", l7Rule.Invert)
d.Set("admin_state_up", l7Rule.AdminStateUp)
return nil
}
func resourceL7RuleUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(clients.Config)
lbClient, err := config.LoadBalancerV2Client(util.GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating VKCS networking client: %s", err)
}
// Assign some required variables for use in updating.
l7policyID := d.Get("l7policy_id").(string)
listenerID := d.Get("listener_id").(string)
ruleType := d.Get("type").(string)
key := d.Get("key").(string)
// Key should always be set
updateOpts := l7policies.UpdateRuleOpts{
Key: &key,
}
if d.HasChange("type") {
updateOpts.RuleType = l7policies.RuleType(ruleType)
}
if d.HasChange("compare_type") {
updateOpts.CompareType = l7policies.CompareType(d.Get("compare_type").(string))
}
if d.HasChange("value") {
updateOpts.Value = d.Get("value").(string)
}
if d.HasChange("invert") {
invert := d.Get("invert").(bool)
updateOpts.Invert = &invert
}
// Ensure the right combination of options have been specified.
err = checkL7RuleType(ruleType, key)
if err != nil {
return diag.FromErr(err)
}
timeout := d.Timeout(schema.TimeoutUpdate)
// Get a clean copy of the parent listener.
parentListener, err := listeners.Get(lbClient, listenerID).Extract()
if err != nil {
return diag.Errorf("Unable to retrieve listener %s: %s", listenerID, err)
}
// Get a clean copy of the parent L7 Policy.
parentL7Policy, err := il7policies.Get(lbClient, l7policyID).Extract()
if err != nil {
return diag.Errorf("Unable to get parent L7 Policy: %s", err)
}
// Get a clean copy of the L7 Rule.
l7Rule, err := il7policies.GetRule(lbClient, l7policyID, d.Id()).Extract()
if err != nil {
return diag.Errorf("Unable to get L7 Rule: %s", err)
}
// Wait for parent L7 Policy to become active before continuing
err = waitForLBL7Policy(ctx, lbClient, parentListener, parentL7Policy, "ACTIVE", getLbPendingStatuses(), timeout)
if err != nil {
return diag.FromErr(err)
}
// Wait for L7 Rule to become active before continuing
err = waitForLBL7Rule(ctx, lbClient, parentListener, parentL7Policy, l7Rule, "ACTIVE", getLbPendingStatuses(), timeout)
if err != nil {
return diag.FromErr(err)
}
log.Printf("[DEBUG] Updating L7 Rule %s with options: %#v", d.Id(), updateOpts)
err = retry.RetryContext(ctx, timeout, func() *retry.RetryError {
_, err := il7policies.UpdateRule(lbClient, l7policyID, d.Id(), updateOpts).Extract()
if err != nil {
return util.CheckForRetryableError(err)
}
return nil
})
if err != nil {
return diag.Errorf("Unable to update L7 Rule %s: %s", d.Id(), err)
}
// Wait for L7 Rule to become active before continuing
err = waitForLBL7Rule(ctx, lbClient, parentListener, parentL7Policy, l7Rule, "ACTIVE", getLbPendingStatuses(), timeout)
if err != nil {
return diag.FromErr(err)
}
return resourceL7RuleRead(ctx, d, meta)
}
func resourceL7RuleDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(clients.Config)
lbClient, err := config.LoadBalancerV2Client(util.GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating VKCS loadbalancer client: %s", err)
}
timeout := d.Timeout(schema.TimeoutDelete)
l7policyID := d.Get("l7policy_id").(string)
listenerID := d.Get("listener_id").(string)
// Get a clean copy of the parent listener.
parentListener, err := listeners.Get(lbClient, listenerID).Extract()
if err != nil {
return diag.Errorf("Unable to retrieve parent listener (%s) for the L7 Rule: %s", listenerID, err)
}
// Get a clean copy of the parent L7 Policy.
parentL7Policy, err := il7policies.Get(lbClient, l7policyID).Extract()
if err != nil {
return diag.Errorf("Unable to retrieve parent L7 Policy (%s) for the L7 Rule: %s", l7policyID, err)
}
// Get a clean copy of the L7 Rule.
l7Rule, err := il7policies.GetRule(lbClient, l7policyID, d.Id()).Extract()
if err != nil {
return diag.FromErr(util.CheckDeleted(d, err, "Unable to retrieve L7 Rule"))
}
// Wait for parent L7 Policy to become active before continuing
err = waitForLBL7Policy(ctx, lbClient, parentListener, parentL7Policy, "ACTIVE", getLbPendingStatuses(), timeout)
if err != nil {
return diag.FromErr(err)
}
log.Printf("[DEBUG] Attempting to delete L7 Rule %s", d.Id())
err = retry.RetryContext(ctx, timeout, func() *retry.RetryError {
err = il7policies.DeleteRule(lbClient, l7policyID, d.Id()).ExtractErr()
if err != nil {
return util.CheckForRetryableError(err)
}
return nil
})
if err != nil {
return diag.FromErr(util.CheckDeleted(d, err, "Error deleting L7 Rule"))
}
err = waitForLBL7Rule(ctx, lbClient, parentListener, parentL7Policy, l7Rule, "DELETED", getLbPendingDeleteStatuses(), timeout)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceL7RuleImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.SplitN(d.Id(), "/", 2)
if len(parts) != 2 {
err := fmt.Errorf("invalid format specified for L7 Rule. Format must be <policy id>/<rule id>")
return nil, err
}
config := meta.(clients.Config)
lbClient, err := config.LoadBalancerV2Client(util.GetRegion(d, config))
if err != nil {
return nil, fmt.Errorf("error creating VKCS loadbalancer client: %s", err)
}
listenerID := ""
l7policyID := parts[0]
l7ruleID := parts[1]
// Get a clean copy of the parent L7 Policy.
parentL7Policy, err := il7policies.Get(lbClient, l7policyID).Extract()
if err != nil {
return nil, fmt.Errorf("unable to get parent L7 Policy: %s", err)
}
if parentL7Policy.ListenerID != "" {
listenerID = parentL7Policy.ListenerID
} else {
// Fallback for the Neutron LBaaS extension
listenerID, err = getListenerIDForL7Policy(lbClient, l7policyID)
if err != nil {
return nil, err
}
}
d.SetId(l7ruleID)
d.Set("l7policy_id", l7policyID)
d.Set("listener_id", listenerID)
return []*schema.ResourceData{d}, nil
}
func checkL7RuleType(ruleType, key string) error {
keyRequired := []string{"COOKIE", "HEADER"}
if util.StrSliceContains(keyRequired, ruleType) && key == "" {
return fmt.Errorf("key attribute is required, when the L7 Rule type is %s", strings.Join(keyRequired, " or "))
} else if !util.StrSliceContains(keyRequired, ruleType) && key != "" {
return fmt.Errorf("key attribute must not be used, when the L7 Rule type is not %s", strings.Join(keyRequired, " or "))
}
return nil
}