/
data_source_ibm_is_virtual_endpoint_gateway.go
201 lines (191 loc) · 6.47 KB
/
data_source_ibm_is_virtual_endpoint_gateway.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"log"
"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"
)
func DataSourceIBMISEndpointGateway() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISEndpointGatewayRead,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
isVirtualEndpointGatewayName: {
Type: schema.TypeString,
Required: true,
Description: "Endpoint gateway name",
},
isVirtualEndpointGatewayResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway resource type",
},
isVirtualEndpointGatewayCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this Endpoint gateway",
},
isVirtualEndpointGatewayResourceGroupID: {
Type: schema.TypeString,
Computed: true,
Description: "The resource group id",
},
isVirtualEndpointGatewayCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway created date and time",
},
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",
},
isVirtualEndpointGatewayHealthState: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway health state",
},
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",
},
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.",
},
isVirtualEndpointGatewayIPs: {
Type: schema.TypeList,
Computed: true,
Description: "Endpoint gateway IPs",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isVirtualEndpointGatewayIPsID: {
Type: schema.TypeString,
Computed: true,
Description: "The IPs id",
},
isVirtualEndpointGatewayIPsName: {
Type: schema.TypeString,
Computed: true,
Description: "The IPs name",
},
isVirtualEndpointGatewayIPsResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway IP resource type",
},
isVirtualEndpointGatewayIPsAddress: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway IP Address",
},
},
},
},
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",
},
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 dataSourceIBMISEndpointGatewayRead(
d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
name := d.Get(isVirtualEndpointGatewayName).(string)
options := sess.NewListEndpointGatewaysOptions()
options.Name = &name
results, response, err := sess.ListEndpointGateways(options)
if err != nil {
return fmt.Errorf("[ERROR] Error fetching endpoint gateways %s\n%s", err, response)
}
allrecs := results.EndpointGateways
if len(allrecs) == 0 {
return fmt.Errorf("[ERROR] No Virtual Endpoints Gateway found with given name %s", name)
}
result := allrecs[0]
d.SetId(*result.ID)
d.Set(isVirtualEndpointGatewayName, result.Name)
d.Set(isVirtualEndpointGatewayAllowDnsResolutionBinding, result.AllowDnsResolutionBinding)
d.Set(isVirtualEndpointGatewayCRN, result.CRN)
d.Set(isVirtualEndpointGatewayHealthState, result.HealthState)
d.Set(isVirtualEndpointGatewayCreatedAt, result.CreatedAt.String())
d.Set(isVirtualEndpointGatewayLifecycleState, result.LifecycleState)
d.Set(isVirtualEndpointGatewayResourceType, result.ResourceType)
d.Set(isVirtualEndpointGatewayIPs, flattenIPs(result.Ips))
d.Set(isVirtualEndpointGatewayResourceGroupID, result.ResourceGroup.ID)
d.Set(isVirtualEndpointGatewayTarget, flattenEndpointGatewayTarget(
result.Target.(*vpcv1.EndpointGatewayTarget)))
d.Set(isVirtualEndpointGatewayVpcID, result.VPC.ID)
if len(result.ServiceEndpoints) > 0 {
d.Set(isVirtualEndpointGatewayServiceEndpoints, result.ServiceEndpoints)
}
tags, err := flex.GetGlobalTagsUsingCRN(meta, *result.CRN, "", isUserTagType)
if err != nil {
log.Printf(
"Error on get of VPE (%s) tags: %s", d.Id(), err)
}
d.Set(isVirtualEndpointGatewayTags, tags)
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *result.CRN, "", isAccessTagType)
if err != nil {
log.Printf(
"Error on get of VPE (%s) access tags: %s", d.Id(), err)
}
d.Set(isVirtualEndpointGatewayAccessTags, accesstags)
if result.SecurityGroups != nil {
d.Set(isVirtualEndpointGatewaySecurityGroups, flattenDataSourceSecurityGroups(result.SecurityGroups))
}
return nil
}