From 074edfefa590e2751fe72a5886cffe9bd6015674 Mon Sep 17 00:00:00 2001 From: Zhenmei Date: Wed, 31 Aug 2016 03:20:10 -0400 Subject: [PATCH] NSX|v3 replace dhcp profile and metadata proxy uuids with names Support configuration of name or uuid (instead of only uuid) for 2 nsx_v3 parameters: dhcp_profile, metadata_proxy. Assert on init if the uuid or name was no found on the backend, or if the name is not unique. Change-Id: Ife6263b7cf1759a2fc309205552eb79138d512a1 --- devstack/lib/vmware_nsx_v3 | 4 +- .../rename_uuid_to_name-e64699df75176d4d.yaml | 9 ++++ vmware_nsx/common/config.py | 22 +++++----- vmware_nsx/nsxlib/v3/__init__.py | 30 +++++++++++++ vmware_nsx/nsxlib/v3/native_dhcp.py | 1 - vmware_nsx/plugins/nsx_v3/plugin.py | 42 ++++++++++++------- vmware_nsx/plugins/nsx_v3/utils.py | 2 +- .../plugins/nsxv3/resources/dhcp_binding.py | 2 +- .../plugins/nsxv3/resources/metadata_proxy.py | 2 +- .../tests/unit/nsx_v3/test_dhcp_metadata.py | 32 +++++++------- vmware_nsx/tests/unit/nsx_v3/test_plugin.py | 12 +++++- 11 files changed, 112 insertions(+), 46 deletions(-) create mode 100644 releasenotes/notes/rename_uuid_to_name-e64699df75176d4d.yaml diff --git a/devstack/lib/vmware_nsx_v3 b/devstack/lib/vmware_nsx_v3 index b24d45d115..24a7dfa5e3 100644 --- a/devstack/lib/vmware_nsx_v3 +++ b/devstack/lib/vmware_nsx_v3 @@ -166,8 +166,8 @@ function neutron_plugin_configure_service { if [[ "$NATIVE_DHCP_METADATA" == "True" ]]; then _nsxv3_ini_set native_dhcp_metadata $NATIVE_DHCP_METADATA _nsxv3_ini_set native_metadata_route $NATIVE_METADATA_ROUTE - _nsxv3_ini_set dhcp_profile_uuid $DHCP_PROFILE_UUID - _nsxv3_ini_set metadata_proxy_uuid $METADATA_PROXY_UUID + _nsxv3_ini_set dhcp_profile $DHCP_PROFILE_UUID + _nsxv3_ini_set metadata_proxy $METADATA_PROXY_UUID iniset $NEUTRON_CONF DEFAULT dhcp_agent_notification False fi } diff --git a/releasenotes/notes/rename_uuid_to_name-e64699df75176d4d.yaml b/releasenotes/notes/rename_uuid_to_name-e64699df75176d4d.yaml new file mode 100644 index 0000000000..fea1e97bee --- /dev/null +++ b/releasenotes/notes/rename_uuid_to_name-e64699df75176d4d.yaml @@ -0,0 +1,9 @@ +--- +prelude: > + - In NSX|v3 plugin, the 'dhcp_profile_uuid' and 'metadata_proxy_uuid' + options have been deprecated and replaced by 'dhcp_profile' + and 'metadata_proxy' respectively, which can accept both name or uuid. +deprecations: + - In NSX|v3 plugin, the 'dhcp_profile_uuid' and 'metadata_proxy_uuid' + options have been deprecated and replaced by 'dhcp_profile' + and 'metadata_proxy' respectively, which can accept both name or uuid. diff --git a/vmware_nsx/common/config.py b/vmware_nsx/common/config.py index 76b3788fdb..214761bc1d 100644 --- a/vmware_nsx/common/config.py +++ b/vmware_nsx/common/config.py @@ -363,11 +363,12 @@ nsx_v3_opts = [ default="169.254.169.254/32", help=_("The metadata route used for native metadata proxy " "service.")), - cfg.StrOpt('dhcp_profile_uuid', - help=_("This is the UUID of the NSX DHCP Profile that will be " - "used to enable native DHCP service. It needs to be " - "created in NSX before starting Neutron with the NSX " - "plugin.")), + cfg.StrOpt('dhcp_profile', + deprecated_name='dhcp_profile_uuid', + help=_("This is the name or UUID of the NSX DHCP Profile " + "that will be used to enable native DHCP service. It " + "needs to be created in NSX before starting Neutron" + "with the NSX plugin")), cfg.IntOpt('dhcp_lease_time', default=86400, help=_("DHCP default lease time.")), @@ -379,11 +380,12 @@ nsx_v3_opts = [ help=_("List of nameservers to configure for the DHCP " "binding entries. These will be used if there are no " "nameservers defined on the subnet.")), - cfg.StrOpt('metadata_proxy_uuid', - help=_("This is the UUID of the NSX Metadata Proxy that will " - "be used to enable native metadata service. It needs " - "to be created in NSX before starting Neutron with " - "the NSX plugin.")), + cfg.StrOpt('metadata_proxy', + deprecated_name='metadata_proxy_uuid', + help=_("This is the name or UUID of the NSX Metadata Proxy " + "that will be used to enable native metadata service. " + "It needs to be created in NSX before starting Neutron " + "with the NSX plugin.")), cfg.BoolOpt('log_security_groups_blocked_traffic', default=False, help=_("(Optional) Indicates whether distributed-firewall " diff --git a/vmware_nsx/nsxlib/v3/__init__.py b/vmware_nsx/nsxlib/v3/__init__.py index 2a6ef45719..0bba97ef41 100644 --- a/vmware_nsx/nsxlib/v3/__init__.py +++ b/vmware_nsx/nsxlib/v3/__init__.py @@ -60,6 +60,10 @@ class NsxLib(object): self.client, nsxlib_config) self.transport_zone = NsxLibTransportZone( self.client, nsxlib_config) + self.native_dhcp_profile = NsxLibDhcpProfile( + self.client, nsxlib_config) + self.native_md_proxy = NsxLibMetadataProxy( + self.client, nsxlib_config) self.firewall_section = security.NsxLibFirewallSection( self.client, nsxlib_config) self.ns_group = security.NsxLibNsGroup( @@ -424,6 +428,32 @@ class NsxLibTransportZone(utils.NsxLibApiBase): 'transport-zones') +class NsxLibDhcpProfile(utils.NsxLibApiBase): + + def get_id_by_name_or_id(self, name_or_id): + """Get a dhcp profile by it's display name or uuid + + Return the dhcp profile data, or raise an exception if not found or + not unique + """ + + return self._get_resource_by_name_or_id(name_or_id, + 'dhcp/server-profiles') + + +class NsxLibMetadataProxy(utils.NsxLibApiBase): + + def get_id_by_name_or_id(self, name_or_id): + """Get a metadata proxy by it's display name or uuid + + Return the metadata proxy data, or raise an exception if not found or + not unique + """ + + return self._get_resource_by_name_or_id(name_or_id, + 'md-proxies') + + class NsxLibBridgeCluster(utils.NsxLibApiBase): def get_id_by_name_or_id(self, name_or_id): diff --git a/vmware_nsx/nsxlib/v3/native_dhcp.py b/vmware_nsx/nsxlib/v3/native_dhcp.py index dfd45e5654..c84b1bedc3 100644 --- a/vmware_nsx/nsxlib/v3/native_dhcp.py +++ b/vmware_nsx/nsxlib/v3/native_dhcp.py @@ -55,7 +55,6 @@ class NsxLibNativeDhcp(utils.NsxLibApiBase): name = utils.get_name_and_uuid(network['name'] or 'dhcpserver', network['id']) return {'name': name, - 'dhcp_profile_id': self.nsxlib_config.dhcp_profile_uuid, 'server_ip': server_ip, 'dns_nameservers': dns_nameservers, 'domain_name': self.nsxlib_config.dns_domain, diff --git a/vmware_nsx/plugins/nsx_v3/plugin.py b/vmware_nsx/plugins/nsx_v3/plugin.py index ba4a70aa62..caaa3baf9c 100644 --- a/vmware_nsx/plugins/nsx_v3/plugin.py +++ b/vmware_nsx/plugins/nsx_v3/plugin.py @@ -178,6 +178,9 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, self.cfg_group = 'nsx_v3' # group name for nsx_v3 section in nsx.ini self.tier0_groups_dict = {} + # Translate configured transport zones, routers, dhcp profile and + # metadata proxy names to uuid. + self._translate_configured_names_to_uuids() self._init_dhcp_metadata() self._port_client = nsx_resources.LogicalPort(self._nsx_client) @@ -207,9 +210,6 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, if cfg.CONF.api_replay_mode: self.supported_extension_aliases.append('api-replay') - # translate configured transport zones/rotuers names to uuid - self._translate_configured_names_2_uuids() - # Register NSXv3 trunk driver to support trunk extensions self.trunk_driver = trunk_driver.NsxV3TrunkDriver.create(self) @@ -257,7 +257,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, return (self._psec_profile, self._no_psec_profile_id, self._dhcp_profile, self._mac_learning_profile) - def _translate_configured_names_2_uuids(self): + def _translate_configured_names_to_uuids(self): # default VLAN transport zone name / uuid self._default_vlan_tz_uuid = None if cfg.CONF.nsx_v3.default_vlan_tz: @@ -279,6 +279,23 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, cfg.CONF.nsx_v3.default_tier0_router) self._default_tier0_router = rtr_id + self._native_dhcp_profile_uuid = None + self._native_md_proxy_uuid = None + if cfg.CONF.nsx_v3.native_dhcp_metadata: + if cfg.CONF.nsx_v3.dhcp_profile: + id = self.nsxlib.native_dhcp_profile.get_id_by_name_or_id( + cfg.CONF.nsx_v3.dhcp_profile) + self._native_dhcp_profile_uuid = id + else: + raise cfg.RequiredOptError("dhcp_profile") + + if cfg.CONF.nsx_v3.metadata_proxy: + proxy_id = self.nsxlib.native_md_proxy.get_id_by_name_or_id( + cfg.CONF.nsx_v3.metadata_proxy) + self._native_md_proxy_uuid = proxy_id + else: + raise cfg.RequiredOptError("metadata_proxy") + def _extend_port_dict_binding(self, context, port_data): port_data[pbin.VIF_TYPE] = pbin.VIF_TYPE_OVS port_data[pbin.VNIC_TYPE] = pbin.VNIC_NORMAL @@ -432,30 +449,26 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, self._start_rpc_notifiers() def _init_native_dhcp(self): - if not cfg.CONF.nsx_v3.dhcp_profile_uuid: - raise cfg.RequiredOptError("dhcp_profile_uuid") try: nsx_resources.DhcpProfile(self._nsx_client).get( - cfg.CONF.nsx_v3.dhcp_profile_uuid) + self._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(_LE("Unable to retrieve DHCP Profile %s, " "native DHCP service is not supported"), - cfg.CONF.nsx_v3.dhcp_profile_uuid) + self._native_dhcp_profile_uuid) def _init_native_metadata(self): - if not cfg.CONF.nsx_v3.metadata_proxy_uuid: - raise cfg.RequiredOptError("metadata_proxy_uuid") try: nsx_resources.MetaDataProxy(self._nsx_client).get( - cfg.CONF.nsx_v3.metadata_proxy_uuid) + self._native_md_proxy_uuid) except nsx_lib_exc.ManagerError: with excutils.save_and_reraise_exception(): LOG.error(_LE("Unable to retrieve Metadata Proxy %s, " "native metadata service is not supported"), - cfg.CONF.nsx_v3.metadata_proxy_uuid) + self._native_md_proxy_uuid) def _setup_rpc(self): self.endpoints = [dhcp_rpc.DhcpRpcCallback(), @@ -720,7 +733,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, 'mdproxy', created_net['name'] or 'network'), created_net['id']) md_port = self._port_client.create( - nsx_net_id, cfg.CONF.nsx_v3.metadata_proxy_uuid, + nsx_net_id, self._native_md_proxy_uuid, tags=tags, name=name, attachment_type=nsxlib_consts.ATTACHMENT_MDPROXY) LOG.debug("Created MD-Proxy logical port %(port)s " @@ -911,7 +924,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, port_data = { "name": "", "admin_state_up": True, - "device_id": cfg.CONF.nsx_v3.dhcp_profile_uuid, + "device_id": self._native_dhcp_profile_uuid, "device_owner": const.DEVICE_OWNER_DHCP, "network_id": network['id'], "tenant_id": network["tenant_id"], @@ -925,6 +938,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, project_name=context.tenant_name) server_data = self.nsxlib.native_dhcp.build_server_config( network, subnet, neutron_port, net_tags) + server_data['dhcp_profile_id'] = self._native_dhcp_profile_uuid nsx_net_id = self._get_network_nsx_id(context, network['id']) port_tags = self.nsxlib.build_v3_tags_payload( neutron_port, resource_type='os-neutron-dport-id', diff --git a/vmware_nsx/plugins/nsx_v3/utils.py b/vmware_nsx/plugins/nsx_v3/utils.py index f7e5667618..dd57019ea9 100644 --- a/vmware_nsx/plugins/nsx_v3/utils.py +++ b/vmware_nsx/plugins/nsx_v3/utils.py @@ -43,5 +43,5 @@ def get_nsxlib_wrapper(): plugin_ver=n_version.version_info.release_string(), dns_nameservers=cfg.CONF.nsx_v3.nameservers, dns_domain=cfg.CONF.nsx_v3.dns_domain, - dhcp_profile_uuid=cfg.CONF.nsx_v3.dhcp_profile_uuid) + dhcp_profile_uuid=cfg.CONF.nsx_v3.dhcp_profile) return v3.NsxLib(nsxlib_config) 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 1622f899f1..91b0c1edd7 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv3/resources/dhcp_binding.py +++ b/vmware_nsx/shell/admin/plugins/nsxv3/resources/dhcp_binding.py @@ -64,7 +64,7 @@ def nsx_update_dhcp_bindings(resource, event, trigger, **kwargs): cfg.CONF.set_override('dhcp_agent_notification', False) cfg.CONF.set_override('native_dhcp_metadata', True, 'nsx_v3') - cfg.CONF.set_override('dhcp_profile_uuid', dhcp_profile_uuid, '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) 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 a901b13232..3a443fd1a7 100644 --- a/vmware_nsx/shell/admin/plugins/nsxv3/resources/metadata_proxy.py +++ b/vmware_nsx/shell/admin/plugins/nsxv3/resources/metadata_proxy.py @@ -74,7 +74,7 @@ def nsx_update_metadata_proxy(resource, event, trigger, **kwargs): cfg.CONF.set_override('dhcp_agent_notification', False) cfg.CONF.set_override('native_dhcp_metadata', True, 'nsx_v3') - cfg.CONF.set_override('metadata_proxy_uuid', metadata_proxy_uuid, 'nsx_v3') + cfg.CONF.set_override('metadata_proxy', metadata_proxy_uuid, 'nsx_v3') plugin = utils.NsxV3PluginWrapper() nsx_client = utils.get_nsxv3_client() diff --git a/vmware_nsx/tests/unit/nsx_v3/test_dhcp_metadata.py b/vmware_nsx/tests/unit/nsx_v3/test_dhcp_metadata.py index c5becc0ecb..6bf8c2c40a 100644 --- a/vmware_nsx/tests/unit/nsx_v3/test_dhcp_metadata.py +++ b/vmware_nsx/tests/unit/nsx_v3/test_dhcp_metadata.py @@ -43,8 +43,10 @@ class NsxNativeDhcpTestCase(test_plugin.NsxV3PluginTestCaseMixin): cfg.CONF.set_override('native_dhcp_metadata', True, 'nsx_v3') self._patcher = mock.patch.object(nsx_resources.DhcpProfile, 'get') self._patcher.start() - # Need to run _init_dhcp_metadata() manually because plugin was started + # Need to run _translate_configured_names_to_uuids and + # _init_dhcp_metadata() manually because plugin was started # before setUp() overrides CONF.nsx_v3.native_dhcp_metadata. + self.plugin._translate_configured_names_to_uuids() self.plugin._init_dhcp_metadata() def tearDown(self): @@ -93,7 +95,7 @@ class NsxNativeDhcpTestCase(test_plugin.NsxV3PluginTestCaseMixin): dhcp_binding['nsx_binding_id'], **binding_data) def test_dhcp_profile_configuration(self): - # Test if dhcp_agent_notification and dhcp_profile_uuid are + # Test if dhcp_agent_notification and dhcp_profile are # configured correctly. orig_dhcp_agent_notification = cfg.CONF.dhcp_agent_notification cfg.CONF.set_override('dhcp_agent_notification', True) @@ -101,11 +103,11 @@ class NsxNativeDhcpTestCase(test_plugin.NsxV3PluginTestCaseMixin): self.plugin._init_dhcp_metadata) cfg.CONF.set_override('dhcp_agent_notification', orig_dhcp_agent_notification) - orig_dhcp_profile_uuid = cfg.CONF.nsx_v3.dhcp_profile_uuid - cfg.CONF.set_override('dhcp_profile_uuid', '', 'nsx_v3') + orig_dhcp_profile_uuid = cfg.CONF.nsx_v3.dhcp_profile + cfg.CONF.set_override('dhcp_profile', '', 'nsx_v3') self.assertRaises(cfg.RequiredOptError, - self.plugin._init_dhcp_metadata) - cfg.CONF.set_override('dhcp_profile_uuid', orig_dhcp_profile_uuid, + self.plugin._translate_configured_names_to_uuids) + cfg.CONF.set_override('dhcp_profile', orig_dhcp_profile_uuid, 'nsx_v3') def test_dhcp_service_with_create_network(self): @@ -420,8 +422,10 @@ class NsxNativeMetadataTestCase(test_plugin.NsxV3PluginTestCaseMixin): cfg.CONF.set_override('native_dhcp_metadata', True, 'nsx_v3') self._patcher = mock.patch.object(nsx_resources.MetaDataProxy, 'get') self._patcher.start() - # Need to run _init_dhcp_metadata() manually because plugin was - # started before setUp() overrides CONF.nsx_v3.native_dhcp_metadata. + # Need to run _translate_configured_names_to_uuids and + # _init_dhcp_metadata() manually because plugin was started + # before setUp() overrides CONF.nsx_v3.native_dhcp_metadata. + self.plugin._translate_configured_names_to_uuids() self.plugin._init_dhcp_metadata() def tearDown(self): @@ -433,7 +437,7 @@ class NsxNativeMetadataTestCase(test_plugin.NsxV3PluginTestCaseMixin): super(NsxNativeMetadataTestCase, self).tearDown() def test_metadata_proxy_configuration(self): - # Test if dhcp_agent_notification and metadata_proxy_uuid are + # Test if dhcp_agent_notification and metadata_proxy are # configured correctly. orig_dhcp_agent_notification = cfg.CONF.dhcp_agent_notification cfg.CONF.set_override('dhcp_agent_notification', True) @@ -441,11 +445,11 @@ class NsxNativeMetadataTestCase(test_plugin.NsxV3PluginTestCaseMixin): self.plugin._init_dhcp_metadata) cfg.CONF.set_override('dhcp_agent_notification', orig_dhcp_agent_notification) - orig_metadata_proxy_uuid = cfg.CONF.nsx_v3.metadata_proxy_uuid - cfg.CONF.set_override('metadata_proxy_uuid', '', 'nsx_v3') + orig_metadata_proxy_uuid = cfg.CONF.nsx_v3.metadata_proxy + cfg.CONF.set_override('metadata_proxy', '', 'nsx_v3') self.assertRaises(cfg.RequiredOptError, - self.plugin._init_dhcp_metadata) - cfg.CONF.set_override('metadata_proxy_uuid', orig_metadata_proxy_uuid, + self.plugin._translate_configured_names_to_uuids) + cfg.CONF.set_override('metadata_proxy', orig_metadata_proxy_uuid, 'nsx_v3') def test_metadata_proxy_with_create_network(self): @@ -463,7 +467,7 @@ class NsxNativeMetadataTestCase(test_plugin.NsxV3PluginTestCaseMixin): 'mdproxy', network['network']['name'] or 'network'), network['network']['id']) create_logical_port.assert_called_once_with( - nsx_net_id, cfg.CONF.nsx_v3.metadata_proxy_uuid, + nsx_net_id, cfg.CONF.nsx_v3.metadata_proxy, tags=tags, name=name, attachment_type=nsx_constants.ATTACHMENT_MDPROXY) diff --git a/vmware_nsx/tests/unit/nsx_v3/test_plugin.py b/vmware_nsx/tests/unit/nsx_v3/test_plugin.py index 91573ef6a1..58d5a2d1c1 100644 --- a/vmware_nsx/tests/unit/nsx_v3/test_plugin.py +++ b/vmware_nsx/tests/unit/nsx_v3/test_plugin.py @@ -118,6 +118,14 @@ def _mock_nsx_backend_calls(): "vmware_nsx.nsxlib.v3.NsxLibLogicalSwitch.create", side_effect=_return_id_key).start() + mock.patch( + "vmware_nsx.nsxlib.v3.NsxLibDhcpProfile.get_id_by_name_or_id", + return_value=NSX_DHCP_PROFILE_ID).start() + + mock.patch( + "vmware_nsx.nsxlib.v3.NsxLibMetadataProxy.get_id_by_name_or_id", + return_value=NSX_METADATA_PROXY_ID).start() + mock.patch( "vmware_nsx.nsxlib.v3.resources.LogicalPort.create", side_effect=_return_id_key).start() @@ -141,9 +149,9 @@ class NsxV3PluginTestCaseMixin(test_plugin.NeutronDbPluginV2TestCase, def setup_conf_overrides(self): cfg.CONF.set_override('default_overlay_tz', NSX_TZ_NAME, 'nsx_v3') cfg.CONF.set_override('native_dhcp_metadata', False, 'nsx_v3') - cfg.CONF.set_override('dhcp_profile_uuid', + cfg.CONF.set_override('dhcp_profile', NSX_DHCP_PROFILE_ID, 'nsx_v3') - cfg.CONF.set_override('metadata_proxy_uuid', + cfg.CONF.set_override('metadata_proxy', NSX_METADATA_PROXY_ID, 'nsx_v3') cfg.CONF.set_override( 'network_scheduler_driver',