Merge pull request #26 from akutz/feature/local-ip-addrs

Reflect local IP addresses back into guestinfo
diff --git a/DataSourceVMwareGuestInfo.py b/DataSourceVMwareGuestInfo.py
index ab392be..3c59406 100644
--- a/DataSourceVMwareGuestInfo.py
+++ b/DataSourceVMwareGuestInfo.py
@@ -41,6 +41,8 @@
 VMWARE_RPCTOOL = find_executable("vmware-rpctool")
 VMX_GUESTINFO = "VMX_GUESTINFO"
 GUESTINFO_EMPTY_YAML_VAL = "---"
+LOCAL_IPV4 = 'local-ipv4'
+LOCAL_IPV6 = 'local-ipv6'
 
 
 class NetworkConfigError(Exception):
@@ -144,6 +146,10 @@
         host_info = get_host_info()
         LOG.info("got host-info: %s", host_info)
 
+        # Reflect any possible local IPv4 or IPv6 addresses in the guest
+        # info.
+        advertise_local_ip_addrs(host_info)
+
         # Ensure the metadata gets updated with information about the
         # host, including the network interfaces, default IP addresses,
         # etc.
@@ -255,6 +261,28 @@
     return val
 
 
+def advertise_local_ip_addrs(host_info):
+    '''
+    advertise_local_ip_addrs gets the local IP address information from
+    the provided host_info map and sets the addresses in the guestinfo
+    namespace
+    '''
+    if not host_info:
+        return
+
+    # Reflect any possible local IPv4 or IPv6 addresses in the guest
+    # info.
+    local_ipv4 = host_info[LOCAL_IPV4]
+    if local_ipv4:
+        set_guestinfo_value(LOCAL_IPV4, local_ipv4)
+        LOG.info("advertised local ipv4 address %s in guestinfo", local_ipv4)
+
+    local_ipv6 = host_info[LOCAL_IPV6]
+    if local_ipv6:
+        set_guestinfo_value(LOCAL_IPV6, local_ipv6)
+        LOG.info("advertised local ipv6 address %s in guestinfo", local_ipv6)
+
+
 def handle_returned_guestinfo_val(key, val):
     '''
     handle_returned_guestinfo_val returns the provided value if it is
@@ -546,9 +574,9 @@
 
     default_ipv4, default_ipv6 = get_default_ip_addrs()
     if default_ipv4:
-        host_info['local-ipv4'] = default_ipv4
+        host_info[LOCAL_IPV4] = default_ipv4
     if default_ipv6:
-        host_info['local-ipv6'] = default_ipv6
+        host_info[LOCAL_IPV6] = default_ipv6
 
     by_mac = host_info['network']['interfaces']['by-mac']
     by_ipv4 = host_info['network']['interfaces']['by-ipv4']
diff --git a/README.md b/README.md
index 487f441..37afd50 100644
--- a/README.md
+++ b/README.md
@@ -171,6 +171,15 @@
 
 Please note that keys are set to the valid YAML string `---` as it is not possible remove an existing key from the guestinfo key-space. A key's analogous encoding property will be set to a single white-space character, causing the datasource to treat the actual key value as plain-text, thereby loading it as an empty YAML doc (hence the aforementioned `---`).
 
+### Reading the local IP addresses
+
+This datasource automatically discovers the local IPv4 and IPv6 addresses for a guest operating system based on the default routes. However, when inspecting a VM externally, it's not possible to know what the _default_ IP address is for the guest OS. That's why this datasource sets the discovered, local IPv4 and IPv6 addresses back in the guestinfo namespace as the following keys:
+
+* `guestinfo.local-ipv4`
+* `guestinfo.local-ipv6`
+
+It is possible that a host may not have any default, local IP addresses. It's also possible the reported, local addresses are link-local addresses. But these two keys may be used to discover what this datasource determined were the local IPv4 and IPv6 addresses for a host.
+
 ## Building the RPM
 
 Building the RPM locally is handled via Docker. Simple execute the following command: