]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fix ATTR_NOT_SPECIFIED comparison errors
authorZhongyue Luo <zhongyue.nah@intel.com>
Thu, 3 Jan 2013 02:40:16 +0000 (10:40 +0800)
committerZhongyue Luo <zhongyue.nah@intel.com>
Wed, 16 Jan 2013 03:04:23 +0000 (11:04 +0800)
Fixes bug #1099663

Replaced equality operators used with ATTR_NOT_SPECIFIED to 'is' or 'is not'.
Used is_attr_set() where comparsion is done to None and ATTR_NOT_SPECIFIED.

Change-Id: I67c87051b46ca0518fa777cbb1c3e6141a533b61

quantum/db/db_base_plugin_v2.py
quantum/db/loadbalancer/loadbalancer_db.py
quantum/plugins/nec/db/nec_plugin_base.py
quantum/plugins/nicira/nicira_nvp_plugin/QuantumPlugin.py
quantum/policy.py
quantum/tests/unit/db/loadbalancer/test_db_loadbalancer.py
quantum/tests/unit/test_db_plugin.py

index 0eb995b135d8bb599d3e9ec06a448f11e4fec202..f5b6d9ed45a2bd2917569d73147b3233b75466ee 100644 (file)
@@ -653,7 +653,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
         p = port['port']
         ips = []
 
-        fixed_configured = (p['fixed_ips'] != attributes.ATTR_NOT_SPECIFIED)
+        fixed_configured = p['fixed_ips'] is not attributes.ATTR_NOT_SPECIFIED
         if fixed_configured:
             configured_ips = self._test_fixed_ips_for_port(context,
                                                            p["network_id"],
@@ -803,7 +803,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
         """
 
         pools = []
-        if subnet['allocation_pools'] == attributes.ATTR_NOT_SPECIFIED:
+        if subnet['allocation_pools'] is attributes.ATTR_NOT_SPECIFIED:
             # Auto allocate the pool around gateway_ip
             net = netaddr.IPNetwork(subnet['cidr'])
             first_ip = net.first + 1
@@ -1008,9 +1008,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
 
         if 'cidr' in s:
             self._validate_ip_version(ip_ver, s['cidr'], 'cidr')
-        if ('gateway_ip' in s and
-            s['gateway_ip'] and
-            s['gateway_ip'] != attributes.ATTR_NOT_SPECIFIED):
+        if attributes.is_attr_set(s.get('gateway_ip')):
             self._validate_ip_version(ip_ver, s['gateway_ip'], 'gateway_ip')
             if (cfg.CONF.force_gateway_on_subnet and
                 not QuantumDbPluginV2._check_subnet_ip(s['cidr'],
@@ -1018,8 +1016,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
                 error_message = _("Gateway is not valid on subnet")
                 raise q_exc.InvalidInput(error_message=error_message)
 
-        if ('dns_nameservers' in s and
-            s['dns_nameservers'] != attributes.ATTR_NOT_SPECIFIED):
+        if attributes.is_attr_set(s.get('dns_nameservers')):
             if len(s['dns_nameservers']) > cfg.CONF.max_dns_nameservers:
                 raise q_exc.DNSNameServersExhausted(
                     subnet_id=id,
@@ -1033,8 +1030,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
                                        dns))
                 self._validate_ip_version(ip_ver, dns, 'dns_nameserver')
 
-        if ('host_routes' in s and
-            s['host_routes'] != attributes.ATTR_NOT_SPECIFIED):
+        if attributes.is_attr_set(s.get('host_routes')):
             if len(s['host_routes']) > cfg.CONF.max_subnet_host_routes:
                 raise q_exc.HostRoutesExhausted(
                     subnet_id=id,
@@ -1048,7 +1044,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
         self._validate_subnet(s)
 
         net = netaddr.IPNetwork(s['cidr'])
-        if s['gateway_ip'] == attributes.ATTR_NOT_SPECIFIED:
+        if s['gateway_ip'] is attributes.ATTR_NOT_SPECIFIED:
             s['gateway_ip'] = str(netaddr.IPAddress(net.first + 1))
 
         tenant_id = self._get_tenant_id_for_create(context, s)
@@ -1072,13 +1068,13 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
             pools = self._allocate_pools_for_subnet(context, s)
 
             context.session.add(subnet)
-            if s['dns_nameservers'] != attributes.ATTR_NOT_SPECIFIED:
+            if s['dns_nameservers'] is not attributes.ATTR_NOT_SPECIFIED:
                 for addr in s['dns_nameservers']:
                     ns = models_v2.DNSNameServer(address=addr,
                                                  subnet_id=subnet.id)
                     context.session.add(ns)
 
-            if s['host_routes'] != attributes.ATTR_NOT_SPECIFIED:
+            if s['host_routes'] is not attributes.ATTR_NOT_SPECIFIED:
                 for rt in s['host_routes']:
                     route = models_v2.Route(subnet_id=subnet.id,
                                             destination=rt['destination'],
@@ -1206,7 +1202,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
 
             # Ensure that a MAC address is defined and it is unique on the
             # network
-            if mac_address == attributes.ATTR_NOT_SPECIFIED:
+            if mac_address is attributes.ATTR_NOT_SPECIFIED:
                 mac_address = QuantumDbPluginV2._generate_mac(context,
                                                               network_id)
             else:
index db12628685cee7933948c4863e8970844eeebd8b..a1ad6882dd9f4a5416e2cba9e598f98263c65ec7 100644 (file)
@@ -288,7 +288,7 @@ class LoadBalancerPluginDb(LoadBalancerPluginBase):
         tenant_id = self._get_tenant_id_for_create(context, v)
 
         with context.session.begin(subtransactions=True):
-            if v['address'] == attributes.ATTR_NOT_SPECIFIED:
+            if v['address'] is attributes.ATTR_NOT_SPECIFIED:
                 address = None
             else:
                 address = v['address']
index a2a2d001e121a776705d3f4c4cd002b1e55ae687..cba8050be00fbf85135fad45f465cefd176449bf 100644 (file)
@@ -78,7 +78,7 @@ class NECPluginV2Base(db_base_plugin_v2.QuantumDbPluginV2):
 
         # validate network ownership
         super(NECPluginV2Base, self).get_network(context, pf['network_id'])
-        if pf.get('in_port') != attributes.ATTR_NOT_SPECIFIED:
+        if pf.get('in_port') is not attributes.ATTR_NOT_SPECIFIED:
             # validate port ownership
             super(NECPluginV2Base, self).get_port(context, pf['in_port'])
 
@@ -99,7 +99,7 @@ class NECPluginV2Base(db_base_plugin_v2.QuantumDbPluginV2):
                       'dst_port': 0,
                       'protocol': ''}
         for key, default in conditions.items():
-            if pf.get(key) == attributes.ATTR_NOT_SPECIFIED:
+            if pf.get(key) is attributes.ATTR_NOT_SPECIFIED:
                 params.update({key: default})
             else:
                 params.update({key: pf.get(key)})
index 62a01596f729a113545bf4c5fa084a5e655fe748..8c497952c36dd4695974c726507a7dfe60142864 100644 (file)
@@ -362,7 +362,7 @@ class NvpPluginV2(db_base_plugin_v2.QuantumDbPluginV2):
         self._handle_provider_create(context, net_data)
         # Replace ATTR_NOT_SPECIFIED with None before sending to NVP
         for attr, value in network['network'].iteritems():
-            if value == attributes.ATTR_NOT_SPECIFIED:
+            if value is attributes.ATTR_NOT_SPECIFIED:
                 net_data[attr] = None
         # FIXME(arosen) implement admin_state_up = False in NVP
         if net_data['admin_state_up'] is False:
index f464a6f0b77fcc54672bcd7f4ea9614a5b6f0530..30e514e7d63d9548fad59735b160e3d50a05822c 100644 (file)
@@ -66,12 +66,10 @@ def _set_rules(data):
 
 def _is_attribute_explicitly_set(attribute_name, resource, target):
     """Verify that an attribute is present and has a non-default value"""
-    if ('default' in resource[attribute_name] and
-        target.get(attribute_name, attributes.ATTR_NOT_SPECIFIED) !=
-        attributes.ATTR_NOT_SPECIFIED):
-        if (target[attribute_name] != resource[attribute_name]['default']):
-            return True
-    return False
+    return ('default' in resource[attribute_name] and
+            attribute_name in target and
+            target[attribute_name] is not attributes.ATTR_NOT_SPECIFIED and
+            target[attribute_name] != resource[attribute_name]['default'])
 
 
 def _build_target(action, original_target, plugin, context):
index 81f5b12022be5a57febd71af29bb803c3065e710..a5fa0626d5e427f74ce91a20f2d228b7e90badd1 100644 (file)
@@ -24,7 +24,6 @@ from quantum import context
 from quantum.api.extensions import PluginAwareExtensionManager
 from quantum.api.extensions import ExtensionMiddleware
 from quantum.api.v2 import attributes
-from quantum.api.v2.attributes import ATTR_NOT_SPECIFIED
 from quantum.api.v2.router import APIRouter
 from quantum.common import config
 from quantum.common import exceptions as q_exc
index e3ecdd26f9caec4ccba2527d3dd574958291ebfd..da4bafe3c12f6ffe4ef203fa5eac22aa39663326 100644 (file)
@@ -249,7 +249,8 @@ class QuantumDbPluginV2TestCase(unittest2.TestCase):
             if arg in kwargs and kwargs[arg] is not None:
                 data['subnet'][arg] = kwargs[arg]
 
-        if kwargs.get('gateway_ip', ATTR_NOT_SPECIFIED) != ATTR_NOT_SPECIFIED:
+        if ('gateway_ip' in kwargs and
+            kwargs['gateway_ip'] is not ATTR_NOT_SPECIFIED):
             data['subnet']['gateway_ip'] = kwargs['gateway_ip']
 
         subnet_req = self.new_create_request('subnets', data, fmt)