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