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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccDataSourceAwsSubnet(t *testing.T) {
    13  	rInt := acctest.RandIntRange(0, 256)
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckVpcDestroy,
    19  		Steps: []resource.TestStep{
    20  			{
    21  				Config: testAccDataSourceAwsSubnetConfig(rInt),
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccDataSourceAwsSubnetCheck("data.aws_subnet.by_id", rInt),
    24  					testAccDataSourceAwsSubnetCheck("data.aws_subnet.by_cidr", rInt),
    25  					testAccDataSourceAwsSubnetCheck("data.aws_subnet.by_tag", rInt),
    26  					testAccDataSourceAwsSubnetCheck("data.aws_subnet.by_vpc", rInt),
    27  					testAccDataSourceAwsSubnetCheck("data.aws_subnet.by_filter", rInt),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccDataSourceAwsSubnetIpv6ByIpv6Filter(t *testing.T) {
    35  	rInt := acctest.RandIntRange(0, 256)
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:  func() { testAccPreCheck(t) },
    38  		Providers: testAccProviders,
    39  		Steps: []resource.TestStep{
    40  			{
    41  				Config: testAccDataSourceAwsSubnetConfigIpv6(rInt),
    42  			},
    43  			{
    44  				Config: testAccDataSourceAwsSubnetConfigIpv6WithDataSourceFilter(rInt),
    45  				Check: resource.ComposeAggregateTestCheckFunc(
    46  					resource.TestCheckResourceAttrSet(
    47  						"data.aws_subnet.by_ipv6_cidr", "ipv6_cidr_block_association_id"),
    48  					resource.TestCheckResourceAttrSet(
    49  						"data.aws_subnet.by_ipv6_cidr", "ipv6_cidr_block"),
    50  				),
    51  			},
    52  		},
    53  	})
    54  }
    55  
    56  func TestAccDataSourceAwsSubnetIpv6ByIpv6CidrBlock(t *testing.T) {
    57  	rInt := acctest.RandIntRange(0, 256)
    58  	resource.Test(t, resource.TestCase{
    59  		PreCheck:  func() { testAccPreCheck(t) },
    60  		Providers: testAccProviders,
    61  		Steps: []resource.TestStep{
    62  			{
    63  				Config: testAccDataSourceAwsSubnetConfigIpv6(rInt),
    64  			},
    65  			{
    66  				Config: testAccDataSourceAwsSubnetConfigIpv6WithDataSourceIpv6CidrBlock(rInt),
    67  				Check: resource.ComposeAggregateTestCheckFunc(
    68  					resource.TestCheckResourceAttrSet(
    69  						"data.aws_subnet.by_ipv6_cidr", "ipv6_cidr_block_association_id"),
    70  				),
    71  			},
    72  		},
    73  	})
    74  }
    75  
    76  func testAccDataSourceAwsSubnetCheck(name string, rInt int) resource.TestCheckFunc {
    77  	return func(s *terraform.State) error {
    78  		rs, ok := s.RootModule().Resources[name]
    79  		if !ok {
    80  			return fmt.Errorf("root module has no resource called %s", name)
    81  		}
    82  
    83  		vpcRs, ok := s.RootModule().Resources["aws_vpc.test"]
    84  		if !ok {
    85  			return fmt.Errorf("can't find aws_vpc.test in state")
    86  		}
    87  		subnetRs, ok := s.RootModule().Resources["aws_subnet.test"]
    88  		if !ok {
    89  			return fmt.Errorf("can't find aws_subnet.test in state")
    90  		}
    91  
    92  		attr := rs.Primary.Attributes
    93  
    94  		if attr["id"] != subnetRs.Primary.Attributes["id"] {
    95  			return fmt.Errorf(
    96  				"id is %s; want %s",
    97  				attr["id"],
    98  				subnetRs.Primary.Attributes["id"],
    99  			)
   100  		}
   101  
   102  		if attr["vpc_id"] != vpcRs.Primary.Attributes["id"] {
   103  			return fmt.Errorf(
   104  				"vpc_id is %s; want %s",
   105  				attr["vpc_id"],
   106  				vpcRs.Primary.Attributes["id"],
   107  			)
   108  		}
   109  
   110  		if attr["cidr_block"] != fmt.Sprintf("172.%d.123.0/24", rInt) {
   111  			return fmt.Errorf("bad cidr_block %s", attr["cidr_block"])
   112  		}
   113  		if attr["availability_zone"] != "us-west-2a" {
   114  			return fmt.Errorf("bad availability_zone %s", attr["availability_zone"])
   115  		}
   116  		if attr["tags.Name"] != fmt.Sprintf("terraform-testacc-subnet-data-source-%d", rInt) {
   117  			return fmt.Errorf("bad Name tag %s", attr["tags.Name"])
   118  		}
   119  
   120  		return nil
   121  	}
   122  }
   123  
   124  func testAccDataSourceAwsSubnetConfig(rInt int) string {
   125  	return fmt.Sprintf(`
   126  		provider "aws" {
   127  		  region = "us-west-2"
   128  		}
   129  
   130  		resource "aws_vpc" "test" {
   131  		  cidr_block = "172.%d.0.0/16"
   132  
   133  		  tags {
   134  		    Name = "terraform-testacc-subnet-data-source"
   135  		  }
   136  		}
   137  
   138  		resource "aws_subnet" "test" {
   139  		  vpc_id            = "${aws_vpc.test.id}"
   140  		  cidr_block        = "172.%d.123.0/24"
   141  		  availability_zone = "us-west-2a"
   142  
   143  		  tags {
   144  		    Name = "terraform-testacc-subnet-data-source-%d"
   145  		  }
   146  		}
   147  
   148  
   149  		data "aws_subnet" "by_id" {
   150  		  id = "${aws_subnet.test.id}"
   151  		}
   152  
   153  		data "aws_subnet" "by_cidr" {
   154  		  cidr_block = "${aws_subnet.test.cidr_block}"
   155  		}
   156  
   157  		data "aws_subnet" "by_tag" {
   158  		  tags {
   159  		    Name = "${aws_subnet.test.tags["Name"]}"
   160  		  }
   161  		}
   162  
   163  		data "aws_subnet" "by_vpc" {
   164  		  vpc_id = "${aws_subnet.test.vpc_id}"
   165  		}
   166  
   167  		data "aws_subnet" "by_filter" {
   168  		  filter {
   169  		    name = "vpc-id"
   170  		    values = ["${aws_subnet.test.vpc_id}"]
   171  		  }
   172  		}
   173  		`, rInt, rInt, rInt)
   174  }
   175  
   176  func testAccDataSourceAwsSubnetConfigIpv6(rInt int) string {
   177  	return fmt.Sprintf(`
   178  resource "aws_vpc" "test" {
   179    cidr_block = "172.%d.0.0/16"
   180    assign_generated_ipv6_cidr_block = true
   181  
   182    tags {
   183      Name = "terraform-testacc-subnet-data-source-ipv6"
   184    }
   185  }
   186  
   187  resource "aws_subnet" "test" {
   188    vpc_id            = "${aws_vpc.test.id}"
   189    cidr_block        = "172.%d.123.0/24"
   190    availability_zone = "us-west-2a"
   191    ipv6_cidr_block = "${cidrsubnet(aws_vpc.test.ipv6_cidr_block, 8, 1)}"
   192  
   193    tags {
   194      Name = "terraform-testacc-subnet-data-sourceipv6-%d"
   195    }
   196  }
   197  `, rInt, rInt, rInt)
   198  }
   199  
   200  func testAccDataSourceAwsSubnetConfigIpv6WithDataSourceFilter(rInt int) string {
   201  	return fmt.Sprintf(`
   202  resource "aws_vpc" "test" {
   203    cidr_block = "172.%d.0.0/16"
   204    assign_generated_ipv6_cidr_block = true
   205  
   206    tags {
   207      Name = "terraform-testacc-subnet-data-source-ipv6"
   208    }
   209  }
   210  
   211  resource "aws_subnet" "test" {
   212    vpc_id            = "${aws_vpc.test.id}"
   213    cidr_block        = "172.%d.123.0/24"
   214    availability_zone = "us-west-2a"
   215    ipv6_cidr_block = "${cidrsubnet(aws_vpc.test.ipv6_cidr_block, 8, 1)}"
   216  
   217    tags {
   218      Name = "terraform-testacc-subnet-data-sourceipv6-%d"
   219    }
   220  }
   221  
   222  data "aws_subnet" "by_ipv6_cidr" {
   223    filter {
   224      name = "ipv6-cidr-block-association.ipv6-cidr-block"
   225      values = ["${aws_subnet.test.ipv6_cidr_block}"]
   226    }
   227  }
   228  `, rInt, rInt, rInt)
   229  }
   230  
   231  func testAccDataSourceAwsSubnetConfigIpv6WithDataSourceIpv6CidrBlock(rInt int) string {
   232  	return fmt.Sprintf(`
   233  resource "aws_vpc" "test" {
   234    cidr_block = "172.%d.0.0/16"
   235    assign_generated_ipv6_cidr_block = true
   236  
   237    tags {
   238      Name = "terraform-testacc-subnet-data-source-ipv6"
   239    }
   240  }
   241  
   242  resource "aws_subnet" "test" {
   243    vpc_id            = "${aws_vpc.test.id}"
   244    cidr_block        = "172.%d.123.0/24"
   245    availability_zone = "us-west-2a"
   246    ipv6_cidr_block = "${cidrsubnet(aws_vpc.test.ipv6_cidr_block, 8, 1)}"
   247  
   248    tags {
   249      Name = "terraform-testacc-subnet-data-sourceipv6-%d"
   250    }
   251  }
   252  
   253  data "aws_subnet" "by_ipv6_cidr" {
   254    ipv6_cidr_block = "${aws_subnet.test.ipv6_cidr_block}"
   255  }
   256  `, rInt, rInt, rInt)
   257  }