/
resource_citrixadc_appfwprofile_cmdinjection_binding.go
266 lines (239 loc) · 7.58 KB
/
resource_citrixadc_appfwprofile_cmdinjection_binding.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
package citrixadc
import (
"github.com/citrix/adc-nitro-go/resource/config/appfw"
"github.com/citrix/adc-nitro-go/service"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"fmt"
"log"
"net/url"
"strings"
)
func resourceCitrixAdcAppfwprofile_cmdinjection_binding() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createAppfwprofile_cmdinjection_bindingFunc,
Read: readAppfwprofile_cmdinjection_bindingFunc,
Delete: deleteAppfwprofile_cmdinjection_bindingFunc,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
},
"cmdinjection": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
},
"formactionurl_cmd": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
},
"alertonly": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"as_scan_location_cmd": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"as_value_expr_cmd": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"as_value_type_cmd": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"comment": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"isautodeployed": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"isregex_cmd": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"isvalueregex_cmd": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"resourceid": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"ruletype": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"state": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
func createAppfwprofile_cmdinjection_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createAppfwprofile_cmdinjection_bindingFunc")
client := meta.(*NetScalerNitroClient).client
name := d.Get("name")
cmdinjection := d.Get("cmdinjection")
formactionurl_cmd := d.Get("formactionurl_cmd")
bindingId := fmt.Sprintf("%s,%s,%s", name, cmdinjection, formactionurl_cmd)
appfwprofile_cmdinjection_binding := appfw.Appfwprofilecmdinjectionbinding{
Alertonly: d.Get("alertonly").(string),
Asscanlocationcmd: d.Get("as_scan_location_cmd").(string),
Asvalueexprcmd: d.Get("as_value_expr_cmd").(string),
Asvaluetypecmd: d.Get("as_value_type_cmd").(string),
Cmdinjection: d.Get("cmdinjection").(string),
Comment: d.Get("comment").(string),
Formactionurlcmd: d.Get("formactionurl_cmd").(string),
Isautodeployed: d.Get("isautodeployed").(string),
Isregexcmd: d.Get("isregex_cmd").(string),
Isvalueregexcmd: d.Get("isvalueregex_cmd").(string),
Name: d.Get("name").(string),
Resourceid: d.Get("resourceid").(string),
Ruletype: d.Get("ruletype").(string),
State: d.Get("state").(string),
}
err := client.UpdateUnnamedResource("appfwprofile_cmdinjection_binding", &appfwprofile_cmdinjection_binding)
if err != nil {
return err
}
d.SetId(bindingId)
err = readAppfwprofile_cmdinjection_bindingFunc(d, meta)
if err != nil {
log.Printf("[ERROR] netscaler-provider: ?? we just created this appfwprofile_cmdinjection_binding but we can't read it ?? %s", bindingId)
return nil
}
return nil
}
func readAppfwprofile_cmdinjection_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readAppfwprofile_cmdinjection_bindingFunc")
client := meta.(*NetScalerNitroClient).client
bindingId := d.Id()
idSlice := strings.SplitN(bindingId, ",", 3)
name := idSlice[0]
cmdinjection := idSlice[1]
formactionurl_cmd := idSlice[2]
log.Printf("[DEBUG] citrixadc-provider: Reading appfwprofile_cmdinjection_binding state %s", bindingId)
findParams := service.FindParams{
ResourceType: "appfwprofile_cmdinjection_binding",
ResourceName: name,
ResourceMissingErrorCode: 258,
}
dataArr, err := client.FindResourceArrayWithParams(findParams)
// Unexpected error
if err != nil {
log.Printf("[DEBUG] citrixadc-provider: Error during FindResourceArrayWithParams %s", err.Error())
return err
}
// Resource is missing
if len(dataArr) == 0 {
log.Printf("[DEBUG] citrixadc-provider: FindResourceArrayWithParams returned empty array")
log.Printf("[WARN] citrixadc-provider: Clearing appfwprofile_cmdinjection_binding state %s", bindingId)
d.SetId("")
return nil
}
// Iterate through results to find the one with the right id
foundIndex := -1
for i, v := range dataArr {
match := true
if v["cmdinjection"].(string) != cmdinjection {
match = false
}
if v["formactionurl_cmd"].(string) != formactionurl_cmd {
match = false
}
if match {
foundIndex = i
break
}
}
// Resource is missing
if foundIndex == -1 {
log.Printf("[DEBUG] citrixadc-provider: FindResourceArrayWithParams secondIdComponent not found in array")
log.Printf("[WARN] citrixadc-provider: Clearing appfwprofile_cmdinjection_binding state %s", bindingId)
d.SetId("")
return nil
}
// Fallthrough
data := dataArr[foundIndex]
d.Set("alertonly", data["alertonly"])
d.Set("as_scan_location_cmd", data["as_scan_location_cmd"])
d.Set("as_value_expr_cmd", data["as_value_expr_cmd"])
d.Set("as_value_type_cmd", data["as_value_type_cmd"])
d.Set("cmdinjection", data["cmdinjection"])
d.Set("comment", data["comment"])
d.Set("formactionurl_cmd", data["formactionurl_cmd"])
d.Set("isautodeployed", data["isautodeployed"])
d.Set("isregex_cmd", data["isregex_cmd"])
d.Set("isvalueregex_cmd", data["isvalueregex_cmd"])
d.Set("name", data["name"])
d.Set("resourceid", data["resourceid"])
d.Set("ruletype", data["ruletype"])
d.Set("state", data["state"])
return nil
}
func deleteAppfwprofile_cmdinjection_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In deleteAppfwprofile_cmdinjection_bindingFunc")
client := meta.(*NetScalerNitroClient).client
bindingId := d.Id()
idSlice := strings.SplitN(bindingId, ",", 3)
name := idSlice[0]
cmdinjection := idSlice[1]
formactionurl_cmd := idSlice[2]
args := make([]string, 0)
args = append(args, fmt.Sprintf("cmdinjection:%s", cmdinjection))
args = append(args, fmt.Sprintf("formactionurl_cmd:%s", url.QueryEscape(formactionurl_cmd)))
if val, ok := d.GetOk("as_scan_location_cmd"); ok {
args = append(args, fmt.Sprintf("as_scan_location_cmd:%s", url.QueryEscape(val.(string))))
}
if val, ok := d.GetOk("as_value_type_cmd"); ok {
args = append(args, fmt.Sprintf("as_value_type_cmd:%s", url.QueryEscape(val.(string))))
}
if val, ok := d.GetOk("as_value_expr_cmd"); ok {
args = append(args, fmt.Sprintf("as_value_expr_cmd:%s", url.QueryEscape(val.(string))))
}
if val, ok := d.GetOk("ruletype"); ok {
args = append(args, fmt.Sprintf("ruletype:%s", url.QueryEscape(val.(string))))
}
err := client.DeleteResourceWithArgs("appfwprofile_cmdinjection_binding", name, args)
if err != nil {
return err
}
d.SetId("")
return nil
}