From 9afebeb07e3c50db50be2f464d115764af7545e6 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 10 Jul 2014 15:52:56 -0700 Subject: [PATCH] providers/aws: route table tests --- .../aws/resource_aws_internet_gateway.go | 2 +- .../providers/aws/resource_aws_route_table.go | 71 ++++++- .../aws/resource_aws_route_table_test.go | 184 ++++++++++++++++++ builtin/providers/aws/resources.go | 1 + 4 files changed, 248 insertions(+), 10 deletions(-) create mode 100644 builtin/providers/aws/resource_aws_route_table_test.go diff --git a/builtin/providers/aws/resource_aws_internet_gateway.go b/builtin/providers/aws/resource_aws_internet_gateway.go index 6a7e09980..3c6e7a436 100644 --- a/builtin/providers/aws/resource_aws_internet_gateway.go +++ b/builtin/providers/aws/resource_aws_internet_gateway.go @@ -288,7 +288,7 @@ func IGAttachStateRefreshFunc(conn *ec2.EC2, id string, expected string) resourc ig := &resp.InternetGateways[0] - if time.Now().Sub(start) > 10 * time.Second { + if time.Now().Sub(start) > 10*time.Second { return ig, expected, nil } diff --git a/builtin/providers/aws/resource_aws_route_table.go b/builtin/providers/aws/resource_aws_route_table.go index 7c9590a53..66f815d3a 100644 --- a/builtin/providers/aws/resource_aws_route_table.go +++ b/builtin/providers/aws/resource_aws_route_table.go @@ -4,9 +4,11 @@ import ( "fmt" "log" "reflect" + "time" "github.com/hashicorp/terraform/flatmap" "github.com/hashicorp/terraform/helper/diff" + "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/goamz/ec2" ) @@ -33,6 +35,22 @@ func resource_aws_route_table_create( s.ID = rt.RouteTableId log.Printf("[INFO] Route Table ID: %s", s.ID) + // Wait for the route table to become available + log.Printf( + "[DEBUG] Waiting for route table (%s) to become available", + s.ID) + stateConf := &resource.StateChangeConf{ + Pending: []string{"pending"}, + Target: "ready", + Refresh: RouteTableStateRefreshFunc(ec2conn, s.ID), + Timeout: 1 * time.Minute, + } + if _, err := stateConf.WaitForState(); err != nil { + return s, fmt.Errorf( + "Error waiting for route table (%s) to become available: %s", + s.ID, err) + } + // Update our routes return resource_aws_route_table_update(s, d, meta) } @@ -124,6 +142,22 @@ func resource_aws_route_table_destroy( return fmt.Errorf("Error deleting route table: %s", err) } + // Wait for the route table to really destroy + log.Printf( + "[DEBUG] Waiting for route table (%s) to become destroyed", + s.ID) + stateConf := &resource.StateChangeConf{ + Pending: []string{"ready"}, + Target: "", + Refresh: RouteTableStateRefreshFunc(ec2conn, s.ID), + Timeout: 1 * time.Minute, + } + if _, err := stateConf.WaitForState(); err != nil { + return fmt.Errorf( + "Error waiting for route table (%s) to become destroyed: %s", + s.ID, err) + } + return nil } @@ -133,21 +167,15 @@ func resource_aws_route_table_refresh( p := meta.(*ResourceProvider) ec2conn := p.ec2conn - resp, err := ec2conn.DescribeRouteTables([]string{s.ID}, ec2.NewFilter()) + rtRaw, _, err := RouteTableStateRefreshFunc(ec2conn, s.ID)() if err != nil { - if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidRouteTableID.NotFound" { - return nil, nil - } - - log.Printf("[ERROR] Error searching for route table: %s", err) return s, err } - - if len(resp.RouteTables) == 0 { + if rtRaw == nil { return nil, nil } - rt := &resp.RouteTables[0] + rt := rtRaw.(*ec2.RouteTable) return resource_aws_route_table_update_state(s, rt) } @@ -259,3 +287,28 @@ func routeTableOps(a interface{}, b interface{}) []routeTableOp { return ops } + +// RouteTableStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch +// a RouteTable. +func RouteTableStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + resp, err := conn.DescribeRouteTables([]string{id}, ec2.NewFilter()) + if err != nil { + if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidRouteTableID.NotFound" { + resp = nil + } else { + log.Printf("Error on RouteTableStateRefresh: %s", err) + return nil, "", err + } + } + + if resp == nil { + // Sometimes AWS just has consistency issues and doesn't see + // our instance yet. Return an empty state. + return nil, "", nil + } + + rt := &resp.RouteTables[0] + return rt, "ready", nil + } +} diff --git a/builtin/providers/aws/resource_aws_route_table_test.go b/builtin/providers/aws/resource_aws_route_table_test.go new file mode 100644 index 000000000..d18308c8f --- /dev/null +++ b/builtin/providers/aws/resource_aws_route_table_test.go @@ -0,0 +1,184 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/mitchellh/goamz/ec2" +) + +func TestAccAWSRouteTable(t *testing.T) { + var v ec2.RouteTable + + testCheck := func(*terraform.State) error { + if len(v.Routes) != 2 { + return fmt.Errorf("bad routes: %#v", v.Routes) + } + + routes := make(map[string]ec2.Route) + for _, r := range v.Routes { + routes[r.DestinationCidrBlock] = r + } + + if _, ok := routes["10.1.0.0/16"]; !ok { + return fmt.Errorf("bad routes: %#v", v.Routes) + } + if _, ok := routes["10.2.0.0/16"]; !ok { + return fmt.Errorf("bad routes: %#v", v.Routes) + } + + return nil + } + + testCheckChange := func(*terraform.State) error { + if len(v.Routes) != 3 { + return fmt.Errorf("bad routes: %#v", v.Routes) + } + + routes := make(map[string]ec2.Route) + for _, r := range v.Routes { + routes[r.DestinationCidrBlock] = r + } + + if _, ok := routes["10.1.0.0/16"]; !ok { + return fmt.Errorf("bad routes: %#v", v.Routes) + } + if _, ok := routes["10.3.0.0/16"]; !ok { + return fmt.Errorf("bad routes: %#v", v.Routes) + } + if _, ok := routes["10.4.0.0/16"]; !ok { + return fmt.Errorf("bad routes: %#v", v.Routes) + } + + return nil + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckRouteTableDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccRouteTableConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckRouteTableExists( + "aws_route_table.foo", &v), + testCheck, + ), + }, + + resource.TestStep{ + Config: testAccRouteTableConfigChange, + Check: resource.ComposeTestCheckFunc( + testAccCheckRouteTableExists( + "aws_route_table.foo", &v), + testCheckChange, + ), + }, + }, + }) +} + +func testAccCheckRouteTableDestroy(s *terraform.State) error { + conn := testAccProvider.ec2conn + + for _, rs := range s.Resources { + if rs.Type != "aws_route_table" { + continue + } + + // Try to find the resource + resp, err := conn.DescribeRouteTables( + []string{rs.ID}, ec2.NewFilter()) + if err == nil { + if len(resp.RouteTables) > 0 { + return fmt.Errorf("still exist.") + } + + return nil + } + + // Verify the error is what we want + ec2err, ok := err.(*ec2.Error) + if !ok { + return err + } + if ec2err.Code != "InvalidRouteTableID.NotFound" { + return err + } + } + + return nil +} + +func testAccCheckRouteTableExists(n string, v *ec2.RouteTable) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.ID == "" { + return fmt.Errorf("No ID is set") + } + + conn := testAccProvider.ec2conn + resp, err := conn.DescribeRouteTables( + []string{rs.ID}, ec2.NewFilter()) + if err != nil { + return err + } + if len(resp.RouteTables) == 0 { + return fmt.Errorf("RouteTable not found") + } + + *v = resp.RouteTables[0] + + return nil + } +} + +const testAccRouteTableConfig = ` +resource "aws_vpc" "foo" { + cidr_block = "10.1.0.0/16" +} + +resource "aws_internet_gateway" "foo" { + vpc_id = "${aws_vpc.foo.id}" +} + +resource "aws_route_table" "foo" { + vpc_id = "${aws_vpc.foo.id}" + + route { + cidr_block = "10.2.0.0/16" + gateway_id = "${aws_internet_gateway.foo.id}" + } +} +` + +const testAccRouteTableConfigChange = ` +resource "aws_vpc" "foo" { + cidr_block = "10.1.0.0/16" +} + +resource "aws_internet_gateway" "foo" { + vpc_id = "${aws_vpc.foo.id}" +} + +resource "aws_route_table" "foo" { + vpc_id = "${aws_vpc.foo.id}" + + route { + cidr_block = "10.3.0.0/16" + gateway_id = "${aws_internet_gateway.foo.id}" + } + + route { + cidr_block = "10.4.0.0/16" + gateway_id = "${aws_internet_gateway.foo.id}" + } +} +` diff --git a/builtin/providers/aws/resources.go b/builtin/providers/aws/resources.go index aeb797eb2..1f0327e32 100644 --- a/builtin/providers/aws/resources.go +++ b/builtin/providers/aws/resources.go @@ -70,6 +70,7 @@ func init() { Destroy: resource_aws_route_table_destroy, Diff: resource_aws_route_table_diff, Refresh: resource_aws_route_table_refresh, + Update: resource_aws_route_table_update, }, "aws_security_group": resource.Resource{