-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_virtual_endpoint_gateways.go
266 lines (255 loc) · 9.3 KB
/
data_source_ibm_is_virtual_endpoint_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
// 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 (
ibmISVirtualEndpointGateways = "ibm_is_virtual_endpoint_gateways"
isVirtualEndpointGateways = "virtual_endpoint_gateways"
)
func DataSourceIBMISEndpointGateways() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISEndpointGatewaysRead,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"resource_group": {
Type: schema.TypeString,
Optional: true,
Description: "The unique identifier of the resource group this endpoint gateway belongs to",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the endpoint gateway group",
},
isVirtualEndpointGateways: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway id",
},
isVirtualEndpointGatewayName: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway name",
},
isVirtualEndpointGatewayAllowDnsResolutionBinding: {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether to allow this endpoint gateway to participate in DNS resolution bindings with a VPC that has dns.enable_hub set to true.",
},
isVirtualEndpointGatewayResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway resource type",
},
isVirtualEndpointGatewayResourceGroupID: {
Type: schema.TypeString,
Computed: true,
Description: "The resource group id",
},
isVirtualEndpointGatewayCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this Endpoint Gateway",
},
isVirtualEndpointGatewayCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway created date and time",
},
isVirtualEndpointGatewayHealthState: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway health state",
},
isVirtualEndpointGatewayServiceEndpoints: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "The fully qualified domain names for the target service. A fully qualified domain name for the target service",
},
isVirtualEndpointGatewayLifecycleState: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway lifecycle state",
},
isVirtualEndpointGatewaySecurityGroups: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: "Endpoint gateway securitygroups list",
},
isVirtualEndpointGatewayIPs: {
Type: schema.TypeList,
Computed: true,
Description: "Collection of reserved IPs bound to an endpoint gateway",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isVirtualEndpointGatewayIPsID: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this reserved IP",
},
isVirtualEndpointGatewayIPsName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined or system-provided name for this reserved IP",
},
isVirtualEndpointGatewayIPsResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type(subnet_reserved_ip)",
},
},
},
},
isVirtualEndpointGatewayTarget: {
Type: schema.TypeList,
Computed: true,
Description: "Endpoint gateway target",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isVirtualEndpointGatewayTargetName: {
Type: schema.TypeString,
Computed: true,
Description: "The target name",
},
isVirtualEndpointGatewayTargetCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The target crn",
},
isVirtualEndpointGatewayTargetResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "The target resource type",
},
},
},
},
isVirtualEndpointGatewayVpcID: {
Type: schema.TypeString,
Computed: true,
Description: "The VPC id",
},
isVirtualEndpointGatewayTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "List of tags for VPE",
},
isVirtualEndpointGatewayAccessTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "List of access management tags",
},
},
},
},
},
}
}
func dataSourceIBMISEndpointGatewaysRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
start := ""
allrecs := []vpcv1.EndpointGateway{}
options := sess.NewListEndpointGatewaysOptions()
if resgroupintf, ok := d.GetOk("resource_group"); ok {
resGroup := resgroupintf.(string)
options.ResourceGroupID = &resGroup
}
if nameintf, ok := d.GetOk("name"); ok {
name := nameintf.(string)
options.Name = &name
}
for {
if start != "" {
options.Start = &start
}
result, response, err := sess.ListEndpointGateways(options)
if err != nil {
return fmt.Errorf("[ERROR] Error fetching endpoint gateways %s\n%s", err, response)
}
start = flex.GetNext(result.Next)
allrecs = append(allrecs, result.EndpointGateways...)
if start == "" {
break
}
}
endpointGateways := []map[string]interface{}{}
for _, endpointGateway := range allrecs {
endpointGatewayOutput := map[string]interface{}{}
endpointGatewayOutput["id"] = *endpointGateway.ID
endpointGatewayOutput[isVirtualEndpointGatewayName] = *endpointGateway.Name
endpointGatewayOutput[isVirtualEndpointGatewayCreatedAt] = (*endpointGateway.CreatedAt).String()
endpointGatewayOutput[isVirtualEndpointGatewayResourceType] = (*endpointGateway.ResourceType)
endpointGatewayOutput[isVirtualEndpointGatewayHealthState] = *endpointGateway.HealthState
endpointGatewayOutput[isVirtualEndpointGatewayLifecycleState] = *endpointGateway.LifecycleState
endpointGatewayOutput[isVirtualEndpointGatewayResourceGroupID] = *endpointGateway.ResourceGroup.ID
endpointGatewayOutput[isVirtualEndpointGatewayCRN] = *endpointGateway.CRN
endpointGatewayOutput[isVirtualEndpointGatewayVpcID] = *endpointGateway.VPC.ID
endpointGatewayOutput[isVirtualEndpointGatewayAllowDnsResolutionBinding] = endpointGateway.AllowDnsResolutionBinding
endpointGatewayOutput[isVirtualEndpointGatewayTarget] =
flattenEndpointGatewayTarget(endpointGateway.Target.(*vpcv1.EndpointGatewayTarget))
if endpointGateway.SecurityGroups != nil {
endpointGatewayOutput[isVirtualEndpointGatewaySecurityGroups] =
flattenDataSourceSecurityGroups(endpointGateway.SecurityGroups)
}
if len(endpointGateway.ServiceEndpoints) > 0 {
endpointGatewayOutput[isVirtualEndpointGatewayServiceEndpoints] = endpointGateway.ServiceEndpoints
}
endpointGatewayOutput[isVirtualEndpointGatewayIPs] =
flattenDataSourceIPs(endpointGateway.Ips)
tags, err := flex.GetGlobalTagsUsingCRN(meta, *endpointGateway.CRN, "", isUserTagType)
if err != nil {
log.Printf(
"Error on get of VPE (%s) tags: %s", d.Id(), err)
}
endpointGatewayOutput[isVirtualEndpointGatewayTags] = tags
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *endpointGateway.CRN, "", isAccessTagType)
if err != nil {
log.Printf(
"Error on get of VPE (%s) access tags: %s", d.Id(), err)
}
endpointGatewayOutput[isVirtualEndpointGatewayAccessTags] = accesstags
endpointGateways = append(endpointGateways, endpointGatewayOutput)
}
d.SetId(dataSourceIBMISEndpointGatewaysCheckID(d))
d.Set(isVirtualEndpointGateways, endpointGateways)
return nil
}
// dataSourceIBMISEndpointGatewaysCheckID returns a reasonable ID for dns zones list.
func dataSourceIBMISEndpointGatewaysCheckID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func flattenDataSourceIPs(ipsList []vpcv1.ReservedIPReference) interface{} {
ipsListOutput := make([]interface{}, 0)
for _, item := range ipsList {
ips := make(map[string]interface{}, 0)
ips[isVirtualEndpointGatewayIPsID] = *item.ID
ips[isVirtualEndpointGatewayIPsName] = *item.Name
ips[isVirtualEndpointGatewayIPsResourceType] = *item.ResourceType
ipsListOutput = append(ipsListOutput, ips)
}
return ipsListOutput
}