github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_customer_gateway_test.go (about)

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