-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_is_vpc_default_routing_table.go
187 lines (176 loc) · 6 KB
/
data_source_ibm_is_vpc_default_routing_table.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isDefaultRoutingTableID = "default_routing_table"
isDefaultRoutingTableHref = "href"
isDefaultRoutingTableName = "name"
isDefaultRoutingTableResourceType = "resource_type"
isDefaultRoutingTableCreatedAt = "created_at"
isDefaultRoutingTableLifecycleState = "lifecycle_state"
isDefaultRoutingTableRoutesList = "routes"
isDefaultRoutingTableSubnetsList = "subnets"
isDefaultRTVpcID = "vpc"
isDefaultRTDirectLinkIngress = "route_direct_link_ingress"
isDefaultRTInternetIngress = "route_internet_ingress"
isDefaultRTTransitGatewayIngress = "route_transit_gateway_ingress"
isDefaultRTVPCZoneIngress = "route_vpc_zone_ingress"
isDefaultRTDefault = "is_default"
)
func DataSourceIBMISVPCDefaultRoutingTable() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISVPCDefaultRoutingTableGet,
Schema: map[string]*schema.Schema{
isDefaultRTVpcID: {
Type: schema.TypeString,
Required: true,
Description: "VPC identifier",
},
isDefaultRoutingTableID: {
Type: schema.TypeString,
Computed: true,
Description: "Default Routing Table ID",
},
isDefaultRoutingTableHref: {
Type: schema.TypeString,
Computed: true,
Description: "Default Routing table Href",
},
isDefaultRoutingTableName: {
Type: schema.TypeString,
Computed: true,
Description: "Default Routing table Name",
},
isDefaultRoutingTableResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "Default Routing table Resource Type",
},
isDefaultRoutingTableCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "Default Routing table Created At",
},
isDefaultRoutingTableLifecycleState: {
Type: schema.TypeString,
Computed: true,
Description: "Default Routing table Lifecycle State",
},
isDefaultRTDirectLinkIngress: {
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.",
},
isDefaultRTInternetIngress: {
Type: schema.TypeBool,
Computed: true,
Description: "If set to true, this routing table will be used to route traffic that originates from the internet. For this to succeed, the VPC must not already have a routing table with this property set to true.",
},
isDefaultRTTransitGatewayIngress: {
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.",
},
isDefaultRTVPCZoneIngress: {
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.",
},
isDefaultRTDefault: {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this is the default routing table for this VPC",
},
isDefaultRoutingTableRoutesList: {
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",
},
},
},
},
isDefaultRoutingTableSubnetsList: {
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 dataSourceIBMISVPCDefaultRoutingTableGet(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
vpcID := d.Get(isDefaultRTVpcID).(string)
getVpcDefaultRoutingTableOptions := sess.NewGetVPCDefaultRoutingTableOptions(vpcID)
result, detail, err := sess.GetVPCDefaultRoutingTable(getVpcDefaultRoutingTableOptions)
if err != nil || result == nil {
log.Printf("Error reading details of VPC Default Routing Table:%s", detail)
return err
}
d.Set(isDefaultRoutingTableID, *result.ID)
d.Set(isDefaultRoutingTableHref, *result.Href)
d.Set(isDefaultRoutingTableName, *result.Name)
d.Set(isDefaultRoutingTableResourceType, *result.ResourceType)
createdAt := *result.CreatedAt
d.Set(isDefaultRoutingTableCreatedAt, createdAt.String())
d.Set(isDefaultRoutingTableLifecycleState, *result.LifecycleState)
d.Set(isDefaultRTDirectLinkIngress, *result.RouteDirectLinkIngress)
d.Set(isDefaultRTInternetIngress, *result.RouteInternetIngress)
d.Set(isDefaultRTTransitGatewayIngress, *result.RouteTransitGatewayIngress)
d.Set(isDefaultRTVPCZoneIngress, *result.RouteVPCZoneIngress)
d.Set(isDefaultRTDefault, *result.IsDefault)
subnetsInfo := make([]map[string]interface{}, 0)
for _, subnet := range result.Subnets {
if subnet.Name != nil && subnet.ID != nil {
l := map[string]interface{}{
"name": *subnet.Name,
"id": *subnet.ID,
}
subnetsInfo = append(subnetsInfo, l)
}
}
d.Set(isDefaultRoutingTableSubnetsList, subnetsInfo)
routesInfo := make([]map[string]interface{}, 0)
for _, route := range result.Routes {
if route.Name != nil && route.ID != nil {
k := map[string]interface{}{
"name": *route.Name,
"id": *route.ID,
}
routesInfo = append(routesInfo, k)
}
}
d.Set(isDefaultRoutingTableRoutesList, routesInfo)
d.Set(isDefaultRTVpcID, vpcID)
d.SetId(*result.ID)
return nil
}