-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_is_vpc_routing_table_routes.go
175 lines (164 loc) · 5.27 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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"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",
},
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",
},
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()
}
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
}
}
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()
}