github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_route_table.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/service/ec2" 9 "github.com/hashicorp/terraform/helper/schema" 10 ) 11 12 func dataSourceAwsRouteTable() *schema.Resource { 13 return &schema.Resource{ 14 Read: dataSourceAwsRouteTableRead, 15 16 Schema: map[string]*schema.Schema{ 17 "subnet_id": { 18 Type: schema.TypeString, 19 Optional: true, 20 Computed: true, 21 }, 22 "route_table_id": { 23 Type: schema.TypeString, 24 Optional: true, 25 Computed: true, 26 }, 27 "vpc_id": { 28 Type: schema.TypeString, 29 Optional: true, 30 Computed: true, 31 }, 32 "filter": ec2CustomFiltersSchema(), 33 "tags": tagsSchemaComputed(), 34 "routes": { 35 Type: schema.TypeList, 36 Computed: true, 37 Elem: &schema.Resource{ 38 Schema: map[string]*schema.Schema{ 39 "cidr_block": { 40 Type: schema.TypeString, 41 Computed: true, 42 }, 43 44 "ipv6_cidr_block": { 45 Type: schema.TypeString, 46 Computed: true, 47 }, 48 49 "egress_only_gateway_id": { 50 Type: schema.TypeString, 51 Computed: true, 52 }, 53 54 "gateway_id": { 55 Type: schema.TypeString, 56 Computed: true, 57 }, 58 59 "instance_id": { 60 Type: schema.TypeString, 61 Computed: true, 62 }, 63 64 "nat_gateway_id": { 65 Type: schema.TypeString, 66 Computed: true, 67 }, 68 69 "vpc_peering_connection_id": { 70 Type: schema.TypeString, 71 Computed: true, 72 }, 73 74 "network_interface_id": { 75 Type: schema.TypeString, 76 Computed: true, 77 }, 78 }, 79 }, 80 }, 81 "associations": { 82 Type: schema.TypeList, 83 Computed: true, 84 Elem: &schema.Resource{ 85 Schema: map[string]*schema.Schema{ 86 "route_table_association_id": { 87 Type: schema.TypeString, 88 Computed: true, 89 }, 90 91 "route_table_id": { 92 Type: schema.TypeString, 93 Computed: true, 94 }, 95 96 "subnet_id": { 97 Type: schema.TypeString, 98 Computed: true, 99 }, 100 101 "main": { 102 Type: schema.TypeBool, 103 Computed: true, 104 }, 105 }, 106 }, 107 }, 108 }, 109 } 110 } 111 112 func dataSourceAwsRouteTableRead(d *schema.ResourceData, meta interface{}) error { 113 conn := meta.(*AWSClient).ec2conn 114 req := &ec2.DescribeRouteTablesInput{} 115 vpcId, vpcIdOk := d.GetOk("vpc_id") 116 subnetId, subnetIdOk := d.GetOk("subnet_id") 117 rtbId, rtbOk := d.GetOk("route_table_id") 118 tags, tagsOk := d.GetOk("tags") 119 filter, filterOk := d.GetOk("filter") 120 121 if !vpcIdOk && !subnetIdOk && !tagsOk && !filterOk && !rtbOk { 122 return fmt.Errorf("One of route_table_id, vpc_id, subnet_id, filters, or tags must be assigned") 123 } 124 req.Filters = buildEC2AttributeFilterList( 125 map[string]string{ 126 "route-table-id": rtbId.(string), 127 "vpc-id": vpcId.(string), 128 "association.subnet-id": subnetId.(string), 129 }, 130 ) 131 req.Filters = append(req.Filters, buildEC2TagFilterList( 132 tagsFromMap(tags.(map[string]interface{})), 133 )...) 134 req.Filters = append(req.Filters, buildEC2CustomFilterList( 135 filter.(*schema.Set), 136 )...) 137 138 log.Printf("[DEBUG] Describe Route Tables %v\n", req) 139 resp, err := conn.DescribeRouteTables(req) 140 if err != nil { 141 return err 142 } 143 if resp == nil || len(resp.RouteTables) == 0 { 144 return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.") 145 } 146 if len(resp.RouteTables) > 1 { 147 return fmt.Errorf("Multiple Route Table matched; use additional constraints to reduce matches to a single Route Table") 148 } 149 150 rt := resp.RouteTables[0] 151 152 d.SetId(aws.StringValue(rt.RouteTableId)) 153 d.Set("route_table_id", rt.RouteTableId) 154 d.Set("vpc_id", rt.VpcId) 155 d.Set("tags", tagsToMap(rt.Tags)) 156 if err := d.Set("routes", dataSourceRoutesRead(rt.Routes)); err != nil { 157 return err 158 } 159 160 if err := d.Set("associations", dataSourceAssociationsRead(rt.Associations)); err != nil { 161 return err 162 } 163 164 return nil 165 } 166 167 func dataSourceRoutesRead(ec2Routes []*ec2.Route) []map[string]interface{} { 168 routes := make([]map[string]interface{}, 0, len(ec2Routes)) 169 // Loop through the routes and add them to the set 170 for _, r := range ec2Routes { 171 if r.GatewayId != nil && *r.GatewayId == "local" { 172 continue 173 } 174 175 if r.Origin != nil && *r.Origin == "EnableVgwRoutePropagation" { 176 continue 177 } 178 179 if r.DestinationPrefixListId != nil { 180 // Skipping because VPC endpoint routes are handled separately 181 // See aws_vpc_endpoint 182 continue 183 } 184 185 m := make(map[string]interface{}) 186 187 if r.DestinationCidrBlock != nil { 188 m["cidr_block"] = *r.DestinationCidrBlock 189 } 190 if r.DestinationIpv6CidrBlock != nil { 191 m["ipv6_cidr_block"] = *r.DestinationIpv6CidrBlock 192 } 193 if r.EgressOnlyInternetGatewayId != nil { 194 m["egress_only_gateway_id"] = *r.EgressOnlyInternetGatewayId 195 } 196 if r.GatewayId != nil { 197 m["gateway_id"] = *r.GatewayId 198 } 199 if r.NatGatewayId != nil { 200 m["nat_gateway_id"] = *r.NatGatewayId 201 } 202 if r.InstanceId != nil { 203 m["instance_id"] = *r.InstanceId 204 } 205 if r.VpcPeeringConnectionId != nil { 206 m["vpc_peering_connection_id"] = *r.VpcPeeringConnectionId 207 } 208 if r.NetworkInterfaceId != nil { 209 m["network_interface_id"] = *r.NetworkInterfaceId 210 } 211 212 routes = append(routes, m) 213 } 214 return routes 215 } 216 217 func dataSourceAssociationsRead(ec2Assocations []*ec2.RouteTableAssociation) []map[string]interface{} { 218 associations := make([]map[string]interface{}, 0, len(ec2Assocations)) 219 // Loop through the routes and add them to the set 220 for _, a := range ec2Assocations { 221 222 m := make(map[string]interface{}) 223 m["route_table_id"] = *a.RouteTableId 224 m["route_table_association_id"] = *a.RouteTableAssociationId 225 // GH[11134] 226 if a.SubnetId != nil { 227 m["subnet_id"] = *a.SubnetId 228 } 229 m["main"] = *a.Main 230 associations = append(associations, m) 231 } 232 return associations 233 }