/
resource_citrixadc_appfwprofile_fieldformat_binding.go
249 lines (222 loc) · 6.91 KB
/
resource_citrixadc_appfwprofile_fieldformat_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
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_fieldformat_binding() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createAppfwprofile_fieldformat_bindingFunc,
Read: readAppfwprofile_fieldformat_bindingFunc,
Delete: deleteAppfwprofile_fieldformat_bindingFunc,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
},
"fieldformat": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
},
"formactionurl_ff": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
},
"alertonly": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"comment": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"fieldformatmaxlength": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"fieldformatminlength": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"fieldtype": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"isautodeployed": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"isregexff": {
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_fieldformat_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createAppfwprofile_fieldformat_bindingFunc")
client := meta.(*NetScalerNitroClient).client
name := d.Get("name")
fieldformat := d.Get("fieldformat")
formactionurl_ff := d.Get("formactionurl_ff")
bindingId := fmt.Sprintf("%s,%s,%s", name, fieldformat, formactionurl_ff)
appfwprofile_fieldformat_binding := appfw.Appfwprofilefieldformatbinding{
Alertonly: d.Get("alertonly").(string),
Comment: d.Get("comment").(string),
Fieldformat: d.Get("fieldformat").(string),
Fieldformatmaxlength: d.Get("fieldformatmaxlength").(int),
Fieldformatminlength: d.Get("fieldformatminlength").(int),
Fieldtype: d.Get("fieldtype").(string),
Formactionurlff: d.Get("formactionurl_ff").(string),
Isautodeployed: d.Get("isautodeployed").(string),
Isregexff: d.Get("isregexff").(string),
Name: d.Get("name").(string),
Resourceid: d.Get("resourceid").(string),
Ruletype: d.Get("ruletype").(string),
State: d.Get("state").(string),
}
err := client.UpdateUnnamedResource(service.Appfwprofile_fieldformat_binding.Type(), &appfwprofile_fieldformat_binding)
if err != nil {
return err
}
d.SetId(bindingId)
err = readAppfwprofile_fieldformat_bindingFunc(d, meta)
if err != nil {
log.Printf("[ERROR] netscaler-provider: ?? we just created this appfwprofile_fieldformat_binding but we can't read it ?? %s", bindingId)
return nil
}
return nil
}
func readAppfwprofile_fieldformat_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readAppfwprofile_fieldformat_bindingFunc")
client := meta.(*NetScalerNitroClient).client
bindingId := d.Id()
idSlice := strings.SplitN(bindingId, ",", 3)
name := idSlice[0]
fieldformat := idSlice[1]
formactionurl_ff := idSlice[2]
log.Printf("[DEBUG] citrixadc-provider: Reading appfwprofile_fieldformat_binding state %s", bindingId)
findParams := service.FindParams{
ResourceType: "appfwprofile_fieldformat_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_fieldformat_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["fieldformat"].(string) != fieldformat {
match = false
}
if v["formactionurl_ff"].(string) != formactionurl_ff {
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_fieldformat_binding state %s", bindingId)
d.SetId("")
return nil
}
// Fallthrough
data := dataArr[foundIndex]
// d.Set("alertonly", data["alertonly"])
d.Set("comment", data["comment"])
d.Set("fieldformat", data["fieldformat"])
d.Set("fieldformatmaxlength", data["fieldformatmaxlength"])
d.Set("fieldformatminlength", data["fieldformatminlength"])
d.Set("fieldtype", data["fieldtype"])
d.Set("formactionurl_ff", data["formactionurl_ff"])
d.Set("isautodeployed", data["isautodeployed"])
// d.Set("isregexff", data["isregexff"])
d.Set("name", data["name"])
d.Set("resourceid", data["resourceid"])
d.Set("ruletype", data["ruletype"])
d.Set("state", data["state"])
return nil
}
func deleteAppfwprofile_fieldformat_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In deleteAppfwprofile_fieldformat_bindingFunc")
client := meta.(*NetScalerNitroClient).client
bindingId := d.Id()
idSlice := strings.SplitN(bindingId, ",", 3)
name := idSlice[0]
fieldformat := idSlice[1]
formactionurl_ff := idSlice[2]
args := make([]string, 0)
args = append(args, fmt.Sprintf("fieldformat:%s", fieldformat))
args = append(args, fmt.Sprintf("formactionurl_ff:%s", url.QueryEscape(formactionurl_ff)))
if val, ok := d.GetOk("ruletype"); ok {
args = append(args, fmt.Sprintf("ruletype:%s", url.QueryEscape(val.(string))))
}
err := client.DeleteResourceWithArgs(service.Appfwprofile_fieldformat_binding.Type(), name, args)
if err != nil {
return err
}
d.SetId("")
return nil
}