From cc8ac92f4c5e69a54804fe1c82befd9c91f6fccb Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Sun, 7 May 2017 12:15:14 +0300 Subject: [PATCH] NSX-V3| Integrate with nsxlib refactored code Change-Id: I88fb83f0cb531794b21a06cabb1f56c54744a0e1 --- vmware_nsx/plugins/nsx_v3/plugin.py | 116 ++++++++---------- vmware_nsx/services/ipam/nsx_v3/driver.py | 7 +- vmware_nsx/services/trunk/nsx_v3/driver.py | 4 +- .../plugins/nsxv3/resources/dhcp_binding.py | 11 +- .../plugins/nsxv3/resources/dhcp_servers.py | 14 +-- .../plugins/nsxv3/resources/metadata_proxy.py | 11 +- .../admin/plugins/nsxv3/resources/routers.py | 10 +- vmware_nsx/tests/unit/nsx_v3/test_plugin.py | 9 +- 8 files changed, 78 insertions(+), 104 deletions(-) diff --git a/vmware_nsx/plugins/nsx_v3/plugin.py b/vmware_nsx/plugins/nsx_v3/plugin.py index bee8687925..1d5a33b13b 100644 --- a/vmware_nsx/plugins/nsx_v3/plugin.py +++ b/vmware_nsx/plugins/nsx_v3/plugin.py @@ -98,9 +98,9 @@ from vmware_nsx.plugins.nsx_v3 import utils as v3_utils from vmware_nsx.services.qos.common import utils as qos_com_utils from vmware_nsx.services.qos.nsx_v3 import driver as qos_driver from vmware_nsx.services.trunk.nsx_v3 import driver as trunk_driver +from vmware_nsxlib.v3 import core_resources as nsx_resources from vmware_nsxlib.v3 import exceptions as nsx_lib_exc from vmware_nsxlib.v3 import nsx_constants as nsxlib_consts -from vmware_nsxlib.v3 import resources as nsx_resources from vmware_nsxlib.v3 import router from vmware_nsxlib.v3 import security from vmware_nsxlib.v3 import utils as nsxlib_utils @@ -192,7 +192,6 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, self._nsx_version = self.nsxlib.get_version() LOG.info("NSX Version: %s", self._nsx_version) - self._nsx_client = self.nsxlib.client self.cfg_group = 'nsx_v3' # group name for nsx_v3 section in nsx.ini self.tier0_groups_dict = {} @@ -206,19 +205,12 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, self._translate_configured_names_to_uuids() self._init_dhcp_metadata() - self._port_client = nsx_resources.LogicalPort(self._nsx_client) self.default_section = self._init_default_section_rules() self._process_security_group_logging() - self._router_client = nsx_resources.LogicalRouter(self._nsx_client) - self._router_port_client = nsx_resources.LogicalRouterPort( - self._nsx_client) - self._routerlib = router.RouterLib(self._router_client, - self._router_port_client, + self._routerlib = router.RouterLib(self.nsxlib.logical_router, + self.nsxlib.logical_router_port, self.nsxlib) - self._switching_profiles = nsx_resources.SwitchingProfile( - self._nsx_client) - # init profiles on nsx backend self._init_nsx_profiles() @@ -259,11 +251,11 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, msg = _("Unable to initialize NSX v3 port spoofguard " "switching profile: %s") % NSX_V3_PSEC_PROFILE_NAME raise nsx_exc.NsxPluginException(err_msg=msg) - profiles = nsx_resources.SwitchingProfile - self._no_psec_profile_id = profiles.build_switch_profile_ids( - self._switching_profiles, - self._switching_profiles.find_by_display_name( - NSX_V3_NO_PSEC_PROFILE_NAME)[0])[0] + profile_client = self.nsxlib.switching_profile + no_psec_prof = profile_client.find_by_display_name( + NSX_V3_NO_PSEC_PROFILE_NAME)[0] + self._no_psec_profile_id = profile_client.build_switch_profile_ids( + profile_client, no_psec_prof)[0] LOG.debug("Initializing NSX v3 DHCP switching profile") try: @@ -359,7 +351,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, events.BEFORE_DELETE) def _validate_dhcp_profile(self, dhcp_profile_uuid): - dhcp_profile = self._switching_profiles.get(dhcp_profile_uuid) + dhcp_profile = self.nsxlib.switching_profile.get(dhcp_profile_uuid) if (dhcp_profile.get('resource_type') != nsx_resources.SwitchingProfileTypes.SWITCH_SECURITY): msg = _("Invalid configuration on the backend for DHCP " @@ -379,7 +371,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, def _init_dhcp_switching_profile(self): with locking.LockManager.get_lock('nsxv3_dhcp_profile_init'): if not self._get_dhcp_security_profile(): - self._switching_profiles.create_dhcp_profile( + self.nsxlib.switching_profile.create_dhcp_profile( NSX_V3_DHCP_PROFILE_NAME, 'Neutron DHCP Security Profile', tags=self.nsxlib.build_v3_api_version_tag()) return self._get_dhcp_security_profile() @@ -387,7 +379,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, def _get_dhcp_security_profile(self): if hasattr(self, '_dhcp_profile') and self._dhcp_profile: return self._dhcp_profile - profile = self._switching_profiles.find_by_display_name( + profile = self.nsxlib.switching_profile.find_by_display_name( NSX_V3_DHCP_PROFILE_NAME) self._dhcp_profile = nsx_resources.SwitchingProfileTypeId( profile_type=(nsx_resources.SwitchingProfileTypes. @@ -398,7 +390,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, def _init_mac_learning_profile(self): with locking.LockManager.get_lock('nsxv3_mac_learning_profile_init'): if not self._get_mac_learning_profile(): - self._switching_profiles.create_mac_learning_profile( + self.nsxlib.switching_profile.create_mac_learning_profile( NSX_V3_MAC_LEARNING_PROFILE_NAME, 'Neutron MAC Learning Profile', tags=self.nsxlib.build_v3_api_version_tag()) @@ -408,7 +400,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, if (hasattr(self, '_mac_learning_profile') and self._mac_learning_profile): return self._mac_learning_profile - profile = self._switching_profiles.find_by_display_name( + profile = self.nsxlib.switching_profile.find_by_display_name( NSX_V3_MAC_LEARNING_PROFILE_NAME) self._mac_learning_profile = nsx_resources.SwitchingProfileTypeId( profile_type=(nsx_resources.SwitchingProfileTypes. @@ -417,13 +409,13 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, return self._mac_learning_profile def _get_port_security_profile_id(self): - return nsx_resources.SwitchingProfile.build_switch_profile_ids( - self._switching_profiles, self._psec_profile)[0] + return self.nsxlib.switching_profile.build_switch_profile_ids( + self.nsxlib.switching_profile, self._psec_profile)[0] def _get_port_security_profile(self): if hasattr(self, '_psec_profile') and self._psec_profile: return self._psec_profile - profile = self._switching_profiles.find_by_display_name( + profile = self.nsxlib.switching_profile.find_by_display_name( NSX_V3_PSEC_PROFILE_NAME) self._psec_profile = profile[0] if profile else None return self._psec_profile @@ -441,7 +433,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, if profile: return profile - self._switching_profiles.create_spoofguard_profile( + self.nsxlib.switching_profile.create_spoofguard_profile( NSX_V3_PSEC_PROFILE_NAME, 'Neutron Port Security Profile', whitelist_ports=True, whitelist_switches=False, tags=self.nsxlib.build_v3_api_version_tag()) @@ -492,10 +484,8 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, def _init_native_dhcp(self): try: for az in self.get_azs_list(): - nsx_resources.DhcpProfile(self._nsx_client).get( + self.nsxlib.native_dhcp_profile.get( az._native_dhcp_profile_uuid) - self._dhcp_server = nsx_resources.LogicalDhcpServer( - self._nsx_client) except nsx_lib_exc.ManagerError: with excutils.save_and_reraise_exception(): LOG.error("Unable to retrieve DHCP Profile %s, " @@ -505,8 +495,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, def _init_native_metadata(self): try: for az in self.get_azs_list(): - nsx_resources.MetaDataProxy(self._nsx_client).get( - az._native_md_proxy_uuid) + self.nsxlib.native_md_proxy.get(az._native_md_proxy_uuid) except nsx_lib_exc.ManagerError: with excutils.save_and_reraise_exception(): LOG.error("Unable to retrieve Metadata Proxy %s, " @@ -817,7 +806,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, name = utils.get_name_and_uuid('%s-%s' % ( 'mdproxy', created_net['name'] or 'network'), created_net['id']) - md_port = self._port_client.create( + md_port = self.nsxlib.logical_port.create( nsx_net_id, az._native_md_proxy_uuid, tags=tags, name=name, attachment_type=nsxlib_consts.ATTACHMENT_MDPROXY) @@ -1039,12 +1028,12 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, project_name=context.tenant_name) dhcp_server = None try: - dhcp_server = self._dhcp_server.create(**server_data) + dhcp_server = self.nsxlib.dhcp_server.create(**server_data) LOG.debug("Created logical DHCP server %(server)s for network " "%(network)s", {'server': dhcp_server['id'], 'network': network['id']}) name = self._get_port_name(context, port_data) - nsx_port = self._port_client.create( + nsx_port = self.nsxlib.logical_port.create( nsx_net_id, dhcp_server['id'], tags=port_tags, name=name, attachment_type=nsxlib_consts.ATTACHMENT_DHCP, switch_profile_ids=[self._dhcp_profile]) @@ -1056,7 +1045,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, LOG.error("Unable to create logical DHCP server for " "network %s", network['id']) if dhcp_server: - self._dhcp_server.delete(dhcp_server['id']) + self.nsxlib.dhcp_server.delete(dhcp_server['id']) super(NsxV3Plugin, self).delete_port( context, neutron_port['id']) @@ -1074,7 +1063,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, LOG.error("Failed to create mapping for DHCP port %s," "deleting port and logical DHCP server", neutron_port['id']) - self._dhcp_server.delete(dhcp_server['id']) + self.nsxlib.dhcp_server.delete(dhcp_server['id']) self._cleanup_port(context, neutron_port['id'], nsx_port['id']) # Configure existing ports to work with the new DHCP server @@ -1108,7 +1097,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, network_id) try: - self._dhcp_server.delete(dhcp_service['nsx_service_id']) + self.nsxlib.dhcp_server.delete(dhcp_service['nsx_service_id']) LOG.debug("Deleted logical DHCP server %(server)s for network " "%(network)s", {'server': dhcp_service['nsx_service_id'], @@ -1205,13 +1194,13 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, subnet['network_id']) if dhcp_info: try: - self._port_client.delete(dhcp_info['nsx_port_id']) + self.nsxlib.logical_port.delete(dhcp_info['nsx_port_id']) except Exception as e: LOG.error("Failed to delete logical port %(id)s " "during rollback. Exception: %(e)s", {'id': dhcp_info['nsx_port_id'], 'e': e}) try: - self._dhcp_server.delete(dhcp_info['nsx_service_id']) + self.nsxlib.dhcp_server.delete(dhcp_info['nsx_service_id']) except Exception as e: LOG.error("Failed to delete logical DHCP server %(id)s " "during rollback. Exception: %(e)s", @@ -1361,7 +1350,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, nsxlib_consts.SERVICE_DHCP) if dhcp_service: try: - self._dhcp_server.update( + self.nsxlib.dhcp_server.update( dhcp_service['nsx_service_id'], **kwargs) except nsx_lib_exc.ManagerError: with excutils.save_and_reraise_exception(): @@ -1503,10 +1492,11 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, def _get_qos_profile_id(self, context, policy_id): switch_profile_id = nsx_db.get_switch_profile_by_qos_policy( context.session, policy_id) - qos_profile = self.nsxlib.qos_switching_profile.get(switch_profile_id) + nsxlib_qos = self.nsxlib.qos_switching_profile + qos_profile = nsxlib_qos.get(switch_profile_id) if qos_profile: - profile_ids = self._switching_profiles.build_switch_profile_ids( - self._switching_profiles, qos_profile) + profile_ids = nsxlib_qos.build_switch_profile_ids( + self.nsxlib.switching_profile, qos_profile) if profile_ids and len(profile_ids) > 0: # We have only 1 QoS profile, so this array is of size 1 return profile_ids[0] @@ -1620,7 +1610,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, nsx_net_id = port_data[pbin.VIF_DETAILS]['nsx-logical-switch-id'] try: - result = self._port_client.create( + result = self.nsxlib.logical_port.create( nsx_net_id, vif_uuid, tags=tags, name=name, @@ -1701,7 +1691,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, def _cleanup_port(self, context, port_id, lport_id): super(NsxV3Plugin, self).delete_port(context, port_id) if lport_id: - self._port_client.delete(lport_id) + self.nsxlib.logical_port.delete(lport_id) def _assert_on_external_net_port_with_qos(self, port_data): # Prevent creating/update port with QoS policy @@ -1773,7 +1763,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, "%(name)s") % {'name': opt_name, 'val': opt_val}) raise n_exc.InvalidInput(error_message=msg) - elif not self._dhcp_server.get_dhcp_opt_code(opt_name): + elif not self.nsxlib.dhcp_server.get_dhcp_opt_code(opt_name): msg = (_("DHCP option %s is not supported") % opt_name) raise n_exc.InvalidInput(error_message=msg) @@ -1799,7 +1789,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, 'network': net, 'next_hop': ip}) else: other_opts.append({ - 'code': self._dhcp_server.get_dhcp_opt_code( + 'code': self.nsxlib.dhcp_server.get_dhcp_opt_code( opt_name), 'values': [opt_val]}) if other_opts: @@ -1815,7 +1805,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, options = self._get_dhcp_options( context, ip, port.get(ext_edo.EXTRADHCPOPTS), port['network_id']) - binding = self._dhcp_server.create_binding( + binding = self.nsxlib.dhcp_server.create_binding( dhcp_service_id, port['mac_address'], ip, hostname, cfg.CONF.nsx_v3.dhcp_lease_time, options, gateway_ip) LOG.debug("Created static binding (mac: %(mac)s, ip: %(ip)s, " @@ -1855,7 +1845,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, def _delete_dhcp_binding_on_server(self, context, binding): try: - self._dhcp_server.delete_binding( + self.nsxlib.dhcp_server.delete_binding( binding['nsx_service_id'], binding['nsx_binding_id']) LOG.debug("Deleted static binding for port %(port)s) on " "logical DHCP server %(server)s", @@ -1918,8 +1908,9 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, if dhcp_service: new_ip = ips_to_add[0][1] try: - self._dhcp_server.update(dhcp_service['nsx_service_id'], - server_ip=new_ip) + self.nsxlib.dhcp_server.update( + dhcp_service['nsx_service_id'], + server_ip=new_ip) LOG.debug("Updated IP %(ip)s for logical DHCP server " "%(server)s", {'ip': new_ip, @@ -1994,7 +1985,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, # Note that None is valid for gateway_ip, means deleting it. data['gateway_ip'] = gateway_ip - self._dhcp_server.update_binding( + self.nsxlib.dhcp_server.update_binding( binding['nsx_service_id'], binding['nsx_binding_id'], **data) LOG.debug("Updated static binding (mac: %(mac)s, ip: %(ip)s, " "gateway: %(gateway)s) for port %(port)s on " @@ -2163,7 +2154,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, if not self._network_is_external(context, port['network_id']): _net_id, nsx_port_id = nsx_db.get_nsx_switch_and_port_id( context.session, port_id) - self._port_client.delete(nsx_port_id) + self.nsxlib.logical_port.delete(nsx_port_id) if not utils.is_nsx_version_1_1_0(self._nsx_version): self._update_lport_with_security_groups( context, nsx_port_id, @@ -2368,7 +2359,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, switch_profile_ids.append(self._mac_learning_profile) try: - self._port_client.update( + self.nsxlib.logical_port.update( lport_id, vif_uuid, name=name, attachment_type=attachment_type, admin_state=updated_port.get('admin_state_up'), @@ -2751,7 +2742,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, # occurred during super.create_router(), which will cause # API retry and leaves dangling backend entries. try: - result = self._router_client.create( + result = self.nsxlib.logical_router.create( display_name=utils.get_name_and_uuid( router['name'] or 'router', router['id']), description=router.get('description'), @@ -2806,7 +2797,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, # It is safe to do now as db-level checks for resource deletion were # passed (and indeed the resource was removed from the Neutron DB try: - self._router_client.delete(nsx_router_id, force=True) + self.nsxlib.logical_router.delete(nsx_router_id, force=True) except nsx_lib_exc.ResourceNotFound: # If the logical router was not found on the backend do not worry # about it. The conditions has already been logged, so there is no @@ -2889,8 +2880,8 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, display_name = utils.get_name_and_uuid(router_name, router_id) nsx_router_id = nsx_router_id or nsx_db.get_nsx_router_id( context.session, router_id) - self._router_client.update(nsx_router_id, - display_name=display_name) + self.nsxlib.logical_router.update(nsx_router_id, + display_name=display_name) # Update the name of associated logical ports. filters = {'device_id': [router_id], 'device_owner': const.ROUTER_INTERFACE_OWNERS} @@ -2902,8 +2893,8 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, name = utils.get_name_and_uuid( router_name, port['id'], tag='port') try: - self._port_client.update(nsx_port_id, None, - name=name) + self.nsxlib.logical_port.update(nsx_port_id, None, + name=name) except Exception as e: LOG.error("Unable to update port %(port_id)s. " "Reason: %(e)s", @@ -2912,7 +2903,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, if 'description' in router_data: nsx_router_id = nsx_db.get_nsx_router_id(context.session, router_id) - self._router_client.update( + self.nsxlib.logical_router.update( nsx_router_id, description=router_data['description']) @@ -3123,13 +3114,14 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, new_using_port_id = ports[0]['id'] _net_id, new_nsx_port_id = nsx_db.get_nsx_switch_and_port_id( context.session, new_using_port_id) - self._router_port_client.update_by_lswitch_id( + self.nsxlib.logical_router_port.update_by_lswitch_id( nsx_router_id, nsx_net_id, linked_logical_switch_port_id={ 'target_id': new_nsx_port_id}, subnets=address_groups) else: - self._router_port_client.delete_by_lswitch_id(nsx_net_id) + self.nsxlib.logical_router_port.delete_by_lswitch_id( + nsx_net_id) except nsx_lib_exc.ResourceNotFound: LOG.error("router port on router %(router_id)s for net " "%(net_id)s not found at the backend", diff --git a/vmware_nsx/services/ipam/nsx_v3/driver.py b/vmware_nsx/services/ipam/nsx_v3/driver.py index d5acf5d1fa..4f9ce3b571 100644 --- a/vmware_nsx/services/ipam/nsx_v3/driver.py +++ b/vmware_nsx/services/ipam/nsx_v3/driver.py @@ -25,7 +25,6 @@ from vmware_nsx._i18n import _ from vmware_nsx.services.ipam.common import driver as common from vmware_nsxlib.v3 import exceptions as nsx_lib_exc from vmware_nsxlib.v3 import nsx_constants as error -from vmware_nsxlib.v3 import resources LOG = logging.getLogger(__name__) @@ -35,8 +34,7 @@ class Nsxv3IpamDriver(common.NsxAbstractIpamDriver): def __init__(self, subnetpool, context): super(Nsxv3IpamDriver, self).__init__(subnetpool, context) - self.nsxlib_ipam = resources.IpPool( - self.get_core_plugin().nsxlib.client) + self.nsxlib_ipam = self.get_core_plugin().nsxlib.ip_pool # Mark which updates to the pool are supported self.support_update_gateway = True @@ -135,8 +133,7 @@ class Nsxv3IpamSubnet(common.NsxAbstractIpamSubnet): def __init__(self, subnet_id, nsx_pool_id, ctx, tenant_id): super(Nsxv3IpamSubnet, self).__init__( subnet_id, nsx_pool_id, ctx, tenant_id) - self.nsxlib_ipam = resources.IpPool( - self.get_core_plugin().nsxlib.client) + self.nsxlib_ipam = self.get_core_plugin().nsxlib.ip_pool def backend_allocate(self, address_request): try: diff --git a/vmware_nsx/services/trunk/nsx_v3/driver.py b/vmware_nsx/services/trunk/nsx_v3/driver.py index b6e51a9448..9cef0e7843 100644 --- a/vmware_nsx/services/trunk/nsx_v3/driver.py +++ b/vmware_nsx/services/trunk/nsx_v3/driver.py @@ -64,7 +64,7 @@ class NsxV3TrunkHandler(object): session=context.session, neutron_id=subport.port_id)[1] # Retrieve child logical port from the backend try: - nsx_child_port = self.plugin_driver._port_client.get( + nsx_child_port = self.plugin_driver.nsxlib.logical_port.get( nsx_child_port_id) except nsxlib_exc.ResourceNotFound: with excutils.save_and_reraise_exception(): @@ -90,7 +90,7 @@ class NsxV3TrunkHandler(object): seg_id = None # Update logical port in the backend to set/unset parent port try: - self.plugin_driver._port_client.update( + self.plugin_driver.nsxlib.logical_port.update( lport_id=nsx_child_port.get('id'), vif_uuid=subport.port_id, name=nsx_child_port.get('display_name'), diff --git a/vmware_nsx/shell/admin/plugins/nsxv3/resources/dhcp_binding.py b/vmware_nsx/shell/admin/plugins/nsxv3/resources/dhcp_binding.py index 28475ddb7b..717f164fd4 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv3/resources/dhcp_binding.py +++ b/vmware_nsx/shell/admin/plugins/nsxv3/resources/dhcp_binding.py @@ -26,7 +26,6 @@ from vmware_nsx.shell.admin.plugins.common import utils as admin_utils from vmware_nsx.shell.admin.plugins.nsxv3.resources import utils import vmware_nsx.shell.resources as shell from vmware_nsxlib.v3 import nsx_constants -from vmware_nsxlib.v3 import resources LOG = logging.getLogger(__name__) neutron_client = utils.NeutronDbClient() @@ -66,10 +65,6 @@ def nsx_update_dhcp_bindings(resource, event, trigger, **kwargs): cfg.CONF.set_override('native_dhcp_metadata', True, 'nsx_v3') cfg.CONF.set_override('dhcp_profile', dhcp_profile_uuid, 'nsx_v3') - nsx_client = utils.get_nsxv3_client() - port_resource = resources.LogicalPort(nsx_client) - dhcp_server_resource = resources.LogicalDhcpServer(nsx_client) - port_bindings = {} # lswitch_id: [(port_id, mac, ip), ...] server_bindings = {} # lswitch_id: dhcp_server_id ports = neutron_client.get_ports() @@ -96,7 +91,7 @@ def nsx_update_dhcp_bindings(resource, event, trigger, **kwargs): server_data = nsxlib.native_dhcp.build_server_config( network, subnet, port, net_tags) server_data['dhcp_profile_id'] = dhcp_profile_uuid - dhcp_server = dhcp_server_resource.create(**server_data) + dhcp_server = nsxlib.dhcp_server.create(**server_data) LOG.info("Created logical DHCP server %(server)s for " "network %(network)s", {'server': dhcp_server['id'], @@ -107,7 +102,7 @@ def nsx_update_dhcp_bindings(resource, event, trigger, **kwargs): # Update logical port for DHCP purpose. lswitch_id, lport_id = ( neutron_client.get_lswitch_and_lport_id(port['id'])) - port_resource.update( + nsxlib.logical_port.update( lport_id, dhcp_server['id'], attachment_type=nsx_constants.ATTACHMENT_DHCP) server_bindings[lswitch_id] = dhcp_server['id'] @@ -136,7 +131,7 @@ def nsx_update_dhcp_bindings(resource, event, trigger, **kwargs): {'network': '%s' % cfg.CONF.nsx_v3.native_metadata_route, 'next_hop': ip}]}} subnet = neutron_client.get_subnet(subnet_id) - binding = dhcp_server_resource.create_binding( + binding = nsxlib.dhcp_server.create_binding( dhcp_server_id, mac, ip, hostname, cfg.CONF.nsx_v3.dhcp_lease_time, options, subnet.get('gateway_ip')) diff --git a/vmware_nsx/shell/admin/plugins/nsxv3/resources/dhcp_servers.py b/vmware_nsx/shell/admin/plugins/nsxv3/resources/dhcp_servers.py index 2b9d453d3c..595c6b74aa 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv3/resources/dhcp_servers.py +++ b/vmware_nsx/shell/admin/plugins/nsxv3/resources/dhcp_servers.py @@ -25,14 +25,10 @@ from vmware_nsx.shell.admin.plugins.common import utils as admin_utils from vmware_nsx.shell.admin.plugins.nsxv3.resources import utils import vmware_nsx.shell.resources as shell from vmware_nsxlib.v3 import nsx_constants -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): @@ -54,7 +50,7 @@ def _get_orphaned_dhcp_servers(dhcp_profile_uuid): server_net_pairs = [] # Find matching DHCP servers for a given dhcp_profile_uuid. - response = dhcp_server_resource.list() + response = nsxlib.dhcp_server.list() for dhcp_server in response['results']: if dhcp_server['dhcp_profile_id'] != dhcp_profile_uuid: continue @@ -140,12 +136,14 @@ def nsx_clean_orphaned_dhcp_servers(resource, event, trigger, **kwargs): for server in orphaned_servers: try: + # TODO(asarfaty): should add this as api to nsxlib instead of + # abusing it resource = ('?attachment_type=DHCP_SERVICE&attachment_id=%s' % server['id']) - response = port_resource._client.url_get(resource) + response = nsxlib.logical_port.get(resource) if response and response['result_count'] > 0: - port_resource.delete(response['results'][0]['id']) - dhcp_server_resource.delete(server['id']) + nsxlib.logical_port.delete(response['results'][0]['id']) + nsxlib.dhcp_server.delete(server['id']) net_id = server.get('neutron_net_id') if net_id: # Delete neutron_net_id -> dhcp_service_id mapping from the DB. 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 55b89881a5..b0b2b83302 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv3/resources/metadata_proxy.py +++ b/vmware_nsx/shell/admin/plugins/nsxv3/resources/metadata_proxy.py @@ -25,7 +25,6 @@ from vmware_nsx.shell.admin.plugins.common import utils as admin_utils from vmware_nsx.shell.admin.plugins.nsxv3.resources import utils import vmware_nsx.shell.resources as shell from vmware_nsxlib.v3 import nsx_constants -from vmware_nsxlib.v3 import resources LOG = logging.getLogger(__name__) neutron_client = utils.NeutronDbClient() @@ -76,9 +75,6 @@ def nsx_update_metadata_proxy(resource, event, trigger, **kwargs): cfg.CONF.set_override('metadata_proxy', metadata_proxy_uuid, 'nsx_v3') with utils.NsxV3PluginWrapper() as plugin: - nsx_client = utils.get_nsxv3_client() - port_resource = resources.LogicalPort(nsx_client) - # For each Neutron network, check if it is an internal metadata # network. # If yes, delete the network and associated router interface. @@ -111,10 +107,11 @@ def nsx_update_metadata_proxy(resource, event, trigger, **kwargs): name = nsx_utils.get_name_and_uuid('%s-%s' % ( 'mdproxy', network['name'] or 'network'), network['id']) # check if this logical port already exists - existing_ports = port_resource.find_by_display_name(name) + existing_ports = nsxlib.logical_port.find_by_display_name( + name) if not existing_ports: # create a new port with the md-proxy - port_resource.create( + nsxlib.logical_port.create( lswitch_id, metadata_proxy_uuid, tags=tags, name=name, attachment_type=nsx_constants.ATTACHMENT_MDPROXY) LOG.info("Enabled native metadata proxy for network %s", @@ -122,7 +119,7 @@ def nsx_update_metadata_proxy(resource, event, trigger, **kwargs): else: # update the MDproxy of this port port = existing_ports[0] - port_resource.update( + nsxlib.logical_port.update( port['id'], metadata_proxy_uuid, attachment_type=nsx_constants.ATTACHMENT_MDPROXY) LOG.info("Updated native metadata proxy for network %s", diff --git a/vmware_nsx/shell/admin/plugins/nsxv3/resources/routers.py b/vmware_nsx/shell/admin/plugins/nsxv3/resources/routers.py index 50117cb026..bba17ed7f4 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv3/resources/routers.py +++ b/vmware_nsx/shell/admin/plugins/nsxv3/resources/routers.py @@ -20,7 +20,6 @@ from vmware_nsx.shell.admin.plugins.common import utils as admin_utils from vmware_nsx.shell.admin.plugins.nsxv3.resources import utils from vmware_nsx.shell import resources as shell from vmware_nsxlib.v3 import exceptions as nsx_exc -from vmware_nsxlib.v3 import resources as nsx_resources from neutron.db import db_base_plugin_v2 from neutron.db import l3_db @@ -29,6 +28,7 @@ from neutron_lib import context as neutron_context from oslo_log import log as logging LOG = logging.getLogger(__name__) +nsxlib = utils.get_connected_nsxlib() class RoutersPlugin(db_base_plugin_v2.NeutronDbPluginV2, @@ -36,11 +36,6 @@ class RoutersPlugin(db_base_plugin_v2.NeutronDbPluginV2, pass -def get_router_client(): - _nsx_client = utils.get_nsxv3_client() - return nsx_resources.LogicalRouter(_nsx_client) - - @admin_utils.output_header def list_missing_routers(resource, event, trigger, **kwargs): """List neutron routers that are missing the NSX backend router @@ -48,7 +43,6 @@ def list_missing_routers(resource, event, trigger, **kwargs): plugin = RoutersPlugin() admin_cxt = neutron_context.get_admin_context() neutron_routers = plugin.get_routers(admin_cxt) - router_client = get_router_client() routers = [] for router in neutron_routers: neutron_id = router['id'] @@ -61,7 +55,7 @@ def list_missing_routers(resource, event, trigger, **kwargs): 'nsx_id': None}) else: try: - router_client.get(nsx_id) + nsxlib.logical_router.get(nsx_id) except nsx_exc.ResourceNotFound: routers.append({'name': router['name'], 'neutron_id': neutron_id, diff --git a/vmware_nsx/tests/unit/nsx_v3/test_plugin.py b/vmware_nsx/tests/unit/nsx_v3/test_plugin.py index 9f0d3a4170..1cab1f8524 100644 --- a/vmware_nsx/tests/unit/nsx_v3/test_plugin.py +++ b/vmware_nsx/tests/unit/nsx_v3/test_plugin.py @@ -86,7 +86,8 @@ def _mock_nsx_backend_calls(): return key mock.patch( - "vmware_nsxlib.v3.resources.SwitchingProfile.find_by_display_name", + "vmware_nsxlib.v3.core_resources.NsxLibSwitchingProfile." + "find_by_display_name", return_value=[fake_profile] ).start() @@ -94,7 +95,7 @@ def _mock_nsx_backend_calls(): "vmware_nsxlib.v3.router.RouterLib.validate_tier0").start() mock.patch( - "vmware_nsxlib.v3.resources.SwitchingProfile." + "vmware_nsxlib.v3.core_resources.NsxLibSwitchingProfile." "create_port_mirror_profile", side_effect=_return_id_key).start() @@ -135,7 +136,7 @@ def _mock_nsx_backend_calls(): side_effect=_return_id_key).start() mock.patch( - "vmware_nsxlib.v3.resources.LogicalRouter.create", + "vmware_nsxlib.v3.core_resources.NsxLibLogicalRouter.create", side_effect=_return_id_key).start() mock.patch( @@ -249,7 +250,7 @@ class TestNetworksV2(test_plugin.TestNetworksV2, NsxV3PluginTestCaseMixin): def test_network_failure_rollback(self): cfg.CONF.set_override('native_dhcp_metadata', True, 'nsx_v3') self.plugin = directory.get_plugin() - with mock.patch.object(self.plugin._port_client, 'create', + with mock.patch.object(self.plugin.nsxlib.logical_port, 'create', side_effect=api_exc.NsxApiException): self.network() ctx = context.get_admin_context()