-
Notifications
You must be signed in to change notification settings - Fork 670
/
data_source_ibm_is_vpn_gateways.go
181 lines (160 loc) · 5.1 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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"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{
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",
},
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) ",
},
},
},
},
},
}
}
func dataSourceIBMVPNGatewaysRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
listvpnGWOptions := sess.NewListVPNGatewaysOptions()
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[isVPNGatewayStatus] = *data.Status
gateway[isVPNGatewayMode] = *data.Mode
gateway[isVPNGatewayResourceGroup] = *data.ResourceGroup.ID
gateway[isVPNGatewaySubnet] = *data.Subnet.ID
gateway[isVPNGatewayCrn] = *data.CRN
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
currentMemberIP["status"] = *memberIP.Status
vpcMembersIpsList = append(vpcMembersIpsList, currentMemberIP)
}
if memberIP.PrivateIP != nil && memberIP.PrivateIP.Address != nil {
currentMemberIP["private_address"] = *memberIP.PrivateIP.Address
}
}
gateway[isVPNGatewayMembers] = vpcMembersIpsList
}
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()
}