From 05b2dfd3e905ac1bc6e4207458f7e89cf3afffca Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Sun, 20 Aug 2017 10:35:28 +0300 Subject: [PATCH] NSX|V3: validate transport zone at provider net creation Validate the existence ant type of the transport zone when creating a provider network Change-Id: I90fe67fb46903ec316440225b0c26609a223fe9c Depends-on: Ibe674ac95f794b5c01dc9fd5d3566f98b7b05d05 --- vmware_nsx/plugins/nsx_v3/plugin.py | 22 ++++++++++++++++--- .../tests/unit/nsx_v3/test_dhcp_metadata.py | 11 ++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/vmware_nsx/plugins/nsx_v3/plugin.py b/vmware_nsx/plugins/nsx_v3/plugin.py index 412c6b0803..7198b8d100 100644 --- a/vmware_nsx/plugins/nsx_v3/plugin.py +++ b/vmware_nsx/plugins/nsx_v3/plugin.py @@ -655,6 +655,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, err_msg = None net_type = network_data.get(pnet.NETWORK_TYPE) + tz_type = self.nsxlib.transport_zone.TRANSPORT_TYPE_VLAN if validators.is_attr_set(net_type): if net_type == utils.NsxV3NetworkTypes.FLAT: if vlan_id is not None: @@ -696,6 +697,7 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, err_msg = (_("Segmentation ID cannot be specified with " "%s network type") % utils.NsxV3NetworkTypes.VXLAN) + tz_type = self.nsxlib.transport_zone.TRANSPORT_TYPE_OVERLAY else: err_msg = (_('%(net_type_param)s %(net_type_value)s not ' 'supported') % @@ -710,13 +712,27 @@ class NsxV3Plugin(agentschedulers_db.AZDhcpAgentSchedulerDbMixin, else: net_type = None - if err_msg: - raise n_exc.InvalidInput(error_message=err_msg) - if physical_net is None: # Default to transport type overlay physical_net = az._default_overlay_tz_uuid + # validate the transport zone existence and type + if not err_msg and is_provider_net and physical_net: + try: + backend_type = self.nsxlib.transport_zone.get_transport_type( + physical_net) + except nsx_lib_exc.ResourceNotFound: + err_msg = (_('Transport zone %s does not exist') % + physical_net) + else: + if backend_type != tz_type: + err_msg = (_('%(tz)s transport zone is required for ' + 'creating a %(net)s provider network') % + {'tz': tz_type, 'net': net_type}) + + if err_msg: + raise n_exc.InvalidInput(error_message=err_msg) + return is_provider_net, net_type, physical_net, vlan_id def _get_edge_cluster(self, tier0_uuid): 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 bf348e1fff..6fbda53a6b 100644 --- a/vmware_nsx/tests/unit/nsx_v3/test_dhcp_metadata.py +++ b/vmware_nsx/tests/unit/nsx_v3/test_dhcp_metadata.py @@ -290,10 +290,13 @@ class NsxNativeDhcpTestCase(test_plugin.NsxV3PluginTestCaseMixin): povidernet_args = {pnet.NETWORK_TYPE: 'vlan', pnet.PHYSICAL_NETWORK: 'tzuuid', pnet.SEGMENTATION_ID: 100} - with self.network(providernet_args=povidernet_args, - arg_list=(pnet.NETWORK_TYPE, - pnet.PHYSICAL_NETWORK, - pnet.SEGMENTATION_ID)) as network: + with mock.patch( + 'vmware_nsxlib.v3.core_resources.NsxLibTransportZone.' + 'get_transport_type', return_value='VLAN'),\ + self.network(providernet_args=povidernet_args, + arg_list=(pnet.NETWORK_TYPE, + pnet.PHYSICAL_NETWORK, + pnet.SEGMENTATION_ID)) as network: subnet = {'subnet': {'network_id': network['network']['id'], 'cidr': '10.0.0.0/24', 'enable_dhcp': True}}