Skip to content

Commit

Permalink
remove statefuncs, revert route53 to current version and update upgra…
Browse files Browse the repository at this point in the history
…de guide
  • Loading branch information
anGie44 committed Jul 21, 2020
1 parent d5498db commit 44e6869
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 64 deletions.
11 changes: 7 additions & 4 deletions aws/resource_aws_route53_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ func resourceAwsRoute53Record() *schema.Resource {
MigrateState: resourceAwsRoute53RecordMigrateState,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: normalizeDomainName,
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: func(v interface{}) string {
value := strings.TrimSuffix(v.(string), ".")
return strings.ToLower(value)
},
},

"fqdn": {
Expand Down
3 changes: 1 addition & 2 deletions aws/resource_aws_route53_resolver_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func resourceAwsRoute53ResolverRule() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: normalizeDomainName,
ValidateFunc: validation.StringLenBetween(1, 256),
},

Expand Down Expand Up @@ -163,7 +162,7 @@ func resourceAwsRoute53ResolverRuleRead(d *schema.ResourceData, meta interface{}

rule := ruleRaw.(*route53resolver.ResolverRule)
d.Set("arn", rule.Arn)
d.Set("domain_name", normalizeDomainName(aws.StringValue(rule.DomainName)))
d.Set("domain_name", cleanDomainName(aws.StringValue(rule.DomainName)))
d.Set("name", rule.Name)
d.Set("owner_id", rule.OwnerId)
d.Set("resolver_endpoint_id", rule.ResolverEndpointId)
Expand Down
16 changes: 7 additions & 9 deletions aws/resource_aws_route53_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ func resourceAwsRoute53Zone() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: normalizeDomainName,
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"comment": {
Expand Down Expand Up @@ -179,7 +178,7 @@ func resourceAwsRoute53ZoneRead(d *schema.ResourceData, meta interface{}) error

d.Set("comment", "")
d.Set("delegation_set_id", "")
d.Set("name", normalizeDomainName(aws.StringValue(output.HostedZone.Name)))
d.Set("name", cleanDomainName(aws.StringValue(output.HostedZone.Name)))
d.Set("zone_id", cleanZoneID(aws.StringValue(output.HostedZone.Id)))

var nameServers []string
Expand Down Expand Up @@ -389,10 +388,9 @@ func cleanZoneID(ID string) string {
return strings.TrimPrefix(ID, "/hostedzone/")
}

// normalizeDomainName is used to remove the trailing period and enforce lowercase
func normalizeDomainName(v interface{}) string {
lc := strings.ToLower(v.(string))
return strings.TrimSuffix(lc, ".")
// cleanDomainName is used to remove the trailing period
func cleanDomainName(v interface{}) string {
return strings.TrimSuffix(v.(string), ".")
}

func getNameServers(zoneId string, zoneName string, r53 *route53.Route53) ([]string, error) {
Expand Down
71 changes: 22 additions & 49 deletions aws/resource_aws_route53_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ func TestCleanChangeID(t *testing.T) {
}
}

func TestNormalizeDomainName(t *testing.T) {
func TestCleanDomainName(t *testing.T) {
cases := []struct {
Input, Output string
}{
{"example.com", "example.com"},
{"example.com.", "example.com"},
{"www.example.com.", "www.example.com"},
{"www.Example.com.", "www.example.com"},
}

for _, tc := range cases {
actual := normalizeDomainName(tc.Input)
actual := cleanDomainName(tc.Input)
if actual != tc.Output {
t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
}
Expand Down Expand Up @@ -133,15 +133,15 @@ func TestAccAWSRoute53Zone_multiple(t *testing.T) {
Config: testAccRoute53ZoneConfigMultiple(),
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53ZoneExists("aws_route53_zone.test.0", &zone0),
testAccCheckDomainName(&zone0, "subdomain0.terraformtest.com."),
testAccCheckDomainName(&zone0, "subdomain0.terraformtest.com"),
testAccCheckRoute53ZoneExists("aws_route53_zone.test.1", &zone1),
testAccCheckDomainName(&zone1, "subdomain1.terraformtest.com."),
testAccCheckDomainName(&zone1, "subdomain1.terraformtest.com"),
testAccCheckRoute53ZoneExists("aws_route53_zone.test.2", &zone2),
testAccCheckDomainName(&zone2, "subdomain2.terraformtest.com."),
testAccCheckDomainName(&zone2, "subdomain2.terraformtest.com"),
testAccCheckRoute53ZoneExists("aws_route53_zone.test.3", &zone3),
testAccCheckDomainName(&zone3, "subdomain3.terraformtest.com."),
testAccCheckDomainName(&zone3, "subdomain3.terraformtest.com"),
testAccCheckRoute53ZoneExists("aws_route53_zone.test.4", &zone4),
testAccCheckDomainName(&zone4, "subdomain4.terraformtest.com."),
testAccCheckDomainName(&zone4, "subdomain4.terraformtest.com"),
),
},
},
Expand Down Expand Up @@ -239,30 +239,9 @@ func TestAccAWSRoute53Zone_ForceDestroy(t *testing.T) {
})
}

func TestAccAWSRoute53Zone_ForceDestroy_TrailingPeriod(t *testing.T) {
var zone route53.GetHostedZoneOutput

rString := acctest.RandString(8)
resourceName := "aws_route53_zone.test"
zoneName := fmt.Sprintf("%s.terraformtest.com", rString)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRoute53ZoneDestroy,
Steps: []resource.TestStep{
{
Config: testAccRoute53ZoneConfigForceDestroyTrailingPeriod(zoneName),
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53ZoneExists(resourceName, &zone),
// Add >100 records to verify pagination works ok
testAccCreateRandomRoute53RecordsInZoneId(&zone, 100),
testAccCreateRandomRoute53RecordsInZoneId(&zone, 5),
),
},
},
})
}
// TestAccAWSRoute53Zone_ForceDestroy_TrailingPeriod removed in v3.0.0
// as the name attribute in the resource is no longer stored with a trailing period
// Reference: https://github.com/terraform-providers/terraform-provider-aws/issues/13510

func TestAccAWSRoute53Zone_Tags(t *testing.T) {
var zone route53.GetHostedZoneOutput
Expand Down Expand Up @@ -571,7 +550,10 @@ func testAccCheckDomainName(zone *route53.GetHostedZoneOutput, domain string) re
return fmt.Errorf("Empty name in HostedZone for domain %s", domain)
}

if *zone.HostedZone.Name == domain {
// To compare the Hosted Zone Domain Name returned from the API
// and that stored in the resource, it too must by cleaned of
// the trailing period
if cleanDomainName(aws.StringValue(zone.HostedZone.Name)) == domain {
return nil
}

Expand All @@ -581,7 +563,7 @@ func testAccCheckDomainName(zone *route53.GetHostedZoneOutput, domain string) re
func testAccRoute53ZoneConfig(zoneName string) string {
return fmt.Sprintf(`
resource "aws_route53_zone" "test" {
name = "%s."
name = "%s"
}
`, zoneName)
}
Expand All @@ -600,7 +582,7 @@ func testAccRoute53ZoneConfigComment(zoneName, comment string) string {
return fmt.Sprintf(`
resource "aws_route53_zone" "test" {
comment = %q
name = "%s."
name = "%s"
}
`, comment, zoneName)
}
Expand All @@ -611,7 +593,7 @@ resource "aws_route53_delegation_set" "test" {}
resource "aws_route53_zone" "test" {
delegation_set_id = "${aws_route53_delegation_set.test.id}"
name = "%s."
name = "%s"
}
`, zoneName)
}
Expand All @@ -625,19 +607,10 @@ resource "aws_route53_zone" "test" {
`, zoneName)
}

func testAccRoute53ZoneConfigForceDestroyTrailingPeriod(zoneName string) string {
return fmt.Sprintf(`
resource "aws_route53_zone" "test" {
force_destroy = true
name = "%s."
}
`, zoneName)
}

func testAccRoute53ZoneConfigTagsSingle(zoneName, tag1Key, tag1Value string) string {
return fmt.Sprintf(`
resource "aws_route53_zone" "test" {
name = "%s."
name = "%s"
tags = {
%q = %q
Expand All @@ -649,7 +622,7 @@ resource "aws_route53_zone" "test" {
func testAccRoute53ZoneConfigTagsMultiple(zoneName, tag1Key, tag1Value, tag2Key, tag2Value string) string {
return fmt.Sprintf(`
resource "aws_route53_zone" "test" {
name = "%s."
name = "%s"
tags = {
%q = %q
Expand All @@ -670,7 +643,7 @@ resource "aws_vpc" "test1" {
}
resource "aws_route53_zone" "test" {
name = "%s."
name = "%s"
vpc {
vpc_id = "${aws_vpc.test1.id}"
Expand Down Expand Up @@ -698,7 +671,7 @@ resource "aws_vpc" "test2" {
}
resource "aws_route53_zone" "test" {
name = "%s."
name = "%s"
vpc {
vpc_id = "${aws_vpc.test1.id}"
Expand Down
14 changes: 14 additions & 0 deletions website/docs/guides/version-3-upgrade.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Upgrade topics:
- [Resource: aws_launch_template](#resource-aws_launch_template)
- [Resource: aws_lb_listener_rule](#resource-aws_lb_listener_rule)
- [Resource: aws_msk_cluster](#resource-aws_msk_cluster)
- [Resource: aws_route53_resolver_rule](#resource-aws_route53_resolver_rule)
- [Resource: aws_route53_zone](#resource-aws_route53_zone)
- [Resource: aws_s3_bucket](#resource-aws_s3_bucket)
- [Resource: aws_security_group](#resource-aws_security_group)
- [Resource: aws_sns_platform_application](#resource-aws_sns_platform_application)
Expand Down Expand Up @@ -475,6 +477,18 @@ resource "aws_msk_cluster" "example" {
}
```

## Resource: aws_route53_resolver_rule

### Removal of domain_name trailing period

Previously the resource returned the Domain Name directly from the API, which included a `.` suffix. This proves difficult when many other AWS services do not accept this trailing period (e.g. API Gateway Domain Name). This period is now automatically removed. For example, when the attribute would previously return a Domain Name such as `www.example.com.`, the attribute now will be returned as `www.example.com`.

## Resource: aws_route53_zone

### Removal of name trailing period

Previously the resource returned the Hosted Zone Domain Name directly from the API, which included a `.` suffix. This proves difficult when many other AWS services do not accept this trailing period (e.g. API Gateway Domain Name). This period is now automatically removed. For example, when the attribute would previously return a Hosted Zone Domain Name such as `www.example.com.`, the attribute now will be returned as `www.example.com`.

## Resource: aws_s3_bucket

### Removal of Automatic aws_s3_bucket_policy Import
Expand Down

0 comments on commit 44e6869

Please sign in to comment.