github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_route_table_association_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/goamz/ec2"
    10  )
    11  
    12  func TestAccAWSRouteTableAssociation(t *testing.T) {
    13  	var v, v2 ec2.RouteTable
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckRouteTableAssociationDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccRouteTableAssociationConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckRouteTableAssociationExists(
    24  						"aws_route_table_association.foo", &v),
    25  				),
    26  			},
    27  
    28  			resource.TestStep{
    29  				Config: testAccRouteTableAssociationConfigChange,
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckRouteTableAssociationExists(
    32  						"aws_route_table_association.foo", &v2),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func testAccCheckRouteTableAssociationDestroy(s *terraform.State) error {
    40  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    41  
    42  	for _, rs := range s.RootModule().Resources {
    43  		if rs.Type != "aws_route_table_association" {
    44  			continue
    45  		}
    46  
    47  		// Try to find the resource
    48  		resp, err := conn.DescribeRouteTables(
    49  			[]string{rs.Primary.Attributes["route_table_Id"]}, ec2.NewFilter())
    50  		if err != nil {
    51  			// Verify the error is what we want
    52  			ec2err, ok := err.(*ec2.Error)
    53  			if !ok {
    54  				return err
    55  			}
    56  			if ec2err.Code != "InvalidRouteTableID.NotFound" {
    57  				return err
    58  			}
    59  			return nil
    60  		}
    61  
    62  		rt := resp.RouteTables[0]
    63  		if len(rt.Associations) > 0 {
    64  			return fmt.Errorf(
    65  				"route table %s has associations", rt.RouteTableId)
    66  
    67  		}
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func testAccCheckRouteTableAssociationExists(n string, v *ec2.RouteTable) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  		rs, ok := s.RootModule().Resources[n]
    76  		if !ok {
    77  			return fmt.Errorf("Not found: %s", n)
    78  		}
    79  
    80  		if rs.Primary.ID == "" {
    81  			return fmt.Errorf("No ID is set")
    82  		}
    83  
    84  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    85  		resp, err := conn.DescribeRouteTables(
    86  			[]string{rs.Primary.Attributes["route_table_id"]}, ec2.NewFilter())
    87  		if err != nil {
    88  			return err
    89  		}
    90  		if len(resp.RouteTables) == 0 {
    91  			return fmt.Errorf("RouteTable not found")
    92  		}
    93  
    94  		*v = resp.RouteTables[0]
    95  
    96  		if len(v.Associations) == 0 {
    97  			return fmt.Errorf("no associations")
    98  		}
    99  
   100  		return nil
   101  	}
   102  }
   103  
   104  const testAccRouteTableAssociationConfig = `
   105  resource "aws_vpc" "foo" {
   106  	cidr_block = "10.1.0.0/16"
   107  }
   108  
   109  resource "aws_subnet" "foo" {
   110  	vpc_id = "${aws_vpc.foo.id}"
   111  	cidr_block = "10.1.1.0/24"
   112  }
   113  
   114  resource "aws_internet_gateway" "foo" {
   115  	vpc_id = "${aws_vpc.foo.id}"
   116  }
   117  
   118  resource "aws_route_table" "foo" {
   119  	vpc_id = "${aws_vpc.foo.id}"
   120  	route {
   121  		cidr_block = "10.0.0.0/8"
   122  		gateway_id = "${aws_internet_gateway.foo.id}"
   123  	}
   124  }
   125  
   126  resource "aws_route_table_association" "foo" {
   127  	route_table_id = "${aws_route_table.foo.id}"
   128  	subnet_id = "${aws_subnet.foo.id}"
   129  }
   130  `
   131  
   132  const testAccRouteTableAssociationConfigChange = `
   133  resource "aws_vpc" "foo" {
   134  	cidr_block = "10.1.0.0/16"
   135  }
   136  
   137  resource "aws_subnet" "foo" {
   138  	vpc_id = "${aws_vpc.foo.id}"
   139  	cidr_block = "10.1.1.0/24"
   140  }
   141  
   142  resource "aws_internet_gateway" "foo" {
   143  	vpc_id = "${aws_vpc.foo.id}"
   144  }
   145  
   146  resource "aws_route_table" "bar" {
   147  	vpc_id = "${aws_vpc.foo.id}"
   148  	route {
   149  		cidr_block = "10.0.0.0/8"
   150  		gateway_id = "${aws_internet_gateway.foo.id}"
   151  	}
   152  }
   153  
   154  resource "aws_route_table_association" "foo" {
   155  	route_table_id = "${aws_route_table.bar.id}"
   156  	subnet_id = "${aws_subnet.foo.id}"
   157  }
   158  `