From 4f56575b1fc840d4667ce4c04dfac95a907493e9 Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Mon, 13 Nov 2017 07:43:28 +0200 Subject: [PATCH] NSX|v+v3: fix validate network callback The registered callback for validating that a router gateway has subnets, is called in both plugins so it should be common, and elevated context should be used to get the subnets. Change-Id: If8cff77e258b3d4df12d385fdbc4be4e7986daa6 --- vmware_nsx/plugins/common/plugin.py | 24 ++++++++++++++++++++++++ vmware_nsx/plugins/nsx_v/plugin.py | 17 ----------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/vmware_nsx/plugins/common/plugin.py b/vmware_nsx/plugins/common/plugin.py index 441c5c5ad6..a323fc4adf 100644 --- a/vmware_nsx/plugins/common/plugin.py +++ b/vmware_nsx/plugins/common/plugin.py @@ -27,6 +27,9 @@ from neutron_lib.api.definitions import network as net_def from neutron_lib.api.definitions import port as port_def from neutron_lib.api.definitions import subnet as subnet_def from neutron_lib.api import validators +from neutron_lib.callbacks import events +from neutron_lib.callbacks import registry +from neutron_lib.callbacks import resources from neutron_lib import constants from neutron_lib import context as n_context from neutron_lib import exceptions as n_exc @@ -308,3 +311,24 @@ class NsxPluginBase(db_base_plugin_v2.NeutronDbPluginV2, "with no subnet") % network_id raise n_exc.BadRequest(resource='router', msg=msg) return gw_info + + def get_subnets_by_network(self, context, network_id): + return [self._make_subnet_dict(subnet_obj) for subnet_obj in + self._get_subnets_by_network(context.elevated(), network_id)] + + +# Register the callback +def _validate_network_has_subnet(resource, event, trigger, **kwargs): + network_id = kwargs.get('network_id') + subnets = kwargs.get('subnets') + if not subnets: + msg = _('No subnet defined on network %s') % network_id + raise n_exc.InvalidInput(error_message=msg) + + +def subscribe(): + registry.subscribe(_validate_network_has_subnet, + resources.ROUTER_GATEWAY, events.BEFORE_CREATE) + + +subscribe() diff --git a/vmware_nsx/plugins/nsx_v/plugin.py b/vmware_nsx/plugins/nsx_v/plugin.py index 64060f5e7f..c424fc28cf 100644 --- a/vmware_nsx/plugins/nsx_v/plugin.py +++ b/vmware_nsx/plugins/nsx_v/plugin.py @@ -4525,20 +4525,3 @@ class NsxVPluginV2(addr_pair_db.AllowedAddressPairsMixin, if not self._nsx_policy_is_hidden(policy): results.append(self._nsx_policy_to_dict(policy)) return results - - -# Register the callback -def _validate_network_has_subnet(resource, event, trigger, **kwargs): - network_id = kwargs.get('network_id') - subnets = kwargs.get('subnets') - if not subnets: - msg = _('No subnet defined on network %s') % network_id - raise n_exc.InvalidInput(error_message=msg) - - -def subscribe(): - registry.subscribe(_validate_network_has_subnet, - resources.ROUTER_GATEWAY, events.BEFORE_CREATE) - - -subscribe()