forked from huaweicloud/terraform-provider-huaweicloud
/
resource_huaweicloud_lb_l7policy_v2.go
404 lines (335 loc) · 11.4 KB
/
resource_huaweicloud_lb_l7policy_v2.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
package huaweicloud
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/lbaas_v2/l7policies"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/lbaas_v2/listeners"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/lbaas_v2/pools"
)
func resourceL7PolicyV2() *schema.Resource {
return &schema.Resource{
Create: resourceL7PolicyV2Create,
Read: resourceL7PolicyV2Read,
Update: resourceL7PolicyV2Update,
Delete: resourceL7PolicyV2Delete,
Importer: &schema.ResourceImporter{
State: resourceL7PolicyV2Import,
},
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,
},
"tenant_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"action": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"REDIRECT_TO_POOL", "REDIRECT_TO_LISTENER",
}, true),
},
"listener_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"position": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Computed: true,
},
"redirect_pool_id": {
Type: schema.TypeString,
ConflictsWith: []string{"redirect_listener_id"},
Optional: true,
},
"redirect_listener_id": {
Type: schema.TypeString,
ConflictsWith: []string{"redirect_pool_id"},
Optional: true,
},
"admin_state_up": {
Type: schema.TypeBool,
Default: true,
Optional: true,
ValidateFunc: validateTrueOnly,
},
},
}
}
func resourceL7PolicyV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
lbClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
}
// Assign some required variables for use in creation.
listenerID := d.Get("listener_id").(string)
action := d.Get("action").(string)
redirectPoolID := d.Get("redirect_pool_id").(string)
redirectListenerID := d.Get("redirect_listener_id").(string)
// Ensure the right combination of options have been specified.
err = checkL7PolicyAction(action, redirectListenerID, redirectPoolID)
if err != nil {
return fmt.Errorf("Unable to create L7 Policy: %s", err)
}
adminStateUp := d.Get("admin_state_up").(bool)
createOpts := l7policies.CreateOpts{
TenantID: d.Get("tenant_id").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Action: l7policies.Action(action),
ListenerID: listenerID,
RedirectPoolID: redirectPoolID,
RedirectListenerID: redirectListenerID,
AdminStateUp: &adminStateUp,
}
if v, ok := d.GetOk("position"); ok {
createOpts.Position = int32(v.(int))
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
timeout := d.Timeout(schema.TimeoutCreate)
// Make sure the associated pool is active before proceeding.
if redirectPoolID != "" {
pool, err := pools.Get(lbClient, redirectPoolID).Extract()
if err != nil {
return fmt.Errorf("Unable to retrieve %s: %s", redirectPoolID, err)
}
err = waitForLBV2Pool(lbClient, pool.ID, "ACTIVE", lbPendingStatuses, timeout)
if err != nil {
return err
}
}
// Get a clean copy of the parent listener.
parentListener, err := listeners.Get(lbClient, listenerID).Extract()
if err != nil {
return fmt.Errorf("Unable to retrieve listener %s: %s", listenerID, err)
}
// Wait for parent Listener to become active before continuing.
err = waitForLBV2Listener(lbClient, parentListener.ID, "ACTIVE", lbPendingStatuses, timeout)
if err != nil {
return err
}
log.Printf("[DEBUG] Attempting to create L7 Policy")
var l7Policy *l7policies.L7Policy
err = resource.Retry(timeout, func() *resource.RetryError {
l7Policy, err = l7policies.Create(lbClient, createOpts).Extract()
if err != nil {
return checkForRetryableError(err)
}
return nil
})
if err != nil {
return fmt.Errorf("Error creating L7 Policy: %s", err)
}
// Wait for L7 Policy to become active before continuing
err = waitForLBV2L7Policy(lbClient, parentListener, l7Policy, "ACTIVE", lbPendingStatuses, timeout)
if err != nil {
return err
}
d.SetId(l7Policy.ID)
return resourceL7PolicyV2Read(d, meta)
}
func resourceL7PolicyV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
lbClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
}
l7Policy, err := l7policies.Get(lbClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "L7 Policy")
}
log.Printf("[DEBUG] Retrieved L7 Policy %s: %#v", d.Id(), l7Policy)
d.Set("action", l7Policy.Action)
d.Set("description", l7Policy.Description)
d.Set("tenant_id", l7Policy.TenantID)
d.Set("name", l7Policy.Name)
d.Set("position", int(l7Policy.Position))
d.Set("redirect_listener_id", l7Policy.RedirectListenerID)
d.Set("redirect_pool_id", l7Policy.RedirectPoolID)
d.Set("region", GetRegion(d, config))
d.Set("admin_state_up", l7Policy.AdminStateUp)
return nil
}
func resourceL7PolicyV2Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
lbClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
}
// Assign some required variables for use in updating.
listenerID := d.Get("listener_id").(string)
action := d.Get("action").(string)
redirectPoolID := d.Get("redirect_pool_id").(string)
redirectListenerID := d.Get("redirect_listener_id").(string)
var updateOpts l7policies.UpdateOpts
if d.HasChange("name") {
name := d.Get("name").(string)
updateOpts.Name = &name
}
if d.HasChange("description") {
description := d.Get("description").(string)
updateOpts.Description = &description
}
if d.HasChange("redirect_pool_id") {
redirectPoolID = d.Get("redirect_pool_id").(string)
updateOpts.RedirectPoolID = &redirectPoolID
}
if d.HasChange("admin_state_up") {
adminStateUp := d.Get("admin_state_up").(bool)
updateOpts.AdminStateUp = &adminStateUp
}
// Ensure the right combination of options have been specified.
err = checkL7PolicyAction(action, redirectListenerID, redirectPoolID)
if err != nil {
return err
}
// Make sure the pool is active before continuing.
timeout := d.Timeout(schema.TimeoutUpdate)
if redirectPoolID != "" {
pool, err := pools.Get(lbClient, redirectPoolID).Extract()
if err != nil {
return fmt.Errorf("Unable to retrieve %s: %s", redirectPoolID, err)
}
err = waitForLBV2Pool(lbClient, pool.ID, "ACTIVE", lbPendingStatuses, timeout)
if err != nil {
return err
}
}
// Get a clean copy of the parent listener.
parentListener, err := listeners.Get(lbClient, listenerID).Extract()
if err != nil {
return fmt.Errorf("Unable to retrieve parent listener %s: %s", listenerID, err)
}
// Get a clean copy of the L7 Policy.
l7Policy, err := l7policies.Get(lbClient, d.Id()).Extract()
if err != nil {
return fmt.Errorf("Unable to retrieve L7 Policy: %s: %s", d.Id(), err)
}
// Wait for parent Listener to become active before continuing.
err = waitForLBV2Listener(lbClient, parentListener.ID, "ACTIVE", lbPendingStatuses, timeout)
if err != nil {
return err
}
// Wait for L7 Policy to become active before continuing
err = waitForLBV2L7Policy(lbClient, parentListener, l7Policy, "ACTIVE", lbPendingStatuses, timeout)
if err != nil {
return err
}
log.Printf("[DEBUG] Updating L7 Policy %s with options: %#v", d.Id(), updateOpts)
err = resource.Retry(timeout, func() *resource.RetryError {
_, err = l7policies.Update(lbClient, d.Id(), updateOpts).Extract()
if err != nil {
return checkForRetryableError(err)
}
return nil
})
if err != nil {
return fmt.Errorf("Unable to update L7 Policy %s: %s", d.Id(), err)
}
// Wait for L7 Policy to become active before continuing
err = waitForLBV2L7Policy(lbClient, parentListener, l7Policy, "ACTIVE", lbPendingStatuses, timeout)
if err != nil {
return err
}
return resourceL7PolicyV2Read(d, meta)
}
func resourceL7PolicyV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
lbClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
}
timeout := d.Timeout(schema.TimeoutDelete)
listenerID := d.Get("listener_id").(string)
// Get a clean copy of the listener.
listener, err := listeners.Get(lbClient, listenerID).Extract()
if err != nil {
return fmt.Errorf("Unable to retrieve parent listener (%s) for the L7 Policy: %s", listenerID, err)
}
// Get a clean copy of the L7 Policy.
l7Policy, err := l7policies.Get(lbClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "Unable to retrieve L7 Policy")
}
// Wait for Listener to become active before continuing.
err = waitForLBV2Listener(lbClient, listener.ID, "ACTIVE", lbPendingStatuses, timeout)
if err != nil {
return err
}
log.Printf("[DEBUG] Attempting to delete L7 Policy %s", d.Id())
err = resource.Retry(timeout, func() *resource.RetryError {
err = l7policies.Delete(lbClient, d.Id()).ExtractErr()
if err != nil {
return checkForRetryableError(err)
}
return nil
})
if err != nil {
return CheckDeleted(d, err, "Error deleting L7 Policy")
}
err = waitForLBV2L7Policy(lbClient, listener, l7Policy, "DELETED", lbPendingDeleteStatuses, timeout)
if err != nil {
return err
}
return nil
}
func resourceL7PolicyV2Import(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
lbClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return nil, fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
}
l7Policy, err := l7policies.Get(lbClient, d.Id()).Extract()
if err != nil {
return nil, CheckDeleted(d, err, "L7 Policy")
}
log.Printf("[DEBUG] Retrieved L7 Policy %s during the import: %#v", d.Id(), l7Policy)
if l7Policy.ListenerID != "" {
d.Set("listener_id", l7Policy.ListenerID)
} else {
// Fallback for the Neutron LBaaSv2 extension
listenerID, err := getListenerIDForL7Policy(lbClient, d.Id())
if err != nil {
return nil, err
}
d.Set("listener_id", listenerID)
}
return []*schema.ResourceData{d}, nil
}
func checkL7PolicyAction(action, redirectListenerID, redirectPoolID string) error {
if action == "REDIRECT_TO_POOL" && redirectListenerID != "" {
return fmt.Errorf("redirect_listener_id must be empty when action is set to %s", action)
}
if action == "REDIRECT_TO_LISTENER" && redirectPoolID != "" {
return fmt.Errorf("redirect_pool_id must be empty when action is set to %s", action)
}
return nil
}