From 09151b77e94ee6c4f9367fc81305655cb3a896df Mon Sep 17 00:00:00 2001
From: Adit Sarfaty <asarfaty@vmware.com>
Date: Tue, 16 Jan 2018 15:13:35 +0200
Subject: [PATCH] TVD|AdminUtils: Add all nsxv/v3 utils to tvd

The user of the TVD plugin can use the admin utilities of the nsxv/nsxv3 plugins
by calling:
nsxadmin --plugin nsxv -r <> -o <>
or:
nsxadmin --plugin nsxv3 -r <> -o <>

A separate patch will make sure that only the relevant neutron objects are being
retrived when using those utilities.

Change-Id: I813f69bf2e08f3845f0135fdb00666746f5d20c6
---
 doc/source/admin_util.rst     |  5 +++++
 vmware_nsx/shell/nsxadmin.py  | 35 ++++++++++++++++++++++++++++++-----
 vmware_nsx/shell/resources.py |  2 ++
 3 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/doc/source/admin_util.rst b/doc/source/admin_util.rst
index 7fd77bd81b..bea53a3914 100644
--- a/doc/source/admin_util.rst
+++ b/doc/source/admin_util.rst
@@ -475,7 +475,12 @@ LBaaS
 NSXtvd
 ------
 
+- All the nsx-v/v3 utilities can be used by calling
+
+    nsxadmin --plugin nsxv/v3 -r <> -o <>
+
 - Add mapping between projects and plugin before starting to use the tvd plugin:
+
     nsxadmin -r projects -o import --property plugin=nsx-v --property project=<>
 
 Config
diff --git a/vmware_nsx/shell/nsxadmin.py b/vmware_nsx/shell/nsxadmin.py
index 461a3d616b..29714dba59 100644
--- a/vmware_nsx/shell/nsxadmin.py
+++ b/vmware_nsx/shell/nsxadmin.py
@@ -110,16 +110,41 @@ def _validate_op_choice(choice, nsx_plugin):
             sys.exit(1)
 
 
+def _validate_plugin_choice(selected_plugin, nsx_plugin):
+    if nsx_plugin == 'nsxtvd':
+        if selected_plugin:
+            if selected_plugin != 'nsxv' and selected_plugin != 'nsxv3':
+                LOG.error('Illegal plugin %s. please select nsxv or nsxv3',
+                          selected_plugin)
+                sys.exit(1)
+            # use nsxv or nsxv3 plugins
+            return selected_plugin
+        else:
+            # use the TVD pluging
+            return nsx_plugin
+    else:
+        if selected_plugin:
+            LOG.error('Cannot select plugin. The current plugin is %s',
+                      nsx_plugin)
+            sys.exit(1)
+        return nsx_plugin
+
+
 def main(argv=sys.argv[1:]):
     _init_cfg()
     nsx_plugin_in_use = resources.get_plugin()
-    resources.init_resource_plugin(
-        nsx_plugin_in_use,
-        resources.get_plugin_dir(nsx_plugin_in_use))
     LOG.info('NSX Plugin in use: %s', nsx_plugin_in_use)
 
-    _validate_resource_choice(cfg.CONF.resource, nsx_plugin_in_use)
-    _validate_op_choice(cfg.CONF.operation, nsx_plugin_in_use)
+    # the user can select the specific plugin
+    selected_plugin = _validate_plugin_choice(cfg.CONF.plugin,
+                                              nsx_plugin_in_use)
+
+    resources.init_resource_plugin(
+        selected_plugin,
+        resources.get_plugin_dir(selected_plugin))
+
+    _validate_resource_choice(cfg.CONF.resource, selected_plugin)
+    _validate_op_choice(cfg.CONF.operation, selected_plugin)
 
     registry.notify(cfg.CONF.resource, cfg.CONF.operation, 'nsxadmin',
                     force=cfg.CONF.force, property=cfg.CONF.property,
diff --git a/vmware_nsx/shell/resources.py b/vmware_nsx/shell/resources.py
index 8e71d77a90..46a832962c 100644
--- a/vmware_nsx/shell/resources.py
+++ b/vmware_nsx/shell/resources.py
@@ -271,6 +271,8 @@ cli_opts = [cfg.StrOpt('fmt',
                        short='o',
                        help='Supported list of operations: {}'
                              .format(', '.join(ops))),
+            cfg.StrOpt('plugin',
+                       help='nsxv or nsxv3 if the tvd plugin is used'),
             cfg.BoolOpt('force',
                         default=False,
                         help='Enables \'force\' mode. No confirmations will '