-
Notifications
You must be signed in to change notification settings - Fork 13
/
resource_vip_pool.go
304 lines (255 loc) · 9.16 KB
/
resource_vip_pool.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
package ddcloud
import (
"fmt"
"log"
"github.com/DimensionDataResearch/dd-cloud-compute-terraform/retry"
"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
"github.com/hashicorp/terraform/helper/schema"
)
const (
resourceKeyVIPPoolName = "name"
resourceKeyVIPPoolDescription = "description"
resourceKeyVIPPoolLoadBalanceMethod = "load_balance_method"
resourceKeyVIPPoolHealthMonitorNames = "health_monitors"
resourceKeyVIPPoolHealthMonitorIDs = "health_monitors_id"
resourceKeyVIPPoolServiceDownAction = "service_down_action"
resourceKeyVIPPoolSlowRampTime = "slow_ramp_time"
resourceKeyVIPPoolNetworkDomainID = "networkdomain"
)
func resourceVIPPool() *schema.Resource {
return &schema.Resource{
Create: resourceVIPPoolCreate,
Read: resourceVIPPoolRead,
Exists: resourceVIPPoolExists,
Update: resourceVIPPoolUpdate,
Delete: resourceVIPPoolDelete,
Schema: map[string]*schema.Schema{
resourceKeyVIPPoolName: &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "A name for the VIP pool",
},
resourceKeyVIPPoolDescription: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "A description of the VIP pool",
},
resourceKeyVIPPoolLoadBalanceMethod: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: compute.LoadBalanceMethodRoundRobin,
Description: "The load-balancing method used by the VIP pool",
},
resourceKeyVIPPoolHealthMonitorNames: &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Default: nil,
Elem: &schema.Schema{
Type: schema.TypeString,
MaxItems: 2,
},
Set: schema.HashString,
},
resourceKeyVIPPoolHealthMonitorIDs: &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Set: schema.HashString,
},
resourceKeyVIPPoolServiceDownAction: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: compute.ServiceDownActionNone,
ValidateFunc: func(data interface{}, fieldName string) (messages []string, errors []error) {
status := data.(string)
switch status {
case compute.ServiceDownActionNone:
case compute.ServiceDownActionDrop:
case compute.ServiceDownActionReselect:
return
default:
errors = append(errors, fmt.Errorf("invalid VIP service-down action '%s'", status))
}
return
},
},
resourceKeyVIPPoolSlowRampTime: &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
resourceKeyVIPPoolNetworkDomainID: &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceVIPPoolCreate(data *schema.ResourceData, provider interface{}) error {
networkDomainID := data.Get(resourceKeyVIPPoolNetworkDomainID).(string)
name := data.Get(resourceKeyVIPPoolName).(string)
description := data.Get(resourceKeyVIPPoolDescription).(string)
log.Printf("Create VIP pool '%s' ('%s') in network domain '%s'.", name, description, networkDomainID)
propertyHelper := propertyHelper(data)
providerState := provider.(*providerState)
apiClient := providerState.Client()
healthMonitorNames := propertyHelper.GetStringSetItems(resourceKeyVIPPoolHealthMonitorNames)
healthMonitorIDs := make([]string, len(healthMonitorNames))
if healthMonitorNames != nil {
log.Printf("Count of health monitor names '%d'", len(healthMonitorNames))
healthMonitorIDsByName, err := getHealthMonitorIDsByName(networkDomainID, apiClient)
if err != nil {
return err
}
for index, healthMonitorName := range healthMonitorNames {
log.Printf("health monitor name '%s'", healthMonitorName)
healthMonitorIDs[index] = healthMonitorIDsByName[healthMonitorName]
}
log.Printf("Count of health monitor ids associated with the pool '%d'", len(healthMonitorIDs))
}
vipPoolID, err := apiClient.CreateVIPPool(compute.NewVIPPoolConfiguration{
Name: name,
Description: description,
LoadBalanceMethod: data.Get(resourceKeyVIPPoolLoadBalanceMethod).(string),
HealthMonitorIDs: healthMonitorIDs,
ServiceDownAction: data.Get(resourceKeyVIPPoolServiceDownAction).(string),
SlowRampTime: data.Get(resourceKeyVIPPoolSlowRampTime).(int),
NetworkDomainID: networkDomainID,
})
if err != nil {
return err
}
data.SetId(vipPoolID)
propertyHelper.SetStringSetItems(resourceKeyVIPPoolHealthMonitorIDs, healthMonitorIDs)
log.Printf("Successfully created VIP pool '%s'.", vipPoolID)
return nil
}
func resourceVIPPoolExists(data *schema.ResourceData, provider interface{}) (bool, error) {
id := data.Id()
log.Printf("Check if VIP pool '%s' exists...", id)
providerState := provider.(*providerState)
apiClient := providerState.Client()
vipPool, err := apiClient.GetVIPPool(id)
if err != nil {
return false, err
}
exists := vipPool != nil
log.Printf("VIP pool '%s' exists: %t.", id, exists)
return exists, nil
}
func resourceVIPPoolRead(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
log.Printf("Read VIP pool '%s'...", id)
providerState := provider.(*providerState)
apiClient := providerState.Client()
vipPool, err := apiClient.GetVIPPool(id)
if err != nil {
return err
}
if vipPool == nil {
data.SetId("") // VIP pool has been deleted
return nil
}
data.Set(resourceKeyVIPPoolName, vipPool.Name)
data.Set(resourceKeyVIPPoolDescription, vipPool.Description)
data.Set(resourceKeyVIPPoolLoadBalanceMethod, vipPool.LoadBalanceMethod)
propertyHelper := propertyHelper(data)
healthMonitorIDs := make([]string, len(vipPool.HealthMonitors))
for index, healthMonitor := range vipPool.HealthMonitors {
healthMonitorIDs[index] = healthMonitor.ID
}
propertyHelper.SetStringSetItems(resourceKeyVIPPoolHealthMonitorIDs, healthMonitorIDs)
return nil
}
func resourceVIPPoolUpdate(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
log.Printf("Update VIP pool '%s'...", id)
configuration := &compute.EditVIPPoolConfiguration{}
providerState := provider.(*providerState)
apiClient := providerState.Client()
propertyHelper := propertyHelper(data)
if data.HasChange(resourceKeyVIPPoolDescription) {
configuration.Description = propertyHelper.GetOptionalString(resourceKeyVIPPoolDescription, true)
}
if data.HasChange(resourceKeyVIPPoolLoadBalanceMethod) {
configuration.LoadBalanceMethod = propertyHelper.GetOptionalString(resourceKeyVIPPoolLoadBalanceMethod, false)
}
if data.HasChange(resourceKeyVIPPoolHealthMonitorNames) {
networkDomainID := data.Get(resourceKeyVIPPoolNetworkDomainID).(string)
healthMonitorNames := propertyHelper.GetStringSetItems(resourceKeyVIPPoolHealthMonitorNames)
healthMonitorIDs := make([]string, len(healthMonitorNames))
if healthMonitorNames != nil {
healthMonitorIDsByName, err := getHealthMonitorIDsByName(networkDomainID, apiClient)
if err != nil {
return err
}
for index, healthMonitorName := range healthMonitorNames {
healthMonitorIDs[index] = healthMonitorIDsByName[healthMonitorName]
}
}
configuration.HealthMonitorIDs = &healthMonitorIDs
}
if data.HasChange(resourceKeyVIPPoolServiceDownAction) {
configuration.ServiceDownAction = propertyHelper.GetOptionalString(resourceKeyVIPPoolServiceDownAction, false)
}
if data.HasChange(resourceKeyVIPPoolSlowRampTime) {
configuration.SlowRampTime = propertyHelper.GetOptionalInt(resourceKeyVIPPoolSlowRampTime, false)
}
operationDescription := fmt.Sprintf("Edit VIP pool '%s'", id)
err := providerState.RetryAction(operationDescription, func(context retry.Context) {
editError := apiClient.EditVIPPool(id, *configuration)
if compute.IsResourceBusyError(editError) {
context.Retry()
} else if editError != nil {
context.Fail(editError)
}
})
if err != nil {
return err
}
return nil
}
func resourceVIPPoolDelete(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
name := data.Get(resourceKeyVIPPoolName).(string)
networkDomainID := data.Get(resourceKeyVIPPoolNetworkDomainID)
log.Printf("Delete VIP pool '%s' ('%s') from network domain '%s'...", name, id, networkDomainID)
providerState := provider.(*providerState)
apiClient := providerState.Client()
operationDescription := fmt.Sprintf("Delete VIP pool '%s'", id)
err := providerState.RetryAction(operationDescription, func(context retry.Context) {
deleteError := apiClient.DeleteVIPPool(id)
if compute.IsResourceBusyError(deleteError) {
context.Retry()
} else if deleteError != nil {
context.Fail(deleteError)
}
})
if err != nil {
return err
}
return nil
}
func getHealthMonitorIDsByName(networkDomainID string, apiClient *compute.Client) (map[string]string, error) {
healthMonitorIdsByName := make(map[string]string)
page := compute.DefaultPaging()
for {
healthMonitors, err := apiClient.ListDefaultHealthMonitors(networkDomainID, page)
if err != nil {
return nil, err
}
if healthMonitors.IsEmpty() {
break
}
for _, healthMonitor := range healthMonitors.Items {
healthMonitorIdsByName[healthMonitor.Name] = healthMonitor.ID
}
page.Next()
}
return healthMonitorIdsByName, nil
}