/
data_source_ibm_dl_route_reports.go
334 lines (305 loc) · 10.4 KB
/
data_source_ibm_dl_route_reports.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
// Copyright IBM Corp. 2022 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package directlink
import (
"log"
"time"
"github.com/IBM/networking-go-sdk/directlinkv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMDLRouteReports() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMDLRouteReportsRead,
Schema: map[string]*schema.Schema{
dlGatewayId: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The Direct Link gateway identifier",
},
dlRouteReports: {
Type: schema.TypeList,
Description: "List of route reports for a gateway",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
dlId: {
Type: schema.TypeString,
Computed: true,
Description: "Id of the route report",
},
dlAdvertisedRoutes: {
Type: schema.TypeList,
Description: "List of connection prefixes advertised to the on-prem network",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
dlAsPath: {
Type: schema.TypeString,
Computed: true,
Description: "The BGP AS path of the route",
},
dlPrefix: {
Type: schema.TypeString,
Computed: true,
Description: "Prefix for advertised routes",
},
},
},
},
dlGatewayRoutes: {
Type: schema.TypeList,
Description: "List of gateway routes",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
dlPrefix: {
Type: schema.TypeString,
Computed: true,
Description: "Prefix for gateway routes",
},
},
},
},
dlOnPremRoutes: {
Type: schema.TypeList,
Description: "List of onprem routes",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
dlAsPath: {
Type: schema.TypeString,
Computed: true,
Description: "The BGP AS path of the route",
},
dlPrefix: {
Type: schema.TypeString,
Computed: true,
Description: "Prefix for onprem routes",
},
dlRouteReportNextHop: {
Type: schema.TypeString,
Computed: true,
Description: "Next Hop address",
},
},
},
},
dlOverlappingRoutes: {
Type: schema.TypeList,
Description: "List of overlapping routes",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
dlRoutes: {
Type: schema.TypeList,
Computed: true,
Description: "overlapping routes",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
dlPrefix: {
Type: schema.TypeString,
Computed: true,
Description: "Prefix for overlapping routes",
},
dlType: {
Type: schema.TypeString,
Computed: true,
Description: "Type of route",
},
dlVirtualConnectionId: {
Type: schema.TypeString,
Computed: true,
Description: "Virtual connection ID",
},
},
},
},
},
},
},
dlVirtualConnectionRoutes: {
Type: schema.TypeList,
Computed: true,
Description: "Virtual Connection Routes",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
dlVirtualConnectionId: {
Type: schema.TypeString,
Computed: true,
Description: "Virtual connection ID",
},
dlVirtualConnectionType: {
Type: schema.TypeString,
Computed: true,
Description: "Virtual connection type",
},
dlVirtualConnectionName: {
Type: schema.TypeString,
Computed: true,
Description: "Virtual connection name",
},
dlRoutes: {
Type: schema.TypeList,
Computed: true,
Description: "Virtual connection routes",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
dlPrefix: {
Type: schema.TypeString,
Computed: true,
Description: "Prefix for virtual connection routes",
},
dlActive: {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether the route is the preferred path of the prefix",
},
dlLocalPreference: {
Type: schema.TypeString,
Computed: true,
Description: "The local preference of the route. This attribute can manipulate the chosen path on routes",
},
},
},
},
},
},
},
dlRouteReportStatus: {
Type: schema.TypeString,
Computed: true,
Description: "Route report status",
},
dlCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "The date and time report was created",
},
dlUpdatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "The date and time resource was created",
},
},
},
},
},
}
}
func dataSourceIBMDLRouteReportsRead(d *schema.ResourceData, meta interface{}) error {
directLink, err := directlinkClient(meta)
if err != nil {
return err
}
gatewayId := d.Get(dlGatewayId).(string)
listGatewayRouteReportsOptionsModel := &directlinkv1.ListGatewayRouteReportsOptions{GatewayID: &gatewayId}
routeReportsList, response, err := directLink.ListGatewayRouteReports(listGatewayRouteReportsOptionsModel)
if err != nil {
log.Println("[WARN] Error listing DL Route Reports", response, err)
return err
}
routeReports := make([]map[string]interface{}, 0)
for _, instance := range routeReportsList.RouteReports {
routeReport := map[string]interface{}{}
if instance.ID != nil {
routeReport[dlId] = *instance.ID
}
if instance.Status != nil {
routeReport[dlRouteReportStatus] = *instance.Status
}
// Build Advertised Routes
advRoutes := make([]map[string]interface{}, 0)
if instance.AdvertisedRoutes != nil {
for _, adRoute := range instance.AdvertisedRoutes {
route := map[string]interface{}{}
route[dlAsPath] = adRoute.AsPath
route[dlPrefix] = adRoute.Prefix
advRoutes = append(advRoutes, route)
}
}
log.Println("[Info] Length DL Route Reports advertised routes:", len(advRoutes))
routeReport[dlAdvertisedRoutes] = advRoutes
// Build Gateway Routes
gatewayRoutes := make([]map[string]interface{}, 0)
if instance.GatewayRoutes != nil {
for _, gatewayRoute := range instance.GatewayRoutes {
route := map[string]interface{}{}
route[dlPrefix] = gatewayRoute.Prefix
gatewayRoutes = append(gatewayRoutes, route)
}
}
log.Println("[INFO] length DL Gateway Routes", len(gatewayRoutes))
routeReport[dlGatewayRoutes] = gatewayRoutes
// Build onPrem Routes
onPremRoutes := make([]map[string]interface{}, 0)
if instance.OnPremRoutes != nil {
for _, onPremRoute := range instance.OnPremRoutes {
route := map[string]interface{}{}
route[dlPrefix] = onPremRoute.Prefix
route[dlAsPath] = onPremRoute.AsPath
route[dlRouteReportNextHop] = onPremRoute.NextHop
onPremRoutes = append(onPremRoutes, route)
}
}
log.Println("[INFO] length DL Onprem routes", len(onPremRoutes))
routeReport[dlOnPremRoutes] = onPremRoutes
// Build Overlapping Routes
overlappingRoutesCollection := make([]map[string]interface{}, 0)
if instance.OverlappingRoutes != nil && len(instance.OverlappingRoutes) > 0 {
for _, o := range instance.OverlappingRoutes {
overlappingRouteItem := map[string]interface{}{}
routes := make([]map[string]interface{}, 0)
for _, r := range o.Routes {
overlappingRoute := map[string]interface{}{}
route := r.(*directlinkv1.RouteReportOverlappingRoute)
overlappingRoute[dlPrefix] = route.Prefix
overlappingRoute[dlType] = route.Type
overlappingRoute[dlVirtualConnectionId] = route.VirtualConnectionID
routes = append(routes, overlappingRoute)
}
overlappingRouteItem[dlRoutes] = routes
overlappingRoutesCollection = append(overlappingRoutesCollection, overlappingRouteItem)
}
}
log.Println("[INFO] length DL overlapping routes", len(overlappingRoutesCollection))
routeReport[dlOverlappingRoutes] = overlappingRoutesCollection
// Build connection routes
virtualConnectionRoutes := make([]map[string]interface{}, 0)
if instance.VirtualConnectionRoutes != nil {
for _, c := range instance.VirtualConnectionRoutes {
conn := map[string]interface{}{}
conn[dlVirtualConnectionId] = c.VirtualConnectionID
conn[dlVirtualConnectionName] = c.VirtualConnectionName
conn[dlVirtualConnectionType] = c.VirtualConnectionType
connectionRoutes := make([]map[string]interface{}, 0)
for _, r := range c.Routes {
routes := map[string]interface{}{}
routes[dlPrefix] = r.Prefix
routes[dlLocalPreference] = r.LocalPreference
routes[dlActive] = r.Active
connectionRoutes = append(connectionRoutes, routes)
}
conn[dlRoutes] = connectionRoutes
virtualConnectionRoutes = append(virtualConnectionRoutes, conn)
}
}
log.Println("[INFO] length DL connection routes", len(virtualConnectionRoutes))
routeReport[dlVirtualConnectionRoutes] = virtualConnectionRoutes
// Add the created and updated dates
if instance.CreatedAt != nil {
routeReport[dlCreatedAt] = instance.CreatedAt.String()
}
if instance.UpdatedAt != nil {
routeReport[dlUpdatedAt] = instance.UpdatedAt.String()
}
routeReports = append(routeReports, routeReport)
}
d.Set(dlRouteReports, routeReports)
d.SetId(dataSourceIBMDirectLinkGatewayRouteReportsID(d))
return nil
}
// dataSourceIBMDirectLinkGatewayRouteReportsID returns a reasonable ID for a directlink gateways list.
func dataSourceIBMDirectLinkGatewayRouteReportsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}