-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_ipsec_policy.go
321 lines (291 loc) · 10 KB
/
data_source_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
// Copyright IBM Corp. 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/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/vpc-go-sdk/vpcv1"
)
func DataSourceIBMIsIpsecPolicy() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsIpsecPolicyRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"name", "ipsec_policy"},
Description: "The IPsec policy name.",
},
"ipsec_policy": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"name", "ipsec_policy"},
Description: "The IPsec policy identifier.",
},
"authentication_algorithm": {
Type: schema.TypeString,
Computed: true,
Description: "The authentication algorithm.",
},
"connections": {
Type: schema.TypeList,
Computed: true,
Description: "The VPN gateway connections that use this IPsec policy.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"deleted": {
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The VPN connection's canonical URL.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this VPN gateway connection.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this VPN connection.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that this IPsec policy was created.",
},
"encapsulation_mode": {
Type: schema.TypeString,
Computed: true,
Description: "The encapsulation mode used. Only `tunnel` is supported.",
},
"encryption_algorithm": {
Type: schema.TypeString,
Computed: true,
Description: "The encryption algorithm.",
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The IPsec policy's canonical URL.",
},
"key_lifetime": {
Type: schema.TypeInt,
Computed: true,
Description: "The key lifetime in seconds.",
},
"pfs": {
Type: schema.TypeString,
Computed: true,
Description: "Perfect Forward Secrecy.",
},
"resource_group": {
Type: schema.TypeList,
Computed: true,
Description: "The resource group for this IPsec policy.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this resource group.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this resource group.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this resource group.",
},
},
},
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
"transform_protocol": {
Type: schema.TypeString,
Computed: true,
Description: "The transform protocol used. Only `esp` is supported.",
},
},
}
}
func dataSourceIBMIsIpsecPolicyRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
name := d.Get("name").(string)
identifier := d.Get("ipsec_policy").(string)
var IPSecPolicy *vpcv1.IPsecPolicy
if name != "" {
start := ""
allrecs := []vpcv1.IPsecPolicy{}
for {
listIPSecPoliciesyOptions := &vpcv1.ListIpsecPoliciesOptions{}
if start != "" {
listIPSecPoliciesyOptions.Start = &start
}
ipSecPolicy, response, err := vpcClient.ListIpsecPolicies(listIPSecPoliciesyOptions)
if err != nil {
return diag.FromErr(fmt.Errorf("Error Fetching IPSec Policies %s\n%s", err, response))
}
start = flex.GetNext(ipSecPolicy.Next)
allrecs = append(allrecs, ipSecPolicy.IpsecPolicies...)
if start == "" {
break
}
}
ipsec_policy_found := false
for _, ipSecPolicyItem := range allrecs {
if *ipSecPolicyItem.Name == name {
IPSecPolicy = &ipSecPolicyItem
ipsec_policy_found = true
break
}
}
if !ipsec_policy_found {
log.Printf("[DEBUG] No ipsec policy found with given name %s", name)
return diag.FromErr(fmt.Errorf("No ipsec policy found with given name %s", name))
}
} else {
getIPSecPolicyOptions := &vpcv1.GetIpsecPolicyOptions{}
getIPSecPolicyOptions.SetID(identifier)
ipsecPolicy1, response, err := vpcClient.GetIpsecPolicyWithContext(context, getIPSecPolicyOptions)
if err != nil {
log.Printf("[DEBUG] GetIpsecPolicyWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetIpsecPolicyWithContext failed %s\n%s", err, response))
}
IPSecPolicy = ipsecPolicy1
}
d.SetId(*IPSecPolicy.ID)
if err = d.Set("authentication_algorithm", IPSecPolicy.AuthenticationAlgorithm); err != nil {
return diag.FromErr(fmt.Errorf("Error setting authentication_algorithm: %s", err))
}
if IPSecPolicy.Connections != nil {
err = d.Set("connections", dataSourceIPsecPolicyFlattenConnections(IPSecPolicy.Connections))
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting connections %s", err))
}
}
if err = d.Set("created_at", flex.DateTimeToString(IPSecPolicy.CreatedAt)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting created_at: %s", err))
}
if err = d.Set("encapsulation_mode", IPSecPolicy.EncapsulationMode); err != nil {
return diag.FromErr(fmt.Errorf("Error setting encapsulation_mode: %s", err))
}
if err = d.Set("encryption_algorithm", IPSecPolicy.EncryptionAlgorithm); err != nil {
return diag.FromErr(fmt.Errorf("Error setting encryption_algorithm: %s", err))
}
if err = d.Set("href", IPSecPolicy.Href); err != nil {
return diag.FromErr(fmt.Errorf("Error setting href: %s", err))
}
if err = d.Set("key_lifetime", flex.IntValue(IPSecPolicy.KeyLifetime)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting key_lifetime: %s", err))
}
if err = d.Set("name", IPSecPolicy.Name); err != nil {
return diag.FromErr(fmt.Errorf("Error setting name: %s", err))
}
if err = d.Set("pfs", IPSecPolicy.Pfs); err != nil {
return diag.FromErr(fmt.Errorf("Error setting pfs: %s", err))
}
if IPSecPolicy.ResourceGroup != nil {
err = d.Set("resource_group", dataSourceIPsecPolicyFlattenResourceGroup(*IPSecPolicy.ResourceGroup))
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting resource_group %s", err))
}
}
if err = d.Set("resource_type", IPSecPolicy.ResourceType); err != nil {
return diag.FromErr(fmt.Errorf("Error setting resource_type: %s", err))
}
if err = d.Set("transform_protocol", IPSecPolicy.TransformProtocol); err != nil {
return diag.FromErr(fmt.Errorf("Error setting transform_protocol: %s", err))
}
return nil
}
func dataSourceIPsecPolicyFlattenConnections(result []vpcv1.VPNGatewayConnectionReference) (connections []map[string]interface{}) {
for _, connectionsItem := range result {
connections = append(connections, dataSourceIPsecPolicyConnectionsToMap(connectionsItem))
}
return connections
}
func dataSourceIPsecPolicyConnectionsToMap(connectionsItem vpcv1.VPNGatewayConnectionReference) (connectionsMap map[string]interface{}) {
connectionsMap = map[string]interface{}{}
if connectionsItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceIPsecPolicyConnectionsDeletedToMap(*connectionsItem.Deleted)
deletedList = append(deletedList, deletedMap)
connectionsMap["deleted"] = deletedList
}
if connectionsItem.Href != nil {
connectionsMap["href"] = connectionsItem.Href
}
if connectionsItem.ID != nil {
connectionsMap["id"] = connectionsItem.ID
}
if connectionsItem.Name != nil {
connectionsMap["name"] = connectionsItem.Name
}
if connectionsItem.ResourceType != nil {
connectionsMap["resource_type"] = connectionsItem.ResourceType
}
return connectionsMap
}
func dataSourceIPsecPolicyConnectionsDeletedToMap(deletedItem vpcv1.VPNGatewayConnectionReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}
func dataSourceIPsecPolicyFlattenResourceGroup(result vpcv1.ResourceGroupReference) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceIPsecPolicyResourceGroupToMap(result)
finalList = append(finalList, finalMap)
return finalList
}
func dataSourceIPsecPolicyResourceGroupToMap(resourceGroupItem vpcv1.ResourceGroupReference) (resourceGroupMap map[string]interface{}) {
resourceGroupMap = map[string]interface{}{}
if resourceGroupItem.Href != nil {
resourceGroupMap["href"] = resourceGroupItem.Href
}
if resourceGroupItem.ID != nil {
resourceGroupMap["id"] = resourceGroupItem.ID
}
if resourceGroupItem.Name != nil {
resourceGroupMap["name"] = resourceGroupItem.Name
}
return resourceGroupMap
}