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

Bug: azurerm_dns_aaaa_record - Normalize IPv6 addresses #5459

Merged
merged 9 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
17 changes: 17 additions & 0 deletions azurerm/helpers/azure/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package azure

import (
"fmt"
"net"
)

phires marked this conversation as resolved.
Show resolved Hide resolved
// CompressIPv6Address creates a canonicalized IPv6 address according to RFC5952
func CompressIPv6Address(ipv6address string) (string, error) {
// Validate, if IPv6 address
ip := net.ParseIP(ipv6address)
if six := ip.To16(); six == nil {
return "", fmt.Errorf("%q is not a valid IPv6 address", ipv6address)
}

return ip.String(), nil
}
28 changes: 28 additions & 0 deletions azurerm/helpers/azure/network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package azure

import (
"testing"
)

func TestCompressIPv6Address(t *testing.T) {
cases := []struct {
input string
expected string
}{
{
input: "2607:f8b0:4005:0800:0000:0000:0000:1003",
expected: "2607:f8b0:4005:800::1003",
},
{
input: "2001:0db8:1234:0000:0000:0000:0000:0000",
expected: "2001:db8:1234::",
},
}

for _, v := range cases {
actual, _ := CompressIPv6Address(v.input)
if v.expected != actual {
t.Fatalf("Expected %q but got %q", v.expected, actual)
}
}
}
19 changes: 16 additions & 3 deletions azurerm/internal/services/dns/resource_arm_dns_aaaa_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dns

import (
"fmt"
"net"
"net/http"
"time"

Expand Down Expand Up @@ -48,9 +49,14 @@ func resourceArmDnsAAAARecord() *schema.Resource {
},

"records": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Copy link
Collaborator

Choose a reason for hiding this comment

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

And we should add a validate.IPv6Address

DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return ipv6AddressDiffSuppress(k, old, new, d)
},
},
Set: schema.HashString,
ConflictsWith: []string{"target_resource_id"},
},
Expand Down Expand Up @@ -207,6 +213,13 @@ func resourceArmDnsAaaaRecordDelete(d *schema.ResourceData, meta interface{}) er
return nil
}

func ipv6AddressDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
phires marked this conversation as resolved.
Show resolved Hide resolved
oldIp := net.ParseIP(old)
newIp := net.ParseIP(new)

return oldIp.Equal(newIp)
phires marked this conversation as resolved.
Show resolved Hide resolved
}

func expandAzureRmDnsAaaaRecords(input []interface{}) *[]dns.AaaaRecord {
records := make([]dns.AaaaRecord, len(input))

Expand Down