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