-
Notifications
You must be signed in to change notification settings - Fork 670
/
resource_ibm_is_ipsec_policy.go
406 lines (362 loc) · 12.2 KB
/
resource_ibm_is_ipsec_policy.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isIpSecName = "name"
isIpSecAuthenticationAlg = "authentication_algorithm"
isIpSecEncryptionAlg = "encryption_algorithm"
isIpSecPFS = "pfs"
isIpSecKeyLifeTime = "key_lifetime"
isIPSecResourceGroup = "resource_group"
isIPSecEncapsulationMode = "encapsulation_mode"
isIPSecTransformProtocol = "transform_protocol"
isIPSecVPNConnections = "vpn_connections"
isIPSecVPNConnectionName = "name"
isIPSecVPNConnectionId = "id"
isIPSecVPNConnectionHref = "href"
)
func ResourceIBMISIPSecPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceIBMISIPSecPolicyCreate,
Read: resourceIBMISIPSecPolicyRead,
Update: resourceIBMISIPSecPolicyUpdate,
Delete: resourceIBMISIPSecPolicyDelete,
Exists: resourceIBMISIPSecPolicyExists,
Importer: &schema.ResourceImporter{},
CustomizeDiff: customdiff.All(
customdiff.Sequence(
func(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
return flex.ResourceIPSecPolicyValidate(diff)
}),
),
Schema: map[string]*schema.Schema{
isIpSecName: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.InvokeValidator("ibm_is_ipsec_policy", isIpSecName),
Description: "IPSEC name",
},
isIpSecAuthenticationAlg: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.InvokeValidator("ibm_is_ipsec_policy", isIpSecAuthenticationAlg),
Description: "Authentication alorothm",
},
isIpSecEncryptionAlg: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.InvokeValidator("ibm_is_ipsec_policy", isIpSecEncryptionAlg),
Description: "Encryption algorithm",
},
isIpSecPFS: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.InvokeValidator("ibm_is_ipsec_policy", isIpSecPFS),
Description: "PFS info",
},
isIPSecResourceGroup: {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Computed: true,
Description: "Resource group info",
},
isIpSecKeyLifeTime: {
Type: schema.TypeInt,
Optional: true,
Default: 3600,
ValidateFunc: validate.ValidateKeyLifeTime,
Description: "IPSEC key lifetime",
},
isIPSecEncapsulationMode: {
Type: schema.TypeString,
Computed: true,
Description: "IPSEC encapsulation mode",
},
isIPSecTransformProtocol: {
Type: schema.TypeString,
Computed: true,
Description: "IPSEC transform protocol",
},
isIPSecVPNConnections: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isIPSecVPNConnectionName: {
Type: schema.TypeString,
Computed: true,
},
isIPSecVPNConnectionId: {
Type: schema.TypeString,
Computed: true,
},
isIPSecVPNConnectionHref: {
Type: schema.TypeString,
Computed: true,
},
},
},
},
flex.ResourceControllerURL: {
Type: schema.TypeString,
Computed: true,
Description: "The URL of the IBM Cloud dashboard that can be used to explore and view details about this instance",
},
flex.ResourceName: {
Type: schema.TypeString,
Computed: true,
Description: "The name of the resource",
},
flex.ResourceCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The crn of the resource",
},
flex.ResourceGroupName: {
Type: schema.TypeString,
Computed: true,
Description: "The resource group name in which resource is provisioned",
},
},
}
}
func ResourceIBMISIPSECValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
authentication_algorithm := "md5, sha1, sha256, sha512, sha384, disabled"
encryption_algorithm := "triple_des, aes128, aes256, aes128gcm16, aes192gcm16, aes256gcm16"
pfs := "disabled, group_2, group_5, group_14, group_19, group_15, group_16, group_17, group_18, group_20, group_21, group_22, group_23, group_24, group_31"
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isIpSecName,
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Required: true,
Regexp: `^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$`,
MinValueLength: 1,
MaxValueLength: 63})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isIpSecAuthenticationAlg,
ValidateFunctionIdentifier: validate.ValidateAllowedStringValue,
Type: validate.TypeString,
Required: true,
AllowedValues: authentication_algorithm})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isIpSecEncryptionAlg,
ValidateFunctionIdentifier: validate.ValidateAllowedStringValue,
Type: validate.TypeString,
Required: true,
AllowedValues: encryption_algorithm})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isIpSecPFS,
ValidateFunctionIdentifier: validate.ValidateAllowedStringValue,
Type: validate.TypeString,
Required: true,
AllowedValues: pfs})
ibmISIPSECResourceValidator := validate.ResourceValidator{ResourceName: "ibm_is_ipsec_policy", Schema: validateSchema}
return &ibmISIPSECResourceValidator
}
func resourceIBMISIPSecPolicyCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Ip Sec create")
name := d.Get(isIpSecName).(string)
authenticationAlg := d.Get(isIpSecAuthenticationAlg).(string)
encryptionAlg := d.Get(isIpSecEncryptionAlg).(string)
pfs := d.Get(isIpSecPFS).(string)
err := ipsecpCreate(d, meta, authenticationAlg, encryptionAlg, name, pfs)
if err != nil {
return err
}
return resourceIBMISIPSecPolicyRead(d, meta)
}
func ipsecpCreate(d *schema.ResourceData, meta interface{}, authenticationAlg, encryptionAlg, name, pfs string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
options := &vpcv1.CreateIpsecPolicyOptions{
AuthenticationAlgorithm: &authenticationAlg,
EncryptionAlgorithm: &encryptionAlg,
Pfs: &pfs,
Name: &name,
}
if keylt, ok := d.GetOk(isIpSecKeyLifeTime); ok {
keyLifetime := int64(keylt.(int))
options.KeyLifetime = &keyLifetime
} else {
keyLifetime := int64(3600)
options.KeyLifetime = &keyLifetime
}
if rgrp, ok := d.GetOk(isIPSecResourceGroup); ok {
rg := rgrp.(string)
options.ResourceGroup = &vpcv1.ResourceGroupIdentity{
ID: &rg,
}
}
ipSec, response, err := sess.CreateIpsecPolicy(options)
if err != nil {
return fmt.Errorf("[DEBUG] ipSec policy err %s\n%s", err, response)
}
d.SetId(*ipSec.ID)
log.Printf("[INFO] ipSec policy : %s", *ipSec.ID)
return nil
}
func resourceIBMISIPSecPolicyRead(d *schema.ResourceData, meta interface{}) error {
id := d.Id()
return ipsecpGet(d, meta, id)
}
func ipsecpGet(d *schema.ResourceData, meta interface{}, id string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
getIpsecPolicyOptions := &vpcv1.GetIpsecPolicyOptions{
ID: &id,
}
ipSec, response, err := sess.GetIpsecPolicy(getIpsecPolicyOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("[ERROR] Error getting IPSEC Policy(%s): %s\n%s", id, err, response)
}
d.Set(isIpSecName, *ipSec.Name)
d.Set(isIpSecAuthenticationAlg, *ipSec.AuthenticationAlgorithm)
d.Set(isIpSecEncryptionAlg, *ipSec.EncryptionAlgorithm)
if ipSec.ResourceGroup != nil {
d.Set(isIPSecResourceGroup, *ipSec.ResourceGroup.ID)
d.Set(flex.ResourceGroupName, *ipSec.ResourceGroup.Name)
} else {
d.Set(isIPSecResourceGroup, nil)
}
d.Set(isIpSecPFS, *ipSec.Pfs)
if ipSec.KeyLifetime != nil {
d.Set(isIpSecKeyLifeTime, *ipSec.KeyLifetime)
}
d.Set(isIPSecEncapsulationMode, *ipSec.EncapsulationMode)
d.Set(isIPSecTransformProtocol, *ipSec.TransformProtocol)
connList := make([]map[string]interface{}, 0)
if ipSec.Connections != nil && len(ipSec.Connections) > 0 {
for _, connection := range ipSec.Connections {
conn := map[string]interface{}{}
conn[isIPSecVPNConnectionName] = *connection.Name
conn[isIPSecVPNConnectionId] = *connection.ID
conn[isIPSecVPNConnectionHref] = *connection.Href
connList = append(connList, conn)
}
}
d.Set(isIPSecVPNConnections, connList)
controller, err := flex.GetBaseController(meta)
if err != nil {
return err
}
d.Set(flex.ResourceControllerURL, controller+"/vpc-ext/network/ipsecpolicies")
d.Set(flex.ResourceName, *ipSec.Name)
// d.Set(flex.ResourceCRN, *ipSec.Crn)
return nil
}
func resourceIBMISIPSecPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
id := d.Id()
err := ipsecpUpdate(d, meta, id)
if err != nil {
return err
}
return resourceIBMISIPSecPolicyRead(d, meta)
}
func ipsecpUpdate(d *schema.ResourceData, meta interface{}, id string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
options := &vpcv1.UpdateIpsecPolicyOptions{
ID: &id,
}
if d.HasChange(isIpSecName) || d.HasChange(isIpSecAuthenticationAlg) || d.HasChange(isIpSecEncryptionAlg) || d.HasChange(isIpSecPFS) || d.HasChange(isIpSecKeyLifeTime) {
name := d.Get(isIpSecName).(string)
authenticationAlg := d.Get(isIpSecAuthenticationAlg).(string)
encryptionAlg := d.Get(isIpSecEncryptionAlg).(string)
pfs := d.Get(isIpSecPFS).(string)
keyLifetime := int64(d.Get(isIpSecKeyLifeTime).(int))
ipsecPolicyPatchModel := &vpcv1.IPsecPolicyPatch{
Name: &name,
AuthenticationAlgorithm: &authenticationAlg,
EncryptionAlgorithm: &encryptionAlg,
Pfs: &pfs,
KeyLifetime: &keyLifetime,
}
ipsecPolicyPatch, err := ipsecPolicyPatchModel.AsPatch()
if err != nil {
return fmt.Errorf("[ERROR] Error calling asPatch for IPsecPolicyPatch: %s", err)
}
options.IPsecPolicyPatch = ipsecPolicyPatch
_, response, err := sess.UpdateIpsecPolicy(options)
if err != nil {
return fmt.Errorf("[ERROR] Error on update of IPSEC Policy(%s): %s\n%s", id, err, response)
}
}
return nil
}
func resourceIBMISIPSecPolicyDelete(d *schema.ResourceData, meta interface{}) error {
id := d.Id()
return ipsecpDelete(d, meta, id)
}
func ipsecpDelete(d *schema.ResourceData, meta interface{}, id string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
getIpsecPolicyOptions := &vpcv1.GetIpsecPolicyOptions{
ID: &id,
}
_, response, err := sess.GetIpsecPolicy(getIpsecPolicyOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("[ERROR] Error getting IPSEC Policy(%s): %s\n%s", id, err, response)
}
deleteIpsecPolicyOptions := &vpcv1.DeleteIpsecPolicyOptions{
ID: &id,
}
response, err = sess.DeleteIpsecPolicy(deleteIpsecPolicyOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Deleting IPSEC Policy(%s): %s\n%s", id, err, response)
}
d.SetId("")
return nil
}
func resourceIBMISIPSecPolicyExists(d *schema.ResourceData, meta interface{}) (bool, error) {
id := d.Id()
return ipsecpExists(d, meta, id)
}
func ipsecpExists(d *schema.ResourceData, meta interface{}, id string) (bool, error) {
sess, err := vpcClient(meta)
if err != nil {
return false, err
}
getIpsecPolicyOptions := &vpcv1.GetIpsecPolicyOptions{
ID: &id,
}
_, response, err := sess.GetIpsecPolicy(getIpsecPolicyOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
return false, nil
}
return false, fmt.Errorf("[ERROR] Error getting IPSEC Policy(%s): %s\n%s", id, err, response)
}
return true, nil
}