Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_route53_record: Suppress uppercase alias name diff #3119

Merged
merged 2 commits into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions aws/resource_aws_route53_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ func resourceAwsRoute53Record() *schema.Resource {
Type: schema.TypeString,
Required: true,
StateFunc: normalizeAwsAliasName,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if strings.ToLower(old) == strings.ToLower(new) {
return true
}
return false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole function can be reduced to a single line:

return strings.ToLower(old) == strings.ToLower(new)

😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 good catch

},
},

"evaluate_target_health": {
Expand Down Expand Up @@ -898,11 +904,11 @@ func nilString(s string) *string {

func normalizeAwsAliasName(alias interface{}) string {
input := alias.(string)
if strings.HasPrefix(input, "dualstack.") {
return strings.Replace(input, "dualstack.", "", -1)
output := strings.ToLower(input)
if strings.HasPrefix(output, "dualstack.") {
output = strings.TrimLeft(output, "dualstack.")
}

return strings.TrimRight(input, ".")
return strings.TrimRight(output, ".")
}

func parseRecordId(id string) [4]string {
Expand Down
67 changes: 67 additions & 0 deletions aws/resource_aws_route53_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ func TestExpandRecordName(t *testing.T) {
}
}

func TestNormalizeAwsAliasName(t *testing.T) {
cases := []struct {
Input, Output string
}{
{"www.nonexample.com", "www.nonexample.com"},
{"www.nonexample.com.", "www.nonexample.com"},
{"dualstack.name-123456789.region.elb.amazonaws.com", "name-123456789.region.elb.amazonaws.com"},
{"NAME-123456789.region.elb.amazonaws.com", "name-123456789.region.elb.amazonaws.com"},
}

for _, tc := range cases {
actual := normalizeAwsAliasName(tc.Input)
if actual != tc.Output {
t.Fatalf("input: %s\noutput: %s", tc.Input, actual)
}
}
}

func TestParseRecordId(t *testing.T) {
cases := []struct {
Input, Zone, Name, Type, Set string
Expand Down Expand Up @@ -280,6 +298,25 @@ func TestAccAWSRoute53Record_alias(t *testing.T) {
})
}

func TestAccAWSRoute53Record_aliasUppercase(t *testing.T) {
rs := acctest.RandString(10)
config := fmt.Sprintf(testAccRoute53ElbAliasRecordUppercase, rs)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_route53_record.alias",
Providers: testAccProviders,
CheckDestroy: testAccCheckRoute53RecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53RecordExists("aws_route53_record.alias"),
),
},
},
})
}

func TestAccAWSRoute53Record_s3_alias(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -968,6 +1005,36 @@ resource "aws_elb" "main" {
}
`

const testAccRoute53ElbAliasRecordUppercase = `
resource "aws_route53_zone" "main" {
name = "notexample.com"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know other zones are static (so far), but can you randomize the name here, please? We can address others in a separate PR in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You got it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha! You'll love this. We actually hardcode it in the exists/destroy functions too: https://github.com/terraform-providers/terraform-provider-aws/blob/b-aws_route53_record-alias-name-diffsuppressfunc/aws/resource_aws_route53_record_test.go#L586

Its all or nothing 👎 -- I will get that nitpick merged though 👍

}

resource "aws_route53_record" "alias" {
zone_id = "${aws_route53_zone.main.zone_id}"
name = "www"
type = "A"

alias {
zone_id = "${aws_elb.main.zone_id}"
name = "${aws_elb.main.dns_name}"
evaluate_target_health = true
}
}

resource "aws_elb" "main" {
name = "FOOBAR-TERRAFORM-ELB-%s"
availability_zones = ["us-west-2a"]

listener {
instance_port = 80
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
}
`

const testAccRoute53AliasRecord = `
resource "aws_route53_zone" "main" {
name = "notexample.com"
Expand Down