github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_vpn_gateway_test.go (about)

     1  // make testacc TEST=./builtin/providers/aws/ TESTARGS='-run=TestAccDataSourceAwsVpnGateway_'
     2  package aws
     3  
     4  import (
     5  	"fmt"
     6  	"regexp"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  )
    12  
    13  func TestAccDataSourceAwsVpnGateway_unattached(t *testing.T) {
    14  	rInt := acctest.RandInt()
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:  func() { testAccPreCheck(t) },
    18  		Providers: testAccProviders,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccDataSourceAwsVpnGatewayUnattachedConfig(rInt),
    22  				Check: resource.ComposeTestCheckFunc(
    23  					resource.TestCheckResourceAttrPair(
    24  						"data.aws_vpn_gateway.test_by_id", "id",
    25  						"aws_vpn_gateway.unattached", "id"),
    26  					resource.TestCheckResourceAttrPair(
    27  						"data.aws_vpn_gateway.test_by_tags", "id",
    28  						"aws_vpn_gateway.unattached", "id"),
    29  					resource.TestCheckResourceAttrSet("data.aws_vpn_gateway.test_by_id", "state"),
    30  					resource.TestCheckResourceAttr("data.aws_vpn_gateway.test_by_tags", "tags.%", "3"),
    31  					resource.TestCheckNoResourceAttr("data.aws_vpn_gateway.test_by_id", "attached_vpc_id"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func TestAccDataSourceAwsVpnGateway_attached(t *testing.T) {
    39  	rInt := acctest.RandInt()
    40  
    41  	resource.Test(t, resource.TestCase{
    42  		PreCheck:  func() { testAccPreCheck(t) },
    43  		Providers: testAccProviders,
    44  		Steps: []resource.TestStep{
    45  			resource.TestStep{
    46  				Config: testAccDataSourceAwsVpnGatewayAttachedConfig(rInt),
    47  				Check: resource.ComposeTestCheckFunc(
    48  					resource.TestCheckResourceAttrPair(
    49  						"data.aws_vpn_gateway.test_by_attached_vpc_id", "id",
    50  						"aws_vpn_gateway.attached", "id"),
    51  					resource.TestCheckResourceAttrPair(
    52  						"data.aws_vpn_gateway.test_by_attached_vpc_id", "attached_vpc_id",
    53  						"aws_vpc.foo", "id"),
    54  					resource.TestMatchResourceAttr("data.aws_vpn_gateway.test_by_attached_vpc_id", "state", regexp.MustCompile("(?i)available")),
    55  				),
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func testAccDataSourceAwsVpnGatewayUnattachedConfig(rInt int) string {
    62  	return fmt.Sprintf(`
    63  provider "aws" {
    64    region = "us-west-2"
    65  }
    66  
    67  resource "aws_vpn_gateway" "unattached" {
    68      tags {
    69  		Name = "terraform-testacc-vpn-gateway-data-source-unattached-%d"
    70        	ABC  = "testacc-%d"
    71  		XYZ  = "testacc-%d"
    72      }
    73  }
    74  
    75  data "aws_vpn_gateway" "test_by_id" {
    76  	id = "${aws_vpn_gateway.unattached.id}"
    77  }
    78  
    79  data "aws_vpn_gateway" "test_by_tags" {
    80  	tags = "${aws_vpn_gateway.unattached.tags}"
    81  }
    82  `, rInt, rInt+1, rInt-1)
    83  }
    84  
    85  func testAccDataSourceAwsVpnGatewayAttachedConfig(rInt int) string {
    86  	return fmt.Sprintf(`
    87  provider "aws" {
    88    region = "us-west-2"
    89  }
    90  
    91  resource "aws_vpc" "foo" {
    92  	cidr_block = "10.1.0.0/16"
    93  
    94    	tags {
    95      	Name = "terraform-testacc-vpn-gateway-data-source-foo-%d"
    96    	}
    97  }
    98  
    99  resource "aws_vpn_gateway" "attached" {
   100      tags {
   101  		Name = "terraform-testacc-vpn-gateway-data-source-attached-%d"
   102      }
   103  }
   104  
   105  resource "aws_vpn_gateway_attachment" "vpn_attachment" {
   106    vpc_id = "${aws_vpc.foo.id}"
   107    vpn_gateway_id = "${aws_vpn_gateway.attached.id}"
   108  }
   109  
   110  data "aws_vpn_gateway" "test_by_attached_vpc_id" {
   111  	attached_vpc_id = "${aws_vpn_gateway_attachment.vpn_attachment.vpc_id}"
   112  }
   113  `, rInt, rInt)
   114  }