github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_customer_gateway_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/ec2"
     9  
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSCustomerGateway_basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckCustomerGatewayDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCustomerGatewayConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckCustomerGateway(
    24  						"aws_customer_gateway.foo",
    25  					),
    26  				),
    27  			},
    28  			resource.TestStep{
    29  				Config: testAccCustomerGatewayConfigUpdateTags,
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckCustomerGateway(
    32  						"aws_customer_gateway.foo",
    33  					),
    34  				),
    35  			},
    36  			resource.TestStep{
    37  				Config: testAccCustomerGatewayConfigForceReplace,
    38  				Check: resource.ComposeTestCheckFunc(
    39  					testAccCheckCustomerGateway(
    40  						"aws_customer_gateway.foo",
    41  					),
    42  				),
    43  			},
    44  		},
    45  	})
    46  }
    47  
    48  func testAccCheckCustomerGatewayDestroy(s *terraform.State) error {
    49  	if len(s.RootModule().Resources) > 0 {
    50  		return fmt.Errorf("Expected all resources to be gone, but found: %#v", s.RootModule().Resources)
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func testAccCheckCustomerGateway(gatewayResource string) resource.TestCheckFunc {
    57  	return func(s *terraform.State) error {
    58  		rs, ok := s.RootModule().Resources[gatewayResource]
    59  		if !ok {
    60  			return fmt.Errorf("Not found: %s", gatewayResource)
    61  		}
    62  
    63  		if rs.Primary.ID == "" {
    64  			return fmt.Errorf("No ID is set")
    65  		}
    66  
    67  		gateway, ok := s.RootModule().Resources[gatewayResource]
    68  		if !ok {
    69  			return fmt.Errorf("Not found: %s", gatewayResource)
    70  		}
    71  
    72  		ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
    73  		gatewayFilter := &ec2.Filter{
    74  			Name:   aws.String("customer-gateway-id"),
    75  			Values: []*string{aws.String(gateway.Primary.ID)},
    76  		}
    77  
    78  		_, err := ec2conn.DescribeCustomerGateways(&ec2.DescribeCustomerGatewaysInput{
    79  			Filters: []*ec2.Filter{gatewayFilter},
    80  		})
    81  
    82  		if err != nil {
    83  			return err
    84  		}
    85  
    86  		return nil
    87  	}
    88  }
    89  
    90  const testAccCustomerGatewayConfig = `
    91  resource "aws_customer_gateway" "foo" {
    92  	bgp_asn = 60000
    93  	ip_address = "172.0.0.1"
    94  	type = "ipsec.1"
    95  	tags {
    96  		Name = "foo-gateway"
    97  	}
    98  }
    99  `
   100  
   101  // Add the Another: "tag" tag.
   102  const testAccCustomerGatewayConfigUpdateTags = `
   103  resource "aws_customer_gateway" "foo" {
   104  	bgp_asn = 60000
   105  	ip_address = "172.0.0.1"
   106  	type = "ipsec.1"
   107  	tags {
   108  		Name = "foo-gateway"
   109  		Another = "tag"
   110  	}
   111  }
   112  `
   113  
   114  // Change the ip_address.
   115  const testAccCustomerGatewayConfigForceReplace = `
   116  resource "aws_customer_gateway" "foo" {
   117  	bgp_asn = 60000
   118  	ip_address = "172.10.10.1"
   119  	type = "ipsec.1"
   120  	tags {
   121  		Name = "foo-gateway"
   122  		Another = "tag"
   123  	}
   124  }
   125  `