-
Notifications
You must be signed in to change notification settings - Fork 642
/
resource_ibm_private_dns_glb_monitor.go
474 lines (420 loc) · 15.5 KB
/
resource_ibm_private_dns_glb_monitor.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package dnsservices
import (
"fmt"
"strings"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/networking-go-sdk/dnssvcsv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
ibmDNSGlbMonitor = "ibm_dns_glb_monitor"
pdnsGlbMonitorName = "name"
pdnsGlbMonitorID = "monitor_id"
pdnsGlbMonitorDescription = "description"
pdnsGlbMonitorType = "type"
pdnsGlbMonitorPort = "port"
pdnsGlbMonitorInterval = "interval"
pdnsGlbMonitorRetries = "retries"
pdnsGlbMonitorTimeout = "timeout"
pdnsGlbMonitorMethod = "method"
pdnsGlbMonitorPath = "path"
pdnsGlbMonitorAllowInsecure = "allow_insecure"
pdnsGlbMonitorExpectedCodes = "expected_codes"
pdnsGlbMonitorExpectedBody = "expected_body"
pdnsGlbMonitorHeaders = "headers"
pdnsGlbMonitorHeadersName = "name"
pdnsGlbMonitorHeadersValue = "value"
pdnsGlbMonitorCreatedOn = "created_on"
pdnsGlbMonitorModifiedOn = "modified_on"
)
func ResourceIBMPrivateDNSGLBMonitor() *schema.Resource {
return &schema.Resource{
Create: resourceIBMPrivateDNSGLBMonitorCreate,
Read: resourceIBMPrivateDNSGLBMonitorRead,
Update: resourceIBMPrivateDNSGLBMonitorUpdate,
Delete: resourceIBMPrivateDNSGLBMonitorDelete,
Exists: resourceIBMPrivateDNSGLBMonitorExists,
Importer: &schema.ResourceImporter{},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
pdnsGlbMonitorID: {
Type: schema.TypeString,
Computed: true,
Description: "Monitor Id",
},
pdnsInstanceID: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Instance Id",
},
pdnsGlbMonitorName: {
Type: schema.TypeString,
Required: true,
Description: "The unique identifier of a service instance.",
},
pdnsGlbMonitorDescription: {
Type: schema.TypeString,
Optional: true,
Description: "Descriptive text of the load balancer monitor",
},
pdnsGlbMonitorType: {
Type: schema.TypeString,
Optional: true,
Default: "HTTP",
ValidateFunc: validate.InvokeValidator(ibmDNSGlbMonitor, pdnsGlbMonitorType),
Description: "The protocol to use for the health check",
},
pdnsGlbMonitorPort: {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "Port number to connect to for the health check",
},
pdnsGlbMonitorInterval: {
Type: schema.TypeInt,
Optional: true,
Default: 60,
Description: "The interval between each health check",
},
pdnsGlbMonitorRetries: {
Type: schema.TypeInt,
Optional: true,
Default: 1,
Description: "The number of retries to attempt in case of a timeout before marking the origin as unhealthy",
},
pdnsGlbMonitorTimeout: {
Type: schema.TypeInt,
Optional: true,
Default: 5,
Description: "The timeout (in seconds) before marking the health check as failed",
},
pdnsGlbMonitorMethod: {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.InvokeValidator(ibmDNSGlbMonitor, pdnsGlbMonitorMethod),
Description: "The method to use for the health check",
},
pdnsGlbMonitorPath: {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The endpoint path to health check against",
},
pdnsGlbMonitorHeaders: {
Type: schema.TypeSet,
Optional: true,
Description: "The HTTP request headers to send in the health check",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
pdnsGlbMonitorHeadersName: {
Type: schema.TypeString,
Description: "The name of HTTP request header",
Required: true,
},
pdnsGlbMonitorHeadersValue: {
Type: schema.TypeList,
Description: "The value of HTTP request header",
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
pdnsGlbMonitorAllowInsecure: {
Type: schema.TypeBool,
Computed: true,
Optional: true,
Description: "Do not validate the certificate when monitor use HTTPS. This parameter is currently only valid for HTTPS monitors.",
},
pdnsGlbMonitorExpectedCodes: {
Type: schema.TypeString,
Computed: true,
Optional: true,
ValidateFunc: validate.InvokeValidator(ibmDNSGlbMonitor, pdnsGlbMonitorExpectedCodes),
Description: "The expected HTTP response code or code range of the health check. This parameter is only valid for HTTP and HTTPS",
},
pdnsGlbMonitorExpectedBody: {
Type: schema.TypeString,
Optional: true,
Description: "A case-insensitive sub-string to look for in the response body",
},
pdnsGlbMonitorCreatedOn: {
Type: schema.TypeString,
Computed: true,
Description: "GLB Monitor creation date",
},
pdnsGlbMonitorModifiedOn: {
Type: schema.TypeString,
Computed: true,
Description: "GLB Monitor Modification date",
},
},
}
}
func ResourceIBMPrivateDNSGLBMonitorValidator() *validate.ResourceValidator {
monitorCheckTypes := "HTTP, HTTPS, TCP"
methods := "GET, HEAD"
expectedcode := "200,201,202,203,204,205,206,207,208,226,2xx"
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: pdnsGlbMonitorType,
ValidateFunctionIdentifier: validate.ValidateAllowedStringValue,
Type: validate.TypeString,
Required: true,
AllowedValues: monitorCheckTypes})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: pdnsGlbMonitorMethod,
ValidateFunctionIdentifier: validate.ValidateAllowedStringValue,
Type: validate.TypeString,
Required: true,
AllowedValues: methods})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: pdnsGlbMonitorExpectedCodes,
ValidateFunctionIdentifier: validate.ValidateAllowedStringValue,
Type: validate.TypeString,
Required: true,
AllowedValues: expectedcode})
dnsMonitorValidator := validate.ResourceValidator{ResourceName: ibmDNSGlbMonitor, Schema: validateSchema}
return &dnsMonitorValidator
}
func resourceIBMPrivateDNSGLBMonitorCreate(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
if err != nil {
return err
}
instanceID := d.Get(pdnsInstanceID).(string)
createMonitorOptions := sess.NewCreateMonitorOptions(instanceID)
monitorname := d.Get(pdnsGlbMonitorName).(string)
monitorinterval := int64(d.Get(pdnsGlbMonitorInterval).(int))
monitorretries := int64(d.Get(pdnsGlbMonitorRetries).(int))
monitortimeout := int64(d.Get(pdnsGlbMonitorTimeout).(int))
createMonitorOptions.SetName(monitorname)
createMonitorOptions.SetInterval(monitorinterval)
createMonitorOptions.SetRetries(monitorretries)
createMonitorOptions.SetTimeout(monitortimeout)
if monitordescription, ok := d.GetOk(pdnsGlbMonitorDescription); ok {
createMonitorOptions.SetDescription(monitordescription.(string))
}
if Mtype, ok := d.GetOk(pdnsGlbMonitorType); ok {
createMonitorOptions.SetType(Mtype.(string))
}
if Mport, ok := d.GetOk(pdnsGlbMonitorPort); ok {
createMonitorOptions.SetPort(int64(Mport.(int)))
}
if monitorpath, ok := d.GetOk(pdnsGlbMonitorPath); ok {
createMonitorOptions.SetPath((monitorpath).(string))
}
if monitorexpectedcodes, ok := d.GetOk(pdnsGlbMonitorExpectedCodes); ok {
createMonitorOptions.SetExpectedCodes((monitorexpectedcodes).(string))
}
if monitormethod, ok := d.GetOk(pdnsGlbMonitorMethod); ok {
createMonitorOptions.SetMethod((monitormethod).(string))
}
if monitorexpectedbody, ok := d.GetOk(pdnsGlbMonitorExpectedBody); ok {
createMonitorOptions.SetExpectedBody((monitorexpectedbody).(string))
}
if monitorheaders, ok := d.GetOk(pdnsGlbMonitorHeaders); ok {
expandedmonitorheaders, err := expandPDNSGLBMonitorsHeader(monitorheaders)
if err != nil {
return err
}
createMonitorOptions.SetHeadersVar(expandedmonitorheaders)
}
if monitorallowinsecure, ok := d.GetOkExists(pdnsGlbMonitorAllowInsecure); ok {
createMonitorOptions.SetAllowInsecure((monitorallowinsecure).(bool))
}
response, detail, err := sess.CreateMonitor(createMonitorOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error creating pdns GLB monitor:%s\n%s", err, detail)
}
d.SetId(fmt.Sprintf("%s/%s", instanceID, *response.ID))
return resourceIBMPrivateDNSGLBMonitorRead(d, meta)
}
func expandPDNSGLBMonitorsHeader(header interface{}) ([]dnssvcsv1.HealthcheckHeader, error) {
headers := header.(*schema.Set).List()
expandheaders := make([]dnssvcsv1.HealthcheckHeader, 0)
for _, v := range headers {
locationConfig := v.(map[string]interface{})
hname := locationConfig[pdnsGlbMonitorHeadersName].(string)
headers := flex.ExpandStringList(locationConfig[pdnsGlbMonitorHeadersValue].([]interface{}))
headerItem := dnssvcsv1.HealthcheckHeader{
Name: &hname,
Value: headers,
}
expandheaders = append(expandheaders, headerItem)
}
return expandheaders, nil
}
func resourceIBMPrivateDNSGLBMonitorRead(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
if err != nil {
return err
}
idset := strings.Split(d.Id(), "/")
getMonitorOptions := sess.NewGetMonitorOptions(idset[0], idset[1])
response, detail, err := sess.GetMonitor(getMonitorOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error fetching pdns GLB Monitor:%s\n%s", err, detail)
}
d.Set(pdnsInstanceID, idset[0])
d.Set(pdnsGlbMonitorID, response.ID)
d.Set(pdnsGlbMonitorName, response.Name)
d.Set(pdnsGlbMonitorCreatedOn, response.CreatedOn.String())
d.Set(pdnsGlbMonitorModifiedOn, response.ModifiedOn.String())
d.Set(pdnsGlbMonitorType, response.Type)
d.Set(pdnsGlbMonitorPort, response.Port)
if response.Path != nil {
d.Set(pdnsGlbMonitorPath, response.Path)
}
if response.Interval != nil {
d.Set(pdnsGlbMonitorInterval, response.Interval)
}
if response.Retries != nil {
d.Set(pdnsGlbMonitorRetries, response.Retries)
}
if response.Timeout != nil {
d.Set(pdnsGlbMonitorTimeout, response.Timeout)
}
if response.Method != nil {
d.Set(pdnsGlbMonitorMethod, response.Method)
}
if response.ExpectedCodes != nil {
d.Set(pdnsGlbMonitorExpectedCodes, response.ExpectedCodes)
}
if response.AllowInsecure != nil {
d.Set(pdnsGlbMonitorAllowInsecure, response.AllowInsecure)
}
if response.Description != nil {
d.Set(pdnsGlbMonitorDescription, response.Description)
}
if response.ExpectedBody != nil {
d.Set(pdnsGlbMonitorExpectedBody, response.ExpectedBody)
}
d.Set(pdnsGlbMonitorHeaders, flattenDataSourceLoadBalancerHeader(response.HeadersVar))
return nil
}
func flattenDataSourceLoadBalancerHeader(header []dnssvcsv1.HealthcheckHeader) interface{} {
flattened := make([]interface{}, 0)
for _, v := range header {
cfg := map[string]interface{}{
pdnsGlbMonitorHeadersName: v.Name,
pdnsGlbMonitorHeadersValue: flex.FlattenStringList(v.Value),
}
flattened = append(flattened, cfg)
}
return flattened
}
func resourceIBMPrivateDNSGLBMonitorUpdate(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
if err != nil {
return err
}
idset := strings.Split(d.Id(), "/")
// Update PDNS GLB Monitor if attributes has any change
if d.HasChange(pdnsGlbMonitorName) ||
d.HasChange(pdnsGlbMonitorDescription) ||
d.HasChange(pdnsGlbMonitorInterval) ||
d.HasChange(pdnsGlbMonitorRetries) ||
d.HasChange(pdnsGlbMonitorTimeout) ||
d.HasChange(pdnsGlbMonitorExpectedBody) ||
d.HasChange(pdnsGlbMonitorType) ||
d.HasChange(pdnsGlbMonitorPort) ||
d.HasChange(pdnsGlbMonitorPath) ||
d.HasChange(pdnsGlbMonitorAllowInsecure) ||
d.HasChange(pdnsGlbMonitorExpectedCodes) ||
d.HasChange(pdnsGlbMonitorHeaders) {
updateMonitorOptions := sess.NewUpdateMonitorOptions(idset[0], idset[1])
uname := d.Get(pdnsGlbMonitorName).(string)
udescription := d.Get(pdnsGlbMonitorDescription).(string)
uinterval := int64(d.Get(pdnsGlbMonitorInterval).(int))
uretries := int64(d.Get(pdnsGlbMonitorRetries).(int))
utimeout := int64(d.Get(pdnsGlbMonitorTimeout).(int))
updateMonitorOptions.SetName(uname)
updateMonitorOptions.SetDescription(udescription)
updateMonitorOptions.SetInterval(uinterval)
updateMonitorOptions.SetRetries(uretries)
updateMonitorOptions.SetTimeout(utimeout)
if Mtype, ok := d.GetOk(pdnsGlbMonitorType); ok {
updateMonitorOptions.SetType(Mtype.(string))
}
if Mport, ok := d.GetOk(pdnsGlbMonitorPort); ok {
updateMonitorOptions.SetPort(int64(Mport.(int)))
}
if monitorpath, ok := d.GetOk(pdnsGlbMonitorPath); ok {
updateMonitorOptions.SetPath((monitorpath).(string))
}
if monitorexpectedcodes, ok := d.GetOk(pdnsGlbMonitorExpectedCodes); ok {
updateMonitorOptions.SetExpectedCodes((monitorexpectedcodes).(string))
}
if monitormethod, ok := d.GetOk(pdnsGlbMonitorMethod); ok {
updateMonitorOptions.SetMethod((monitormethod).(string))
}
if monitorexpectedbody, ok := d.GetOk(pdnsGlbMonitorExpectedBody); ok {
updateMonitorOptions.SetExpectedBody((monitorexpectedbody).(string))
}
if monitorheaders, ok := d.GetOk(pdnsGlbMonitorHeaders); ok {
expandedmonitorheaders, err := expandPDNSGLBMonitorsHeader(monitorheaders)
if err != nil {
return err
}
updateMonitorOptions.SetHeadersVar(expandedmonitorheaders)
}
if monitorallowinsecure, ok := d.GetOkExists(pdnsGlbMonitorAllowInsecure); ok {
updateMonitorOptions.SetAllowInsecure((monitorallowinsecure).(bool))
}
_, detail, err := sess.UpdateMonitor(updateMonitorOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error updating pdns GLB Monitor:%s\n%s", err, detail)
}
}
return resourceIBMPrivateDNSGLBMonitorRead(d, meta)
}
func resourceIBMPrivateDNSGLBMonitorDelete(d *schema.ResourceData, meta interface{}) error {
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
if err != nil {
return err
}
idset := strings.Split(d.Id(), "/")
DeleteMonitorOptions := sess.NewDeleteMonitorOptions(idset[0], idset[1])
response, err := sess.DeleteMonitor(DeleteMonitorOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error deleting pdns GLB Monitor:%s\n%s", err, response)
}
d.SetId("")
return nil
}
func resourceIBMPrivateDNSGLBMonitorExists(d *schema.ResourceData, meta interface{}) (bool, error) {
sess, err := meta.(conns.ClientSession).PrivateDNSClientSession()
if err != nil {
return false, err
}
idset := strings.Split(d.Id(), "/")
if len(idset) < 2 {
return false, fmt.Errorf("[ERROR] Incorrect ID %s: Id should be a combination of InstanceID/monitorID", d.Id())
}
getMonitorOptions := sess.NewGetMonitorOptions(idset[0], idset[1])
response, detail, err := sess.GetMonitor(getMonitorOptions)
if err != nil {
if response != nil && detail != nil && detail.StatusCode == 404 {
return false, nil
}
return false, err
}
return true, nil
}