/
resource_barracudawaf_json_key_profiles.go
249 lines (197 loc) · 8.09 KB
/
resource_barracudawaf_json_key_profiles.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
package barracudawaf
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var (
subResourceJsonKeyProfilesParams = map[string][]string{}
)
func resourceCudaWAFJsonKeyProfiles() *schema.Resource {
return &schema.Resource{
Create: resourceCudaWAFJsonKeyProfilesCreate,
Read: resourceCudaWAFJsonKeyProfilesRead,
Update: resourceCudaWAFJsonKeyProfilesUpdate,
Delete: resourceCudaWAFJsonKeyProfilesDelete,
Schema: map[string]*schema.Schema{
"allow_null": {Type: schema.TypeString, Optional: true, Description: "Allow NULL"},
"allowed_metachars": {
Type: schema.TypeString,
Optional: true,
Description: "Allowed Metacharacters",
},
"base64_decode_parameter_value": {Type: schema.TypeString, Optional: true, Description: "Base64 Decode"},
"comments": {Type: schema.TypeString, Optional: true, Description: "Comments"},
"value_class": {
Type: schema.TypeString,
Required: true,
Description: "Custom Parameter Class",
},
"exception_patterns": {Type: schema.TypeString, Optional: true, Description: "Exception Patterns"},
"key": {Type: schema.TypeString, Required: true, Description: "Key"},
"max_array_elements": {Type: schema.TypeString, Optional: true, Description: "Max Array Elements"},
"max_length": {Type: schema.TypeString, Optional: true, Description: "Max Length"},
"max_number_value": {Type: schema.TypeString, Optional: true, Description: "Max Number Value"},
"max_keys": {Type: schema.TypeString, Optional: true, Description: "Max Number Of Keys"},
"name": {Type: schema.TypeString, Required: true, Description: "Profile Name"},
"status": {Type: schema.TypeString, Optional: true, Description: "Status"},
"validate_key": {Type: schema.TypeString, Optional: true, Description: "Validate Key"},
"value_type": {Type: schema.TypeString, Optional: true, Description: "Value Type"},
"parent": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Required: true,
},
},
Description: "`barracudawaf_json_key_profiles` manages `Json Key Profiles` on the Barracuda Web Application Firewall.",
}
}
func resourceCudaWAFJsonKeyProfilesCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*BarracudaWAF)
name := d.Get("name").(string)
log.Println("[INFO] Creating Barracuda WAF resource " + name)
resourceEndpoint := "/services/" + d.Get("parent.0").(string) + "/json-profiles/" + d.Get("parent.1").(string) + "/json-key-profiles"
err := client.CreateBarracudaWAFResource(name, hydrateBarracudaWAFJsonKeyProfilesResource(d, "post", resourceEndpoint))
if err != nil {
log.Printf("[ERROR] Unable to create Barracuda WAF resource (%s) (%v) ", name, err)
return err
}
err = client.hydrateBarracudaWAFJsonKeyProfilesSubResource(d, name, resourceEndpoint)
if err != nil {
log.Printf("[ERROR] Unable to create Barracuda WAF sub resource (%s) (%v) ", name, err)
return err
}
d.SetId(name)
return resourceCudaWAFJsonKeyProfilesRead(d, m)
}
func resourceCudaWAFJsonKeyProfilesRead(d *schema.ResourceData, m interface{}) error {
client := m.(*BarracudaWAF)
name := d.Id()
log.Println("[INFO] Fetching Barracuda WAF resource " + name)
resourceEndpoint := "/services/" + d.Get("parent.0").(string) + "/json-profiles/" + d.Get("parent.1").(string) + "/json-key-profiles"
request := &APIRequest{
Method: "get",
URL: resourceEndpoint,
}
var dataItems map[string]interface{}
resources, err := client.GetBarracudaWAFResource(name, request)
if err != nil {
log.Printf("[ERROR] Unable to Retrieve Barracuda WAF resource (%s) (%v) ", name, err)
return err
}
if resources.Data == nil {
log.Printf("[WARN] Barracuda WAF resource (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
for _, dataItems = range resources.Data {
if dataItems["name"] == name {
break
}
}
if dataItems["name"] != name {
return fmt.Errorf("Barracuda WAF resource (%s) not found on the system", name)
}
d.Set("name", name)
return nil
}
func resourceCudaWAFJsonKeyProfilesUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*BarracudaWAF)
name := d.Id()
log.Println("[INFO] Updating Barracuda WAF resource " + name)
resourceEndpoint := "/services/" + d.Get("parent.0").(string) + "/json-profiles/" + d.Get("parent.1").(string) + "/json-key-profiles"
err := client.UpdateBarracudaWAFResource(name, hydrateBarracudaWAFJsonKeyProfilesResource(d, "put", resourceEndpoint))
if err != nil {
log.Printf("[ERROR] Unable to update the Barracuda WAF resource (%s) (%v)", name, err)
return err
}
err = client.hydrateBarracudaWAFJsonKeyProfilesSubResource(d, name, resourceEndpoint)
if err != nil {
log.Printf("[ERROR] Unable to update the Barracuda WAF sub resource (%s) (%v)", name, err)
return err
}
return resourceCudaWAFJsonKeyProfilesRead(d, m)
}
func resourceCudaWAFJsonKeyProfilesDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*BarracudaWAF)
name := d.Id()
log.Println("[INFO] Deleting Barracuda WAF resource " + name)
resourceEndpoint := "/services/" + d.Get("parent.0").(string) + "/json-profiles/" + d.Get("parent.1").(string) + "/json-key-profiles"
request := &APIRequest{
Method: "delete",
URL: resourceEndpoint,
}
err := client.DeleteBarracudaWAFResource(name, request)
if err != nil {
return fmt.Errorf("Unable to delete the Barracuda WAF resource (%s) (%v)", name, err)
}
return nil
}
func hydrateBarracudaWAFJsonKeyProfilesResource(d *schema.ResourceData, method string, endpoint string) *APIRequest {
//resourcePayload : payload for the resource
resourcePayload := map[string]string{
"allow-null": d.Get("allow_null").(string),
"allowed-metachars": d.Get("allowed_metachars").(string),
"base64-decode-parameter-value": d.Get("base64_decode_parameter_value").(string),
"comments": d.Get("comments").(string),
"value-class": d.Get("value_class").(string),
"exception-patterns": d.Get("exception_patterns").(string),
"key": d.Get("key").(string),
"max-array-elements": d.Get("max_array_elements").(string),
"max-length": d.Get("max_length").(string),
"max-number-value": d.Get("max_number_value").(string),
"max-keys": d.Get("max_keys").(string),
"name": d.Get("name").(string),
"status": d.Get("status").(string),
"validate-key": d.Get("validate_key").(string),
"value-type": d.Get("value_type").(string),
}
// parameters not supported for updates
if method == "put" {
updatePayloadExceptions := [...]string{}
for _, param := range updatePayloadExceptions {
delete(resourcePayload, param)
}
}
// remove empty parameters from resource payload
for key, val := range resourcePayload {
if len(val) == 0 {
delete(resourcePayload, key)
}
}
return &APIRequest{
URL: endpoint,
Body: resourcePayload,
}
}
func (b *BarracudaWAF) hydrateBarracudaWAFJsonKeyProfilesSubResource(
d *schema.ResourceData,
name string,
endpoint string,
) error {
for subResource, subResourceParams := range subResourceJsonKeyProfilesParams {
subResourceParamsLength := d.Get(subResource + ".#").(int)
log.Printf("[INFO] Updating Barracuda WAF sub resource (%s) (%s)", name, subResource)
for i := 0; i < subResourceParamsLength; i++ {
subResourcePayload := map[string]string{}
suffix := fmt.Sprintf(".%d", i)
for _, param := range subResourceParams {
paramSuffix := fmt.Sprintf(".%s", param)
paramVaule := d.Get(subResource + suffix + paramSuffix).(string)
if len(paramVaule) > 0 {
param = strings.Replace(param, "_", "-", -1)
subResourcePayload[param] = paramVaule
}
}
err := b.UpdateBarracudaWAFSubResource(name, endpoint, &APIRequest{
URL: strings.Replace(subResource, "_", "-", -1),
Body: subResourcePayload,
})
if err != nil {
return err
}
}
}
return nil
}