Added access_ip_v6 support

This commit populates access_ip_v6 by either the AccessIPv6 attribute
or by finding the first available IPv6 address.

This commit retains the original feature of setting the default ssh
connection to the IPv4 address unless one is not found. IPv6 access
can still be enabled by explicitly setting it in the resource paramters.

This commit also removes d.Set("host") in favor of SetConnInfo
This commit is contained in:
Joe Topjian 2015-02-10 05:47:35 +00:00 committed by Jon Perritt
parent 633e98dffe
commit 4c9a44b69f
1 changed files with 50 additions and 14 deletions

View File

@ -326,42 +326,78 @@ func resourceComputeInstanceV2Read(d *schema.ResourceData, meta interface{}) err
d.Set("access_ip_v4", server.AccessIPv4) d.Set("access_ip_v4", server.AccessIPv4)
d.Set("access_ip_v6", server.AccessIPv6) d.Set("access_ip_v6", server.AccessIPv6)
host := server.AccessIPv4 hostv4 := server.AccessIPv4
if host == "" { if hostv4 == "" {
if publicAddressesRaw, ok := server.Addresses["public"]; ok { if publicAddressesRaw, ok := server.Addresses["public"]; ok {
publicAddresses := publicAddressesRaw.([]interface{}) publicAddresses := publicAddressesRaw.([]interface{})
for _, paRaw := range publicAddresses { for _, paRaw := range publicAddresses {
pa := paRaw.(map[string]interface{}) pa := paRaw.(map[string]interface{})
if pa["version"].(float64) == 4 { if pa["version"].(float64) == 4 {
host = pa["addr"].(string) hostv4 = pa["addr"].(string)
break break
} }
} }
} }
} }
// If no host found, just get the first IP we find // If no host found, just get the first IPv4 we find
if host == "" { if hostv4 == "" {
for _, networkAddresses := range server.Addresses { for _, networkAddresses := range server.Addresses {
for _, element := range networkAddresses.([]interface{}) { for _, element := range networkAddresses.([]interface{}) {
address := element.(map[string]interface{}) address := element.(map[string]interface{})
if address["version"].(float64) == 4 { if address["version"].(float64) == 4 {
host = address["addr"].(string) hostv4 = address["addr"].(string)
break break
} }
} }
} }
} }
d.Set("access_ip_v4", host) d.Set("access_ip_v4", hostv4)
d.Set("host", host) log.Printf("hostv4: %s", hostv4)
log.Printf("host: %s", host) hostv6 := server.AccessIPv6
if hostv6 == "" {
if publicAddressesRaw, ok := server.Addresses["public"]; ok {
publicAddresses := publicAddressesRaw.([]interface{})
for _, paRaw := range publicAddresses {
pa := paRaw.(map[string]interface{})
if pa["version"].(float64) == 4 {
hostv6 = fmt.Sprintf("[%s]", pa["addr"].(string))
break
}
}
}
}
// If no hostv6 found, just get the first IPv6 we find
if hostv6 == "" {
for _, networkAddresses := range server.Addresses {
for _, element := range networkAddresses.([]interface{}) {
address := element.(map[string]interface{})
if address["version"].(float64) == 6 {
hostv6 = fmt.Sprintf("[%s]", address["addr"].(string))
break
}
}
}
}
d.Set("access_ip_v6", hostv6)
log.Printf("hostv6: %s", hostv6)
preferredv := ""
if hostv4 != "" {
preferredv = hostv4
} else if hostv6 != "" {
preferredv = hostv6
}
if preferredv != "" {
// Initialize the connection info // Initialize the connection info
d.SetConnInfo(map[string]string{ d.SetConnInfo(map[string]string{
"type": "ssh", "type": "ssh",
"host": host, "host": preferredv,
}) })
}
d.Set("metadata", server.Metadata) d.Set("metadata", server.Metadata)