github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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.Set("destination_ipv6_cidr_block", route.DestinationIpv6CidrBlock)
    55  			d.SetId(routeIDHash(d, route))
    56  			results = append(results, d)
    57  		}
    58  	}
    59  
    60  	{
    61  		// Construct the associations
    62  		subResource := resourceAwsRouteTableAssociation()
    63  		for _, assoc := range table.Associations {
    64  			if *assoc.Main {
    65  				// Ignore
    66  				continue
    67  			}
    68  
    69  			// Minimal data for route
    70  			d := subResource.Data(nil)
    71  			d.SetType("aws_route_table_association")
    72  			d.Set("route_table_id", assoc.RouteTableId)
    73  			d.SetId(*assoc.RouteTableAssociationId)
    74  			results = append(results, d)
    75  		}
    76  	}
    77  
    78  	{
    79  		// Construct the main associations. We could do this above but
    80  		// I keep this as a separate section since it is a separate resource.
    81  		subResource := resourceAwsMainRouteTableAssociation()
    82  		for _, assoc := range table.Associations {
    83  			if !*assoc.Main {
    84  				// Ignore
    85  				continue
    86  			}
    87  
    88  			// Minimal data for route
    89  			d := subResource.Data(nil)
    90  			d.SetType("aws_main_route_table_association")
    91  			d.Set("route_table_id", id)
    92  			d.Set("vpc_id", table.VpcId)
    93  			d.SetId(*assoc.RouteTableAssociationId)
    94  			results = append(results, d)
    95  		}
    96  	}
    97  
    98  	return results, nil
    99  }