github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/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/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/ec2"
    10  
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSCustomerGateway_basic(t *testing.T) {
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:      func() { testAccPreCheck(t) },
    18  		IDRefreshName: "aws_customer_gateway.foo",
    19  		Providers:     testAccProviders,
    20  		CheckDestroy:  testAccCheckCustomerGatewayDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccCustomerGatewayConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckCustomerGateway(
    26  						"aws_customer_gateway.foo",
    27  					),
    28  				),
    29  			},
    30  			resource.TestStep{
    31  				Config: testAccCustomerGatewayConfigUpdateTags,
    32  				Check: resource.ComposeTestCheckFunc(
    33  					testAccCheckCustomerGateway(
    34  						"aws_customer_gateway.foo",
    35  					),
    36  				),
    37  			},
    38  			resource.TestStep{
    39  				Config: testAccCustomerGatewayConfigForceReplace,
    40  				Check: resource.ComposeTestCheckFunc(
    41  					testAccCheckCustomerGateway(
    42  						"aws_customer_gateway.foo",
    43  					),
    44  				),
    45  			},
    46  		},
    47  	})
    48  }
    49  
    50  func testAccCheckCustomerGatewayDestroy(s *terraform.State) error {
    51  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    52  
    53  	for _, rs := range s.RootModule().Resources {
    54  		if rs.Type != "aws_customer_gatewah" {
    55  			continue
    56  		}
    57  
    58  		gatewayFilter := &ec2.Filter{
    59  			Name:   aws.String("customer-gateway-id"),
    60  			Values: []*string{aws.String(rs.Primary.ID)},
    61  		}
    62  
    63  		resp, err := conn.DescribeCustomerGateways(&ec2.DescribeCustomerGatewaysInput{
    64  			Filters: []*ec2.Filter{gatewayFilter},
    65  		})
    66  
    67  		if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidCustomerGatewayID.NotFound" {
    68  			continue
    69  		}
    70  
    71  		if err == nil {
    72  			if len(resp.CustomerGateways) > 0 {
    73  				return fmt.Errorf("Customer gateway still exists: %v", resp.CustomerGateways)
    74  			}
    75  		}
    76  
    77  		return err
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  func testAccCheckCustomerGateway(gatewayResource string) resource.TestCheckFunc {
    84  	return func(s *terraform.State) error {
    85  		rs, ok := s.RootModule().Resources[gatewayResource]
    86  		if !ok {
    87  			return fmt.Errorf("Not found: %s", gatewayResource)
    88  		}
    89  
    90  		if rs.Primary.ID == "" {
    91  			return fmt.Errorf("No ID is set")
    92  		}
    93  
    94  		gateway, ok := s.RootModule().Resources[gatewayResource]
    95  		if !ok {
    96  			return fmt.Errorf("Not found: %s", gatewayResource)
    97  		}
    98  
    99  		ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
   100  		gatewayFilter := &ec2.Filter{
   101  			Name:   aws.String("customer-gateway-id"),
   102  			Values: []*string{aws.String(gateway.Primary.ID)},
   103  		}
   104  
   105  		_, err := ec2conn.DescribeCustomerGateways(&ec2.DescribeCustomerGatewaysInput{
   106  			Filters: []*ec2.Filter{gatewayFilter},
   107  		})
   108  
   109  		if err != nil {
   110  			return err
   111  		}
   112  
   113  		return nil
   114  	}
   115  }
   116  
   117  const testAccCustomerGatewayConfig = `
   118  resource "aws_customer_gateway" "foo" {
   119  	bgp_asn = 65000
   120  	ip_address = "172.0.0.1"
   121  	type = "ipsec.1"
   122  	tags {
   123  		Name = "foo-gateway"
   124  	}
   125  }
   126  `
   127  
   128  // Add the Another: "tag" tag.
   129  const testAccCustomerGatewayConfigUpdateTags = `
   130  resource "aws_customer_gateway" "foo" {
   131  	bgp_asn = 65000
   132  	ip_address = "172.0.0.1"
   133  	type = "ipsec.1"
   134  	tags {
   135  		Name = "foo-gateway"
   136  		Another = "tag"
   137  	}
   138  }
   139  `
   140  
   141  // Change the ip_address.
   142  const testAccCustomerGatewayConfigForceReplace = `
   143  resource "aws_customer_gateway" "foo" {
   144  	bgp_asn = 65000
   145  	ip_address = "172.10.10.1"
   146  	type = "ipsec.1"
   147  	tags {
   148  		Name = "foo-gateway"
   149  		Another = "tag"
   150  	}
   151  }
   152  `