-
Notifications
You must be signed in to change notification settings - Fork 13
/
resource_vip_pool_member.go
253 lines (204 loc) · 6.79 KB
/
resource_vip_pool_member.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
package ddcloud
import (
"fmt"
"log"
"strconv"
"github.com/DimensionDataResearch/dd-cloud-compute-terraform/retry"
"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
"github.com/hashicorp/terraform/helper/schema"
)
const (
resourceKeyVIPPoolMemberPoolID = "pool"
resourceKeyVIPPoolMemberPoolName = "pool_name"
resourceKeyVIPPoolMemberNodeID = "node"
resourceKeyVIPPoolMemberNodeName = "node_name"
resourceKeyVIPPoolMemberPort = "port"
resourceKeyVIPPoolMemberStatus = "status"
)
func resourceVIPPoolMember() *schema.Resource {
return &schema.Resource{
Create: resourceVIPPoolMemberCreate,
Read: resourceVIPPoolMemberRead,
Exists: resourceVIPPoolMemberExists,
Update: resourceVIPPoolMemberUpdate,
Delete: resourceVIPPoolMemberDelete,
Schema: map[string]*schema.Schema{
resourceKeyVIPPoolMemberPoolID: &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
resourceKeyVIPPoolMemberPoolName: &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
resourceKeyVIPPoolMemberNodeID: &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
resourceKeyVIPPoolMemberNodeName: &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
resourceKeyVIPPoolMemberPort: &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
resourceKeyVIPPoolMemberStatus: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: compute.VIPNodeStatusEnabled,
ValidateFunc: vipStatusValidator("VIP pool member"),
},
},
}
}
func resourceVIPPoolMemberCreate(data *schema.ResourceData, provider interface{}) error {
propertyHelper := propertyHelper(data)
nodeID := data.Get(resourceKeyVIPPoolMemberNodeID).(string)
poolID := data.Get(resourceKeyVIPPoolMemberPoolID).(string)
port := propertyHelper.GetOptionalInt(resourceKeyVIPPoolMemberPort, false)
status := data.Get(resourceKeyVIPPoolMemberStatus).(string)
log.Printf("Add node '%s' as a member of VIP pool '%s'.", nodeID, poolID)
providerState := provider.(*providerState)
apiClient := providerState.Client()
var (
memberID string
err error
)
operationDescription := fmt.Sprintf("Add node '%s' to VIP pool '%s'", nodeID, poolID)
err = providerState.RetryAction(operationDescription, func(context retry.Context) {
asyncLock := providerState.AcquireAsyncOperationLock(operationDescription)
defer asyncLock.Release() // Released at the end of the current attempt.
var addError error
memberID, addError = apiClient.AddVIPPoolMember(poolID, nodeID, status, port)
if compute.IsResourceBusyError(addError) {
context.Retry()
} else if addError != nil {
context.Fail(addError)
}
asyncLock.Release()
})
if err != nil {
return err
}
data.SetId(memberID)
log.Printf("Successfully added node '%s' to VIP pool '%s' as member '%s'.", nodeID, poolID, memberID)
member, err := apiClient.GetVIPPoolMember(memberID)
if err != nil {
return err
}
if member == nil {
return fmt.Errorf("unable to find newly-created pool member '%s'", memberID)
}
data.Set(resourceKeyVIPPoolMemberNodeName, member.Node.Name)
data.Set(resourceKeyVIPPoolMemberPoolName, member.Pool.Name)
data.Set(resourceKeyVIPPoolMemberStatus, member.Status)
return nil
}
func resourceVIPPoolMemberExists(data *schema.ResourceData, provider interface{}) (bool, error) {
id := data.Id()
log.Printf("Check if VIP pool member '%s' exists...", id)
providerState := provider.(*providerState)
apiClient := providerState.Client()
member, err := apiClient.GetVIPPoolMember(id)
if err != nil {
return false, err
}
exists := member != nil
log.Printf("VIP pool member '%s' exists: %t.", id, exists)
return exists, nil
}
func resourceVIPPoolMemberRead(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
log.Printf("Read VIP pool '%s'...", id)
providerState := provider.(*providerState)
apiClient := providerState.Client()
member, err := apiClient.GetVIPPoolMember(id)
if err != nil {
return err
}
if member == nil {
data.SetId("") // VIP pool member has been deleted
return nil
}
data.Set(resourceKeyVIPPoolMemberNodeName, member.Node.Name)
data.Set(resourceKeyVIPPoolMemberPoolName, member.Pool.Name)
data.Set(resourceKeyVIPPoolMemberStatus, member.Status)
return nil
}
func resourceVIPPoolMemberUpdate(data *schema.ResourceData, provider interface{}) error {
id := data.Id()
log.Printf("Update VIP pool member '%s'...", id)
if !data.HasChange(resourceKeyVIPPoolMemberStatus) {
return nil
}
providerState := provider.(*providerState)
apiClient := providerState.Client()
status := data.Get(resourceKeyVIPPoolMemberStatus).(string)
operationDescription := fmt.Sprintf("Edit VIP pool member '%s'", id)
err := providerState.RetryAction(operationDescription, func(context retry.Context) {
asyncLock := providerState.AcquireAsyncOperationLock(operationDescription)
defer asyncLock.Release() // Released at the end of the current attempt.
editError := apiClient.EditVIPPoolMember(id, status)
if compute.IsResourceBusyError(editError) {
context.Retry()
} else if editError != nil {
context.Fail(editError)
}
asyncLock.Release()
})
if err != nil {
return err
}
return nil
}
func resourceVIPPoolMemberDelete(data *schema.ResourceData, provider interface{}) error {
memberID := data.Id()
poolID := data.Get(resourceKeyVIPPoolMemberPoolID).(string)
log.Printf("Delete member '%s' from VIP pool '%s'.", memberID, poolID)
providerState := provider.(*providerState)
apiClient := providerState.Client()
operationDescription := fmt.Sprintf("Remove member '%s' from VIP pool '%s'", memberID, poolID)
err := providerState.RetryAction(operationDescription, func(context retry.Context) {
asyncLock := providerState.AcquireAsyncOperationLock(operationDescription)
defer asyncLock.Release() // Released at the end of the current attempt.
removeError := apiClient.RemoveVIPPoolMember(memberID)
if compute.IsResourceBusyError(removeError) {
context.Retry()
} else if removeError != nil {
context.Fail(removeError)
}
asyncLock.Release()
})
if err != nil {
return err
}
return nil
}
func hashVIPPoolMember(item interface{}) int {
member, ok := item.(compute.VIPPoolMember)
if ok {
port := "ANY"
if member.Port != nil {
port = strconv.Itoa(*member.Port)
}
return schema.HashString(fmt.Sprintf(
"%s/%s/%s/%s",
member.Pool.ID,
member.Node.ID,
port,
member.Status,
))
}
memberData := item.(map[string]interface{})
return schema.HashString(fmt.Sprintf(
"%s/%s/%s/%s",
memberData[resourceKeyVIPPoolMemberPoolID].(string),
memberData[resourceKeyVIPPoolMemberNodeID].(string),
memberData[resourceKeyVIPPoolMemberPort].(string),
memberData[resourceKeyVIPPoolMemberStatus].(string),
))
}