/
resource_ibm_lb_vpx_ha.go
352 lines (304 loc) · 9.03 KB
/
resource_ibm_lb_vpx_ha.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
package ibm
import (
"fmt"
"log"
"strconv"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/minsikl/netscaler-nitro-go/client"
dt "github.com/minsikl/netscaler-nitro-go/datatypes"
"github.com/minsikl/netscaler-nitro-go/op"
)
func resourceIBMLbVpxHa() *schema.Resource {
return &schema.Resource{
Create: resourceIBMLbVpxHaCreate,
Read: resourceIBMLbVpxHaRead,
Update: resourceIBMLbVpxHaUpdate,
Delete: resourceIBMLbVpxHaDelete,
Exists: resourceIBMLbVpxHaExists,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"primary_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "primary ID",
},
"secondary_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "Secondary ID",
},
"stay_secondary": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
Description: "Boolean value for stay secondary",
},
"tags": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: "Tags set for the resource",
},
},
}
}
func configureHA(nClient1 *client.NitroClient, nClient2 *client.NitroClient, staySecondary bool) error {
// 1. VPX2 : Sync password
systemuserReq2 := dt.SystemuserReq{
Systemuser: &dt.Systemuser{
Username: op.String("root"),
Password: op.String(nClient1.Password),
},
}
err := nClient2.Update(&systemuserReq2)
if err != nil {
return err
}
nClient2.Password = nClient1.Password
// 2. VPX1 : Register hanode
hanodeReq1 := dt.HanodeReq{
Hanode: &dt.Hanode{
Id: op.String("2"),
Ipaddress: op.String(nClient2.IpAddress),
},
}
err = nClient1.Add(&hanodeReq1)
if err != nil {
return err
}
// Wait 5 secs to make VPX1 a primary node.
time.Sleep(time.Second * 5)
// 3. VPX2 : Register hanode
hanodeReq2 := dt.HanodeReq{
Hanode: &dt.Hanode{
Id: op.String("2"),
Ipaddress: op.String(nClient1.IpAddress),
},
}
err = nClient2.Add(&hanodeReq2)
if err != nil {
return err
}
// 4. VPX2 : Update STAYSECONDARY
stay := dt.HanodeReq{Hanode: &dt.Hanode{}}
if staySecondary {
stay.Hanode.Hastatus = op.String("STAYSECONDARY")
} else {
stay.Hanode.Hastatus = op.String("ENABLE")
}
err = nClient2.Update(&stay)
if err != nil {
return err
}
// 5. VPX1 : Register rpcnode
nsrpcnode1 := dt.NsrpcnodeReq{
Nsrpcnode: &dt.Nsrpcnode{
Ipaddress: op.String(nClient1.IpAddress),
Password: op.String(nClient1.Password),
},
}
err = nClient1.Update(&nsrpcnode1)
if err != nil {
return err
}
nsrpcnode1.Nsrpcnode.Ipaddress = op.String(nClient2.IpAddress)
err = nClient1.Update(&nsrpcnode1)
if err != nil {
return err
}
// 6. VPX2 : Register rpcnode
nsrpcnode2 := dt.NsrpcnodeReq{
Nsrpcnode: &dt.Nsrpcnode{
Ipaddress: op.String(nClient1.IpAddress),
Password: op.String(nClient1.Password),
},
}
err = nClient2.Update(&nsrpcnode2)
if err != nil {
return err
}
nsrpcnode2.Nsrpcnode.Ipaddress = op.String(nClient2.IpAddress)
err = nClient2.Update(&nsrpcnode2)
if err != nil {
return err
}
// 7. VPX1 : Sync files
hafiles := dt.HafilesReq{
Hafiles: &dt.Hafiles{
Mode: []string{"all"},
},
}
err = nClient1.Add(&hafiles, "action=sync")
if err != nil {
return err
}
return nil
}
func deleteHA(nClient1 *client.NitroClient, nClient2 *client.NitroClient) error {
// 1. VPX2 : Delete hanode
err := nClient2.Delete(&dt.HanodeReq{}, "2")
if err != nil {
return err
}
// 2. VPX1 : Delete hanode
err = nClient1.Delete(&dt.HanodeReq{}, "2")
if err != nil {
return err
}
return nil
}
func parseHAId(id string) (int, int, error) {
if len(id) < 1 {
return 0, 0, fmt.Errorf("Failed to parse id : Unable to get netscaler Ids")
}
idList := strings.Split(id, ":")
if len(idList) != 2 || len(idList[0]) < 1 || len(idList[1]) < 1 {
return 0, 0, fmt.Errorf("Failed to parse id : Invalid HA ID")
}
primaryId, err := strconv.Atoi(idList[0])
if err != nil {
return 0, 0, fmt.Errorf("Failed to parse id : Unable to get a primaryId %s", err)
}
secondaryId, err := strconv.Atoi(idList[1])
if err != nil {
return 0, 0, fmt.Errorf("Failed to parse id : Unable to get a secondaryId %s", err)
}
return primaryId, secondaryId, nil
}
func resourceIBMLbVpxHaCreate(d *schema.ResourceData, meta interface{}) error {
primaryId := d.Get("primary_id").(int)
secondaryId := d.Get("secondary_id").(int)
staySecondary := false
if stay, ok := d.GetOk("stay_secondary"); ok {
staySecondary = stay.(bool)
}
nClientPrimary, err := getNitroClient(meta.(ClientSession).SoftLayerSession(), primaryId)
if err != nil {
return fmt.Errorf("Error getting primary netscaler information ID: %d", primaryId)
}
nClientSecondary, err := getNitroClient(meta.(ClientSession).SoftLayerSession(), secondaryId)
if err != nil {
return fmt.Errorf("Error getting secondary netscaler information ID: %d", secondaryId)
}
err = configureHA(nClientPrimary, nClientSecondary, staySecondary)
if err != nil {
return fmt.Errorf("Error configuration HA %s", err.Error())
}
d.SetId(fmt.Sprintf("%d:%d", primaryId, secondaryId))
log.Printf("[INFO] Netscaler HA ID: %s", d.Id())
return resourceIBMLbVpxHaRead(d, meta)
}
func resourceIBMLbVpxHaRead(d *schema.ResourceData, meta interface{}) error {
primaryId, secondaryId, err := parseHAId(d.Id())
if err != nil {
return fmt.Errorf("Error reading HA %s", err.Error())
}
nClientPrimary, err := getNitroClient(meta.(ClientSession).SoftLayerSession(), primaryId)
if err != nil {
return fmt.Errorf("Error getting primary netscaler information ID: %d", primaryId)
}
nClientSecondary, err := getNitroClient(meta.(ClientSession).SoftLayerSession(), secondaryId)
if err != nil {
return fmt.Errorf("Error getting primary netscaler information ID: %d", primaryId)
}
nClientSecondary.Password = nClientPrimary.Password
res := dt.HanodeRes{}
err = nClientSecondary.Get(&res, "")
if err != nil {
fmt.Printf("Error getting hnode information : %s", err.Error())
}
staySecondary := false
if *res.Hanode[0].Hastatus == "STAYSECONDARY" {
staySecondary = true
}
d.Set("primary_id", primaryId)
d.Set("secondary_id", secondaryId)
d.Set("stay_secondary", staySecondary)
return nil
}
func resourceIBMLbVpxHaUpdate(d *schema.ResourceData, meta interface{}) error {
primaryId, secondaryId, err := parseHAId(d.Id())
if err != nil {
return fmt.Errorf("Error deleting HA %s", err.Error())
}
nClientPrimary, err := getNitroClient(meta.(ClientSession).SoftLayerSession(), primaryId)
if err != nil {
return fmt.Errorf("Error getting primary netscaler information ID: %d", primaryId)
}
nClientSecondary, err := getNitroClient(meta.(ClientSession).SoftLayerSession(), secondaryId)
if err != nil {
return fmt.Errorf("Error getting secondary netscaler information ID: %d", secondaryId)
}
nClientSecondary.Password = nClientPrimary.Password
staySecondary := false
if stay, ok := d.GetOk("stay_secondary"); ok {
staySecondary = stay.(bool)
}
stay := dt.HanodeReq{Hanode: &dt.Hanode{}}
if staySecondary {
stay.Hanode.Hastatus = op.String("STAYSECONDARY")
} else {
stay.Hanode.Hastatus = op.String("ENABLE")
}
err = nClientSecondary.Update(&stay)
if err != nil {
return err
}
return nil
}
func resourceIBMLbVpxHaDelete(d *schema.ResourceData, meta interface{}) error {
primaryId, secondaryId, err := parseHAId(d.Id())
if err != nil {
return fmt.Errorf("Error deleting HA %s", err.Error())
}
nClientPrimary, err := getNitroClient(meta.(ClientSession).SoftLayerSession(), primaryId)
if err != nil {
return fmt.Errorf("Error getting primary netscaler information ID: %d", primaryId)
}
nClientSecondary, err := getNitroClient(meta.(ClientSession).SoftLayerSession(), secondaryId)
if err != nil {
return fmt.Errorf("Error getting secondary netscaler information ID: %d", secondaryId)
}
secondaryPassword := nClientSecondary.Password
nClientSecondary.Password = nClientPrimary.Password
err = deleteHA(nClientPrimary, nClientSecondary)
if err != nil {
return fmt.Errorf("Error deleting HA %s", err.Error())
}
// Restore password of the secondary VPX
systemuserReq := dt.SystemuserReq{
Systemuser: &dt.Systemuser{
Username: op.String("root"),
Password: op.String(secondaryPassword),
},
}
err = nClientSecondary.Update(&systemuserReq)
if err != nil {
return err
}
return nil
}
func resourceIBMLbVpxHaExists(d *schema.ResourceData, meta interface{}) (bool, error) {
primaryId, _, err := parseHAId(d.Id())
if err != nil {
return false, fmt.Errorf("Error reading HA %s", err.Error())
}
nClientPrimary, err := getNitroClient(meta.(ClientSession).SoftLayerSession(), primaryId)
if err != nil {
return false, fmt.Errorf("Error getting primary netscaler information ID in Exist: %d", primaryId)
}
res := dt.HanodeRes{}
err = nClientPrimary.Get(&res, "")
if err != nil {
return false, fmt.Errorf("Error getting hnode information in Exist: %s", err.Error())
}
if len(res.Hanode) < 2 {
return false, nil
}
return true, nil
}