github.com/dougneal/terraform@v0.6.15-0.20170330092735-b6a3840768a4/builtin/providers/aws/resource_aws_customer_gateway_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/aws/awserr"
    11  	"github.com/aws/aws-sdk-go/service/ec2"
    12  
    13  	"github.com/hashicorp/terraform/helper/acctest"
    14  	"github.com/hashicorp/terraform/helper/resource"
    15  	"github.com/hashicorp/terraform/terraform"
    16  )
    17  
    18  func TestAccAWSCustomerGateway_basic(t *testing.T) {
    19  	var gateway ec2.CustomerGateway
    20  	randInt := acctest.RandInt()
    21  	resource.Test(t, resource.TestCase{
    22  		PreCheck:      func() { testAccPreCheck(t) },
    23  		IDRefreshName: "aws_customer_gateway.foo",
    24  		Providers:     testAccProviders,
    25  		CheckDestroy:  testAccCheckCustomerGatewayDestroy,
    26  		Steps: []resource.TestStep{
    27  			{
    28  				Config: testAccCustomerGatewayConfig(randInt),
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckCustomerGateway("aws_customer_gateway.foo", &gateway),
    31  				),
    32  			},
    33  			{
    34  				Config: testAccCustomerGatewayConfigUpdateTags(randInt),
    35  				Check: resource.ComposeTestCheckFunc(
    36  					testAccCheckCustomerGateway("aws_customer_gateway.foo", &gateway),
    37  				),
    38  			},
    39  			{
    40  				Config: testAccCustomerGatewayConfigForceReplace(randInt),
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckCustomerGateway("aws_customer_gateway.foo", &gateway),
    43  				),
    44  			},
    45  		},
    46  	})
    47  }
    48  
    49  func TestAccAWSCustomerGateway_similarAlreadyExists(t *testing.T) {
    50  	var gateway ec2.CustomerGateway
    51  	randInt := acctest.RandInt()
    52  	resource.Test(t, resource.TestCase{
    53  		PreCheck:      func() { testAccPreCheck(t) },
    54  		IDRefreshName: "aws_customer_gateway.foo",
    55  		Providers:     testAccProviders,
    56  		CheckDestroy:  testAccCheckCustomerGatewayDestroy,
    57  		Steps: []resource.TestStep{
    58  			{
    59  				Config: testAccCustomerGatewayConfig(randInt),
    60  				Check: resource.ComposeTestCheckFunc(
    61  					testAccCheckCustomerGateway("aws_customer_gateway.foo", &gateway),
    62  				),
    63  			},
    64  			{
    65  				Config:      testAccCustomerGatewayConfigIdentical(randInt),
    66  				ExpectError: regexp.MustCompile("An existing customer gateway"),
    67  			},
    68  		},
    69  	})
    70  }
    71  
    72  func TestAccAWSCustomerGateway_disappears(t *testing.T) {
    73  	var gateway ec2.CustomerGateway
    74  	randInt := acctest.RandInt()
    75  	resource.Test(t, resource.TestCase{
    76  		PreCheck:     func() { testAccPreCheck(t) },
    77  		Providers:    testAccProviders,
    78  		CheckDestroy: testAccCheckCustomerGatewayDestroy,
    79  		Steps: []resource.TestStep{
    80  			{
    81  				Config: testAccCustomerGatewayConfig(randInt),
    82  				Check: resource.ComposeTestCheckFunc(
    83  					testAccCheckCustomerGateway("aws_customer_gateway.foo", &gateway),
    84  					testAccAWSCustomerGatewayDisappears(&gateway),
    85  				),
    86  				ExpectNonEmptyPlan: true,
    87  			},
    88  		},
    89  	})
    90  }
    91  
    92  func testAccAWSCustomerGatewayDisappears(gateway *ec2.CustomerGateway) resource.TestCheckFunc {
    93  	return func(s *terraform.State) error {
    94  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    95  		opts := &ec2.DeleteCustomerGatewayInput{
    96  			CustomerGatewayId: gateway.CustomerGatewayId,
    97  		}
    98  		if _, err := conn.DeleteCustomerGateway(opts); err != nil {
    99  			return err
   100  		}
   101  		return resource.Retry(40*time.Minute, func() *resource.RetryError {
   102  			opts := &ec2.DescribeCustomerGatewaysInput{
   103  				CustomerGatewayIds: []*string{gateway.CustomerGatewayId},
   104  			}
   105  			resp, err := conn.DescribeCustomerGateways(opts)
   106  			if err != nil {
   107  				cgw, ok := err.(awserr.Error)
   108  				if ok && cgw.Code() == "InvalidCustomerGatewayID.NotFound" {
   109  					return nil
   110  				}
   111  				return resource.NonRetryableError(
   112  					fmt.Errorf("Error retrieving Customer Gateway: %s", err))
   113  			}
   114  			if *resp.CustomerGateways[0].State == "deleted" {
   115  				return nil
   116  			}
   117  			return resource.RetryableError(fmt.Errorf(
   118  				"Waiting for Customer Gateway: %v", gateway.CustomerGatewayId))
   119  		})
   120  	}
   121  }
   122  
   123  func testAccCheckCustomerGatewayDestroy(s *terraform.State) error {
   124  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
   125  
   126  	for _, rs := range s.RootModule().Resources {
   127  		if rs.Type != "aws_customer_gatewah" {
   128  			continue
   129  		}
   130  
   131  		gatewayFilter := &ec2.Filter{
   132  			Name:   aws.String("customer-gateway-id"),
   133  			Values: []*string{aws.String(rs.Primary.ID)},
   134  		}
   135  
   136  		resp, err := conn.DescribeCustomerGateways(&ec2.DescribeCustomerGatewaysInput{
   137  			Filters: []*ec2.Filter{gatewayFilter},
   138  		})
   139  
   140  		if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidCustomerGatewayID.NotFound" {
   141  			continue
   142  		}
   143  
   144  		if err == nil {
   145  			if len(resp.CustomerGateways) > 0 {
   146  				return fmt.Errorf("Customer gateway still exists: %v", resp.CustomerGateways)
   147  			}
   148  
   149  			if *resp.CustomerGateways[0].State == "deleted" {
   150  				continue
   151  			}
   152  		}
   153  
   154  		return err
   155  	}
   156  
   157  	return nil
   158  }
   159  
   160  func testAccCheckCustomerGateway(gatewayResource string, cgw *ec2.CustomerGateway) resource.TestCheckFunc {
   161  	return func(s *terraform.State) error {
   162  		rs, ok := s.RootModule().Resources[gatewayResource]
   163  		if !ok {
   164  			return fmt.Errorf("Not found: %s", gatewayResource)
   165  		}
   166  
   167  		if rs.Primary.ID == "" {
   168  			return fmt.Errorf("No ID is set")
   169  		}
   170  
   171  		gateway, ok := s.RootModule().Resources[gatewayResource]
   172  		if !ok {
   173  			return fmt.Errorf("Not found: %s", gatewayResource)
   174  		}
   175  
   176  		ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
   177  		gatewayFilter := &ec2.Filter{
   178  			Name:   aws.String("customer-gateway-id"),
   179  			Values: []*string{aws.String(gateway.Primary.ID)},
   180  		}
   181  
   182  		resp, err := ec2conn.DescribeCustomerGateways(&ec2.DescribeCustomerGatewaysInput{
   183  			Filters: []*ec2.Filter{gatewayFilter},
   184  		})
   185  
   186  		if err != nil {
   187  			return err
   188  		}
   189  
   190  		respGateway := resp.CustomerGateways[0]
   191  		*cgw = *respGateway
   192  
   193  		return nil
   194  	}
   195  }
   196  
   197  func testAccCustomerGatewayConfig(randInt int) string {
   198  	return fmt.Sprintf(`
   199  	resource "aws_customer_gateway" "foo" {
   200  		bgp_asn = 65000
   201  		ip_address = "172.0.0.1"
   202  		type = "ipsec.1"
   203  		tags {
   204  			Name = "foo-gateway-%d"
   205  		}
   206  	}
   207  	`, randInt)
   208  }
   209  
   210  func testAccCustomerGatewayConfigIdentical(randInt int) string {
   211  	return fmt.Sprintf(`
   212  		resource "aws_customer_gateway" "foo" {
   213  			bgp_asn = 65000
   214  			ip_address = "172.0.0.1"
   215  			type = "ipsec.1"
   216  			tags {
   217  				Name = "foo-gateway-%d"
   218  			}
   219  		}
   220  
   221  		resource "aws_customer_gateway" "identical" {
   222  			bgp_asn = 65000
   223  			ip_address = "172.0.0.1"
   224  			type = "ipsec.1"
   225  			tags {
   226  				Name = "foo-gateway-identical-%d"
   227  			}
   228  		}
   229  		`, randInt, randInt)
   230  }
   231  
   232  // Add the Another: "tag" tag.
   233  func testAccCustomerGatewayConfigUpdateTags(randInt int) string {
   234  	return fmt.Sprintf(`
   235  		resource "aws_customer_gateway" "foo" {
   236  			bgp_asn = 65000
   237  			ip_address = "172.0.0.1"
   238  			type = "ipsec.1"
   239  			tags {
   240  				Name = "foo-gateway-%d"
   241  				Another = "tag-%d"
   242  			}
   243  		}
   244  		`, randInt, randInt)
   245  }
   246  
   247  // Change the ip_address.
   248  func testAccCustomerGatewayConfigForceReplace(randInt int) string {
   249  	return fmt.Sprintf(`
   250  	resource "aws_customer_gateway" "foo" {
   251  		bgp_asn = 65000
   252  		ip_address = "172.10.10.1"
   253  		type = "ipsec.1"
   254  		tags {
   255  			Name = "foo-gateway-%d"
   256  			Another = "tag-%d"
   257  		}
   258  	}
   259  	`, randInt, randInt)
   260  }