2015-05-16 19:12:53 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-05-31 16:05:41 +02:00
|
|
|
"reflect"
|
2015-05-16 19:12:53 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
|
2015-05-31 16:05:41 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/route53"
|
2015-05-16 19:12:53 +02:00
|
|
|
)
|
|
|
|
|
2015-07-21 16:42:02 +02:00
|
|
|
func TestAccAWSRoute53DelegationSet_basic(t *testing.T) {
|
2015-05-16 19:12:53 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 22:58:56 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_route53_delegation_set.test",
|
|
|
|
IDRefreshIgnore: []string{"reference_name"},
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckRoute53ZoneDestroy,
|
2015-05-16 19:12:53 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRoute53DelegationSetConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckRoute53DelegationSetExists("aws_route53_delegation_set.test"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-21 16:42:02 +02:00
|
|
|
func TestAccAWSRoute53DelegationSet_withZones(t *testing.T) {
|
2015-05-31 16:05:41 +02:00
|
|
|
var zone route53.GetHostedZoneOutput
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 22:58:56 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_route53_delegation_set.main",
|
|
|
|
IDRefreshIgnore: []string{"reference_name"},
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckRoute53ZoneDestroy,
|
2015-05-31 16:05:41 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccRoute53DelegationSetWithZonesConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckRoute53DelegationSetExists("aws_route53_delegation_set.main"),
|
|
|
|
testAccCheckRoute53ZoneExists("aws_route53_zone.primary", &zone),
|
|
|
|
testAccCheckRoute53ZoneExists("aws_route53_zone.secondary", &zone),
|
|
|
|
testAccCheckRoute53NameServersMatch("aws_route53_delegation_set.main", "aws_route53_zone.primary"),
|
|
|
|
testAccCheckRoute53NameServersMatch("aws_route53_delegation_set.main", "aws_route53_zone.secondary"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-16 19:12:53 +02:00
|
|
|
func testAccCheckRoute53DelegationSetDestroy(s *terraform.State, provider *schema.Provider) error {
|
|
|
|
conn := provider.Meta().(*AWSClient).r53conn
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_route53_delegation_set" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
_, err := conn.GetReusableDelegationSet(&route53.GetReusableDelegationSetInput{Id: aws.String(rs.Primary.ID)})
|
2015-05-16 19:12:53 +02:00
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Delegation set still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckRoute53DelegationSetExists(n string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).r53conn
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No delegation set ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := conn.GetReusableDelegationSet(&route53.GetReusableDelegationSetInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
Id: aws.String(rs.Primary.ID),
|
2015-05-16 19:12:53 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Delegation set does not exist: %#v", rs.Primary.ID)
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
setID := cleanDelegationSetId(*out.DelegationSet.Id)
|
2015-05-16 19:12:53 +02:00
|
|
|
if setID != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Delegation set ID does not match:\nExpected: %#v\nReturned: %#v", rs.Primary.ID, setID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-31 16:05:41 +02:00
|
|
|
func testAccCheckRoute53NameServersMatch(delegationSetName, zoneName string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).r53conn
|
|
|
|
|
|
|
|
delegationSetLocal, ok := s.RootModule().Resources[delegationSetName]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", delegationSetName)
|
|
|
|
}
|
|
|
|
delegationSet, err := conn.GetReusableDelegationSet(&route53.GetReusableDelegationSetInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
Id: aws.String(delegationSetLocal.Primary.ID),
|
2015-05-31 16:05:41 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Delegation set does not exist: %#v", delegationSetLocal.Primary.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
hostedZoneLocal, ok := s.RootModule().Resources[zoneName]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", zoneName)
|
|
|
|
}
|
|
|
|
hostedZone, err := conn.GetHostedZone(&route53.GetHostedZoneInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
Id: aws.String(hostedZoneLocal.Primary.ID),
|
2015-05-31 16:05:41 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Delegation set does not exist: %#v", hostedZoneLocal.Primary.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(delegationSet.DelegationSet.NameServers, hostedZone.DelegationSet.NameServers) {
|
|
|
|
return fmt.Errorf("Name servers do not match:\nDelegation Set: %#v\nHosted Zone:%#v",
|
|
|
|
delegationSet.DelegationSet.NameServers, hostedZone.DelegationSet.NameServers)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-16 19:12:53 +02:00
|
|
|
const testAccRoute53DelegationSetConfig = `
|
|
|
|
resource "aws_route53_delegation_set" "test" {
|
|
|
|
reference_name = "test"
|
|
|
|
}
|
|
|
|
`
|
2015-05-31 16:05:41 +02:00
|
|
|
|
|
|
|
const testAccRoute53DelegationSetWithZonesConfig = `
|
|
|
|
resource "aws_route53_delegation_set" "main" {
|
|
|
|
reference_name = "main"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route53_zone" "primary" {
|
|
|
|
name = "hashicorp.com"
|
|
|
|
delegation_set_id = "${aws_route53_delegation_set.main.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route53_zone" "secondary" {
|
|
|
|
name = "terraform.io"
|
|
|
|
delegation_set_id = "${aws_route53_delegation_set.main.id}"
|
|
|
|
}
|
|
|
|
`
|