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 15, 2019
1 parent c100df2 commit 73f7cd8
Showing 1 changed file with 19 additions and 5 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
}

0 comments on commit 73f7cd8

Please sign in to comment.