/
resource_ibm_cis_domain_settings.go
294 lines (278 loc) · 9.31 KB
/
resource_ibm_cis_domain_settings.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
package ibm
import (
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
v1 "github.com/IBM-Cloud/bluemix-go/api/cis/cisv1"
)
func resourceIBMCISSettings() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"cis_id": {
Type: schema.TypeString,
Description: "CIS instance crn",
Required: true,
},
"domain_id": {
Type: schema.TypeString,
Description: "Associated CIS domain",
Required: true,
},
"waf": {
Type: schema.TypeString,
Description: "WAF setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"off", "on"}),
},
"ssl": {
Type: schema.TypeString,
Description: "SSL/TLS setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"off", "flexible", "full", "strict", "origin_pull"}),
},
"certificate_status": {
Type: schema.TypeString,
Description: "Certificate status",
Computed: true,
},
"min_tls_version": {
Type: schema.TypeString,
Description: "Minimum version of TLS required",
Optional: true,
ValidateFunc: validateAllowedStringValue([]string{"1.1", "1.2", "1.3", "1.4"}),
Default: "1.1",
},
"cname_flattening": {
Type: schema.TypeString,
Description: "cname_flattening setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"flatten_at_root", "flatten_all", "flatten_none"}),
},
"opportunistic_encryption": {
Type: schema.TypeString,
Description: "opportunistic_encryption setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"automatic_https_rewrites": {
Type: schema.TypeString,
Description: "automatic_https_rewrites setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"always_use_https": {
Type: schema.TypeString,
Description: "always_use_https setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"ipv6": {
Type: schema.TypeString,
Description: "ipv6 setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"browser_check": {
Type: schema.TypeString,
Description: "browser_check setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"hotlink_protection": {
Type: schema.TypeString,
Description: "hotlink_protection setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"http2": {
Type: schema.TypeString,
Description: "http2 setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"image_load_optimization": {
Type: schema.TypeString,
Description: "image_load_optimization setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"image_size_optimization": {
Type: schema.TypeString,
Description: "image_size_optimization setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"lossless", "off", "lossy"}),
},
"ip_geolocation": {
Type: schema.TypeString,
Description: "ip_geolocation setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"origin_error_page_pass_thru": {
Type: schema.TypeString,
Description: "origin_error_page_pass_thru setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"brotli": {
Type: schema.TypeString,
Description: "brotli setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"pseudo_ipv4": {
Type: schema.TypeString,
Description: "pseudo_ipv4 setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"overwrite_header", "off", "add_header"}),
},
"prefetch_preload": {
Type: schema.TypeString,
Description: "prefetch_preload setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"response_buffering": {
Type: schema.TypeString,
Description: "response_buffering setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"script_load_optimization": {
Type: schema.TypeString,
Description: "script_load_optimization setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"server_side_exclude": {
Type: schema.TypeString,
Description: "server_side_exclude setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"tls_client_auth": {
Type: schema.TypeString,
Description: "tls_client_auth setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"true_client_ip_header": {
Type: schema.TypeString,
Description: "true_client_ip_header setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
"websockets": {
Type: schema.TypeString,
Description: "websockets setting",
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"on", "off"}),
},
},
Create: resourceCISSettingsUpdate,
Read: resourceCISSettingsRead,
Update: resourceCISSettingsUpdate,
Delete: resourceCISSettingsDelete,
Importer: &schema.ResourceImporter{},
}
}
var settingsList = [...]string{"waf", "ssl", "min_tls_version", "automatic_https_rewrites", "opportunistic_encryption", "cname_flattening", "always_use_https", "ipv6", "browser_check", "hotlink_protection", "http2", "image_load_optimization", "image_size_optimization", "ip_geolocation", "origin_error_page_pass_thru", "brotli", "pseudo_ipv4", "prefetch_preload", "response_buffering", "script_load_optimization", "server_side_exclude", "tls_client_auth", "true_client_ip_header", "websockets"}
func resourceCISSettingsUpdate(d *schema.ResourceData, meta interface{}) error {
cisClient, err := meta.(ClientSession).CisAPI()
if err != nil {
return err
}
zoneId, cisId, _ := convertTftoCisTwoVar(d.Get("domain_id").(string))
type Setting struct {
Name string
Value string
}
for _, item := range settingsList {
if d.HasChange(item) {
if v, ok := d.GetOk(item); ok {
value := v.(string)
settingsNew := v1.SettingsBody{Value: value}
_, err = cisClient.Settings().UpdateSetting(cisId, zoneId, item, settingsNew)
if err != nil {
log.Printf("Update settings Failed on %s, %s\n", item, err)
return err
}
}
}
}
d.SetId(convertCisToTfTwoVar(zoneId, cisId))
return resourceCISSettingsRead(d, meta)
}
func resourceCISSettingsRead(d *schema.ResourceData, meta interface{}) error {
cisClient, err := meta.(ClientSession).CisAPI()
if err != nil {
return err
}
settingsId, cisId, _ := convertTftoCisTwoVar(d.Id())
for _, item := range settingsList {
settingsResult, err := cisClient.Settings().GetSetting(cisId, settingsId, item)
if err != nil {
if checkCisSettingsDeleted(d, meta, err, cisId) {
d.SetId("")
return nil
}
if strings.Contains(err.Error(), "Request failed with status code: 405") {
log.Printf("[WARN] This action is unavailable for the current plan")
return nil
}
log.Printf("[WARN] Error getting zone during DomainRead %v\n", err)
return err
} else {
settingsObj := *settingsResult
d.Set(item, settingsObj.Value)
}
}
return nil
}
func resourceCISSettingsDelete(d *schema.ResourceData, meta interface{}) error {
// Nothing to delete on CIS resource
d.SetId("")
return nil
}
func checkCisSettingsDeleted(d *schema.ResourceData, meta interface{}, errCheck error, cisId string) 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
}
exists, errNew := rcInstanceExists(cisId, "ibm_cis", meta)
if errNew != nil {
log.Printf("resourceCISdomainRead - Failure validating service exists %s\n", errNew)
return false
}
if !exists {
log.Printf("[WARN] Removing domain settings from state because parent cis instance is in removed state")
return true
}
return false
}