Nsx admin: Initialize nsx-lib on demand

Avoid initializing cluster (including xsrf token acquisition) on
process init. Connection parameters may change as a result of admin
command, in which case connections will need to be reinitialized.

Change-Id: I7131cab15df82eea1e4bdc8a48e853abb2a3a575
This commit is contained in:
Anna Khmelnitsky 2017-09-01 14:31:13 -07:00
parent 9ac5edd12e
commit 3c51978d6b
7 changed files with 25 additions and 7 deletions

View File

@ -29,7 +29,6 @@ from vmware_nsxlib.v3 import nsx_constants
LOG = logging.getLogger(__name__)
neutron_client = utils.NeutronDbClient()
nsxlib = utils.get_connected_nsxlib()
@admin_utils.output_header
@ -46,6 +45,7 @@ def list_dhcp_bindings(resource, event, trigger, **kwargs):
def nsx_update_dhcp_bindings(resource, event, trigger, **kwargs):
"""Resync DHCP bindings for NSXv3 CrossHairs."""
nsxlib = utils.get_connected_nsxlib()
nsx_version = nsxlib.get_version()
if not nsx_utils.is_nsx_version_1_1_0(nsx_version):
LOG.error("This utility is not available for NSX version %s",

View File

@ -28,7 +28,6 @@ from vmware_nsxlib.v3 import nsx_constants
LOG = logging.getLogger(__name__)
neutron_client = utils.NeutronDbClient()
nsxlib = utils.get_connected_nsxlib()
def _get_dhcp_profile_uuid(**kwargs):
@ -37,6 +36,8 @@ def _get_dhcp_profile_uuid(**kwargs):
dhcp_profile_uuid = properties.get('dhcp_profile_uuid')
if dhcp_profile_uuid:
return dhcp_profile_uuid
nsxlib = utils.get_connected_nsxlib()
if cfg.CONF.nsx_v3.dhcp_profile:
return nsxlib.native_dhcp_profile.get_id_by_name_or_id(
cfg.CONF.nsx_v3.dhcp_profile)
@ -50,6 +51,7 @@ def _get_orphaned_dhcp_servers(dhcp_profile_uuid):
server_net_pairs = []
# Find matching DHCP servers for a given dhcp_profile_uuid.
nsxlib = utils.get_connected_nsxlib()
response = nsxlib.dhcp_server.list()
for dhcp_server in response['results']:
if dhcp_server['dhcp_profile_id'] != dhcp_profile_uuid:
@ -91,6 +93,7 @@ def _get_orphaned_dhcp_servers(dhcp_profile_uuid):
def nsx_list_orphaned_dhcp_servers(resource, event, trigger, **kwargs):
"""List logical DHCP servers without associated DHCP-enabled subnet."""
nsxlib = utils.get_connected_nsxlib()
nsx_version = nsxlib.get_version()
if not nsx_utils.is_nsx_version_1_1_0(nsx_version):
LOG.error("This utility is not available for NSX version %s",
@ -117,6 +120,7 @@ def nsx_clean_orphaned_dhcp_servers(resource, event, trigger, **kwargs):
# (2) delete the logical DHCP server,
# (3) clean corresponding neutron DB entry.
nsxlib = utils.get_connected_nsxlib()
nsx_version = nsxlib.get_version()
if not nsx_utils.is_nsx_version_1_1_0(nsx_version):
LOG.error("This utility is not available for NSX version %s",

View File

@ -21,7 +21,6 @@ from vmware_nsx.shell.admin.plugins.nsxv3.resources import utils
from vmware_nsxlib.v3 import nsx_constants as consts
LOG = logging.getLogger(__name__)
nsxlib = utils.get_connected_nsxlib()
@admin_utils.list_handler(constants.LB_SERVICES)
@ -29,6 +28,7 @@ nsxlib = utils.get_connected_nsxlib()
def nsx_list_lb_services(resource, event, trigger, **kwargs):
"""List LB services on NSX backend"""
nsxlib = utils.get_connected_nsxlib()
if not nsxlib.feature_supported(consts.FEATURE_LOAD_BALANCER):
LOG.error("This utility is not available for NSX version %s",
nsxlib.get_version())
@ -46,6 +46,7 @@ def nsx_list_lb_services(resource, event, trigger, **kwargs):
def nsx_list_lb_virtual_servers(resource, event, trigger, **kwargs):
"""List LB virtual servers on NSX backend"""
nsxlib = utils.get_connected_nsxlib()
if not nsxlib.feature_supported(consts.FEATURE_LOAD_BALANCER):
LOG.error("This utility is not available for NSX version %s",
nsxlib.get_version())
@ -62,6 +63,7 @@ def nsx_list_lb_virtual_servers(resource, event, trigger, **kwargs):
@admin_utils.output_header
def nsx_list_lb_pools(resource, event, trigger, **kwargs):
nsxlib = utils.get_connected_nsxlib()
if not nsxlib.feature_supported(consts.FEATURE_LOAD_BALANCER):
LOG.error("This utility is not available for NSX version %s",
nsxlib.get_version())
@ -78,6 +80,7 @@ def nsx_list_lb_pools(resource, event, trigger, **kwargs):
@admin_utils.output_header
def nsx_list_lb_monitors(resource, event, trigger, **kwargs):
nsxlib = utils.get_connected_nsxlib()
if not nsxlib.feature_supported(consts.FEATURE_LOAD_BALANCER):
LOG.error("This utility is not available for NSX version %s",
nsxlib.get_version())

View File

@ -28,7 +28,6 @@ from vmware_nsxlib.v3 import nsx_constants
LOG = logging.getLogger(__name__)
neutron_client = utils.NeutronDbClient()
nsxlib = utils.get_connected_nsxlib()
def _is_metadata_network(network):
@ -56,6 +55,7 @@ def list_metadata_networks(resource, event, trigger, **kwargs):
def nsx_update_metadata_proxy(resource, event, trigger, **kwargs):
"""Update Metadata proxy for NSXv3 CrossHairs."""
nsxlib = utils.get_connected_nsxlib()
nsx_version = nsxlib.get_version()
if not nsx_utils.is_nsx_version_1_1_0(nsx_version):
LOG.error("This utility is not available for NSX version %s",

View File

@ -29,7 +29,6 @@ from oslo_log import log as logging
LOG = logging.getLogger(__name__)
neutron_client = utils.NeutronDbClient()
nsxlib = utils.get_connected_nsxlib()
def get_network_nsx_id(context, neutron_id):
@ -43,6 +42,7 @@ def get_network_nsx_id(context, neutron_id):
def list_missing_networks(resource, event, trigger, **kwargs):
"""List neutron networks that are missing the NSX backend network
"""
nsxlib = utils.get_connected_nsxlib()
plugin = db_base_plugin_v2.NeutronDbPluginV2()
admin_cxt = neutron_context.get_admin_context()
neutron_networks = plugin.get_networks(admin_cxt)
@ -73,6 +73,7 @@ def list_missing_networks(resource, event, trigger, **kwargs):
@admin_utils.output_header
def list_orphaned_networks(resource, event, trigger, **kwargs):
nsxlib = utils.get_connected_nsxlib()
nsx_switches = nsxlib.logical_switch.list()['results']
missing_networks = []
for nsx_switch in nsx_switches:
@ -104,6 +105,7 @@ def delete_backend_network(resource, event, trigger, **kwargs):
LOG.error("%s", errmsg)
return
nsxlib = utils.get_connected_nsxlib()
# check if the network exists
try:
nsxlib.logical_switch.get(nsx_id, silent=True)

View File

@ -31,7 +31,6 @@ from oslo_log import log as logging
LOG = logging.getLogger(__name__)
neutron_client = utils.NeutronDbClient()
nsxlib = utils.get_connected_nsxlib()
class RoutersPlugin(db_base_plugin_v2.NeutronDbPluginV2,
@ -43,6 +42,7 @@ class RoutersPlugin(db_base_plugin_v2.NeutronDbPluginV2,
def list_missing_routers(resource, event, trigger, **kwargs):
"""List neutron routers that are missing the NSX backend router
"""
nsxlib = utils.get_connected_nsxlib()
plugin = RoutersPlugin()
admin_cxt = neutron_context.get_admin_context()
neutron_routers = plugin.get_routers(admin_cxt)
@ -110,6 +110,7 @@ def update_nat_rules(resource, event, trigger, **kwargs):
@admin_utils.output_header
def list_orphaned_routers(resource, event, trigger, **kwargs):
nsxlib = utils.get_connected_nsxlib()
nsx_routers = nsxlib.logical_router.list()['results']
missing_routers = []
for nsx_router in nsx_routers:
@ -128,6 +129,7 @@ def list_orphaned_routers(resource, event, trigger, **kwargs):
@admin_utils.output_header
def delete_backend_router(resource, event, trigger, **kwargs):
nsxlib = utils.get_connected_nsxlib()
errmsg = ("Need to specify nsx-id property. Add --property nsx-id=<id>")
if not kwargs.get('property'):
LOG.error("%s", errmsg)

View File

@ -105,7 +105,6 @@ class NeutronSecurityGroupApi(securitygroups_db.SecurityGroupDbMixin,
neutron_sg = NeutronSecurityGroupApi()
neutron_db = v3_utils.NeutronDbClient()
nsxlib = v3_utils.get_connected_nsxlib()
def _log_info(resource, data, attrs=['display_name', 'id']):
@ -125,6 +124,7 @@ def list_security_groups_mappings(resource, event, trigger, **kwargs):
@admin_utils.list_handler(constants.FIREWALL_SECTIONS)
@admin_utils.output_header
def nsx_list_dfw_sections(resource, event, trigger, **kwargs):
nsxlib = v3_utils.get_connected_nsxlib()
fw_sections = nsxlib.firewall_section.list()
_log_info(constants.FIREWALL_SECTIONS, fw_sections)
return bool(fw_sections)
@ -133,12 +133,14 @@ def nsx_list_dfw_sections(resource, event, trigger, **kwargs):
@admin_utils.list_handler(constants.FIREWALL_NSX_GROUPS)
@admin_utils.output_header
def nsx_list_security_groups(resource, event, trigger, **kwargs):
nsxlib = v3_utils.get_connected_nsxlib()
nsx_secgroups = nsxlib.ns_group.list()
_log_info(constants.FIREWALL_NSX_GROUPS, nsx_secgroups)
return bool(nsx_secgroups)
def _find_missing_security_groups():
nsxlib = v3_utils.get_connected_nsxlib()
nsx_secgroups = nsxlib.ns_group.list()
sg_mappings = neutron_sg.get_security_groups_mappings()
missing_secgroups = {}
@ -168,6 +170,7 @@ def list_missing_security_groups(resource, event, trigger, **kwargs):
def _find_missing_sections():
nsxlib = v3_utils.get_connected_nsxlib()
fw_sections = nsxlib.firewall_section.list()
sg_mappings = neutron_sg.get_security_groups_mappings()
missing_sections = {}
@ -200,6 +203,7 @@ def fix_security_groups(resource, event, trigger, **kwargs):
inconsistent_secgroups = _find_missing_sections()
inconsistent_secgroups.update(_find_missing_security_groups())
nsxlib = v3_utils.get_connected_nsxlib()
with v3_utils.NsxV3PluginWrapper() as plugin:
for sg_id, sg in inconsistent_secgroups.items():
secgroup = plugin.get_security_group(context_, sg_id)
@ -245,6 +249,7 @@ def fix_security_groups(resource, event, trigger, **kwargs):
def _update_ports_dynamic_criteria_tags():
nsxlib = v3_utils.get_connected_nsxlib()
port_client, _ = ports.get_port_and_profile_clients()
for port in neutron_db.get_ports():
secgroups = neutron_sg.get_port_security_groups(port['id'])
@ -258,6 +263,7 @@ def _update_ports_dynamic_criteria_tags():
def _update_security_group_dynamic_criteria():
nsxlib = v3_utils.get_connected_nsxlib()
secgroups = neutron_sg.get_security_groups()
for sg in secgroups:
nsgroup_id = neutron_sg.get_nsgroup_id(sg['id'])
@ -278,6 +284,7 @@ def _update_security_group_dynamic_criteria():
@admin_utils.output_header
def migrate_nsgroups_to_dynamic_criteria(resource, event, trigger, **kwargs):
nsxlib = v3_utils.get_connected_nsxlib()
if not nsxlib.feature_supported(consts.FEATURE_DYNAMIC_CRITERIA):
LOG.error("Dynamic criteria grouping feature isn't supported by "
"this NSX version.")