From fd979d0a8e94740bb000a499781ba4fb063fd196 Mon Sep 17 00:00:00 2001 From: stungtoat Date: Sat, 18 Oct 2014 23:03:37 -0700 Subject: [PATCH 1/4] providers/google: add external_address; needed for connection --- builtin/providers/google/resource_compute_instance.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/builtin/providers/google/resource_compute_instance.go b/builtin/providers/google/resource_compute_instance.go index 696d8ca91..8a091c430 100644 --- a/builtin/providers/google/resource_compute_instance.go +++ b/builtin/providers/google/resource_compute_instance.go @@ -95,11 +95,15 @@ func resourceComputeInstance() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "internal_address": &schema.Schema{ Type: schema.TypeString, Computed: true, }, + "external_address": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + }, }, }, @@ -338,6 +342,11 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error for i, iface := range instance.NetworkInterfaces { prefix := fmt.Sprintf("network.%d", i) d.Set(prefix+".name", iface.Name) + + if len(iface.AccessConfigs) > 0 { + // Get the first one. + d.Set(prefix+".external_address", iface.AccessConfigs[0].NatIP) + } d.Set(prefix+".internal_address", iface.NetworkIP) } From 6cc10998ec041b1732ae43336aa67b79737fc6bf Mon Sep 17 00:00:00 2001 From: stungtoat Date: Sat, 18 Oct 2014 23:15:43 -0700 Subject: [PATCH 2/4] go fmt --- builtin/providers/google/resource_compute_instance.go | 1 - 1 file changed, 1 deletion(-) diff --git a/builtin/providers/google/resource_compute_instance.go b/builtin/providers/google/resource_compute_instance.go index 8a091c430..c8ce03ae9 100644 --- a/builtin/providers/google/resource_compute_instance.go +++ b/builtin/providers/google/resource_compute_instance.go @@ -103,7 +103,6 @@ func resourceComputeInstance() *schema.Resource { Type: schema.TypeString, Computed: true, }, - }, }, }, From fe4e89bef747daaf84a75fc08db2ebf364c3e45b Mon Sep 17 00:00:00 2001 From: stungtoat Date: Sat, 18 Oct 2014 23:17:14 -0700 Subject: [PATCH 3/4] consistent spacing --- builtin/providers/google/resource_compute_instance.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/builtin/providers/google/resource_compute_instance.go b/builtin/providers/google/resource_compute_instance.go index c8ce03ae9..2c757d31e 100644 --- a/builtin/providers/google/resource_compute_instance.go +++ b/builtin/providers/google/resource_compute_instance.go @@ -95,10 +95,12 @@ func resourceComputeInstance() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "internal_address": &schema.Schema{ Type: schema.TypeString, Computed: true, }, + "external_address": &schema.Schema{ Type: schema.TypeString, Computed: true, From afa1666cad21a7674af9f07fdf70711a569b41a7 Mon Sep 17 00:00:00 2001 From: stungtoat Date: Sun, 19 Oct 2014 00:04:17 -0700 Subject: [PATCH 4/4] set default host on connection info --- .../google/resource_compute_instance.go | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/builtin/providers/google/resource_compute_instance.go b/builtin/providers/google/resource_compute_instance.go index 2c757d31e..f6b0fde7a 100644 --- a/builtin/providers/google/resource_compute_instance.go +++ b/builtin/providers/google/resource_compute_instance.go @@ -340,17 +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) - if len(iface.AccessConfigs) > 0 { - // Get the first one. - d.Set(prefix+".external_address", iface.AccessConfigs[0].NatIP) + // 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) @@ -516,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 +}