github.com/keshavdv/terraform@v0.7.0-rc2.0.20160711232630-d69256dcb425/builtin/providers/aws/resource_aws_route53_zone_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     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 TestCleanPrefix(t *testing.T) {
    17  	cases := []struct {
    18  		Input, Prefix, Output string
    19  	}{
    20  		{"/hostedzone/foo", "/hostedzone/", "foo"},
    21  		{"/change/foo", "/change/", "foo"},
    22  		{"/bar", "/test", "/bar"},
    23  	}
    24  
    25  	for _, tc := range cases {
    26  		actual := cleanPrefix(tc.Input, tc.Prefix)
    27  		if actual != tc.Output {
    28  			t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
    29  		}
    30  	}
    31  }
    32  
    33  func TestCleanZoneID(t *testing.T) {
    34  	cases := []struct {
    35  		Input, Output string
    36  	}{
    37  		{"/hostedzone/foo", "foo"},
    38  		{"/change/foo", "/change/foo"},
    39  		{"/bar", "/bar"},
    40  	}
    41  
    42  	for _, tc := range cases {
    43  		actual := cleanZoneID(tc.Input)
    44  		if actual != tc.Output {
    45  			t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
    46  		}
    47  	}
    48  }
    49  
    50  func TestCleanChangeID(t *testing.T) {
    51  	cases := []struct {
    52  		Input, Output string
    53  	}{
    54  		{"/hostedzone/foo", "/hostedzone/foo"},
    55  		{"/change/foo", "foo"},
    56  		{"/bar", "/bar"},
    57  	}
    58  
    59  	for _, tc := range cases {
    60  		actual := cleanChangeID(tc.Input)
    61  		if actual != tc.Output {
    62  			t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
    63  		}
    64  	}
    65  }
    66  
    67  func TestAccAWSRoute53Zone_basic(t *testing.T) {
    68  	var zone route53.GetHostedZoneOutput
    69  	var td route53.ResourceTagSet
    70  
    71  	resource.Test(t, resource.TestCase{
    72  		PreCheck:      func() { testAccPreCheck(t) },
    73  		IDRefreshName: "aws_route53_zone.main",
    74  		Providers:     testAccProviders,
    75  		CheckDestroy:  testAccCheckRoute53ZoneDestroy,
    76  		Steps: []resource.TestStep{
    77  			resource.TestStep{
    78  				Config: testAccRoute53ZoneConfig,
    79  				Check: resource.ComposeTestCheckFunc(
    80  					testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone),
    81  					testAccLoadTagsR53(&zone, &td),
    82  					testAccCheckTagsR53(&td.Tags, "foo", "bar"),
    83  				),
    84  			},
    85  		},
    86  	})
    87  }
    88  
    89  func TestAccAWSRoute53Zone_updateComment(t *testing.T) {
    90  	var zone route53.GetHostedZoneOutput
    91  	var td route53.ResourceTagSet
    92  
    93  	resource.Test(t, resource.TestCase{
    94  		PreCheck:      func() { testAccPreCheck(t) },
    95  		IDRefreshName: "aws_route53_zone.main",
    96  		Providers:     testAccProviders,
    97  		CheckDestroy:  testAccCheckRoute53ZoneDestroy,
    98  		Steps: []resource.TestStep{
    99  			resource.TestStep{
   100  				Config: testAccRoute53ZoneConfig,
   101  				Check: resource.ComposeTestCheckFunc(
   102  					testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone),
   103  					testAccLoadTagsR53(&zone, &td),
   104  					testAccCheckTagsR53(&td.Tags, "foo", "bar"),
   105  					resource.TestCheckResourceAttr(
   106  						"aws_route53_zone.main", "comment", "Custom comment"),
   107  				),
   108  			},
   109  
   110  			resource.TestStep{
   111  				Config: testAccRoute53ZoneConfigUpdateComment,
   112  				Check: resource.ComposeTestCheckFunc(
   113  					testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone),
   114  					testAccLoadTagsR53(&zone, &td),
   115  					resource.TestCheckResourceAttr(
   116  						"aws_route53_zone.main", "comment", "Change Custom Comment"),
   117  				),
   118  			},
   119  		},
   120  	})
   121  }
   122  
   123  func TestAccAWSRoute53Zone_private_basic(t *testing.T) {
   124  	var zone route53.GetHostedZoneOutput
   125  
   126  	resource.Test(t, resource.TestCase{
   127  		PreCheck:      func() { testAccPreCheck(t) },
   128  		IDRefreshName: "aws_route53_zone.main",
   129  		Providers:     testAccProviders,
   130  		CheckDestroy:  testAccCheckRoute53ZoneDestroy,
   131  		Steps: []resource.TestStep{
   132  			resource.TestStep{
   133  				Config: testAccRoute53PrivateZoneConfig,
   134  				Check: resource.ComposeTestCheckFunc(
   135  					testAccCheckRoute53ZoneExists("aws_route53_zone.main", &zone),
   136  					testAccCheckRoute53ZoneAssociatesWithVpc("aws_vpc.main", &zone),
   137  				),
   138  			},
   139  		},
   140  	})
   141  }
   142  
   143  func TestAccAWSRoute53Zone_private_region(t *testing.T) {
   144  	var zone route53.GetHostedZoneOutput
   145  
   146  	// record the initialized providers so that we can use them to
   147  	// check for the instances in each region
   148  	var providers []*schema.Provider
   149  	providerFactories := map[string]terraform.ResourceProviderFactory{
   150  		"aws": func() (terraform.ResourceProvider, error) {
   151  			p := Provider()
   152  			providers = append(providers, p.(*schema.Provider))
   153  			return p, nil
   154  		},
   155  	}
   156  
   157  	resource.Test(t, resource.TestCase{
   158  		PreCheck:          func() { testAccPreCheck(t) },
   159  		IDRefreshName:     "aws_route53_zone.main",
   160  		ProviderFactories: providerFactories,
   161  		CheckDestroy:      testAccCheckRoute53ZoneDestroyWithProviders(&providers),
   162  		Steps: []resource.TestStep{
   163  			resource.TestStep{
   164  				Config: testAccRoute53PrivateZoneRegionConfig,
   165  				Check: resource.ComposeTestCheckFunc(
   166  					testAccCheckRoute53ZoneExistsWithProviders("aws_route53_zone.main", &zone, &providers),
   167  					testAccCheckRoute53ZoneAssociatesWithVpc("aws_vpc.main", &zone),
   168  				),
   169  			},
   170  		},
   171  	})
   172  }
   173  
   174  func testAccCheckRoute53ZoneDestroy(s *terraform.State) error {
   175  	return testAccCheckRoute53ZoneDestroyWithProvider(s, testAccProvider)
   176  }
   177  
   178  func testAccCheckRoute53ZoneDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc {
   179  	return func(s *terraform.State) error {
   180  		for _, provider := range *providers {
   181  			if provider.Meta() == nil {
   182  				continue
   183  			}
   184  			if err := testAccCheckRoute53ZoneDestroyWithProvider(s, provider); err != nil {
   185  				return err
   186  			}
   187  		}
   188  		return nil
   189  	}
   190  }
   191  
   192  func testAccCheckRoute53ZoneDestroyWithProvider(s *terraform.State, provider *schema.Provider) error {
   193  	conn := provider.Meta().(*AWSClient).r53conn
   194  	for _, rs := range s.RootModule().Resources {
   195  		if rs.Type != "aws_route53_zone" {
   196  			continue
   197  		}
   198  
   199  		_, err := conn.GetHostedZone(&route53.GetHostedZoneInput{Id: aws.String(rs.Primary.ID)})
   200  		if err == nil {
   201  			return fmt.Errorf("Hosted zone still exists")
   202  		}
   203  	}
   204  	return nil
   205  }
   206  
   207  func testAccCheckRoute53ZoneExists(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc {
   208  	return func(s *terraform.State) error {
   209  		return testAccCheckRoute53ZoneExistsWithProvider(s, n, zone, testAccProvider)
   210  	}
   211  }
   212  
   213  func testAccCheckRoute53ZoneExistsWithProviders(n string, zone *route53.GetHostedZoneOutput, providers *[]*schema.Provider) resource.TestCheckFunc {
   214  	return func(s *terraform.State) error {
   215  		for _, provider := range *providers {
   216  			if provider.Meta() == nil {
   217  				continue
   218  			}
   219  			if err := testAccCheckRoute53ZoneExistsWithProvider(s, n, zone, provider); err != nil {
   220  				return err
   221  			}
   222  		}
   223  		return nil
   224  	}
   225  }
   226  
   227  func testAccCheckRoute53ZoneExistsWithProvider(s *terraform.State, n string, zone *route53.GetHostedZoneOutput, provider *schema.Provider) error {
   228  	rs, ok := s.RootModule().Resources[n]
   229  	if !ok {
   230  		return fmt.Errorf("Not found: %s", n)
   231  	}
   232  
   233  	if rs.Primary.ID == "" {
   234  		return fmt.Errorf("No hosted zone ID is set")
   235  	}
   236  
   237  	conn := provider.Meta().(*AWSClient).r53conn
   238  	resp, err := conn.GetHostedZone(&route53.GetHostedZoneInput{Id: aws.String(rs.Primary.ID)})
   239  	if err != nil {
   240  		return fmt.Errorf("Hosted zone err: %v", err)
   241  	}
   242  
   243  	aws_comment := *resp.HostedZone.Config.Comment
   244  	rs_comment := rs.Primary.Attributes["comment"]
   245  	if rs_comment != "" && rs_comment != aws_comment {
   246  		return fmt.Errorf("Hosted zone with comment '%s' found but does not match '%s'", aws_comment, rs_comment)
   247  	}
   248  
   249  	if !*resp.HostedZone.Config.PrivateZone {
   250  		sorted_ns := make([]string, len(resp.DelegationSet.NameServers))
   251  		for i, ns := range resp.DelegationSet.NameServers {
   252  			sorted_ns[i] = *ns
   253  		}
   254  		sort.Strings(sorted_ns)
   255  		for idx, ns := range sorted_ns {
   256  			attribute := fmt.Sprintf("name_servers.%d", idx)
   257  			dsns := rs.Primary.Attributes[attribute]
   258  			if dsns != ns {
   259  				return fmt.Errorf("Got: %v for %v, Expected: %v", dsns, attribute, ns)
   260  			}
   261  		}
   262  	}
   263  
   264  	*zone = *resp
   265  	return nil
   266  }
   267  
   268  func testAccCheckRoute53ZoneAssociatesWithVpc(n string, zone *route53.GetHostedZoneOutput) resource.TestCheckFunc {
   269  	return func(s *terraform.State) error {
   270  		rs, ok := s.RootModule().Resources[n]
   271  		if !ok {
   272  			return fmt.Errorf("Not found: %s", n)
   273  		}
   274  
   275  		if rs.Primary.ID == "" {
   276  			return fmt.Errorf("No VPC ID is set")
   277  		}
   278  
   279  		var associatedVPC *route53.VPC
   280  		for _, vpc := range zone.VPCs {
   281  			if *vpc.VPCId == rs.Primary.ID {
   282  				associatedVPC = vpc
   283  			}
   284  		}
   285  		if associatedVPC == nil {
   286  			return fmt.Errorf("VPC: %v is not associated to Zone: %v", n, cleanZoneID(*zone.HostedZone.Id))
   287  		}
   288  		return nil
   289  	}
   290  }
   291  
   292  func testAccLoadTagsR53(zone *route53.GetHostedZoneOutput, td *route53.ResourceTagSet) resource.TestCheckFunc {
   293  	return func(s *terraform.State) error {
   294  		conn := testAccProvider.Meta().(*AWSClient).r53conn
   295  
   296  		zone := cleanZoneID(*zone.HostedZone.Id)
   297  		req := &route53.ListTagsForResourceInput{
   298  			ResourceId:   aws.String(zone),
   299  			ResourceType: aws.String("hostedzone"),
   300  		}
   301  
   302  		resp, err := conn.ListTagsForResource(req)
   303  		if err != nil {
   304  			return err
   305  		}
   306  
   307  		if resp.ResourceTagSet != nil {
   308  			*td = *resp.ResourceTagSet
   309  		}
   310  
   311  		return nil
   312  	}
   313  }
   314  
   315  const testAccRoute53ZoneConfig = `
   316  resource "aws_route53_zone" "main" {
   317  	name = "hashicorp.com."
   318  	comment = "Custom comment"
   319  
   320  	tags {
   321  		foo = "bar"
   322  		Name = "tf-route53-tag-test"
   323  	}
   324  }
   325  `
   326  
   327  const testAccRoute53ZoneConfigUpdateComment = `
   328  resource "aws_route53_zone" "main" {
   329  	name = "hashicorp.com."
   330  	comment = "Change Custom Comment"
   331  
   332  	tags {
   333  		foo = "bar"
   334  		Name = "tf-route53-tag-test"
   335  	}
   336  }
   337  `
   338  
   339  const testAccRoute53PrivateZoneConfig = `
   340  resource "aws_vpc" "main" {
   341  	cidr_block = "172.29.0.0/24"
   342  	instance_tenancy = "default"
   343  	enable_dns_support = true
   344  	enable_dns_hostnames = true
   345  }
   346  
   347  resource "aws_route53_zone" "main" {
   348  	name = "hashicorp.com."
   349  	vpc_id = "${aws_vpc.main.id}"
   350  }
   351  `
   352  
   353  const testAccRoute53PrivateZoneRegionConfig = `
   354  provider "aws" {
   355  	alias = "west"
   356  	region = "us-west-2"
   357  }
   358  
   359  provider "aws" {
   360  	alias = "east"
   361  	region = "us-east-1"
   362  }
   363  
   364  resource "aws_vpc" "main" {
   365  	provider = "aws.east"
   366  	cidr_block = "172.29.0.0/24"
   367  	instance_tenancy = "default"
   368  	enable_dns_support = true
   369  	enable_dns_hostnames = true
   370  }
   371  
   372  resource "aws_route53_zone" "main" {
   373  	provider = "aws.west"
   374  	name = "hashicorp.com."
   375  	vpc_id = "${aws_vpc.main.id}"
   376  	vpc_region = "us-east-1"
   377  }
   378  `