github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/import_aws_route_table.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/aws/aws-sdk-go/service/ec2"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  // Route table import also imports all the rules
    11  func resourceAwsRouteTableImportState(
    12  	d *schema.ResourceData,
    13  	meta interface{}) ([]*schema.ResourceData, error) {
    14  	conn := meta.(*AWSClient).ec2conn
    15  
    16  	// First query the resource itself
    17  	id := d.Id()
    18  	resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
    19  		RouteTableIds: []*string{&id},
    20  	})
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  	if len(resp.RouteTables) < 1 || resp.RouteTables[0] == nil {
    25  		return nil, fmt.Errorf("route table %s is not found", id)
    26  	}
    27  	table := resp.RouteTables[0]
    28  
    29  	// Start building our results
    30  	results := make([]*schema.ResourceData, 1,
    31  		2+len(table.Associations)+len(table.Routes))
    32  	results[0] = d
    33  
    34  	{
    35  		// Construct the routes
    36  		subResource := resourceAwsRoute()
    37  		for _, route := range table.Routes {
    38  			// Ignore the local/default route
    39  			if route.GatewayId != nil && *route.GatewayId == "local" {
    40  				continue
    41  			}
    42  
    43  			if route.DestinationPrefixListId != nil {
    44  				// Skipping because VPC endpoint routes are handled separately
    45  				// See aws_vpc_endpoint
    46  				continue
    47  			}
    48  
    49  			// Minimal data for route
    50  			d := subResource.Data(nil)
    51  			d.SetType("aws_route")
    52  			d.Set("route_table_id", id)
    53  			d.Set("destination_cidr_block", route.DestinationCidrBlock)
    54  			d.SetId(routeIDHash(d, route))
    55  			results = append(results, d)
    56  		}
    57  	}
    58  
    59  	{
    60  		// Construct the associations
    61  		subResource := resourceAwsRouteTableAssociation()
    62  		for _, assoc := range table.Associations {
    63  			if *assoc.Main {
    64  				// Ignore
    65  				continue
    66  			}
    67  
    68  			// Minimal data for route
    69  			d := subResource.Data(nil)
    70  			d.SetType("aws_route_table_association")
    71  			d.Set("route_table_id", assoc.RouteTableId)
    72  			d.SetId(*assoc.RouteTableAssociationId)
    73  			results = append(results, d)
    74  		}
    75  	}
    76  
    77  	{
    78  		// Construct the main associations. We could do this above but
    79  		// I keep this as a separate section since it is a separate resource.
    80  		subResource := resourceAwsMainRouteTableAssociation()
    81  		for _, assoc := range table.Associations {
    82  			if !*assoc.Main {
    83  				// Ignore
    84  				continue
    85  			}
    86  
    87  			// Minimal data for route
    88  			d := subResource.Data(nil)
    89  			d.SetType("aws_main_route_table_association")
    90  			d.Set("route_table_id", id)
    91  			d.Set("vpc_id", table.VpcId)
    92  			d.SetId(*assoc.RouteTableAssociationId)
    93  			results = append(results, d)
    94  		}
    95  	}
    96  
    97  	return results, nil
    98  }