-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug:
azurerm_dns_aaaa_record
- Normalize IPv6 addresses (#5459)
fixa #5321 .
- Loading branch information
Showing
4 changed files
with
168 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package azure | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode" | ||
) | ||
|
||
// NormalizeIPv6Address returns the normalized notation of an IPv6 | ||
func NormalizeIPv6Address(ipv6 interface{}) string { | ||
if ipv6 == nil || ipv6.(string) == "" { | ||
return "" | ||
} | ||
r := net.ParseIP(ipv6.(string)) | ||
if r == nil { | ||
return "" | ||
} | ||
return r.String() | ||
} | ||
|
||
// HashIPv6Address normalizes an IPv6 address and returns a hash for it | ||
func HashIPv6Address(ipv6 interface{}) int { | ||
return hashcode.String(NormalizeIPv6Address(ipv6)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package azure | ||
|
||
import "testing" | ||
|
||
func TestIPv6Compression(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Input interface{} | ||
Output string | ||
Valid bool | ||
}{ | ||
{ | ||
Name: "input empty", | ||
Input: "", | ||
Output: "", | ||
Valid: true, | ||
}, | ||
{ | ||
Name: "valid IPv6 input, invalid compression", | ||
Input: "2001:0db8:85a3:0:0:8a2e:0370:7334", | ||
Output: "2001:0db8:85a3:0:0:8a2e:0370:7334", | ||
Valid: false, | ||
}, | ||
{ | ||
Name: "invalid IPv6 input", | ||
Input: "2001::invalid", | ||
Output: "", | ||
Valid: false, | ||
}, | ||
{ | ||
Name: "valid IPv6 compression", | ||
Input: "2001:0db8:85a3:0:0:8a2e:0370:7334", | ||
Output: "2001:db8:85a3::8a2e:370:7334", | ||
Valid: true, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
r := NormalizeIPv6Address(tc.Input) | ||
if (r != tc.Output) && tc.Valid { | ||
t.Fatalf("Expected NormalizeIPv6Address to return '%q' for '%q' (got '%q')", tc.Output, tc.Input, r) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestHashIPv6Address(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Input interface{} | ||
Output int | ||
}{ | ||
{ | ||
Name: "input empty", | ||
Input: "", | ||
Output: 0, | ||
}, | ||
{ | ||
Name: "uncompressed", | ||
Input: "2001:0db8:85a3:0:0:8a2e:0370:7334", | ||
Output: 3242211790, | ||
}, | ||
{ | ||
Name: "invalid", | ||
Input: "2001::invalid", | ||
Output: 0, | ||
}, | ||
{ | ||
Name: "compressed", | ||
Input: "2001:db8:85a3::8a2e:370:7334", | ||
Output: 3242211790, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
r := HashIPv6Address(tc.Input) | ||
if r != tc.Output { | ||
t.Fatalf("Expected HashIPv6Address to return '%d' for '%q' (got '%d')", tc.Output, tc.Input, r) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters