providers/aws: route table import

This commit is contained in:
Mitchell Hashimoto 2016-05-13 11:52:52 -07:00
parent a4e48b19c0
commit 08b7f67227
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,46 @@
package aws
import (
"fmt"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)
// Route table import also imports all the rules
func resourceAwsRouteTableImportState(
d *schema.ResourceData,
meta interface{}) ([]*schema.ResourceData, error) {
conn := meta.(*AWSClient).ec2conn
// First query the resource itself
id := d.Id()
resp, err := conn.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
RouteTableIds: []*string{&id},
})
if err != nil {
return nil, err
}
if len(resp.RouteTables) < 1 || resp.RouteTables[0] == nil {
return nil, fmt.Errorf("route table %s is not found", id)
}
table := resp.RouteTables[0]
// Start building our results
results := make([]*schema.ResourceData, 1, 1+len(table.Routes))
results[0] = d
// Construct the routes
subResource := resourceAwsRoute()
for _, route := range table.Routes {
// Minimal data for route
d := subResource.Data(nil)
d.SetType("aws_route")
d.Set("route_table_id", id)
d.Set("destination_cidr_block", route.DestinationCidrBlock)
d.SetId(routeIDHash(d, route))
results = append(results, d)
}
return results, nil
}

View File

@ -0,0 +1,37 @@
package aws
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAWSRouteTable_importBasic(t *testing.T) {
checkFn := func(s []*terraform.InstanceState) error {
// Expect 2: group, 1 rules
if len(s) != 3 {
return fmt.Errorf("bad states: %#v", s)
}
return nil
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRouteTableDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRouteTableConfig,
},
resource.TestStep{
ResourceName: "aws_route_table.foo",
ImportState: true,
ImportStateCheck: checkFn,
},
},
})
}

View File

@ -20,6 +20,9 @@ func resourceAwsRouteTable() *schema.Resource {
Read: resourceAwsRouteTableRead,
Update: resourceAwsRouteTableUpdate,
Delete: resourceAwsRouteTableDelete,
Importer: &schema.ResourceImporter{
State: resourceAwsRouteTableImportState,
},
Schema: map[string]*schema.Schema{
"vpc_id": &schema.Schema{