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