-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_is_vpn_gateways.go
370 lines (341 loc) · 11.7 KB
/
data_source_ibm_is_vpn_gateways.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"log"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isvpnGateways = "vpn_gateways"
isVPNGatewayResourceType = "resource_type"
isVPNGatewayCrn = "crn"
)
func DataSourceIBMISVPNGateways() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMVPNGatewaysRead,
Schema: map[string]*schema.Schema{
"resource_group": {
Type: schema.TypeString,
Optional: true,
Description: "The unique identifier of the resource group this vpn gateway belongs to",
},
"mode": {
Type: schema.TypeString,
Optional: true,
Description: "The mode of this vpn gateway.",
},
isvpnGateways: {
Type: schema.TypeList,
Description: "Collection of VPN Gateways",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isVPNGatewayName: {
Type: schema.TypeString,
Computed: true,
Description: "VPN Gateway instance name",
},
isVPNGatewayCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that this VPN gateway was created",
},
isVPNGatewayCrn: {
Type: schema.TypeString,
Computed: true,
Description: "The VPN gateway's CRN",
},
isVPNGatewayMembers: {
Type: schema.TypeList,
Computed: true,
Description: "Collection of VPN gateway members",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address": {
Type: schema.TypeString,
Computed: true,
Description: "The public IP address assigned to the VPN gateway member",
},
"private_address": {
Type: schema.TypeString,
Computed: true,
Description: "The private IP address assigned to the VPN gateway member",
},
"role": {
Type: schema.TypeString,
Computed: true,
Description: "The high availability role assigned to the VPN gateway member",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The status of the VPN gateway member",
},
},
},
},
isVPNGatewayResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
isVPNGatewayStatus: {
Type: schema.TypeString,
Computed: true,
Description: "The status of the VPN gateway",
},
isVPNGatewayHealthState: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The health of this resource.- `ok`: Healthy- `degraded`: Suffering from compromised performance, capacity, or connectivity- `faulted`: Completely unreachable, inoperative, or otherwise entirely incapacitated- `inapplicable`: The health state does not apply because of the current lifecycle state. A resource with a lifecycle state of `failed` or `deleting` will have a health state of `inapplicable`. A `pending` resource may also have this state.",
},
isVPNGatewayHealthReasons: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"code": {
Type: schema.TypeString,
Computed: true,
Description: "A snake case string succinctly identifying the reason for this health state.",
},
"message": {
Type: schema.TypeString,
Computed: true,
Description: "An explanation of the reason for this health state.",
},
"more_info": {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about the reason for this health state.",
},
},
},
},
isVPNGatewayLifecycleState: &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The lifecycle state of the VPN route.",
},
isVPNGatewayLifecycleReasons: {
Type: schema.TypeList,
Computed: true,
Description: "The reasons for the current lifecycle_state (if any).",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"code": {
Type: schema.TypeString,
Computed: true,
Description: "A snake case string succinctly identifying the reason for this lifecycle state.",
},
"message": {
Type: schema.TypeString,
Computed: true,
Description: "An explanation of the reason for this lifecycle state.",
},
"more_info": {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about the reason for this lifecycle state.",
},
},
},
},
isVPNGatewaySubnet: {
Type: schema.TypeString,
Computed: true,
Description: "VPNGateway subnet info",
},
isVPNGatewayResourceGroup: {
Type: schema.TypeString,
Computed: true,
Description: "resource group identifiers ",
},
isVPNGatewayMode: {
Type: schema.TypeString,
Computed: true,
Description: " VPN gateway mode(policy/route) ",
},
"vpc": {
Type: schema.TypeList,
Computed: true,
Description: "VPC for the VPN Gateway",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this VPC.",
},
"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 URL for this VPC.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this VPC.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The unique user-defined name for this VPC.",
},
},
},
},
isVPNGatewayTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "VPN Gateway tags list",
},
isVPNGatewayAccessTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "List of access management tags",
},
},
},
},
},
}
}
func dataSourceIBMVPNGatewaysRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
listvpnGWOptions := sess.NewListVPNGatewaysOptions()
if resgroupintf, ok := d.GetOk("resource_group"); ok {
resGroup := resgroupintf.(string)
listvpnGWOptions.ResourceGroupID = &resGroup
}
if modeIntf, ok := d.GetOk("mode"); ok {
mode := modeIntf.(string)
listvpnGWOptions.Mode = &mode
}
start := ""
allrecs := []vpcv1.VPNGatewayIntf{}
for {
if start != "" {
listvpnGWOptions.Start = &start
}
availableVPNGateways, detail, err := sess.ListVPNGateways(listvpnGWOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error reading list of VPN Gateways:%s\n%s", err, detail)
}
start = flex.GetNext(availableVPNGateways.Next)
allrecs = append(allrecs, availableVPNGateways.VPNGateways...)
if start == "" {
break
}
}
vpngateways := make([]map[string]interface{}, 0)
for _, instance := range allrecs {
gateway := map[string]interface{}{}
data := instance.(*vpcv1.VPNGateway)
gateway[isVPNGatewayName] = *data.Name
gateway[isVPNGatewayCreatedAt] = data.CreatedAt.String()
gateway[isVPNGatewayResourceType] = *data.ResourceType
gateway[isVPNGatewayHealthState] = *data.HealthState
gateway[isVPNGatewayHealthReasons] = resourceVPNGatewayRouteFlattenHealthReasons(data.HealthReasons)
gateway[isVPNGatewayLifecycleState] = *data.LifecycleState
gateway[isVPNGatewayLifecycleReasons] = resourceVPNGatewayFlattenLifecycleReasons(data.LifecycleReasons)
gateway[isVPNGatewayMode] = *data.Mode
gateway[isVPNGatewayResourceGroup] = *data.ResourceGroup.ID
gateway[isVPNGatewaySubnet] = *data.Subnet.ID
gateway[isVPNGatewayCrn] = *data.CRN
tags, err := flex.GetGlobalTagsUsingCRN(meta, *data.CRN, "", isUserTagType)
if err != nil {
log.Printf(
"Error on get of resource vpc VPN Gateway (%s) tags: %s", d.Id(), err)
}
gateway[isVPNGatewayTags] = tags
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *data.CRN, "", isAccessTagType)
if err != nil {
log.Printf(
"Error on get of resource VPC VPN Gateway (%s) access tags: %s", d.Id(), err)
}
gateway[isVPNGatewayAccessTags] = accesstags
if data.Members != nil {
vpcMembersIpsList := make([]map[string]interface{}, 0)
for _, memberIP := range data.Members {
currentMemberIP := map[string]interface{}{}
if memberIP.PublicIP != nil {
currentMemberIP["address"] = *memberIP.PublicIP.Address
currentMemberIP["role"] = *memberIP.Role
vpcMembersIpsList = append(vpcMembersIpsList, currentMemberIP)
}
if memberIP.PrivateIP != nil && memberIP.PrivateIP.Address != nil {
currentMemberIP["private_address"] = *memberIP.PrivateIP.Address
}
}
gateway[isVPNGatewayMembers] = vpcMembersIpsList
}
if data.VPC != nil {
vpcList := []map[string]interface{}{}
vpcList = append(vpcList, dataSourceVPNServerCollectionVPNGatewayVpcReferenceToMap(data.VPC))
gateway["vpc"] = vpcList
}
vpngateways = append(vpngateways, gateway)
}
d.SetId(dataSourceIBMVPNGatewaysID(d))
d.Set(isvpnGateways, vpngateways)
return nil
}
// dataSourceIBMVPNGatewaysID returns a reasonable ID list.
func dataSourceIBMVPNGatewaysID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceVPNServerCollectionVPNGatewayVpcReferenceToMap(vpcsItem *vpcv1.VPCReference) (vpcsMap map[string]interface{}) {
vpcsMap = map[string]interface{}{}
if vpcsItem.CRN != nil {
vpcsMap["crn"] = vpcsItem.CRN
}
if vpcsItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceVPNGatewayCollectionVpcsDeletedToMap(*vpcsItem.Deleted)
deletedList = append(deletedList, deletedMap)
vpcsMap["deleted"] = deletedList
}
if vpcsItem.Href != nil {
vpcsMap["href"] = vpcsItem.Href
}
if vpcsItem.ID != nil {
vpcsMap["id"] = vpcsItem.ID
}
if vpcsItem.Name != nil {
vpcsMap["name"] = vpcsItem.Name
}
return vpcsMap
}
func dataSourceVPNGatewayCollectionVpcsDeletedToMap(deletedItem vpcv1.VPCReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}