-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_vpc_routing_table_routes.go
360 lines (343 loc) · 11.6 KB
/
data_source_ibm_is_vpc_routing_table_routes.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright IBM Corp. 2017, 2022 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 (
isRoutingTableRouteID = "route_id"
isRoutingTableRouteHref = "href"
isRoutingTableRouteName = "name"
isRoutingTableRouteCreatedAt = "created_at"
isRoutingTableRouteLifecycleState = "lifecycle_state"
isRoutingTableRouteAction = "action"
isRoutingTableRouteDestination = "destination"
isRoutingTableRouteNexthop = "nexthop"
isRoutingTableRouteZoneName = "zone"
isRoutingTableRouteVpcID = "vpc"
isRouteTableID = "routing_table"
isRoutingTableRoutes = "routes"
)
func DataSourceIBMISVPCRoutingTableRoutes() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISVPCRoutingTableRoutesList,
Schema: map[string]*schema.Schema{
isRoutingTableRouteVpcID: {
Type: schema.TypeString,
Required: true,
Description: "VPC identifier",
},
isRouteTableID: {
Type: schema.TypeString,
Required: true,
Description: "Routing table identifier",
},
isRoutingTableRoutes: {
Type: schema.TypeList,
Description: "Collection of Routing Table Routes",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isRoutingTableRouteID: {
Type: schema.TypeString,
Computed: true,
Description: "Routing Table Route ID",
},
isRoutingTableRouteHref: {
Type: schema.TypeString,
Computed: true,
Description: "Routing Table Route Href",
},
isRoutingTableRouteName: {
Type: schema.TypeString,
Computed: true,
Description: "Routing Table Route Name",
},
isRoutingTableRouteCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "Routing Table Route Created At",
},
"creator": {
Type: schema.TypeList,
Computed: true,
Description: "If present, the resource that created the route. Routes with this property present cannot bedirectly deleted. All routes with an `origin` of `learned` or `service` will have thisproperty set, and future `origin` values may also have this property set.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The VPN gateway's CRN.",
},
"deleted": {
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The VPN gateway's canonical URL.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this VPN gateway.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this VPN gateway.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
isRoutingTableRouteLifecycleState: {
Type: schema.TypeString,
Computed: true,
Description: "Routing Table Route Lifecycle State",
},
isRoutingTableRouteAction: {
Type: schema.TypeString,
Computed: true,
Description: "Routing Table Route Action",
},
isRoutingTableRouteDestination: {
Type: schema.TypeString,
Computed: true,
Description: "Routing Table Route Destination",
},
isRoutingTableRouteNexthop: {
Type: schema.TypeString,
Computed: true,
Description: "Routing Table Route Nexthop Address or VPN Gateway Connection ID",
},
"origin": {
Type: schema.TypeString,
Computed: true,
Description: "The origin of this route:- `service`: route was directly created by a service- `user`: route was directly created by a userThe enumerated values for this property are expected to expand in the future. When processing this property, check for and log unknown values. Optionally halt processing and surface the error, or bypass the route on which the unexpected property value was encountered.",
},
"priority": {
Type: schema.TypeInt,
Computed: true,
Description: "The route's priority. Smaller values have higher priority. If a routing table contains routes with the same destination, the route with the highest priority (smallest value) is selected.",
},
isRoutingTableRouteZoneName: {
Type: schema.TypeString,
Computed: true,
Description: "Routing Table Route Zone Name",
},
},
},
},
},
}
}
func dataSourceIBMISVPCRoutingTableRoutesList(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
vpcID := d.Get(isRoutingTableRouteVpcID).(string)
routingTableID := d.Get(isRouteTableID).(string)
start := ""
allrecs := []vpcv1.Route{}
for {
listVpcRoutingTablesRoutesOptions := sess.NewListVPCRoutingTableRoutesOptions(vpcID, routingTableID)
if start != "" {
listVpcRoutingTablesRoutesOptions.Start = &start
}
result, detail, err := sess.ListVPCRoutingTableRoutes(listVpcRoutingTablesRoutesOptions)
if err != nil {
log.Printf("Error reading list of VPC Routing Table Routes:%s\n%s", err, detail)
return err
}
start = flex.GetNext(result.Next)
allrecs = append(allrecs, result.Routes...)
if start == "" {
break
}
}
vpcRoutingTableRoutes := make([]map[string]interface{}, 0)
for _, instance := range allrecs {
route := map[string]interface{}{}
if instance.ID != nil {
route[isRoutingTableRouteID] = *instance.ID
}
if instance.Href != nil {
route[isRoutingTableRouteHref] = *instance.Href
}
if instance.Name != nil {
route[isRoutingTableRouteName] = *instance.Name
}
if instance.CreatedAt != nil {
route[isRoutingTableRouteCreatedAt] = (*instance.CreatedAt).String()
}
// creator changes
creator := []map[string]interface{}{}
if instance.Creator != nil {
mm, err := dataSourceIBMIsRouteCreatorToMap(instance.Creator)
if err != nil {
log.Printf("Error reading list of VPC Routing Table Routes' creator:%s", err)
return err
}
creator = append(creator, mm)
}
route["creator"] = creator
if instance.LifecycleState != nil {
route[isRoutingTableRouteLifecycleState] = *instance.LifecycleState
}
if instance.Destination != nil {
route[isRoutingTableRouteDestination] = *instance.Destination
}
if instance.Zone != nil && instance.Zone.Name != nil {
route[isRoutingTableRouteZoneName] = *instance.Zone.Name
}
if instance.NextHop != nil {
nexthop := *instance.NextHop.(*vpcv1.RouteNextHop)
if nexthop.Address != nil {
route[isRoutingTableRouteNexthop] = *nexthop.Address
} else {
route[isRoutingTableRouteNexthop] = *nexthop.ID
}
}
//orgin
if instance.Origin != nil {
route["origin"] = *instance.Origin
}
// priority
if instance.Priority != nil {
route["priority"] = *instance.Priority
}
vpcRoutingTableRoutes = append(vpcRoutingTableRoutes, route)
}
d.SetId(dataSourceIBMISVPCRoutingTableRoutesID(d))
d.Set(isRoutingTableRouteVpcID, vpcID)
d.Set(isRouteTableID, routingTableID)
d.Set(isRoutingTableRoutes, vpcRoutingTableRoutes)
return nil
}
// dataSourceIBMISVPCRoutingTablesID returns a reasonable ID for dns zones list.
func dataSourceIBMISVPCRoutingTableRoutesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceIBMIsRouteCreatorToMap(model vpcv1.RouteCreatorIntf) (map[string]interface{}, error) {
if _, ok := model.(*vpcv1.RouteCreatorVPNGatewayReference); ok {
return DataSourceIBMIsRouteCreatorVPNGatewayReferenceToMap(model.(*vpcv1.RouteCreatorVPNGatewayReference))
} else if _, ok := model.(*vpcv1.RouteCreatorVPNServerReference); ok {
return DataSourceIBMIsRouteCreatorVPNServerReferenceToMap(model.(*vpcv1.RouteCreatorVPNServerReference))
} else if _, ok := model.(*vpcv1.RouteCreator); ok {
modelMap := make(map[string]interface{})
model := model.(*vpcv1.RouteCreator)
if model.CRN != nil {
modelMap["crn"] = *model.CRN
}
if model.Deleted != nil {
deletedMap, err := DataSourceIBMIsRouteVPNGatewayReferenceDeletedToMap(model.Deleted)
if err != nil {
return modelMap, err
}
modelMap["deleted"] = []map[string]interface{}{deletedMap}
}
if model.Href != nil {
modelMap["href"] = *model.Href
}
if model.ID != nil {
modelMap["id"] = *model.ID
}
if model.Name != nil {
modelMap["name"] = *model.Name
}
if model.ResourceType != nil {
modelMap["resource_type"] = *model.ResourceType
}
return modelMap, nil
} else {
return nil, fmt.Errorf("[Error] unrecognized vpcv1.RouteCreatorIntf subtype encountered")
}
}
func DataSourceIBMIsRouteCreatorVPNGatewayReferenceToMap(model *vpcv1.RouteCreatorVPNGatewayReference) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.CRN != nil {
modelMap["crn"] = *model.CRN
}
if model.Deleted != nil {
deletedMap, err := DataSourceIBMIsRouteVPNGatewayReferenceDeletedToMap(model.Deleted)
if err != nil {
return modelMap, err
}
modelMap["deleted"] = []map[string]interface{}{deletedMap}
}
if model.Href != nil {
modelMap["href"] = *model.Href
}
if model.ID != nil {
modelMap["id"] = *model.ID
}
if model.Name != nil {
modelMap["name"] = *model.Name
}
if model.ResourceType != nil {
modelMap["resource_type"] = *model.ResourceType
}
return modelMap, nil
}
func DataSourceIBMIsRouteCreatorVPNServerReferenceToMap(model *vpcv1.RouteCreatorVPNServerReference) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.CRN != nil {
modelMap["crn"] = *model.CRN
}
if model.Deleted != nil {
deletedMap, err := DataSourceIBMIsRouteVPNServerReferenceDeletedToMap(model.Deleted)
if err != nil {
return modelMap, err
}
modelMap["deleted"] = []map[string]interface{}{deletedMap}
}
if model.Href != nil {
modelMap["href"] = *model.Href
}
if model.ID != nil {
modelMap["id"] = *model.ID
}
if model.Name != nil {
modelMap["name"] = *model.Name
}
if model.ResourceType != nil {
modelMap["resource_type"] = *model.ResourceType
}
return modelMap, nil
}
func DataSourceIBMIsRouteVPNGatewayReferenceDeletedToMap(model *vpcv1.VPNGatewayReferenceDeleted) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.MoreInfo != nil {
modelMap["more_info"] = *model.MoreInfo
}
return modelMap, nil
}
func DataSourceIBMIsRouteVPNServerReferenceDeletedToMap(model *vpcv1.VPNServerReferenceDeleted) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.MoreInfo != nil {
modelMap["more_info"] = *model.MoreInfo
}
return modelMap, nil
}