github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/data_source_aws_route53_zone_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 TestAccDataSourceAwsRoute53Zone(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: testAccDataSourceAwsRoute53ZoneConfig, 18 Check: resource.ComposeTestCheckFunc( 19 testAccDataSourceAwsRoute53ZoneCheck("data.aws_route53_zone.by_zone_id"), 20 testAccDataSourceAwsRoute53ZoneCheck("data.aws_route53_zone.by_name"), 21 testAccDataSourceAwsRoute53ZoneCheckPrivate("data.aws_route53_zone.by_vpc"), 22 testAccDataSourceAwsRoute53ZoneCheckPrivate("data.aws_route53_zone.by_tag"), 23 ), 24 }, 25 }, 26 }) 27 } 28 29 func testAccDataSourceAwsRoute53ZoneCheck(name string) resource.TestCheckFunc { 30 return func(s *terraform.State) error { 31 rs, ok := s.RootModule().Resources[name] 32 if !ok { 33 return fmt.Errorf("root module has no resource called %s", name) 34 } 35 36 hostedZone, ok := s.RootModule().Resources["aws_route53_zone.test"] 37 if !ok { 38 return fmt.Errorf("can't find aws_hosted_zone.test in state") 39 } 40 attr := rs.Primary.Attributes 41 if attr["id"] != hostedZone.Primary.Attributes["id"] { 42 return fmt.Errorf( 43 "id is %s; want %s", 44 attr["id"], 45 hostedZone.Primary.Attributes["id"], 46 ) 47 } 48 49 if attr["name"] != "terraformtestacchz.com." { 50 return fmt.Errorf( 51 "Route53 Zone name is %s; want terraformtestacchz.com.", 52 attr["name"], 53 ) 54 } 55 56 return nil 57 } 58 } 59 60 func testAccDataSourceAwsRoute53ZoneCheckPrivate(name string) resource.TestCheckFunc { 61 return func(s *terraform.State) error { 62 rs, ok := s.RootModule().Resources[name] 63 if !ok { 64 return fmt.Errorf("root module has no resource called %s", name) 65 } 66 67 hostedZone, ok := s.RootModule().Resources["aws_route53_zone.test_private"] 68 if !ok { 69 return fmt.Errorf("can't find aws_hosted_zone.test in state") 70 } 71 72 attr := rs.Primary.Attributes 73 if attr["id"] != hostedZone.Primary.Attributes["id"] { 74 return fmt.Errorf( 75 "id is %s; want %s", 76 attr["id"], 77 hostedZone.Primary.Attributes["id"], 78 ) 79 } 80 81 if attr["name"] != "test.acc." { 82 return fmt.Errorf( 83 "Route53 Zone name is %s; want test.acc.", 84 attr["name"], 85 ) 86 } 87 88 return nil 89 } 90 } 91 92 const testAccDataSourceAwsRoute53ZoneConfig = ` 93 94 provider "aws" { 95 region = "us-east-2" 96 } 97 98 resource "aws_vpc" "test" { 99 cidr_block = "172.16.0.0/16" 100 } 101 102 resource "aws_route53_zone" "test_private" { 103 name = "test.acc." 104 vpc_id = "${aws_vpc.test.id}" 105 tags { 106 Environment = "dev" 107 } 108 } 109 data "aws_route53_zone" "by_vpc" { 110 name = "${aws_route53_zone.test_private.name}" 111 vpc_id = "${aws_vpc.test.id}" 112 } 113 114 data "aws_route53_zone" "by_tag" { 115 name = "${aws_route53_zone.test_private.name}" 116 private_zone = true 117 tags { 118 Environment = "dev" 119 } 120 } 121 122 resource "aws_route53_zone" "test" { 123 name = "terraformtestacchz.com." 124 } 125 data "aws_route53_zone" "by_zone_id" { 126 zone_id = "${aws_route53_zone.test.zone_id}" 127 } 128 129 data "aws_route53_zone" "by_name" { 130 name = "${data.aws_route53_zone.by_zone_id.name}" 131 } 132 133 `