forked from hashicorp/terraform-provider-aws
/
data_source_aws_route.go
180 lines (158 loc) · 4.64 KB
/
data_source_aws_route.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
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceAwsRoute() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsRouteRead,
Schema: map[string]*schema.Schema{
"route_table_id": {
Type: schema.TypeString,
Required: true,
},
"destination_cidr_block": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"destination_ipv6_cidr_block": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"egress_only_gateway_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"gateway_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"instance_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"nat_gateway_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"vpc_peering_connection_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"network_interface_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func dataSourceAwsRouteRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
req := &ec2.DescribeRouteTablesInput{}
rtbId := d.Get("route_table_id")
cidr := d.Get("destination_cidr_block")
ipv6Cidr := d.Get("destination_ipv6_cidr_block")
req.Filters = buildEC2AttributeFilterList(
map[string]string{
"route-table-id": rtbId.(string),
"route.destination-cidr-block": cidr.(string),
"route.destination-ipv6-cidr-block": ipv6Cidr.(string),
},
)
log.Printf("[DEBUG] Reading Route Table: %s", req)
resp, err := conn.DescribeRouteTables(req)
if err != nil {
return err
}
if resp == nil || len(resp.RouteTables) == 0 {
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
}
if len(resp.RouteTables) > 1 {
return fmt.Errorf("Your query returned more than one route table. Please change your search criteria and try again.")
}
results := getRoutes(resp.RouteTables[0], d)
if len(results) == 0 {
return fmt.Errorf("No routes matching supplied arguments found in table(s)")
}
if len(results) > 1 {
return fmt.Errorf("Multiple routes matched; use additional constraints to reduce matches to a single route")
}
route := results[0]
d.SetId(resourceAwsRouteID(d, route)) // using function from "resource_aws_route.go"
d.Set("destination_cidr_block", route.DestinationCidrBlock)
d.Set("destination_ipv6_cidr_block", route.DestinationIpv6CidrBlock)
d.Set("egress_only_gateway_id", route.EgressOnlyInternetGatewayId)
d.Set("gateway_id", route.GatewayId)
d.Set("instance_id", route.InstanceId)
d.Set("nat_gateway_id", route.NatGatewayId)
d.Set("vpc_peering_connection_id", route.VpcPeeringConnectionId)
d.Set("network_interface_id", route.NetworkInterfaceId)
return nil
}
func getRoutes(table *ec2.RouteTable, d *schema.ResourceData) []*ec2.Route {
ec2Routes := table.Routes
routes := make([]*ec2.Route, 0, len(ec2Routes))
// Loop through the routes and add them to the set
for _, r := range ec2Routes {
if r.Origin != nil && *r.Origin == "EnableVgwRoutePropagation" {
continue
}
if r.DestinationPrefixListId != nil {
// Skipping because VPC endpoint routes are handled separately
// See aws_vpc_endpoint
continue
}
if v, ok := d.GetOk("destination_cidr_block"); ok {
if r.DestinationCidrBlock == nil || *r.DestinationCidrBlock != v.(string) {
continue
}
}
if v, ok := d.GetOk("destination_ipv6_cidr_block"); ok {
if r.DestinationIpv6CidrBlock == nil || *r.DestinationIpv6CidrBlock != v.(string) {
continue
}
}
if v, ok := d.GetOk("egress_only_gateway_id"); ok {
if r.EgressOnlyInternetGatewayId == nil || *r.EgressOnlyInternetGatewayId != v.(string) {
continue
}
}
if v, ok := d.GetOk("gateway_id"); ok {
if r.GatewayId == nil || *r.GatewayId != v.(string) {
continue
}
}
if v, ok := d.GetOk("instance_id"); ok {
if r.InstanceId == nil || *r.InstanceId != v.(string) {
continue
}
}
if v, ok := d.GetOk("nat_gateway_id"); ok {
if r.NatGatewayId == nil || *r.NatGatewayId != v.(string) {
continue
}
}
if v, ok := d.GetOk("vpc_peering_connection_id"); ok {
if r.VpcPeeringConnectionId == nil || *r.VpcPeeringConnectionId != v.(string) {
continue
}
}
if v, ok := d.GetOk("network_interface_id"); ok {
if r.NetworkInterfaceId == nil || *r.NetworkInterfaceId != v.(string) {
continue
}
}
routes = append(routes, r)
}
return routes
}