github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  			// Minimal data for route
    44  			d := subResource.Data(nil)
    45  			d.SetType("aws_route")
    46  			d.Set("route_table_id", id)
    47  			d.Set("destination_cidr_block", route.DestinationCidrBlock)
    48  			d.SetId(routeIDHash(d, route))
    49  			results = append(results, d)
    50  		}
    51  	}
    52  
    53  	{
    54  		// Construct the associations
    55  		subResource := resourceAwsRouteTableAssociation()
    56  		for _, assoc := range table.Associations {
    57  			if *assoc.Main {
    58  				// Ignore
    59  				continue
    60  			}
    61  
    62  			// Minimal data for route
    63  			d := subResource.Data(nil)
    64  			d.SetType("aws_route_table_association")
    65  			d.Set("route_table_id", assoc.RouteTableId)
    66  			d.SetId(*assoc.RouteTableAssociationId)
    67  			results = append(results, d)
    68  		}
    69  	}
    70  
    71  	{
    72  		// Construct the main associations. We could do this above but
    73  		// I keep this as a separate section since it is a separate resource.
    74  		subResource := resourceAwsMainRouteTableAssociation()
    75  		for _, assoc := range table.Associations {
    76  			if !*assoc.Main {
    77  				// Ignore
    78  				continue
    79  			}
    80  
    81  			// Minimal data for route
    82  			d := subResource.Data(nil)
    83  			d.SetType("aws_main_route_table_association")
    84  			d.Set("route_table_id", id)
    85  			d.Set("vpc_id", table.VpcId)
    86  			d.SetId(*assoc.RouteTableAssociationId)
    87  			results = append(results, d)
    88  		}
    89  	}
    90  
    91  	return results, nil
    92  }