-
Notifications
You must be signed in to change notification settings - Fork 669
/
data_source_ibm_is_ipsec_policies.go
310 lines (284 loc) · 10 KB
/
data_source_ibm_is_ipsec_policies.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
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"time"
"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 DataSourceIBMIsIpsecPolicies() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsIpsecPoliciesRead,
Schema: map[string]*schema.Schema{
"ipsec_policies": {
Type: schema.TypeList,
Computed: true,
Description: "Collection of IPsec policies.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"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.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this IPsec policy.",
},
"key_lifetime": {
Type: schema.TypeInt,
Computed: true,
Description: "The key lifetime in seconds.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this IPsec policy.",
},
"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 dataSourceIBMIsIpsecPoliciesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
start := ""
allrecs := []vpcv1.IPsecPolicy{}
for {
listIpsecPoliciesOptions := &vpcv1.ListIpsecPoliciesOptions{}
if start != "" {
listIpsecPoliciesOptions.Start = &start
}
iPsecPolicyCollection, response, err := vpcClient.ListIpsecPoliciesWithContext(context, listIpsecPoliciesOptions)
if err != nil || iPsecPolicyCollection == nil {
log.Printf("[DEBUG] ListIpsecPoliciesWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("ListIpsecPoliciesWithContext failed %s\n%s", err, response))
}
start = flex.GetNext(iPsecPolicyCollection.Next)
allrecs = append(allrecs, iPsecPolicyCollection.IpsecPolicies...)
if start == "" {
break
}
}
d.SetId(dataSourceIBMIsIpsecPoliciesID(d))
err = d.Set("ipsec_policies", dataSourceIPsecPolicyCollectionFlattenIpsecPolicies(allrecs))
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting ipsec_policies %s", err))
}
return nil
}
// dataSourceIBMIsIpsecPoliciesID returns a reasonable ID for the list.
func dataSourceIBMIsIpsecPoliciesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceIPsecPolicyCollectionFlattenIpsecPolicies(result []vpcv1.IPsecPolicy) (ipsecPolicies []map[string]interface{}) {
for _, ipsecPoliciesItem := range result {
ipsecPolicies = append(ipsecPolicies, dataSourceIPsecPolicyCollectionIpsecPoliciesToMap(ipsecPoliciesItem))
}
return ipsecPolicies
}
func dataSourceIPsecPolicyCollectionIpsecPoliciesToMap(ipsecPoliciesItem vpcv1.IPsecPolicy) (ipsecPoliciesMap map[string]interface{}) {
ipsecPoliciesMap = map[string]interface{}{}
if ipsecPoliciesItem.AuthenticationAlgorithm != nil {
ipsecPoliciesMap["authentication_algorithm"] = ipsecPoliciesItem.AuthenticationAlgorithm
}
if ipsecPoliciesItem.Connections != nil {
connectionsList := []map[string]interface{}{}
for _, connectionsItem := range ipsecPoliciesItem.Connections {
connectionsList = append(connectionsList, dataSourceIPsecPolicyCollectionIpsecPoliciesConnectionsToMap(connectionsItem))
}
ipsecPoliciesMap["connections"] = connectionsList
}
if ipsecPoliciesItem.CreatedAt != nil {
ipsecPoliciesMap["created_at"] = ipsecPoliciesItem.CreatedAt.String()
}
if ipsecPoliciesItem.EncapsulationMode != nil {
ipsecPoliciesMap["encapsulation_mode"] = ipsecPoliciesItem.EncapsulationMode
}
if ipsecPoliciesItem.EncryptionAlgorithm != nil {
ipsecPoliciesMap["encryption_algorithm"] = ipsecPoliciesItem.EncryptionAlgorithm
}
if ipsecPoliciesItem.Href != nil {
ipsecPoliciesMap["href"] = ipsecPoliciesItem.Href
}
if ipsecPoliciesItem.ID != nil {
ipsecPoliciesMap["id"] = ipsecPoliciesItem.ID
}
if ipsecPoliciesItem.KeyLifetime != nil {
ipsecPoliciesMap["key_lifetime"] = ipsecPoliciesItem.KeyLifetime
}
if ipsecPoliciesItem.Name != nil {
ipsecPoliciesMap["name"] = ipsecPoliciesItem.Name
}
if ipsecPoliciesItem.Pfs != nil {
ipsecPoliciesMap["pfs"] = ipsecPoliciesItem.Pfs
}
if ipsecPoliciesItem.ResourceGroup != nil {
resourceGroupList := []map[string]interface{}{}
resourceGroupMap := dataSourceIPsecPolicyCollectionIpsecPoliciesResourceGroupToMap(*ipsecPoliciesItem.ResourceGroup)
resourceGroupList = append(resourceGroupList, resourceGroupMap)
ipsecPoliciesMap["resource_group"] = resourceGroupList
}
if ipsecPoliciesItem.ResourceType != nil {
ipsecPoliciesMap["resource_type"] = ipsecPoliciesItem.ResourceType
}
if ipsecPoliciesItem.TransformProtocol != nil {
ipsecPoliciesMap["transform_protocol"] = ipsecPoliciesItem.TransformProtocol
}
return ipsecPoliciesMap
}
func dataSourceIPsecPolicyCollectionIpsecPoliciesConnectionsToMap(connectionsItem vpcv1.VPNGatewayConnectionReference) (connectionsMap map[string]interface{}) {
connectionsMap = map[string]interface{}{}
if connectionsItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceIPsecPolicyCollectionConnectionsDeletedToMap(*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 dataSourceIPsecPolicyCollectionConnectionsDeletedToMap(deletedItem vpcv1.VPNGatewayConnectionReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}
func dataSourceIPsecPolicyCollectionIpsecPoliciesResourceGroupToMap(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
}