]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Decompose _create_subnet_from_pool
authorPavel Bondar <pbondar@infoblox.com>
Fri, 19 Jun 2015 14:58:57 +0000 (17:58 +0300)
committerPavel Bondar <pbondar@infoblox.com>
Mon, 22 Jun 2015 08:15:54 +0000 (08:15 +0000)
Moved validations into separate methods:
- _validate_pools_with_subnetpool
Verifies that allocation pools are set only for specific subnet request.
For any subnet request allocation pools can not be set manually
- _validate_ip_version_with_subnetpool
Verifies that subnet has the same ip version as subnet pool

Partially-Implements: blueprint neutron-ipam

Change-Id: I63f6aa2a0c94c3437fa624ac800943976f4fc50f

neutron/db/db_base_plugin_v2.py
neutron/db/ipam_backend_mixin.py

index ca0bd27666cca0bbce00ce61825f66caef46db08..42c4eb97514778b59d7af7c9722d22c492cb9fea 100644 (file)
@@ -582,23 +582,11 @@ class NeutronDbPluginV2(ipam_non_pluggable_backend.IpamNonPluggableBackend,
     def _create_subnet_from_pool(self, context, subnet, subnetpool_id):
         s = subnet['subnet']
         tenant_id = self._get_tenant_id_for_create(context, s)
-        has_allocpool = attributes.is_attr_set(s['allocation_pools'])
-        is_any_subnetpool_request = not attributes.is_attr_set(s['cidr'])
-        if is_any_subnetpool_request and has_allocpool:
-            reason = _("allocation_pools allowed only "
-                       "for specific subnet requests.")
-            raise n_exc.BadRequest(resource='subnets', msg=reason)
+        self._validate_pools_with_subnetpool(s)
 
         with context.session.begin(subtransactions=True):
             subnetpool = self._get_subnetpool(context, subnetpool_id)
-            ip_version = s.get('ip_version')
-            has_ip_version = attributes.is_attr_set(ip_version)
-            if has_ip_version and ip_version != subnetpool.ip_version:
-                args = {'req_ver': str(s['ip_version']),
-                        'pool_ver': str(subnetpool.ip_version)}
-                reason = _("Cannot allocate IPv%(req_ver)s subnet from "
-                           "IPv%(pool_ver)s subnet pool") % args
-                raise n_exc.BadRequest(resource='subnets', msg=reason)
+            self._validate_ip_version_with_subnetpool(s, subnetpool)
 
             network = self._get_network(context, s["network_id"])
             allocator = subnet_alloc.SubnetAllocator(subnetpool, context)
index ea4ec26b6f98b5b775c50becf1e8deddf57219f4..47c8141d887a6235a3265c59791cfc81ece85ba2 100644 (file)
@@ -20,6 +20,7 @@ from oslo_config import cfg
 from oslo_db import exception as db_exc
 from oslo_log import log as logging
 
+from neutron.api.v2 import attributes
 from neutron.common import constants
 from neutron.common import exceptions as n_exc
 from neutron.common import ipv6_utils
@@ -43,6 +44,29 @@ class IpamBackendMixin(db_base_plugin_common.DbBasePluginCommon):
         """
         pass
 
+    def _validate_pools_with_subnetpool(self, subnet):
+        """Verifies that allocation pools are set correctly
+
+        Allocation pools can be set for specific subnet request only
+        """
+        has_allocpool = attributes.is_attr_set(subnet['allocation_pools'])
+        is_any_subnetpool_request = not attributes.is_attr_set(subnet['cidr'])
+        if is_any_subnetpool_request and has_allocpool:
+            reason = _("allocation_pools allowed only "
+                       "for specific subnet requests.")
+            raise n_exc.BadRequest(resource='subnets', msg=reason)
+
+    def _validate_ip_version_with_subnetpool(self, subnet, subnetpool):
+        """Validates ip version for subnet_pool and requested subnet"""
+        ip_version = subnet.get('ip_version')
+        has_ip_version = attributes.is_attr_set(ip_version)
+        if has_ip_version and ip_version != subnetpool.ip_version:
+            args = {'req_ver': str(subnet['ip_version']),
+                    'pool_ver': str(subnetpool.ip_version)}
+            reason = _("Cannot allocate IPv%(req_ver)s subnet from "
+                       "IPv%(pool_ver)s subnet pool") % args
+            raise n_exc.BadRequest(resource='subnets', msg=reason)
+
     def _update_db_port(self, context, db_port, new_port, network_id, new_mac):
         # Remove all attributes in new_port which are not in the port DB model
         # and then update the port