/
resource_ibm_cis_healthcheck.go
332 lines (316 loc) · 9.23 KB
/
resource_ibm_cis_healthcheck.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
package ibm
import (
"log"
"reflect"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
v1 "github.com/IBM-Cloud/bluemix-go/api/cis/cisv1"
)
func resourceIBMCISHealthCheck() *schema.Resource {
return &schema.Resource{
Create: resourceCIShealthCheckCreate,
Read: resourceCIShealthCheckRead,
Update: resourceCIShealthCheckUpdate,
Delete: resourceCIShealthCheckDelete,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"cis_id": {
Type: schema.TypeString,
Description: "CIS instance crn",
Required: true,
},
"path": {
Type: schema.TypeString,
Description: "path",
Optional: true,
},
"expected_body": {
Type: schema.TypeString,
Description: "expected_body",
Optional: true,
},
"expected_codes": {
Type: schema.TypeString,
Description: "expected_codes",
Optional: true,
},
"description": {
Type: schema.TypeString,
Description: "description",
Optional: true,
},
"type": {
Type: schema.TypeString,
Description: "type",
Optional: true,
Default: "http",
ValidateFunc: validateAllowedStringValue([]string{"http", "https", "tcp"}),
},
"method": {
Type: schema.TypeString,
Description: "method",
Optional: true,
ValidateFunc: validateAllowedStringValue([]string{"GET", "HEAD"}),
},
"timeout": {
Type: schema.TypeInt,
Description: "timeout",
Optional: true,
Default: 5,
//ValidateFunc: validation.IntBetween(1, 10),
},
"retries": {
Type: schema.TypeInt,
Description: "retries",
Optional: true,
Default: 2,
//ValidateFunc: validation.IntBetween(1, 5),
},
"interval": {
Type: schema.TypeInt,
Description: "interval",
Optional: true,
Default: 60,
},
"follow_redirects": {
Type: schema.TypeBool,
Description: "follow_redirects",
Optional: true,
},
"allow_insecure": {
Type: schema.TypeBool,
Description: "allow_insecure",
Optional: true,
Default: false,
},
"created_on": {
Type: schema.TypeString,
Computed: true,
},
"modified_on": {
Type: schema.TypeString,
Computed: true,
},
// "header": {
// Type: schema.TypeMap,
// Optional: true,
// Elem: &schema.Resource{
// Schema: map[string]*schema.Schema{
// "header": {
// Type: schema.TypeString,
// Required: true,
// },
// "values": {
// Type: schema.TypeSet,
// Required: true,
// Elem: &schema.Schema{
// Type: schema.TypeString,
// },
// },
// },
// },
// Set: HashByMapKey("header"),
// },
"port": {
Type: schema.TypeInt,
Optional: true,
},
},
}
}
func resourceCIShealthCheckCreate(d *schema.ResourceData, meta interface{}) error {
cisClient, err := meta.(ClientSession).CisAPI()
if err != nil {
return err
}
cisId := d.Get("cis_id").(string)
monitorNew := v1.MonitorBody{
MonType: d.Get("type").(string),
}
if expCodes, ok := d.GetOk("expected_codes"); ok {
monitorNew.ExpCodes = expCodes.(string)
}
if expBody, ok := d.GetOk("expected_body"); ok {
monitorNew.ExpBody = expBody.(string)
}
if monPath, ok := d.GetOk("path"); ok {
monitorNew.Path = monPath.(string)
}
if description, ok := d.GetOk("description"); ok {
monitorNew.Description = description.(string)
}
if method, ok := d.GetOk("method"); ok {
monitorNew.Method = method.(string)
}
if timeout, ok := d.GetOk("timeout"); ok {
monitorNew.Timeout = timeout.(int)
}
if retries, ok := d.GetOk("retries"); ok {
monitorNew.Retries = retries.(int)
}
if interval, ok := d.GetOk("interval"); ok {
monitorNew.Interval = interval.(int)
}
if follow_redirects, ok := d.GetOk("follow_redirects"); ok {
monitorNew.FollowRedirects = follow_redirects.(bool)
}
if allow_insecure, ok := d.GetOk("allow_insecure"); ok {
monitorNew.AllowInsecure = allow_insecure.(bool)
}
if port, ok := d.GetOk("port"); ok {
monitorNew.Port = port.(int)
}
var monitor *v1.Monitor
var monitorObj v1.Monitor
monitor, err = cisClient.Monitors().CreateMonitor(cisId, monitorNew)
if err != nil {
log.Printf("CreateMonitors Failed %s\n", err)
return err
}
monitorObj = *monitor
//Set unique TF Id from concatenated CIS Ids
d.SetId(monitorObj.Id + ":" + cisId)
d.Set("path", monitorObj.Path)
return resourceCIShealthCheckRead(d, meta)
}
func resourceCIShealthCheckRead(d *schema.ResourceData, meta interface{}) error {
cisClient, err := meta.(ClientSession).CisAPI()
if err != nil {
return err
}
monitorId, cisId, err := convertTftoCisTwoVar(d.Id())
if err != nil {
return err
}
var monitor *v1.Monitor
monitor, err = cisClient.Monitors().GetMonitor(cisId, monitorId)
if err != nil {
if checkCisMonitorDeleted(d, meta, err, monitor) {
d.SetId("")
return nil
}
log.Printf("[WARN] Error getting zone during MonitorRead %v\n", err)
return err
}
monitorObj := *monitor
d.Set("cis_id", cisId)
d.Set("description", monitorObj.Description)
d.Set("path", monitorObj.Path)
d.Set("expected_body", monitorObj.ExpBody)
d.Set("expected_codes", monitorObj.ExpCodes)
d.Set("type", monitorObj.MonType)
d.Set("method", monitorObj.Method)
d.Set("timeout", monitorObj.Timeout)
d.Set("retries", monitorObj.Retries)
d.Set("interval", monitorObj.Interval)
d.Set("follow_redirects", monitorObj.FollowRedirects)
d.Set("allow_insecure", monitorObj.AllowInsecure)
d.Set("port", monitorObj.Port)
return nil
}
func resourceCIShealthCheckUpdate(d *schema.ResourceData, meta interface{}) error {
cisClient, err := meta.(ClientSession).CisAPI()
if err != nil {
return err
}
monitorId, cisId, err := convertTftoCisTwoVar(d.Id())
if err != nil {
return err
}
monitorUpdate := v1.MonitorBody{}
if d.HasChange("type") || d.HasChange("description") || d.HasChange("path") || d.HasChange("expected_body") || d.HasChange("expected_codes") || d.HasChange("method") || d.HasChange("timeout") || d.HasChange("retries") || d.HasChange("interval") || d.HasChange("follow_redirects") || d.HasChange("allow_insecure") || d.HasChange("port") {
if monType, ok := d.GetOk("type"); ok {
monitorUpdate.MonType = monType.(string)
}
if expCodes, ok := d.GetOk("expected_codes"); ok {
monitorUpdate.ExpCodes = expCodes.(string)
}
if expBody, ok := d.GetOk("expected_body"); ok {
monitorUpdate.ExpBody = expBody.(string)
}
if monPath, ok := d.GetOk("path"); ok {
monitorUpdate.Path = monPath.(string)
}
if description, ok := d.GetOk("description"); ok {
monitorUpdate.Description = description.(string)
}
if method, ok := d.GetOk("method"); ok {
monitorUpdate.Method = method.(string)
}
if timeout, ok := d.GetOk("timeout"); ok {
monitorUpdate.Timeout = timeout.(int)
}
if retries, ok := d.GetOk("retries"); ok {
monitorUpdate.Retries = retries.(int)
}
if interval, ok := d.GetOk("interval"); ok {
monitorUpdate.Interval = interval.(int)
}
if follow_redirects, ok := d.GetOk("follow_redirects"); ok {
monitorUpdate.FollowRedirects = follow_redirects.(bool)
}
if allow_insecure, ok := d.GetOk("allow_insecure"); ok {
monitorUpdate.AllowInsecure = allow_insecure.(bool)
}
if port, ok := d.GetOk("port"); ok {
monitorUpdate.Port = port.(int)
}
_, err = cisClient.Monitors().UpdateMonitor(cisId, monitorId, monitorUpdate)
if err != nil {
log.Printf("[WARN] Error getting zone during MonitorUpdate %v \n", err)
return err
}
}
return resourceCIShealthCheckRead(d, meta)
}
func resourceCIShealthCheckDelete(d *schema.ResourceData, meta interface{}) error {
cisClient, err := meta.(ClientSession).CisAPI()
if err != nil {
return err
}
monitorId, cisId, err := convertTftoCisTwoVar(d.Id())
if err != nil {
return err
}
var monitor *v1.Monitor
emptyMonitor := new(v1.Monitor)
monitor, err = cisClient.Monitors().GetMonitor(cisId, monitorId)
if err != nil {
if checkCisMonitorDeleted(d, meta, err, monitor) {
d.SetId("")
return nil
}
log.Printf("[WARN] Error getting zone during MonitorRead %v\n", err)
return err
}
monitorObj := *monitor
if !reflect.DeepEqual(emptyMonitor, monitorObj) {
err = cisClient.Monitors().DeleteMonitor(cisId, monitorId)
if err != nil {
log.Printf("[WARN] DeleteMonitor Failed %s\n", err)
return err
}
}
d.SetId("")
return nil
}
func checkCisMonitorDeleted(d *schema.ResourceData, meta interface{}, errCheck error, monitor *v1.Monitor) bool {
// Check if error is due to removal of Cis resource and hence all subresources
if strings.Contains(errCheck.Error(), "Object not found") ||
strings.Contains(errCheck.Error(), "status code: 404") ||
strings.Contains(errCheck.Error(), "Invalid zone identifier") { //code 400
log.Printf("[WARN] Removing resource from state because it's not found via the CIS API")
return true
}
_, cisId, _ := convertTftoCisTwoVar(d.Id())
exists, errNew := rcInstanceExists(cisId, "ibm_cis", meta)
if errNew != nil {
log.Printf("resourceCISmonitorRead - Failure validating service exists %s\n", errNew)
return false
}
if !exists {
log.Printf("[WARN] Removing monitor from state because parent cis instance is in removed state")
return true
}
return false
}