2014-07-22 23:55:19 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-04-23 15:48:19 +02:00
|
|
|
"sort"
|
2014-07-22 23:55:19 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-05-08 21:47:42 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-22 23:55:19 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-02-12 17:48:48 +01:00
|
|
|
|
2015-04-16 20:42:16 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
|
|
|
"github.com/awslabs/aws-sdk-go/service/route53"
|
2014-07-22 23:55:19 +02:00
|
|
|
)
|
|
|
|
|
2015-02-12 17:48:48 +01:00
|
|
|
func TestCleanPrefix(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input, Prefix, Output string
|
|
|
|
}{
|
|
|
|
{"/hostedzone/foo", "/hostedzone/", "foo"},
|
|
|
|
{"/change/foo", "/change/", "foo"},
|
|
|
|
{"/bar", "/test", "/bar"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
actual := cleanPrefix(tc.Input, tc.Prefix)
|
|
|
|
if actual != tc.Output {
|
|
|
|
t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCleanZoneID(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input, Output string
|
|
|
|
}{
|
|
|
|
{"/hostedzone/foo", "foo"},
|
|
|
|
{"/change/foo", "/change/foo"},
|
|
|
|
{"/bar", "/bar"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
actual := cleanZoneID(tc.Input)
|
|
|
|
if actual != tc.Output {
|
|
|
|
t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCleanChangeID(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input, Output string
|
|
|
|
}{
|
|
|
|
{"/hostedzone/foo", "/hostedzone/foo"},
|
|
|
|
{"/change/foo", "foo"},
|
|
|
|
{"/bar", "/bar"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
actual := cleanChangeID(tc.Input)
|
|
|
|
if actual != tc.Output {
|
|
|
|
t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 16:41:59 +02:00
|
|
|
func TestAccRoute53Zone_basic(t *testing.T) {
|
2015-05-08 16:28:47 +02:00
|
|
|
var zone route53.GetHostedZoneOutput
|
2015-03-27 21:41:42 +01:00
|
|
|
var td route53.ResourceTagSet
|
|
|
|
|
2014-07-22 23:55:19 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckRoute53ZoneDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRoute53ZoneConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-03-27 21:41:42 +01:00
|
|
|
testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone),
|
|
|
|
testAccLoadTagsR53(&zone, &td),
|
|
|
|
testAccCheckTagsR53(&td.Tags, "foo", "bar"),
|
2014-07-22 23:55:19 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-11 16:41:59 +02:00
|
|
|
func TestAccRoute53Zone_private_basic(t *testing.T) {
|
2015-05-08 16:28:47 +02:00
|
|
|
var zone route53.GetHostedZoneOutput
|
2015-04-13 19:42:20 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckRoute53ZoneDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRoute53PrivateZoneConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone),
|
2015-05-08 17:59:31 +02:00
|
|
|
testAccCheckRoute53ZoneAssociatesWithVpc("aws_vpc.main", &zone),
|
2015-04-13 19:42:20 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-11 16:41:59 +02:00
|
|
|
func TestAccRoute53Zone_private_region(t *testing.T) {
|
2015-05-08 21:47:42 +02:00
|
|
|
var zone route53.GetHostedZoneOutput
|
|
|
|
|
|
|
|
// record the initialized providers so that we can use them to
|
|
|
|
// check for the instances in each region
|
|
|
|
var providers []*schema.Provider
|
|
|
|
providerFactories := map[string]terraform.ResourceProviderFactory{
|
|
|
|
"aws": func() (terraform.ResourceProvider, error) {
|
|
|
|
p := Provider()
|
|
|
|
providers = append(providers, p.(*schema.Provider))
|
|
|
|
return p, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2015-05-09 05:21:22 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
2015-05-08 21:47:42 +02:00
|
|
|
ProviderFactories: providerFactories,
|
2015-05-09 05:21:22 +02:00
|
|
|
CheckDestroy: testAccCheckRoute53ZoneDestroyWithProviders(&providers),
|
2015-05-08 21:47:42 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRoute53PrivateZoneRegionConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckRoute53ZoneExistsWithProviders("aws_route53_zone.main", &zone, &providers),
|
|
|
|
testAccCheckRoute53ZoneAssociatesWithVpc("aws_vpc.main", &zone),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-22 23:55:19 +02:00
|
|
|
func testAccCheckRoute53ZoneDestroy(s *terraform.State) error {
|
2015-05-08 21:47:42 +02:00
|
|
|
return testAccCheckRoute53ZoneDestroyWithProvider(s, testAccProvider)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckRoute53ZoneDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
for _, provider := range *providers {
|
|
|
|
if provider.Meta() == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := testAccCheckRoute53ZoneDestroyWithProvider(s, provider); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckRoute53ZoneDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
|
|
|
|
conn := provider.Meta().(*AWSClient).r53conn
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-22 23:55:19 +02:00
|
|
|
if rs.Type != "aws_route53_zone" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-04-16 20:42:16 +02:00
|
|
|
_, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(rs.Primary.ID)})
|
2014-07-22 23:55:19 +02:00
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Hosted zone still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-08 16:28:47 +02:00
|
|
|
func testAccCheckRoute53ZoneExists(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc {
|
2014-07-22 23:55:19 +02:00
|
|
|
return func(s *terraform.State) error {
|
2015-05-08 21:47:42 +02:00
|
|
|
return testAccCheckRoute53ZoneExistsWithProvider(s, n, zone, testAccProvider)
|
|
|
|
}
|
|
|
|
}
|
2014-07-22 23:55:19 +02:00
|
|
|
|
2015-05-08 21:47:42 +02:00
|
|
|
func testAccCheckRoute53ZoneExistsWithProviders(n string, zone *route53.GetHostedZoneOutput, providers *[]*schema.Provider) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
for _, provider := range *providers {
|
|
|
|
if provider.Meta() == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := testAccCheckRoute53ZoneExistsWithProvider(s, n, zone, provider); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-22 23:55:19 +02:00
|
|
|
}
|
2015-05-08 21:47:42 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2014-07-22 23:55:19 +02:00
|
|
|
|
2015-05-08 21:47:42 +02:00
|
|
|
func testAccCheckRoute53ZoneExistsWithProvider(s *terraform.State, n string, zone *route53.GetHostedZoneOutput, provider *schema.Provider) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
2015-04-14 21:17:01 +02:00
|
|
|
|
2015-05-08 21:47:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No hosted zone ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := provider.Meta().(*AWSClient).r53conn
|
|
|
|
resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{ID: aws.String(rs.Primary.ID)})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Hosted zone err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-09 05:21:22 +02:00
|
|
|
if !*resp.HostedZone.Config.PrivateZone {
|
2015-05-08 21:47:42 +02:00
|
|
|
sorted_ns := make([]string, len(resp.DelegationSet.NameServers))
|
|
|
|
for i, ns := range resp.DelegationSet.NameServers {
|
|
|
|
sorted_ns[i] = *ns
|
|
|
|
}
|
|
|
|
sort.Strings(sorted_ns)
|
|
|
|
for idx, ns := range sorted_ns {
|
|
|
|
attribute := fmt.Sprintf("name_servers.%d", idx)
|
|
|
|
dsns := rs.Primary.Attributes[attribute]
|
|
|
|
if dsns != ns {
|
|
|
|
return fmt.Errorf("Got: %v for %v, Expected: %v", dsns, attribute, ns)
|
2015-04-14 21:17:01 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-27 21:41:42 +01:00
|
|
|
}
|
2015-05-08 21:47:42 +02:00
|
|
|
|
|
|
|
*zone = *resp
|
|
|
|
return nil
|
2015-03-27 21:41:42 +01:00
|
|
|
}
|
|
|
|
|
2015-05-08 17:59:31 +02:00
|
|
|
func testAccCheckRoute53ZoneAssociatesWithVpc(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc {
|
2015-05-08 16:28:47 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No VPC ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
var associatedVPC *route53.VPC
|
|
|
|
for _, vpc := range zone.VPCs {
|
|
|
|
if *vpc.VPCID == rs.Primary.ID {
|
|
|
|
associatedVPC = vpc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if associatedVPC == nil {
|
2015-05-08 17:06:27 +02:00
|
|
|
return fmt.Errorf("VPC: %v is not associated to Zone: %v", n, cleanZoneID(*zone.HostedZone.ID))
|
2015-05-08 16:28:47 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccLoadTagsR53(zone *route53.GetHostedZoneOutput, td *route53.ResourceTagSet) resource.TestCheckFunc {
|
2015-03-27 21:41:42 +01:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).r53conn
|
|
|
|
|
2015-05-08 16:28:47 +02:00
|
|
|
zone := cleanZoneID(*zone.HostedZone.ID)
|
2015-04-16 20:42:16 +02:00
|
|
|
req := &route53.ListTagsForResourceInput{
|
2015-03-27 21:41:42 +01:00
|
|
|
ResourceID: aws.String(zone),
|
|
|
|
ResourceType: aws.String("hostedzone"),
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := conn.ListTagsForResource(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.ResourceTagSet != nil {
|
2015-03-27 22:05:54 +01:00
|
|
|
*td = *resp.ResourceTagSet
|
2015-03-27 21:41:42 +01:00
|
|
|
}
|
|
|
|
|
2014-07-22 23:55:19 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccRoute53ZoneConfig = `
|
|
|
|
resource "aws_route53_zone" "main" {
|
|
|
|
name = "hashicorp.com"
|
2015-03-27 21:41:42 +01:00
|
|
|
|
|
|
|
tags {
|
|
|
|
foo = "bar"
|
|
|
|
Name = "tf-route53-tag-test"
|
|
|
|
}
|
2014-07-22 23:55:19 +02:00
|
|
|
}
|
|
|
|
`
|
2015-04-13 19:42:20 +02:00
|
|
|
|
|
|
|
const testAccRoute53PrivateZoneConfig = `
|
|
|
|
resource "aws_vpc" "main" {
|
|
|
|
cidr_block = "172.29.0.0/24"
|
|
|
|
instance_tenancy = "default"
|
|
|
|
enable_dns_support = true
|
|
|
|
enable_dns_hostnames = true
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route53_zone" "main" {
|
|
|
|
name = "hashicorp.com"
|
|
|
|
vpc_id = "${aws_vpc.main.id}"
|
|
|
|
}
|
|
|
|
`
|
2015-05-08 21:47:42 +02:00
|
|
|
|
|
|
|
const testAccRoute53PrivateZoneRegionConfig = `
|
|
|
|
provider "aws" {
|
|
|
|
alias = "west"
|
|
|
|
region = "us-west-2"
|
|
|
|
}
|
|
|
|
|
|
|
|
provider "aws" {
|
|
|
|
alias = "east"
|
|
|
|
region = "us-east-1"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpc" "main" {
|
|
|
|
provider = "aws.east"
|
|
|
|
cidr_block = "172.29.0.0/24"
|
|
|
|
instance_tenancy = "default"
|
|
|
|
enable_dns_support = true
|
|
|
|
enable_dns_hostnames = true
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route53_zone" "main" {
|
|
|
|
provider = "aws.west"
|
|
|
|
name = "hashicorp.com"
|
|
|
|
vpc_id = "${aws_vpc.main.id}"
|
|
|
|
vpc_region = "us-east-1"
|
|
|
|
}
|
|
|
|
`
|