diff --git a/builtin/providers/google/resource_compute_instance.go b/builtin/providers/google/resource_compute_instance.go index 696d8ca912..f6b0fde7a1 100644 --- a/builtin/providers/google/resource_compute_instance.go +++ b/builtin/providers/google/resource_compute_instance.go @@ -100,6 +100,11 @@ func resourceComputeInstance() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "external_address": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, }, }, }, @@ -335,12 +340,27 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error d.Set("can_ip_forward", instance.CanIpForward) // Set the networks + externalIP := "" for i, iface := range instance.NetworkInterfaces { prefix := fmt.Sprintf("network.%d", i) d.Set(prefix+".name", iface.Name) + + // Use the first external IP found for the default connection info. + natIP := resourceInstanceNatIP(iface) + if externalIP == "" && natIP != "" { + externalIP = natIP + } + d.Set(prefix+".external_address", natIP) + d.Set(prefix+".internal_address", iface.NetworkIP) } + // Initialize the connection info + d.SetConnInfo(map[string]string{ + "type": "ssh", + "host": externalIP, + }) + // Set the metadata fingerprint if there is one. if instance.Metadata != nil { d.Set("metadata_fingerprint", instance.Metadata.Fingerprint) @@ -506,3 +526,16 @@ func resourceInstanceTags(d *schema.ResourceData) *compute.Tags { return tags } + +// resourceInstanceNatIP acquires the first NatIP with a "ONE_TO_ONE_NAT" type +// in the compute.NetworkInterface's AccessConfigs. +func resourceInstanceNatIP(iface *compute.NetworkInterface) (natIP string) { + for _, config := range iface.AccessConfigs { + if config.Type == "ONE_TO_ONE_NAT" { + natIP = config.NatIP + break + } + } + + return natIP +}