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