github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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/resource" 8 "github.com/hashicorp/terraform/terraform" 9 ) 10 11 func TestAccDataSourceAwsSubnet(t *testing.T) { 12 resource.Test(t, resource.TestCase{ 13 PreCheck: func() { testAccPreCheck(t) }, 14 Providers: testAccProviders, 15 Steps: []resource.TestStep{ 16 resource.TestStep{ 17 Config: testAccDataSourceAwsSubnetConfig, 18 Check: resource.ComposeTestCheckFunc( 19 testAccDataSourceAwsSubnetCheck("data.aws_subnet.by_id"), 20 testAccDataSourceAwsSubnetCheck("data.aws_subnet.by_cidr"), 21 testAccDataSourceAwsSubnetCheck("data.aws_subnet.by_tag"), 22 testAccDataSourceAwsSubnetCheck("data.aws_subnet.by_vpc"), 23 testAccDataSourceAwsSubnetCheck("data.aws_subnet.by_filter"), 24 ), 25 }, 26 }, 27 }) 28 } 29 30 func testAccDataSourceAwsSubnetCheck(name string) resource.TestCheckFunc { 31 return func(s *terraform.State) error { 32 rs, ok := s.RootModule().Resources[name] 33 if !ok { 34 return fmt.Errorf("root module has no resource called %s", name) 35 } 36 37 vpcRs, ok := s.RootModule().Resources["aws_vpc.test"] 38 if !ok { 39 return fmt.Errorf("can't find aws_vpc.test in state") 40 } 41 subnetRs, ok := s.RootModule().Resources["aws_subnet.test"] 42 if !ok { 43 return fmt.Errorf("can't find aws_subnet.test in state") 44 } 45 46 attr := rs.Primary.Attributes 47 48 if attr["id"] != subnetRs.Primary.Attributes["id"] { 49 return fmt.Errorf( 50 "id is %s; want %s", 51 attr["id"], 52 subnetRs.Primary.Attributes["id"], 53 ) 54 } 55 56 if attr["vpc_id"] != vpcRs.Primary.Attributes["id"] { 57 return fmt.Errorf( 58 "vpc_id is %s; want %s", 59 attr["vpc_id"], 60 vpcRs.Primary.Attributes["id"], 61 ) 62 } 63 64 if attr["cidr_block"] != "172.16.123.0/24" { 65 return fmt.Errorf("bad cidr_block %s", attr["cidr_block"]) 66 } 67 if attr["availability_zone"] != "us-west-2a" { 68 return fmt.Errorf("bad availability_zone %s", attr["availability_zone"]) 69 } 70 if attr["tags.Name"] != "terraform-testacc-subnet-data-source" { 71 return fmt.Errorf("bad Name tag %s", attr["tags.Name"]) 72 } 73 74 return nil 75 } 76 } 77 78 const testAccDataSourceAwsSubnetConfig = ` 79 provider "aws" { 80 region = "us-west-2" 81 } 82 83 resource "aws_vpc" "test" { 84 cidr_block = "172.16.0.0/16" 85 86 tags { 87 Name = "terraform-testacc-subnet-data-source" 88 } 89 } 90 91 resource "aws_subnet" "test" { 92 vpc_id = "${aws_vpc.test.id}" 93 cidr_block = "172.16.123.0/24" 94 availability_zone = "us-west-2a" 95 96 tags { 97 Name = "terraform-testacc-subnet-data-source" 98 } 99 } 100 101 data "aws_subnet" "by_id" { 102 id = "${aws_subnet.test.id}" 103 } 104 105 data "aws_subnet" "by_cidr" { 106 cidr_block = "${aws_subnet.test.cidr_block}" 107 } 108 109 data "aws_subnet" "by_tag" { 110 tags { 111 Name = "${aws_subnet.test.tags["Name"]}" 112 } 113 } 114 115 data "aws_subnet" "by_vpc" { 116 vpc_id = "${aws_subnet.test.vpc_id}" 117 } 118 119 data "aws_subnet" "by_filter" { 120 filter { 121 name = "vpc-id" 122 values = ["${aws_subnet.test.vpc_id}"] 123 } 124 } 125 `