github.com/bradfeehan/terraform@v0.7.0-rc3.0.20170529055808-34b45c5ad841/builtin/providers/aws/resource_aws_main_route_table_association_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws/awserr"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccAWSMainRouteTableAssociation_basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:     func() { testAccPreCheck(t) },
    15  		Providers:    testAccProviders,
    16  		CheckDestroy: testAccCheckMainRouteTableAssociationDestroy,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccMainRouteTableAssociationConfig,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccCheckMainRouteTableAssociation(
    22  						"aws_main_route_table_association.foo",
    23  						"aws_vpc.foo",
    24  						"aws_route_table.foo",
    25  					),
    26  				),
    27  			},
    28  			resource.TestStep{
    29  				Config: testAccMainRouteTableAssociationConfigUpdate,
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckMainRouteTableAssociation(
    32  						"aws_main_route_table_association.foo",
    33  						"aws_vpc.foo",
    34  						"aws_route_table.bar",
    35  					),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func testAccCheckMainRouteTableAssociationDestroy(s *terraform.State) error {
    43  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    44  
    45  	for _, rs := range s.RootModule().Resources {
    46  		if rs.Type != "aws_main_route_table_association" {
    47  			continue
    48  		}
    49  
    50  		mainAssociation, err := findMainRouteTableAssociation(
    51  			conn,
    52  			rs.Primary.Attributes["vpc_id"],
    53  		)
    54  		if err != nil {
    55  			// Verify the error is what we want
    56  			if ae, ok := err.(awserr.Error); ok && ae.Code() == "ApplicationDoesNotExistException" {
    57  				continue
    58  			}
    59  			return err
    60  		}
    61  
    62  		if mainAssociation != nil {
    63  			return fmt.Errorf("still exists")
    64  		}
    65  	}
    66  
    67  	return nil
    68  }
    69  
    70  func testAccCheckMainRouteTableAssociation(
    71  	mainRouteTableAssociationResource string,
    72  	vpcResource string,
    73  	routeTableResource string) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  		rs, ok := s.RootModule().Resources[mainRouteTableAssociationResource]
    76  		if !ok {
    77  			return fmt.Errorf("Not found: %s", mainRouteTableAssociationResource)
    78  		}
    79  
    80  		if rs.Primary.ID == "" {
    81  			return fmt.Errorf("No ID is set")
    82  		}
    83  
    84  		vpc, ok := s.RootModule().Resources[vpcResource]
    85  		if !ok {
    86  			return fmt.Errorf("Not found: %s", vpcResource)
    87  		}
    88  
    89  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    90  		mainAssociation, err := findMainRouteTableAssociation(conn, vpc.Primary.ID)
    91  		if err != nil {
    92  			return err
    93  		}
    94  
    95  		if *mainAssociation.RouteTableAssociationId != rs.Primary.ID {
    96  			return fmt.Errorf("Found wrong main association: %s",
    97  				*mainAssociation.RouteTableAssociationId)
    98  		}
    99  
   100  		return nil
   101  	}
   102  }
   103  
   104  const testAccMainRouteTableAssociationConfig = `
   105  resource "aws_vpc" "foo" {
   106  	cidr_block = "10.1.0.0/16"
   107  	tags {
   108  		Name = "testAccMainRouteTableAssociationConfig"
   109  	}
   110  }
   111  
   112  resource "aws_subnet" "foo" {
   113  	vpc_id = "${aws_vpc.foo.id}"
   114  	cidr_block = "10.1.1.0/24"
   115  }
   116  
   117  resource "aws_internet_gateway" "foo" {
   118  	vpc_id = "${aws_vpc.foo.id}"
   119  }
   120  
   121  resource "aws_route_table" "foo" {
   122  	vpc_id = "${aws_vpc.foo.id}"
   123  	route {
   124  		cidr_block = "10.0.0.0/8"
   125  		gateway_id = "${aws_internet_gateway.foo.id}"
   126  	}
   127  }
   128  
   129  resource "aws_main_route_table_association" "foo" {
   130  	vpc_id = "${aws_vpc.foo.id}"
   131  	route_table_id = "${aws_route_table.foo.id}"
   132  }
   133  `
   134  
   135  const testAccMainRouteTableAssociationConfigUpdate = `
   136  resource "aws_vpc" "foo" {
   137  	cidr_block = "10.1.0.0/16"
   138  	tags {
   139  		Name = "testAccMainRouteTableAssociationConfigUpdate"
   140  	}
   141  }
   142  
   143  resource "aws_subnet" "foo" {
   144  	vpc_id = "${aws_vpc.foo.id}"
   145  	cidr_block = "10.1.1.0/24"
   146  }
   147  
   148  resource "aws_internet_gateway" "foo" {
   149  	vpc_id = "${aws_vpc.foo.id}"
   150  }
   151  
   152  // Need to keep the old route table around when we update the
   153  // main_route_table_association, otherwise Terraform will try to destroy the
   154  // route table too early, and will fail because it's still the main one
   155  resource "aws_route_table" "foo" {
   156  	vpc_id = "${aws_vpc.foo.id}"
   157  	route {
   158  		cidr_block = "10.0.0.0/8"
   159  		gateway_id = "${aws_internet_gateway.foo.id}"
   160  	}
   161  }
   162  
   163  resource "aws_route_table" "bar" {
   164  	vpc_id = "${aws_vpc.foo.id}"
   165  	route {
   166  		cidr_block = "10.0.0.0/8"
   167  		gateway_id = "${aws_internet_gateway.foo.id}"
   168  	}
   169  }
   170  
   171  resource "aws_main_route_table_association" "foo" {
   172  	vpc_id = "${aws_vpc.foo.id}"
   173  	route_table_id = "${aws_route_table.bar.id}"
   174  }
   175  `