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.

(cherry picked from commit 3c51978d6b)

Change-Id: I9a26f4662a3e4200ce3745377f51e8187cca21bd
This commit is contained in:
Anna Khmelnitsky 2017-09-01 14:31:13 -07:00 committed by Adit Sarfaty
parent 7fcbc0ffa1
commit aa8e690326
5 changed files with 22 additions and 8 deletions

View File

@ -31,7 +31,6 @@ from vmware_nsxlib.v3 import resources
LOG = logging.getLogger(__name__)
neutron_client = utils.NeutronDbClient()
nsxlib = utils.get_connected_nsxlib()
@admin_utils.output_header
@ -48,6 +47,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(_LE("This utility is not available for NSX version %s"),

View File

@ -31,10 +31,6 @@ from vmware_nsxlib.v3 import resources
LOG = logging.getLogger(__name__)
neutron_client = utils.NeutronDbClient()
nsx_client = utils.get_nsxv3_client()
nsxlib = utils.get_connected_nsxlib()
port_resource = resources.LogicalPort(nsx_client)
dhcp_server_resource = resources.LogicalDhcpServer(nsx_client)
def _get_dhcp_profile_uuid(**kwargs):
@ -43,6 +39,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)
@ -56,6 +54,8 @@ def _get_orphaned_dhcp_servers(dhcp_profile_uuid):
server_net_pairs = []
# Find matching DHCP servers for a given dhcp_profile_uuid.
nsx_client = utils.get_nsxv3_client()
dhcp_server_resource = resources.LogicalDhcpServer(nsx_client)
response = dhcp_server_resource.list()
for dhcp_server in response['results']:
if dhcp_server['dhcp_profile_id'] != dhcp_profile_uuid:
@ -97,6 +97,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(_LE("This utility is not available for NSX version %s"),
@ -123,6 +124,10 @@ 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_client = utils.get_nsxv3_client()
port_resource = resources.LogicalPort(nsx_client)
dhcp_server_resource = resources.LogicalDhcpServer(nsx_client)
nsx_version = nsxlib.get_version()
if not nsx_utils.is_nsx_version_1_1_0(nsx_version):
LOG.error(_LE("This utility is not available for NSX version %s"),

View File

@ -31,7 +31,6 @@ from vmware_nsxlib.v3 import resources
LOG = logging.getLogger(__name__)
neutron_client = utils.NeutronDbClient()
nsxlib = utils.get_connected_nsxlib()
def _is_metadata_network(network):
@ -59,6 +58,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(_LE("This utility is not available for NSX version %s"),

View File

@ -31,7 +31,6 @@ from neutron.db import db_base_plugin_v2
LOG = logging.getLogger(__name__)
neutron_client = utils.NeutronDbClient()
nsxlib = utils.get_connected_nsxlib()
def get_network_nsx_id(context, neutron_id):
@ -45,6 +44,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)
@ -75,6 +75,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:
@ -106,6 +107,7 @@ def delete_backend_network(resource, event, trigger, **kwargs):
LOG.error(_LE("%s"), errmsg)
return
nsxlib = utils.get_connected_nsxlib()
# check if the network exists
try:
nsxlib.logical_switch.get(nsx_id, silent=True)

View File

@ -109,7 +109,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']):
@ -129,6 +128,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)
@ -137,12 +137,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 = {}
@ -172,6 +174,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 = {}
@ -201,6 +204,7 @@ def list_missing_firewall_sections(resource, event, trigger, **kwargs):
@admin_utils.output_header
def fix_security_groups(resource, event, trigger, **kwargs):
context_ = neutron_context.get_admin_context()
nsxlib = v3_utils.get_connected_nsxlib()
plugin = v3_utils.NsxV3PluginWrapper()
inconsistent_secgroups = _find_missing_sections()
inconsistent_secgroups.update(_find_missing_security_groups())
@ -249,6 +253,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'])
@ -270,6 +275,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'])
@ -290,6 +296,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 utils.is_nsx_version_1_1_0(nsxlib.get_version()):
LOG.error(_LE("Dynamic criteria grouping feature isn't supported by "
"this NSX version."))