/
data_source_ibm_is_virtual_endpoint_gateway_ips.go
136 lines (128 loc) · 4.2 KB
/
data_source_ibm_is_virtual_endpoint_gateway_ips.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
// 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"
)
func DataSourceIBMISEndpointGatewayIPs() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISEndpointGatewayIPsRead,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
isVirtualEndpointGatewayID: {
Type: schema.TypeString,
Required: true,
},
isVirtualEndpointGatewayIPs: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isVirtualEndpointGatewayIPID: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway IP id",
},
isVirtualEndpointGatewayIPName: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway IP name",
},
isVirtualEndpointGatewayIPResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway IP resource type",
},
isVirtualEndpointGatewayIPCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway IP created date and time",
},
isVirtualEndpointGatewayIPAutoDelete: {
Type: schema.TypeBool,
Computed: true,
Description: "Endpoint gateway IP auto delete",
},
isVirtualEndpointGatewayIPAddress: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway IP address",
},
isVirtualEndpointGatewayIPTarget: {
Type: schema.TypeList,
Computed: true,
Description: "Endpoint gateway detail",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isVirtualEndpointGatewayIPTargetID: {
Type: schema.TypeString,
Computed: true,
Description: "The IPs target id",
},
isVirtualEndpointGatewayIPTargetName: {
Type: schema.TypeString,
Computed: true,
Description: "The IPs target name",
},
isVirtualEndpointGatewayIPTargetResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway resource type",
},
},
},
},
},
},
},
},
}
}
func dataSourceIBMISEndpointGatewayIPsRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
gatewayID := d.Get(isVirtualEndpointGatewayID).(string)
start := ""
allrecs := []vpcv1.ReservedIP{}
for {
options := sess.NewListEndpointGatewayIpsOptions(gatewayID)
if start != "" {
options.Start = &start
}
result, response, err := sess.ListEndpointGatewayIps(options)
if err != nil {
return fmt.Errorf("[ERROR] Error fetching endpoint gateway ips %s\n%s", err, response)
}
start = flex.GetNext(result.Next)
allrecs = append(allrecs, result.Ips...)
if start == "" {
break
}
}
endpointGatewayIPs := []map[string]interface{}{}
for _, ip := range allrecs {
ipsOutput := map[string]interface{}{}
ipsOutput[isVirtualEndpointGatewayIPID] = *ip.ID
ipsOutput[isVirtualEndpointGatewayIPName] = *ip.Name
ipsOutput[isVirtualEndpointGatewayIPCreatedAt] = (*ip.CreatedAt).String()
ipsOutput[isVirtualEndpointGatewayIPAddress] = *ip.Address
ipsOutput[isVirtualEndpointGatewayIPAutoDelete] = *ip.AutoDelete
ipsOutput[isVirtualEndpointGatewayIPResourceType] = *ip.ResourceType
ipsOutput[isVirtualEndpointGatewayIPTarget] =
flattenEndpointGatewayIPTarget(ip.Target.(*vpcv1.ReservedIPTarget))
endpointGatewayIPs = append(endpointGatewayIPs, ipsOutput)
}
d.SetId(dataSourceIBMISEndpointGatewayIPsCheckID(d))
d.Set(isVirtualEndpointGatewayIPs, endpointGatewayIPs)
return nil
}
// dataSourceIBMISEndpointGatewayIPsCheckID returns a reasonable ID for dns zones list.
func dataSourceIBMISEndpointGatewayIPsCheckID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}