mirror of https://github.com/hashicorp/terraform
parent
d772389518
commit
bc5518f993
@ -0,0 +1,17 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
// IpFormat formats the IP correctly, so we don't provide IPv6 address in an IPv4 format during node communication. We return the ip parameter as is if it's an IPv4 address or a hostname.
|
||||
func IpFormat(ip string) string {
|
||||
ipObj := net.ParseIP(ip)
|
||||
// Return the ip/host as is if it's either a hostname or an IPv4 address.
|
||||
if ipObj == nil || ipObj.To4() != nil {
|
||||
return ip
|
||||
}
|
||||
|
||||
return fmt.Sprintf("[%s]", ip)
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIpFormatting_Ipv4(t *testing.T) {
|
||||
formatted := IpFormat("127.0.0.1")
|
||||
if formatted != "127.0.0.1" {
|
||||
t.Fatal("expected", "127.0.0.1", "got", formatted)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIpFormatting_Hostname(t *testing.T) {
|
||||
formatted := IpFormat("example.com")
|
||||
if formatted != "example.com" {
|
||||
t.Fatal("expected", "example.com", "got", formatted)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIpFormatting_Ipv6(t *testing.T) {
|
||||
formatted := IpFormat("::1")
|
||||
if formatted != "[::1]" {
|
||||
t.Fatal("expected", "[::1]", "got", formatted)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue