/
resource_ibm_cis_domain_settings.go
161 lines (146 loc) · 4.66 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
package ibm
import (
v1 "github.com/IBM-Cloud/bluemix-go/api/cis/cisv1"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
"strings"
)
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"}),
},
},
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"}
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 {
value := d.Get(item).(string)
if value != "" {
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
}
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
}