-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_vpc_routing_tables.go
239 lines (224 loc) · 7.17 KB
/
data_source_ibm_is_vpc_routing_tables.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
//"encoding/json"
"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 (
isRoutingTableID = "routing_table"
isRoutingTableHref = "href"
isRoutingTableName = "name"
isRoutingTableResourceType = "resource_type"
isRoutingTableCreatedAt = "created_at"
isRoutingTableLifecycleState = "lifecycle_state"
isRoutingTableRoutesList = "routes"
isRoutingTableSubnetsList = "subnets"
isRoutingTables = "routing_tables"
isVpcID = "vpc"
isRoutingTableDirectLinkIngress = "route_direct_link_ingress"
isRoutingTableTransitGatewayIngress = "route_transit_gateway_ingress"
isRoutingTableVPCZoneIngress = "route_vpc_zone_ingress"
isRoutingTableDefault = "is_default"
)
func DataSourceIBMISVPCRoutingTables() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISVPCRoutingTablesList,
Schema: map[string]*schema.Schema{
isVpcID: {
Type: schema.TypeString,
Required: true,
Description: "VPC identifier",
},
isRoutingTables: {
Type: schema.TypeList,
Description: "Collection of Routing tables",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isRoutingTableID: {
Type: schema.TypeString,
Computed: true,
Description: "Routing Table ID",
},
isRoutingTableHref: {
Type: schema.TypeString,
Computed: true,
Description: "Routing table Href",
},
isRoutingTableName: {
Type: schema.TypeString,
Computed: true,
Description: "Routing table Name",
},
isRoutingTableResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "Routing table Resource Type",
},
isRoutingTableCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "Routing table Created At",
},
isRoutingTableLifecycleState: {
Type: schema.TypeString,
Computed: true,
Description: "Routing table Lifecycle State",
},
isRoutingTableDirectLinkIngress: {
Type: schema.TypeBool,
Computed: true,
Description: "If set to true, this routing table will be used to route traffic that originates from Direct Link to this VPC.",
},
isRoutingTableTransitGatewayIngress: {
Type: schema.TypeBool,
Computed: true,
Description: "If set to true, this routing table will be used to route traffic that originates from Transit Gateway to this VPC.",
},
isRoutingTableVPCZoneIngress: {
Type: schema.TypeBool,
Computed: true,
Description: "If set to true, this routing table will be used to route traffic that originates from subnets in other zones in this VPC.",
},
isRoutingTableDefault: {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this is the default routing table for this VPC",
},
isRoutingTableRoutesList: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Route name",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Route ID",
},
},
},
},
isRoutingTableSubnetsList: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Subnet name",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Subnet ID",
},
},
},
},
},
},
},
},
}
}
func dataSourceIBMISVPCRoutingTablesList(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
vpcID := d.Get(isVpcID).(string)
start := ""
allrecs := []vpcv1.RoutingTable{}
for {
listOptions := sess.NewListVPCRoutingTablesOptions(vpcID)
if start != "" {
listOptions.Start = &start
}
result, detail, err := sess.ListVPCRoutingTables(listOptions)
if err != nil {
log.Printf("Error reading list of VPC Routing Tables:%s\n%s", err, detail)
return err
}
start = flex.GetNext(result.Next)
allrecs = append(allrecs, result.RoutingTables...)
if start == "" {
break
}
}
vpcRoutingTables := make([]map[string]interface{}, 0)
for _, routingTable := range allrecs {
rtable := map[string]interface{}{}
if routingTable.ID != nil {
rtable[isRoutingTableID] = *routingTable.ID
}
if routingTable.Href != nil {
rtable[isRoutingTableHref] = *routingTable.Href
}
if routingTable.Name != nil {
rtable[isRoutingTableName] = *routingTable.Name
}
if routingTable.ResourceType != nil {
rtable[isRoutingTableResourceType] = *routingTable.ResourceType
}
if routingTable.CreatedAt != nil {
rtable[isRoutingTableCreatedAt] = (*routingTable.CreatedAt).String()
}
if routingTable.LifecycleState != nil {
rtable[isRoutingTableLifecycleState] = *routingTable.LifecycleState
}
if routingTable.RouteDirectLinkIngress != nil {
rtable[isRoutingTableDirectLinkIngress] = *routingTable.RouteDirectLinkIngress
}
if routingTable.RouteTransitGatewayIngress != nil {
rtable[isRoutingTableTransitGatewayIngress] = *routingTable.RouteTransitGatewayIngress
}
if routingTable.RouteVPCZoneIngress != nil {
rtable[isRoutingTableVPCZoneIngress] = *routingTable.RouteVPCZoneIngress
}
if routingTable.IsDefault != nil {
rtable[isRoutingTableDefault] = *routingTable.IsDefault
}
subnetsInfo := make([]map[string]interface{}, 0)
for _, subnet := range routingTable.Subnets {
if subnet.Name != nil && subnet.ID != nil {
l := map[string]interface{}{
"name": *subnet.Name,
"id": *subnet.ID,
}
subnetsInfo = append(subnetsInfo, l)
}
}
rtable[isRoutingTableSubnetsList] = subnetsInfo
routesInfo := make([]map[string]interface{}, 0)
for _, route := range routingTable.Routes {
if route.Name != nil && route.ID != nil {
k := map[string]interface{}{
"name": *route.Name,
"id": *route.ID,
}
routesInfo = append(routesInfo, k)
}
}
rtable[isRoutingTableRoutesList] = routesInfo
vpcRoutingTables = append(vpcRoutingTables, rtable)
}
d.SetId(dataSourceIBMISVPCRoutingTablesID(d))
d.Set(isVpcID, vpcID)
d.Set(isRoutingTables, vpcRoutingTables)
return nil
}
// dataSourceIBMISVPCRoutingTablesID returns a reasonable ID for dns zones list.
func dataSourceIBMISVPCRoutingTablesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}