github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_subnet_ids_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 ) 10 11 func TestAccDataSourceAwsSubnetIDs(t *testing.T) { 12 rInt := acctest.RandIntRange(0, 256) 13 resource.Test(t, resource.TestCase{ 14 PreCheck: func() { testAccPreCheck(t) }, 15 Providers: testAccProviders, 16 CheckDestroy: testAccCheckVpcDestroy, 17 Steps: []resource.TestStep{ 18 { 19 Config: testAccDataSourceAwsSubnetIDsConfig(rInt), 20 }, 21 { 22 Config: testAccDataSourceAwsSubnetIDsConfigWithDataSource(rInt), 23 Check: resource.ComposeTestCheckFunc( 24 resource.TestCheckResourceAttr("data.aws_subnet_ids.selected", "ids.#", "3"), 25 resource.TestCheckResourceAttr("data.aws_subnet_ids.private", "ids.#", "2"), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func testAccDataSourceAwsSubnetIDsConfigWithDataSource(rInt int) string { 33 return fmt.Sprintf( 34 ` 35 resource "aws_vpc" "test" { 36 cidr_block = "172.%d.0.0/16" 37 38 tags { 39 Name = "terraform-testacc-subnet-ids-data-source" 40 } 41 } 42 43 resource "aws_subnet" "test_public_a" { 44 vpc_id = "${aws_vpc.test.id}" 45 cidr_block = "172.%d.123.0/24" 46 availability_zone = "us-west-2a" 47 48 tags { 49 Name = "terraform-testacc-subnet-ids-data-source-public-a" 50 Tier = "Public" 51 } 52 } 53 54 resource "aws_subnet" "test_private_a" { 55 vpc_id = "${aws_vpc.test.id}" 56 cidr_block = "172.%d.125.0/24" 57 availability_zone = "us-west-2a" 58 59 tags { 60 Name = "terraform-testacc-subnet-ids-data-source-private-a" 61 Tier = "Private" 62 } 63 } 64 65 resource "aws_subnet" "test_private_b" { 66 vpc_id = "${aws_vpc.test.id}" 67 cidr_block = "172.%d.126.0/24" 68 availability_zone = "us-west-2b" 69 70 tags { 71 Name = "terraform-testacc-subnet-ids-data-source-private-b" 72 Tier = "Private" 73 } 74 } 75 76 data "aws_subnet_ids" "selected" { 77 vpc_id = "${aws_vpc.test.id}" 78 } 79 80 data "aws_subnet_ids" "private" { 81 vpc_id = "${aws_vpc.test.id}" 82 tags { 83 Tier = "Private" 84 } 85 } 86 `, rInt, rInt, rInt, rInt) 87 } 88 89 func testAccDataSourceAwsSubnetIDsConfig(rInt int) string { 90 return fmt.Sprintf(` 91 resource "aws_vpc" "test" { 92 cidr_block = "172.%d.0.0/16" 93 94 tags { 95 Name = "terraform-testacc-subnet-ids-data-source" 96 } 97 } 98 99 resource "aws_subnet" "test_public_a" { 100 vpc_id = "${aws_vpc.test.id}" 101 cidr_block = "172.%d.123.0/24" 102 availability_zone = "us-west-2a" 103 104 tags { 105 Name = "terraform-testacc-subnet-ids-data-source-public-a" 106 Tier = "Public" 107 } 108 } 109 110 resource "aws_subnet" "test_private_a" { 111 vpc_id = "${aws_vpc.test.id}" 112 cidr_block = "172.%d.125.0/24" 113 availability_zone = "us-west-2a" 114 115 tags { 116 Name = "terraform-testacc-subnet-ids-data-source-private-a" 117 Tier = "Private" 118 } 119 } 120 121 resource "aws_subnet" "test_private_b" { 122 vpc_id = "${aws_vpc.test.id}" 123 cidr_block = "172.%d.126.0/24" 124 availability_zone = "us-west-2b" 125 126 tags { 127 Name = "terraform-testacc-subnet-ids-data-source-private-b" 128 Tier = "Private" 129 } 130 } 131 `, rInt, rInt, rInt, rInt) 132 }