Skip to content

Commit

Permalink
resource/cloudflare_zone: Add unicode support
Browse files Browse the repository at this point in the history
Introduce unicode support for using zone names and ensure that the
schema comparison is performed after first converting both values to
unicode. This change ensures that the punycode and unicode values are
both stored as the unicode version to match what the Cloudflare API
returns.

Closes #399
  • Loading branch information
jacobbednarz committed Jul 16, 2019
1 parent c100df2 commit 37454bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
24 changes: 19 additions & 5 deletions cloudflare/resource_cloudflare_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package cloudflare
import (
"fmt"
"log"
"regexp"
"strconv"
"strings"

"golang.org/x/net/idna"

cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
Expand Down Expand Up @@ -40,10 +41,10 @@ func resourceCloudflareZone() *schema.Resource {

Schema: map[string]*schema.Schema{
"zone": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile("^([a-zA-Z0-9][\\-a-zA-Z0-9]*\\.)+[\\-a-zA-Z0-9]{2,20}$"), ""),
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: zoneDiffFunc,
},
"jump_start": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -264,3 +265,16 @@ func planNameForID(id string) string {
}
return ""
}

// zoneDiffFunc is a DiffSuppressFunc that accepts two strings and then converts
// them to unicode before performing the comparison whether or not the value has
// changed. This ensures that zones which could be either are evaluated
// consistently and align with what the Cloudflare API returns.
func zoneDiffFunc(k, old, new string, d *schema.ResourceData) bool {
var p *idna.Profile
p = idna.New()
unicodeOld, _ := p.ToUnicode(old)
unicodeNew, _ := p.ToUnicode(new)

return unicodeOld == unicodeNew
}
6 changes: 3 additions & 3 deletions cloudflare/resource_cloudflare_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestZone(t *testing.T) {
})
}

func TestZoneWithUnicodeIsStoredAsUnicode(t *testing.T) {
func TestAccZoneWithUnicodeIsStoredAsUnicode(t *testing.T) {
name := "cloudflare_zone.tf-acc-unicode-test-1"

resource.Test(t, resource.TestCase{
Expand All @@ -79,7 +79,7 @@ func TestZoneWithUnicodeIsStoredAsUnicode(t *testing.T) {
})
}

func TestZoneWithoutUnicodeIsStoredAsUnicode(t *testing.T) {
func TestAccZoneWithoutUnicodeIsStoredAsUnicode(t *testing.T) {
name := "cloudflare_zone.tf-acc-unicode-test-2"

resource.Test(t, resource.TestCase{
Expand All @@ -100,7 +100,7 @@ func TestZoneWithoutUnicodeIsStoredAsUnicode(t *testing.T) {
})
}

func TestZonePerformsUnicodeComparison(t *testing.T) {
func TestAccZonePerformsUnicodeComparison(t *testing.T) {
name := "cloudflare_zone.tf-acc-unicode-test-3"

resource.Test(t, resource.TestCase{
Expand Down

0 comments on commit 37454bd

Please sign in to comment.