From 4970636e210dd3a7088a8f729df6fdff92c02fb7 Mon Sep 17 00:00:00 2001 From: Zhongyue Luo Date: Thu, 3 Jan 2013 10:40:16 +0800 Subject: [PATCH] Fix ATTR_NOT_SPECIFIED comparison errors 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 | 22 ++++++++----------- quantum/db/loadbalancer/loadbalancer_db.py | 2 +- quantum/plugins/nec/db/nec_plugin_base.py | 4 ++-- .../nicira/nicira_nvp_plugin/QuantumPlugin.py | 2 +- quantum/policy.py | 10 ++++----- .../db/loadbalancer/test_db_loadbalancer.py | 1 - quantum/tests/unit/test_db_plugin.py | 3 ++- 7 files changed, 19 insertions(+), 25 deletions(-) diff --git a/quantum/db/db_base_plugin_v2.py b/quantum/db/db_base_plugin_v2.py index 0eb995b13..f5b6d9ed4 100644 --- a/quantum/db/db_base_plugin_v2.py +++ b/quantum/db/db_base_plugin_v2.py @@ -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: diff --git a/quantum/db/loadbalancer/loadbalancer_db.py b/quantum/db/loadbalancer/loadbalancer_db.py index db1262868..a1ad6882d 100644 --- a/quantum/db/loadbalancer/loadbalancer_db.py +++ b/quantum/db/loadbalancer/loadbalancer_db.py @@ -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'] diff --git a/quantum/plugins/nec/db/nec_plugin_base.py b/quantum/plugins/nec/db/nec_plugin_base.py index a2a2d001e..cba8050be 100644 --- a/quantum/plugins/nec/db/nec_plugin_base.py +++ b/quantum/plugins/nec/db/nec_plugin_base.py @@ -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)}) diff --git a/quantum/plugins/nicira/nicira_nvp_plugin/QuantumPlugin.py b/quantum/plugins/nicira/nicira_nvp_plugin/QuantumPlugin.py index 62a01596f..8c497952c 100644 --- a/quantum/plugins/nicira/nicira_nvp_plugin/QuantumPlugin.py +++ b/quantum/plugins/nicira/nicira_nvp_plugin/QuantumPlugin.py @@ -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: diff --git a/quantum/policy.py b/quantum/policy.py index f464a6f0b..30e514e7d 100644 --- a/quantum/policy.py +++ b/quantum/policy.py @@ -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): diff --git a/quantum/tests/unit/db/loadbalancer/test_db_loadbalancer.py b/quantum/tests/unit/db/loadbalancer/test_db_loadbalancer.py index 81f5b1202..a5fa0626d 100644 --- a/quantum/tests/unit/db/loadbalancer/test_db_loadbalancer.py +++ b/quantum/tests/unit/db/loadbalancer/test_db_loadbalancer.py @@ -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 diff --git a/quantum/tests/unit/test_db_plugin.py b/quantum/tests/unit/test_db_plugin.py index e3ecdd26f..da4bafe3c 100644 --- a/quantum/tests/unit/test_db_plugin.py +++ b/quantum/tests/unit/test_db_plugin.py @@ -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) -- 2.45.2