-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_viettelidc_lb_loadbalancer_v2.go
371 lines (318 loc) · 10.8 KB
/
resource_viettelidc_lb_loadbalancer_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
package viettelidc
import (
"context"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
octavialoadbalancers "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/loadbalancers"
neutronloadbalancers "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/loadbalancers"
)
func resourceLoadBalancerV2() *schema.Resource {
return &schema.Resource{
CreateContext: resourceLoadBalancerV2Create,
ReadContext: resourceLoadBalancerV2Read,
UpdateContext: resourceLoadBalancerV2Update,
DeleteContext: resourceLoadBalancerV2Delete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"vip_network_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"vip_subnet_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"vip_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"vip_port_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"admin_state_up": {
Type: schema.TypeBool,
Default: true,
Optional: true,
},
"flavor_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"loadbalancer_provider": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"availability_zone": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"security_group_ids": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"tags": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}
func resourceLoadBalancerV2Create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
lbClient, err := chooseLBV2Client(d, config)
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
var (
lbID string
vipPortID string
lbProvider string
)
if v, ok := d.GetOk("loadbalancer_provider"); ok {
lbProvider = v.(string)
}
adminStateUp := d.Get("admin_state_up").(bool)
if lbClient.Type == octaviaLBClientType {
createOpts := octavialoadbalancers.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
VipNetworkID: d.Get("vip_network_id").(string),
VipSubnetID: d.Get("vip_subnet_id").(string),
VipPortID: d.Get("vip_port_id").(string),
ProjectID: d.Get("tenant_id").(string),
VipAddress: d.Get("vip_address").(string),
AdminStateUp: &adminStateUp,
FlavorID: d.Get("flavor_id").(string),
Provider: lbProvider,
}
// availability_zone requires octavia minor version 2.14. Only set when specified.
if v, ok := d.GetOk("availability_zone"); ok {
aZ := v.(string)
createOpts.AvailabilityZone = aZ
}
if v, ok := d.GetOk("tags"); ok {
tags := v.(*schema.Set).List()
createOpts.Tags = expandToStringSlice(tags)
}
log.Printf("[DEBUG][Octavia] openstack_lb_loadbalancer_v2 create options: %#v", createOpts)
lb, err := octavialoadbalancers.Create(lbClient, createOpts).Extract()
if err != nil {
return diag.Errorf("Error creating openstack_lb_loadbalancer_v2: %s", err)
}
lbID = lb.ID
vipPortID = lb.VipPortID
} else {
createOpts := neutronloadbalancers.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
VipSubnetID: d.Get("vip_subnet_id").(string),
TenantID: d.Get("tenant_id").(string),
VipAddress: d.Get("vip_address").(string),
AdminStateUp: &adminStateUp,
FlavorID: d.Get("flavor_id").(string),
Provider: lbProvider,
}
log.Printf("[DEBUG][Neutron] openstack_lb_loadbalancer_v2 create options: %#v", createOpts)
lb, err := neutronloadbalancers.Create(lbClient, createOpts).Extract()
if err != nil {
return diag.Errorf("Error creating openstack_lb_loadbalancer_v2: %s", err)
}
lbID = lb.ID
vipPortID = lb.VipPortID
}
// Wait for load-balancer to become active before continuing.
timeout := d.Timeout(schema.TimeoutCreate)
err = waitForLBV2LoadBalancer(ctx, lbClient, lbID, "ACTIVE", getLbPendingStatuses(), timeout)
if err != nil {
return diag.FromErr(err)
}
// Once the load-balancer has been created, apply any requested security groups
// to the port that was created behind the scenes.
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
if err := resourceLoadBalancerV2SetSecurityGroups(networkingClient, vipPortID, d); err != nil {
return diag.Errorf("Error setting openstack_lb_loadbalancer_v2 security groups: %s", err)
}
d.SetId(lbID)
return resourceLoadBalancerV2Read(ctx, d, meta)
}
func resourceLoadBalancerV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
lbClient, err := chooseLBV2Client(d, config)
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
var vipPortID string
if lbClient.Type == octaviaLBClientType {
lb, err := octavialoadbalancers.Get(lbClient, d.Id()).Extract()
if err != nil {
return diag.FromErr(CheckDeleted(d, err, "Unable to retrieve openstack_lb_loadbalancer_v2"))
}
log.Printf("[DEBUG][Octavia] Retrieved openstack_lb_loadbalancer_v2 %s: %#v", d.Id(), lb)
d.Set("name", lb.Name)
d.Set("description", lb.Description)
d.Set("vip_subnet_id", lb.VipSubnetID)
d.Set("vip_network_id", lb.VipNetworkID)
d.Set("tenant_id", lb.ProjectID)
d.Set("vip_address", lb.VipAddress)
d.Set("vip_port_id", lb.VipPortID)
d.Set("admin_state_up", lb.AdminStateUp)
d.Set("flavor_id", lb.FlavorID)
d.Set("loadbalancer_provider", lb.Provider)
d.Set("availability_zone", lb.AvailabilityZone)
d.Set("region", GetRegion(d, config))
d.Set("tags", lb.Tags)
vipPortID = lb.VipPortID
} else {
lb, err := neutronloadbalancers.Get(lbClient, d.Id()).Extract()
if err != nil {
return diag.FromErr(CheckDeleted(d, err, "Unable to retrieve openstack_lb_loadbalancer_v2"))
}
log.Printf("[DEBUG][Neutron] Retrieved openstack_lb_loadbalancer_v2 %s: %#v", d.Id(), lb)
d.Set("name", lb.Name)
d.Set("description", lb.Description)
d.Set("vip_subnet_id", lb.VipSubnetID)
d.Set("tenant_id", lb.TenantID)
d.Set("vip_address", lb.VipAddress)
d.Set("vip_port_id", lb.VipPortID)
d.Set("admin_state_up", lb.AdminStateUp)
d.Set("flavor_id", lb.FlavorID)
d.Set("loadbalancer_provider", lb.Provider)
d.Set("region", GetRegion(d, config))
vipPortID = lb.VipPortID
}
// Get any security groups on the VIP Port.
if vipPortID != "" {
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
if err := resourceLoadBalancerV2GetSecurityGroups(networkingClient, vipPortID, d); err != nil {
return diag.Errorf("Error getting port security groups for openstack_lb_loadbalancer_v2: %s", err)
}
}
return nil
}
func resourceLoadBalancerV2Update(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
lbClient, err := chooseLBV2Client(d, config)
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
updateOpts, err := chooseLBV2LoadbalancerUpdateOpts(d, config)
if err != nil {
return diag.Errorf("Error building openstack_lb_loadbalancer_v2 update options: %s", err)
}
if updateOpts != nil {
// Wait for load-balancer to become active before continuing.
timeout := d.Timeout(schema.TimeoutUpdate)
err = waitForLBV2LoadBalancer(ctx, lbClient, d.Id(), "ACTIVE", getLbPendingStatuses(), timeout)
if err != nil {
return diag.FromErr(err)
}
log.Printf("[DEBUG] Updating openstack_lb_loadbalancer_v2 %s with options: %#v", d.Id(), updateOpts)
err = resource.Retry(timeout, func() *resource.RetryError {
_, err = neutronloadbalancers.Update(lbClient, d.Id(), updateOpts).Extract()
if err != nil {
return checkForRetryableError(err)
}
return nil
})
if err != nil {
return diag.Errorf("Error updating openstack_lb_loadbalancer_v2 %s: %s", d.Id(), err)
}
// Wait for load-balancer to become active before continuing.
err = waitForLBV2LoadBalancer(ctx, lbClient, d.Id(), "ACTIVE", getLbPendingStatuses(), timeout)
if err != nil {
return diag.FromErr(err)
}
}
// Security Groups get updated separately.
if d.HasChange("security_group_ids") {
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
vipPortID := d.Get("vip_port_id").(string)
if err := resourceLoadBalancerV2SetSecurityGroups(networkingClient, vipPortID, d); err != nil {
return diag.Errorf("Error setting openstack_lb_loadbalancer_v2 security groups: %s", err)
}
}
return resourceLoadBalancerV2Read(ctx, d, meta)
}
func resourceLoadBalancerV2Delete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
lbClient, err := chooseLBV2Client(d, config)
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
log.Printf("[DEBUG] Deleting openstack_lb_loadbalancer_v2 %s", d.Id())
timeout := d.Timeout(schema.TimeoutDelete)
err = resource.Retry(timeout, func() *resource.RetryError {
err = neutronloadbalancers.Delete(lbClient, d.Id()).ExtractErr()
if err != nil {
return checkForRetryableError(err)
}
return nil
})
if err != nil {
return diag.FromErr(CheckDeleted(d, err, "Error deleting openstack_lb_loadbalancer_v2"))
}
// Wait for load-balancer to become deleted.
err = waitForLBV2LoadBalancer(ctx, lbClient, d.Id(), "DELETED", getLbPendingDeleteStatuses(), timeout)
if err != nil {
return diag.FromErr(err)
}
return nil
}