/
resource_nsxt_policy_ipsec_vpn_ike_profile.go
233 lines (199 loc) · 7.31 KB
/
resource_nsxt_policy_ipsec_vpn_ike_profile.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
/* Copyright © 2020 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */
package nsxt
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
)
var IPSecVpnIkeProfileEncryptionAlgorithms = []string{
model.IPSecVpnIkeProfile_ENCRYPTION_ALGORITHMS_128,
model.IPSecVpnIkeProfile_ENCRYPTION_ALGORITHMS_256,
model.IPSecVpnIkeProfile_ENCRYPTION_ALGORITHMS_GCM_128,
model.IPSecVpnIkeProfile_ENCRYPTION_ALGORITHMS_GCM_192,
model.IPSecVpnIkeProfile_ENCRYPTION_ALGORITHMS_256,
}
var IPSecVpnIkeProfileDigestAlgorithms = []string{
model.IPSecVpnIkeProfile_DIGEST_ALGORITHMS_SHA1,
model.IPSecVpnIkeProfile_DIGEST_ALGORITHMS_SHA2_256,
model.IPSecVpnIkeProfile_DIGEST_ALGORITHMS_SHA2_384,
model.IPSecVpnIkeProfile_DIGEST_ALGORITHMS_SHA2_512,
}
var IPSecVpnIkeProfileDhGroups = []string{
model.IPSecVpnIkeProfile_DH_GROUPS_GROUP14,
model.IPSecVpnIkeProfile_DH_GROUPS_GROUP2,
model.IPSecVpnIkeProfile_DH_GROUPS_GROUP15,
model.IPSecVpnIkeProfile_DH_GROUPS_GROUP16,
model.IPSecVpnIkeProfile_DH_GROUPS_GROUP19,
model.IPSecVpnIkeProfile_DH_GROUPS_GROUP20,
model.IPSecVpnIkeProfile_DH_GROUPS_GROUP21,
}
var IPSecVpnIkeProfile = []string{
model.IPSecVpnIkeProfile_IKE_VERSION_V1,
model.IPSecVpnIkeProfile_IKE_VERSION_V2,
model.IPSecVpnIkeProfile_IKE_VERSION_FLEX,
}
func resourceNsxtPolicyIpsecVpnIkeProfile() *schema.Resource {
return &schema.Resource{
Create: resourceNsxtPolicyIpsecVpnIkeProfileCreate,
Read: resourceNsxtPolicyIpsecVpnIkeProfileRead,
Update: resourceNsxtPolicyIpsecVpnIkeProfileUpdate,
Delete: resourceNsxtPolicyIpsecVpnIkeProfileDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"nsx_id": getNsxIDSchema(),
"path": getPathSchema(),
"display_name": getDisplayNameSchema(),
"description": getDescriptionSchema(),
"revision": getRevisionSchema(),
"tag": getTagsSchema(),
"ike_version": {
Type: schema.TypeString,
Description: "IKE protocol version to be used. IKE-Flex will initiate IKE-V2 and responds to both IKE-V1 and IKE-V2.",
ValidateFunc: validation.StringInSlice(IPSecVpnIkeProfile, false),
Optional: true,
Default: "IKE_V2",
},
"encryption_algorithms": {
Type: schema.TypeSet,
Description: "Encryption algorithm is used during Internet Key Exchange(IKE) negotiation. Default is AES_128.",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(IPSecVpnIkeProfileEncryptionAlgorithms, false),
},
Optional: true,
},
"digest_algorithms": {
Type: schema.TypeSet,
Description: "Algorithm to be used for message digest during Internet Key Exchange(IKE) negotiation. Default is SHA2_256.",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(IPSecVpnIkeProfileDigestAlgorithms, false),
},
Optional: true,
},
"dh_groups": {
Type: schema.TypeSet,
Description: "Diffie-Hellman group to be used if PFS is enabled. Default is GROUP14.",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(IPSecVpnIkeProfileDhGroups, false),
},
Optional: true,
},
},
}
}
func resourceNsxtPolicyIpsecVpnIkeProfileExists(id string, connector *client.RestConnector, isGlobalManager bool) (bool, error) {
var err error
client := infra.NewDefaultIpsecVpnIkeProfilesClient(connector)
_, err = client.Get(id)
if err == nil {
return true, nil
}
if isNotFoundError(err) {
return false, nil
}
return false, logAPIError("Error retrieving resource", err)
}
func resourceNsxtPolicyIpsecVpnIkeProfileCreate(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
// Initialize resource Id and verify this ID is not yet used
id, err := getOrGenerateID(d, m, resourceNsxtPolicyIpsecVpnIkeProfileExists)
if err != nil {
return err
}
displayName := d.Get("display_name").(string)
description := d.Get("description").(string)
ikeVersion := d.Get("ike_version").(string)
DhGroups := getStringListFromSchemaSet(d, "dh_groups")
DigestAlgorithms := getStringListFromSchemaSet(d, "digest_algorithms")
EncryptionAlgorithms := getStringListFromSchemaSet(d, "encryption_algorithms")
obj := model.IPSecVpnIkeProfile{
DisplayName: &displayName,
Description: &description,
IkeVersion: &ikeVersion,
DhGroups: DhGroups,
DigestAlgorithms: DigestAlgorithms,
EncryptionAlgorithms: EncryptionAlgorithms,
}
// Create the resource using PATCH
log.Printf("[INFO] Creating IpsecVpnIkeProfile with ID %s", id)
client := infra.NewDefaultIpsecVpnIkeProfilesClient(connector)
err = client.Patch(id, obj)
if err != nil {
return handleCreateError("IpsecVpnIkeProfile", id, err)
}
d.SetId(id)
d.Set("nsx_id", id)
return resourceNsxtPolicyIpsecVpnIkeProfileRead(d, m)
}
func resourceNsxtPolicyIpsecVpnIkeProfileRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
id := d.Id()
if id == "" {
return fmt.Errorf("Error obtaining IpsecVpnIkeProfile ID")
}
var obj model.IPSecVpnIkeProfile
client := infra.NewDefaultIpsecVpnIkeProfilesClient(connector)
var err error
obj, err = client.Get(id)
if err != nil {
return handleReadError(d, "IpsecVpnIkeProfile", id, err)
}
d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)
setPolicyTagsInSchema(d, obj.Tags)
d.Set("nsx_id", id)
d.Set("path", obj.Path)
d.Set("revision", obj.Revision)
return nil
}
func resourceNsxtPolicyIpsecVpnIkeProfileUpdate(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
id := d.Id()
if id == "" {
return fmt.Errorf("Error obtaining IpsecVpnIkeProfile ID")
}
displayName := d.Get("display_name").(string)
description := d.Get("description").(string)
ikeVersion := d.Get("ike_version").(string)
DhGroups := getStringListFromSchemaSet(d, "dh_groups")
DigestAlgorithms := getStringListFromSchemaSet(d, "digest_algorithms")
EncryptionAlgorithms := getStringListFromSchemaSet(d, "encryption_algorithms")
obj := model.IPSecVpnIkeProfile{
DisplayName: &displayName,
Description: &description,
IkeVersion: &ikeVersion,
DhGroups: DhGroups,
DigestAlgorithms: DigestAlgorithms,
EncryptionAlgorithms: EncryptionAlgorithms,
}
var err error
client := infra.NewDefaultIpsecVpnIkeProfilesClient(connector)
err = client.Patch(id, obj)
if err != nil {
return handleUpdateError("IpsecVpnIkeProfile", id, err)
}
return resourceNsxtPolicyIpsecVpnIkeProfileRead(d, m)
}
func resourceNsxtPolicyIpsecVpnIkeProfileDelete(d *schema.ResourceData, m interface{}) error {
id := d.Id()
if id == "" {
return fmt.Errorf("Error obtaining IpsecVpnIkeProfile ID")
}
connector := getPolicyConnector(m)
var err error
client := infra.NewDefaultIpsecVpnIkeProfilesClient(connector)
err = client.Delete(id)
if err != nil {
return handleDeleteError("IpsecVpnIkeProfile", id, err)
}
return nil
}