From 142927151ad9abffa62341fa9e9dd891cfc61fd0 Mon Sep 17 00:00:00 2001
From: Gary Kotton <gkotton@vmware.com>
Date: Wed, 24 Jan 2018 19:23:47 +0200
Subject: [PATCH] NSX|V3: enahance admin utility for metadata proxy list

This will now list the status of the service on the NSX.

Depends-On: I542dd84a6690410744a22328001ac9c2de0a53b6
Change-Id: Ie93c1c7092da4ddc07c8868a774e0969bf51338d
---
 .../plugins/nsxv3/resources/metadata_proxy.py | 32 +++++++++++++++----
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/vmware_nsx/shell/admin/plugins/nsxv3/resources/metadata_proxy.py b/vmware_nsx/shell/admin/plugins/nsxv3/resources/metadata_proxy.py
index dee42b5a96..e73c4859ff 100644
--- a/vmware_nsx/shell/admin/plugins/nsxv3/resources/metadata_proxy.py
+++ b/vmware_nsx/shell/admin/plugins/nsxv3/resources/metadata_proxy.py
@@ -50,12 +50,32 @@ def _is_metadata_network(network):
 @admin_utils.output_header
 def list_metadata_networks(resource, event, trigger, **kwargs):
     """List Metadata networks in Neutron."""
-
-    meta_networks = [network for network in neutron_client.get_networks()
-                     if _is_metadata_network(network)]
-    LOG.info(formatters.output_formatter(constants.METADATA_PROXY,
-                                         meta_networks,
-                                         ['id', 'name', 'subnets']))
+    if not cfg.CONF.nsx_v3.native_metadata_route:
+        meta_networks = [network
+                         for network in neutron_client.get_networks()
+                         if _is_metadata_network(network)]
+        LOG.info(formatters.output_formatter(constants.METADATA_PROXY,
+                                             meta_networks,
+                                             ['id', 'name', 'subnets']))
+    else:
+        nsxlib = utils.get_connected_nsxlib()
+        tags = [{'scope': 'os-neutron-net-id'}]
+        ports = nsxlib.search_by_tags(resource_type='LogicalPort', tags=tags)
+        for port in ports['results']:
+            if port['attachment']['attachment_type'] == 'METADATA_PROXY':
+                net_id = None
+                for tag in port['tags']:
+                    if tag['scope'] == 'os-neutron-net-id':
+                        net_id = tag['tag']
+                        break
+                uri = '/md-proxies/%s/%s/status' % (port['attachment']['id'],
+                                                    port['logical_switch_id'])
+                status = nsxlib.client.get(uri)
+                LOG.info("Status for MD proxy on neutron network %s (logical "
+                         "switch %s) is %s",
+                         net_id,
+                         port['logical_switch_id'],
+                         status.get('proxy_status', 'Unknown'))
 
 
 @admin_utils.output_header