github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_route53_delegation_set_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  	"github.com/hashicorp/terraform/terraform"
    11  
    12  	"github.com/aws/aws-sdk-go/aws"
    13  	"github.com/aws/aws-sdk-go/service/route53"
    14  )
    15  
    16  func TestAccAWSRoute53DelegationSet_basic(t *testing.T) {
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:        func() { testAccPreCheck(t) },
    19  		IDRefreshName:   "aws_route53_delegation_set.test",
    20  		IDRefreshIgnore: []string{"reference_name"},
    21  		Providers:       testAccProviders,
    22  		CheckDestroy:    testAccCheckRoute53ZoneDestroy,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccRoute53DelegationSetConfig,
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckRoute53DelegationSetExists("aws_route53_delegation_set.test"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccAWSRoute53DelegationSet_withZones(t *testing.T) {
    35  	var zone route53.GetHostedZoneOutput
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:        func() { testAccPreCheck(t) },
    39  		IDRefreshName:   "aws_route53_delegation_set.main",
    40  		IDRefreshIgnore: []string{"reference_name"},
    41  		Providers:       testAccProviders,
    42  		CheckDestroy:    testAccCheckRoute53ZoneDestroy,
    43  		Steps: []resource.TestStep{
    44  			resource.TestStep{
    45  				Config: testAccRoute53DelegationSetWithZonesConfig,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccCheckRoute53DelegationSetExists("aws_route53_delegation_set.main"),
    48  					testAccCheckRoute53ZoneExists("aws_route53_zone.primary", &zone),
    49  					testAccCheckRoute53ZoneExists("aws_route53_zone.secondary", &zone),
    50  					testAccCheckRoute53NameServersMatch("aws_route53_delegation_set.main", "aws_route53_zone.primary"),
    51  					testAccCheckRoute53NameServersMatch("aws_route53_delegation_set.main", "aws_route53_zone.secondary"),
    52  				),
    53  			},
    54  		},
    55  	})
    56  }
    57  
    58  func testAccCheckRoute53DelegationSetDestroy(s *terraform.State, provider *schema.Provider) error {
    59  	conn := provider.Meta().(*AWSClient).r53conn
    60  	for _, rs := range s.RootModule().Resources {
    61  		if rs.Type != "aws_route53_delegation_set" {
    62  			continue
    63  		}
    64  
    65  		_, err := conn.GetReusableDelegationSet(&route53.GetReusableDelegationSetInput{Id: aws.String(rs.Primary.ID)})
    66  		if err == nil {
    67  			return fmt.Errorf("Delegation set still exists")
    68  		}
    69  	}
    70  	return nil
    71  }
    72  
    73  func testAccCheckRoute53DelegationSetExists(n string) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  		conn := testAccProvider.Meta().(*AWSClient).r53conn
    76  		rs, ok := s.RootModule().Resources[n]
    77  		if !ok {
    78  			return fmt.Errorf("Not found: %s", n)
    79  		}
    80  
    81  		if rs.Primary.ID == "" {
    82  			return fmt.Errorf("No delegation set ID is set")
    83  		}
    84  
    85  		out, err := conn.GetReusableDelegationSet(&route53.GetReusableDelegationSetInput{
    86  			Id: aws.String(rs.Primary.ID),
    87  		})
    88  
    89  		if err != nil {
    90  			return fmt.Errorf("Delegation set does not exist: %#v", rs.Primary.ID)
    91  		}
    92  
    93  		setID := cleanDelegationSetId(*out.DelegationSet.Id)
    94  		if setID != rs.Primary.ID {
    95  			return fmt.Errorf("Delegation set ID does not match:\nExpected: %#v\nReturned: %#v", rs.Primary.ID, setID)
    96  		}
    97  
    98  		return nil
    99  	}
   100  }
   101  
   102  func testAccCheckRoute53NameServersMatch(delegationSetName, zoneName string) resource.TestCheckFunc {
   103  	return func(s *terraform.State) error {
   104  		conn := testAccProvider.Meta().(*AWSClient).r53conn
   105  
   106  		delegationSetLocal, ok := s.RootModule().Resources[delegationSetName]
   107  		if !ok {
   108  			return fmt.Errorf("Not found: %s", delegationSetName)
   109  		}
   110  		delegationSet, err := conn.GetReusableDelegationSet(&route53.GetReusableDelegationSetInput{
   111  			Id: aws.String(delegationSetLocal.Primary.ID),
   112  		})
   113  		if err != nil {
   114  			return fmt.Errorf("Delegation set does not exist: %#v", delegationSetLocal.Primary.ID)
   115  		}
   116  
   117  		hostedZoneLocal, ok := s.RootModule().Resources[zoneName]
   118  		if !ok {
   119  			return fmt.Errorf("Not found: %s", zoneName)
   120  		}
   121  		hostedZone, err := conn.GetHostedZone(&route53.GetHostedZoneInput{
   122  			Id: aws.String(hostedZoneLocal.Primary.ID),
   123  		})
   124  		if err != nil {
   125  			return fmt.Errorf("Delegation set does not exist: %#v", hostedZoneLocal.Primary.ID)
   126  		}
   127  
   128  		if !reflect.DeepEqual(delegationSet.DelegationSet.NameServers, hostedZone.DelegationSet.NameServers) {
   129  			return fmt.Errorf("Name servers do not match:\nDelegation Set: %#v\nHosted Zone:%#v",
   130  				delegationSet.DelegationSet.NameServers, hostedZone.DelegationSet.NameServers)
   131  		}
   132  
   133  		return nil
   134  	}
   135  }
   136  
   137  const testAccRoute53DelegationSetConfig = `
   138  resource "aws_route53_delegation_set" "test" {
   139  	reference_name = "test"
   140  }
   141  `
   142  
   143  const testAccRoute53DelegationSetWithZonesConfig = `
   144  resource "aws_route53_delegation_set" "main" {
   145      reference_name = "main"
   146  }
   147  
   148  resource "aws_route53_zone" "primary" {
   149      name = "hashicorp.com"
   150      delegation_set_id = "${aws_route53_delegation_set.main.id}"
   151  }
   152  
   153  resource "aws_route53_zone" "secondary" {
   154      name = "terraform.io"
   155      delegation_set_id = "${aws_route53_delegation_set.main.id}"
   156  }
   157  `