-
Notifications
You must be signed in to change notification settings - Fork 15
/
resource_vip_node.go
342 lines (295 loc) · 10.5 KB
/
resource_vip_node.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
package ddcloud
import (
"fmt"
"log"
"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
"github.com/hashicorp/terraform/helper/schema"
)
const (
resourceKeyVIPNodeName = "name"
resourceKeyVIPNodeDescription = "description"
resourceKeyVIPNodeIPv4Address = "ipv4_address"
resourceKeyVIPNodeIPv6Address = "ipv6_address"
resourceKeyVIPNodeStatus = "status"
resourceKeyVIPNodeHealthMonitorName = "health_monitor"
resourceKeyVIPNodeHealthMonitorID = "health_monitor_id"
resourceKeyVIPNodeConnectionLimit = "connection_limit"
resourceKeyVIPNodeConnectionRateLimit = "connection_rate_limit"
resourceKeyVIPNodeNetworkDomainID = "networkdomain"
)
func resourceVIPNode() *schema.Resource {
return &schema.Resource{
Create: resourceVIPNodeCreate,
Read: resourceVIPNodeRead,
Exists: resourceVIPNodeExists,
Update: resourceVIPNodeUpdate,
Delete: resourceVIPNodeDelete,
Schema: map[string]*schema.Schema{
resourceKeyVIPNodeName: &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "A name for the VIP node",
},
resourceKeyVIPNodeDescription: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "A description for the VIP node",
},
resourceKeyVIPNodeIPv4Address: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The VIP node's IPv4 address",
ConflictsWith: []string{
resourceKeyVIPNodeIPv6Address,
},
},
resourceKeyVIPNodeIPv6Address: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The VIP node's IPv6 address",
ConflictsWith: []string{
resourceKeyVIPNodeIPv4Address,
},
},
resourceKeyVIPNodeStatus: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: compute.VIPNodeStatusEnabled,
Description: "The VIP node status",
ValidateFunc: vipStatusValidator("VIP node"),
},
resourceKeyVIPNodeHealthMonitorName: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "CCDEFAULT.Icmp",
Description: "The Name of the VIP node's associated health monitor (if any)",
},
resourceKeyVIPNodeHealthMonitorID: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The Id of the VIP node's associated health monitor (if any)",
},
resourceKeyVIPNodeConnectionLimit: &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 20000,
Description: "The number of active connections that the node supports",
ValidateFunc: func(data interface{}, fieldName string) (messages []string, errors []error) {
connectionRate := data.(int)
if connectionRate > 0 {
return
}
errors = append(errors,
fmt.Errorf("connection rate ('%s') must be greater than 0", fieldName),
)
return
},
},
resourceKeyVIPNodeConnectionRateLimit: &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 2000,
Description: "The number of connections per second that the node supports",
ValidateFunc: func(data interface{}, fieldName string) (messages []string, errors []error) {
connectionRate := data.(int)
if connectionRate > 0 {
return
}
errors = append(errors,
fmt.Errorf("connection rate limit ('%s') must be greater than 0", fieldName),
)
return
},
},
resourceKeyVIPNodeNetworkDomainID: &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The Id of the network domain in which the VIP node is created",
},
},
}
}
func resourceVIPNodeCreate(data *schema.ResourceData, provider interface{}) error {
networkDomainID := data.Get(resourceKeyVIPNodeNetworkDomainID).(string)
name := data.Get(resourceKeyVIPNodeName).(string)
description := data.Get(resourceKeyVIPNodeDescription).(string)
healthMonitorName := data.Get(resourceKeyVIPNodeHealthMonitorName).(string)
providerState := provider.(*providerState)
apiClient := providerState.Client()
log.Printf("Create VIP node '%s' ('%s') in network domain '%s'.", name, description, networkDomainID)
healthMonitorID := ""
if len(healthMonitorName) > 0 {
log.Printf("Find Healt Monitor ID by Name '%s' in network domain '%s'.", healthMonitorName, networkDomainID)
page := compute.DefaultPaging()
page.PageSize = 50
healthMonitors, err := apiClient.ListDefaultHealthMonitors(networkDomainID, page)
if err != nil {
return err
}
for _, healthMonitor := range healthMonitors.Items {
if healthMonitor.Name == healthMonitorName {
healthMonitorID = healthMonitor.ID
}
}
}
vipNodeID, err := apiClient.CreateVIPNode(compute.NewVIPNodeConfiguration{
Name: name,
Description: description,
Status: data.Get(resourceKeyVIPNodeStatus).(string),
IPv4Address: data.Get(resourceKeyVIPNodeIPv4Address).(string),
IPv6Address: data.Get(resourceKeyVIPNodeIPv6Address).(string),
HealthMonitorID: healthMonitorID,
ConnectionLimit: data.Get(resourceKeyVIPNodeConnectionLimit).(int),
ConnectionRateLimit: data.Get(resourceKeyVIPNodeConnectionRateLimit).(int),
NetworkDomainID: networkDomainID,
})
if err != nil {
return err
}
data.SetId(vipNodeID)
log.Printf("Successfully created VIP node '%s'.", vipNodeID)
vipNode, err := apiClient.GetVIPNode(vipNodeID)
if err != nil {
return err
}
if vipNode == nil {
return fmt.Errorf("cannot find newly-added VIP node '%s'", vipNodeID)
}
data.Set(resourceKeyVIPNodeDescription, vipNode.Description)
data.Set(resourceKeyVIPNodeStatus, vipNode.Status)
data.Set(resourceKeyVIPNodeHealthMonitorID, healthMonitorID)
data.Set(resourceKeyVIPNodeConnectionLimit, vipNode.ConnectionLimit)
data.Set(resourceKeyVIPNodeConnectionRateLimit, vipNode.ConnectionRateLimit)
return nil
}
func resourceVIPNodeExists(data *schema.ResourceData, provider interface{}) (bool, error) {
id := data.Id()
log.Printf("Check if VIP node '%s' exists...", id)
providerState := provider.(*providerState)
apiClient := providerState.Client()
vipNode, err := apiClient.GetVIPNode(id)
if err != nil {
return false, err
}
exists := vipNode != nil
log.Printf("VIP node '%s' exists: %t.", id, exists)
return exists, nil
}
func resourceVIPNodeRead(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
log.Printf("Read VIP node '%s'...", id)
providerState := provider.(*providerState)
apiClient := providerState.Client()
vipNode, err := apiClient.GetVIPNode(id)
if err != nil {
return err
}
if vipNode == nil {
data.SetId("") // VIP node has been deleted
return nil
}
data.Set(resourceKeyVIPNodeStatus, vipNode.Status)
return nil
}
func resourceVIPNodeUpdate(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
log.Printf("Update VIP node '%s'...", id)
networkDomainID := data.Get(resourceKeyVIPNodeNetworkDomainID).(string)
configuration := &compute.EditVIPNodeConfiguration{}
providerState := provider.(*providerState)
apiClient := providerState.Client()
propertyHelper := propertyHelper(data)
if data.HasChange(resourceKeyVIPNodeDescription) {
configuration.Description = propertyHelper.GetOptionalString(resourceKeyVIPNodeDescription, true)
}
if data.HasChange(resourceKeyVIPNodeStatus) {
configuration.Status = propertyHelper.GetOptionalString(resourceKeyVIPNodeStatus, false)
}
if data.HasChange(resourceKeyVIPNodeHealthMonitorName) {
healthMonitorName := propertyHelper.GetOptionalString(resourceKeyVIPNodeHealthMonitorName, false)
healthMonitorID := ""
if len(*healthMonitorName) > 0 {
page := compute.DefaultPaging()
page.PageSize = 50
healthMonitors, err := apiClient.ListDefaultHealthMonitors(networkDomainID, page)
if err != nil {
return err
}
for _, healthMonitor := range healthMonitors.Items {
if healthMonitor.Name == *healthMonitorName {
healthMonitorID = healthMonitor.ID
}
}
}
configuration.HealthMonitorID = propertyHelper.GetOptionalString(healthMonitorID, true)
}
if data.HasChange(resourceKeyVIPNodeConnectionLimit) {
configuration.ConnectionLimit = propertyHelper.GetOptionalInt(resourceKeyVIPNodeConnectionLimit, false)
}
if data.HasChange(resourceKeyVIPNodeConnectionRateLimit) {
configuration.ConnectionRateLimit = propertyHelper.GetOptionalInt(resourceKeyVIPNodeConnectionRateLimit, false)
}
return apiClient.EditVIPNode(id, *configuration)
}
func resourceVIPNodeDelete(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
name := data.Get(resourceKeyVIPNodeName).(string)
networkDomainID := data.Get(resourceKeyVIPNodeNetworkDomainID).(string)
log.Printf("Delete VIP node '%s' ('%s') from network domain '%s'...", name, id, networkDomainID)
providerState := provider.(*providerState)
apiClient := providerState.Client()
poolMemberships, err := getVIPNodePoolMemberships(apiClient, id, networkDomainID)
if err != nil {
return err
}
for _, membership := range poolMemberships {
log.Printf("Removing membership '%s' (pool '%s') for node '%s'...", membership.ID, membership.Pool.ID, id)
err = apiClient.RemoveVIPPoolMember(membership.ID)
if err != nil {
return err
}
log.Printf("Removed membership '%s' (pool '%s') for node '%s'.", membership.ID, membership.Pool.ID, id)
}
return apiClient.DeleteVIPNode(id)
}
func getVIPNodePoolMemberships(apiClient *compute.Client, nodeID string, networkDomainID string) (memberships []compute.VIPPoolMember, err error) {
page := compute.DefaultPaging()
page.PageSize = 50
for {
var members *compute.VIPPoolMembers
members, err = apiClient.ListVIPPoolMembershipsInNetworkDomain(networkDomainID, page)
if err != nil {
return
}
if members.IsEmpty() {
break // We're done.
}
for _, member := range members.Items {
if member.Node.ID == nodeID {
memberships = append(memberships, member)
}
}
page.Next()
}
return
}
func vipStatusValidator(targetDescription string) schema.SchemaValidateFunc {
return func(data interface{}, fieldName string) (messages []string, errors []error) {
status := data.(string)
switch status {
case compute.VIPNodeStatusEnabled:
case compute.VIPNodeStatusDisabled:
case compute.VIPNodeStatusForcedOffline:
return
default:
errors = append(errors, fmt.Errorf("invalid %s status value '%s' for field '%s'", targetDescription, status, fieldName))
}
return
}
}