/
resource_citrixadc_sslcipher.go
282 lines (237 loc) · 8.42 KB
/
resource_citrixadc_sslcipher.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
package citrixadc
import (
"github.com/citrix/adc-nitro-go/resource/config/ssl"
"github.com/citrix/adc-nitro-go/service"
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"bytes"
"fmt"
"log"
"sort"
"strconv"
)
func resourceCitrixAdcSslcipher() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createSslcipherFunc,
Read: readSslcipherFunc,
Update: updateSslcipherFunc,
Delete: deleteSslcipherFunc,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"ciphergroupname": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
// sslcipher_sslciphersuite_binidng is MANDATORY attribute
"ciphersuitebinding": {
Type: schema.TypeSet,
Required: true,
Set: sslcipherCipherSuitebindingMappingHash,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ciphername": {
Type: schema.TypeString,
Required: true,
},
"cipherpriority": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
},
},
},
},
}
}
func createSslcipherFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createSslcipherFunc")
client := meta.(*NetScalerNitroClient).client
sslcipherGroupName := d.Get("ciphergroupname").(string)
sslcipher := ssl.Sslcipher{
Ciphergroupname: sslcipherGroupName,
}
_, err := client.AddResource(service.Sslcipher.Type(), sslcipherGroupName, &sslcipher)
if err != nil {
return err
}
d.SetId(sslcipherGroupName)
err = updateSslCipherCipherSuiteBindings(d, meta)
if err != nil {
return err
}
err = readSslcipherFunc(d, meta)
if err != nil {
log.Printf("[ERROR] netscaler-provider: ?? we just created this sslcipher but we can't read it ?? %s", sslcipherGroupName)
return err
}
return nil
}
func updateSslcipherFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In updateSslcipherFunc")
sslcipherGroupName := d.Get("ciphergroupname").(string)
hasChange := false
if d.HasChange("ciphersuitebinding") {
log.Printf("[DEBUG] citrixadc-provider: Ciphersuitebinding has changed for sslcipherGroupName %s, starting update", sslcipherGroupName)
hasChange = true
}
if hasChange {
err := updateSslCipherCipherSuiteBindings(d, meta)
if err != nil {
return err
}
}
return nil
}
func readSslcipherFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readSslcipherFunc")
client := meta.(*NetScalerNitroClient).client
sslcipherGroupName := d.Id()
log.Printf("[DEBUG] citrixadc-provider: Reading sslcipher state %s", sslcipherGroupName)
data, err := client.FindResource(service.Sslcipher.Type(), sslcipherGroupName)
if err != nil {
log.Printf("[WARN] citrixadc-provider: Clearing sslcipher state %s", sslcipherGroupName)
d.SetId("")
return nil
}
err = readSslCipherCipherSuitebindings(d, meta)
if err != nil {
return err
}
d.Set("ciphergroupname", data["ciphergroupname"])
return nil
}
func deleteSslcipherFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In deleteSslcipherFunc")
client := meta.(*NetScalerNitroClient).client
sslcipherGroupName := d.Id()
err := client.DeleteResource(service.Sslcipher.Type(), sslcipherGroupName)
if err != nil {
return err
}
d.SetId("")
return nil
}
type cipherPriority struct {
cipherName string
cipherPriority uint32
}
type cipherPriorities []cipherPriority
func (n cipherPriorities) Len() int {
return len(n)
}
func getSortedCipherBindigs(unsortedCipherBindings *schema.Set) cipherPriorities {
log.Printf("[DEBUG] citrixadc-provider: In getSortedCipherBindigs")
sortedciphers := make(cipherPriorities, 0, unsortedCipherBindings.Len())
for _, v := range unsortedCipherBindings.List() {
val := v.(map[string]interface{})
ciphername := val["ciphername"].(string)
cipherpriority := uint32(val["cipherpriority"].(int))
cipher := cipherPriority{
cipherName: ciphername,
cipherPriority: cipherpriority,
}
sortedciphers = append(sortedciphers, cipher)
}
sort.Slice(sortedciphers, func(i, j int) bool {
return sortedciphers[i].cipherPriority < sortedciphers[j].cipherPriority
})
return sortedciphers
}
func deleteSingleSslCipherCipherSuiteBinding(d *schema.ResourceData, meta interface{}, binding map[string]interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In deleteSingleSslCipherCipherSuiteBinding")
client := meta.(*NetScalerNitroClient).client
ciphergroupname := d.Get("ciphergroupname").(string)
// Construct args from binding data
args := make([]string, 0, 1)
if d, ok := binding["ciphername"]; ok {
s := fmt.Sprintf("ciphername:%v", d.(string))
args = append(args, s)
}
log.Printf("args is %v", args)
if err := client.DeleteResourceWithArgs(service.Sslcipher_sslciphersuite_binding.Type(), ciphergroupname, args); err != nil {
log.Printf("[DEBUG] citrixadc-provider: Error deleting sslciphersuite binding %v\n", binding)
return err
}
return nil
}
func addSingleSslCipherCipherSuiteBinding(d *schema.ResourceData, meta interface{}, binding cipherPriority) error {
log.Printf("[DEBUG] citrixadc-provider: In addSingleSslCipherCipherSuiteBinding")
client := meta.(*NetScalerNitroClient).client
log.Printf("Adding binding %v", binding)
bindingStruct := ssl.Sslcipherciphersuitebinding{}
bindingStruct.Ciphergroupname = d.Get("ciphergroupname").(string)
bindingStruct.Ciphername = binding.cipherName
bindingStruct.Cipherpriority = binding.cipherPriority
// We need to do a HTTP PUT hence the UpdateResource
if _, err := client.UpdateResource(service.Sslcipher_sslciphersuite_binding.Type(), bindingStruct.Ciphergroupname, bindingStruct); err != nil {
return err
}
return nil
}
func updateSslCipherCipherSuiteBindings(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In updateSslCipherCipherSuiteBindings")
client := meta.(*NetScalerNitroClient).client
findParams := service.FindParams{
ResourceType: "sslcipher_sslciphersuite_binding",
ResourceName: d.Get("ciphergroupname").(string),
}
dataArr, err := client.FindResourceArrayWithParams(findParams)
if err != nil {
return err
}
// We need to do this since adding a ciphersuite with lower priority than an existing one
// will bump the existing priority by one.
// Delete all existing bindings
for _, data := range dataArr {
binding := make(map[string]interface{})
binding["ciphername"] = data["ciphername"]
binding["cipherpriority"] = data["cipherpriority"]
if err := deleteSingleSslCipherCipherSuiteBinding(d, meta, binding); err != nil {
return err
}
}
// Add all configured bindings
add := d.Get("ciphersuitebinding").(*schema.Set)
for _, binding := range getSortedCipherBindigs(add) {
if err := addSingleSslCipherCipherSuiteBinding(d, meta, binding); err != nil {
return err
}
}
return nil
}
func sslcipherCipherSuitebindingMappingHash(v interface{}) int {
log.Printf("[DEBUG] citrixadc-provider: In sslcipherCipherSuitebindingMappingHash")
var buf bytes.Buffer
m := v.(map[string]interface{})
if d, ok := m["ciphername"]; ok {
buf.WriteString(fmt.Sprintf("%s-", d.(string)))
}
if d, ok := m["cipherpriority"]; ok {
buf.WriteString(fmt.Sprintf("%d-", d.(int)))
}
return hashcode.String(buf.String())
}
func readSslCipherCipherSuitebindings(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readSslCipherCipherSuitebindings")
client := meta.(*NetScalerNitroClient).client
ciphergroupname := d.Get("ciphergroupname").(string)
bindings, _ := client.FindResourceArray(service.Sslcipher_sslciphersuite_binding.Type(), ciphergroupname)
log.Printf("bindings %v\n", bindings)
processedBindings := make([]interface{}, len(bindings))
for i := range bindings {
processedBindings[i] = make(map[string]interface{})
processedBindings[i].(map[string]interface{})["ciphername"] = bindings[i]["ciphername"].(string)
processedBindings[i].(map[string]interface{})["cipherpriority"], _ = strconv.Atoi(bindings[i]["cipherpriority"].(string))
}
updatedSet := schema.NewSet(sslcipherCipherSuitebindingMappingHash, processedBindings)
log.Printf("updatedSet %v\n", updatedSet)
if err := d.Set("ciphersuitebinding", updatedSet); err != nil {
return err
}
return nil
}