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
This commit is contained in:
Zhongyue Luo 2013-01-03 10:40:16 +08:00
parent eae2ae5cc4
commit e3721d2e05
7 changed files with 19 additions and 25 deletions

View File

@ -653,7 +653,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
p = port['port'] p = port['port']
ips = [] ips = []
fixed_configured = (p['fixed_ips'] != attributes.ATTR_NOT_SPECIFIED) fixed_configured = p['fixed_ips'] is not attributes.ATTR_NOT_SPECIFIED
if fixed_configured: if fixed_configured:
configured_ips = self._test_fixed_ips_for_port(context, configured_ips = self._test_fixed_ips_for_port(context,
p["network_id"], p["network_id"],
@ -803,7 +803,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
""" """
pools = [] 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 # Auto allocate the pool around gateway_ip
net = netaddr.IPNetwork(subnet['cidr']) net = netaddr.IPNetwork(subnet['cidr'])
first_ip = net.first + 1 first_ip = net.first + 1
@ -1008,9 +1008,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
if 'cidr' in s: if 'cidr' in s:
self._validate_ip_version(ip_ver, s['cidr'], 'cidr') self._validate_ip_version(ip_ver, s['cidr'], 'cidr')
if ('gateway_ip' in s and if attributes.is_attr_set(s.get('gateway_ip')):
s['gateway_ip'] and
s['gateway_ip'] != attributes.ATTR_NOT_SPECIFIED):
self._validate_ip_version(ip_ver, s['gateway_ip'], 'gateway_ip') self._validate_ip_version(ip_ver, s['gateway_ip'], 'gateway_ip')
if (cfg.CONF.force_gateway_on_subnet and if (cfg.CONF.force_gateway_on_subnet and
not QuantumDbPluginV2._check_subnet_ip(s['cidr'], 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") error_message = _("Gateway is not valid on subnet")
raise q_exc.InvalidInput(error_message=error_message) raise q_exc.InvalidInput(error_message=error_message)
if ('dns_nameservers' in s and if attributes.is_attr_set(s.get('dns_nameservers')):
s['dns_nameservers'] != attributes.ATTR_NOT_SPECIFIED):
if len(s['dns_nameservers']) > cfg.CONF.max_dns_nameservers: if len(s['dns_nameservers']) > cfg.CONF.max_dns_nameservers:
raise q_exc.DNSNameServersExhausted( raise q_exc.DNSNameServersExhausted(
subnet_id=id, subnet_id=id,
@ -1033,8 +1030,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
dns)) dns))
self._validate_ip_version(ip_ver, dns, 'dns_nameserver') self._validate_ip_version(ip_ver, dns, 'dns_nameserver')
if ('host_routes' in s and if attributes.is_attr_set(s.get('host_routes')):
s['host_routes'] != attributes.ATTR_NOT_SPECIFIED):
if len(s['host_routes']) > cfg.CONF.max_subnet_host_routes: if len(s['host_routes']) > cfg.CONF.max_subnet_host_routes:
raise q_exc.HostRoutesExhausted( raise q_exc.HostRoutesExhausted(
subnet_id=id, subnet_id=id,
@ -1048,7 +1044,7 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
self._validate_subnet(s) self._validate_subnet(s)
net = netaddr.IPNetwork(s['cidr']) 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)) s['gateway_ip'] = str(netaddr.IPAddress(net.first + 1))
tenant_id = self._get_tenant_id_for_create(context, s) 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) pools = self._allocate_pools_for_subnet(context, s)
context.session.add(subnet) 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']: for addr in s['dns_nameservers']:
ns = models_v2.DNSNameServer(address=addr, ns = models_v2.DNSNameServer(address=addr,
subnet_id=subnet.id) subnet_id=subnet.id)
context.session.add(ns) 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']: for rt in s['host_routes']:
route = models_v2.Route(subnet_id=subnet.id, route = models_v2.Route(subnet_id=subnet.id,
destination=rt['destination'], 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 # Ensure that a MAC address is defined and it is unique on the
# network # network
if mac_address == attributes.ATTR_NOT_SPECIFIED: if mac_address is attributes.ATTR_NOT_SPECIFIED:
mac_address = QuantumDbPluginV2._generate_mac(context, mac_address = QuantumDbPluginV2._generate_mac(context,
network_id) network_id)
else: else:

View File

@ -288,7 +288,7 @@ class LoadBalancerPluginDb(LoadBalancerPluginBase):
tenant_id = self._get_tenant_id_for_create(context, v) tenant_id = self._get_tenant_id_for_create(context, v)
with context.session.begin(subtransactions=True): with context.session.begin(subtransactions=True):
if v['address'] == attributes.ATTR_NOT_SPECIFIED: if v['address'] is attributes.ATTR_NOT_SPECIFIED:
address = None address = None
else: else:
address = v['address'] address = v['address']

View File

@ -78,7 +78,7 @@ class NECPluginV2Base(db_base_plugin_v2.QuantumDbPluginV2):
# validate network ownership # validate network ownership
super(NECPluginV2Base, self).get_network(context, pf['network_id']) 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 # validate port ownership
super(NECPluginV2Base, self).get_port(context, pf['in_port']) super(NECPluginV2Base, self).get_port(context, pf['in_port'])
@ -99,7 +99,7 @@ class NECPluginV2Base(db_base_plugin_v2.QuantumDbPluginV2):
'dst_port': 0, 'dst_port': 0,
'protocol': ''} 'protocol': ''}
for key, default in conditions.items(): 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}) params.update({key: default})
else: else:
params.update({key: pf.get(key)}) params.update({key: pf.get(key)})

View File

@ -362,7 +362,7 @@ class NvpPluginV2(db_base_plugin_v2.QuantumDbPluginV2):
self._handle_provider_create(context, net_data) self._handle_provider_create(context, net_data)
# Replace ATTR_NOT_SPECIFIED with None before sending to NVP # Replace ATTR_NOT_SPECIFIED with None before sending to NVP
for attr, value in network['network'].iteritems(): for attr, value in network['network'].iteritems():
if value == attributes.ATTR_NOT_SPECIFIED: if value is attributes.ATTR_NOT_SPECIFIED:
net_data[attr] = None net_data[attr] = None
# FIXME(arosen) implement admin_state_up = False in NVP # FIXME(arosen) implement admin_state_up = False in NVP
if net_data['admin_state_up'] is False: if net_data['admin_state_up'] is False:

View File

@ -66,12 +66,10 @@ def _set_rules(data):
def _is_attribute_explicitly_set(attribute_name, resource, target): def _is_attribute_explicitly_set(attribute_name, resource, target):
"""Verify that an attribute is present and has a non-default value""" """Verify that an attribute is present and has a non-default value"""
if ('default' in resource[attribute_name] and return ('default' in resource[attribute_name] and
target.get(attribute_name, attributes.ATTR_NOT_SPECIFIED) != attribute_name in target and
attributes.ATTR_NOT_SPECIFIED): target[attribute_name] is not attributes.ATTR_NOT_SPECIFIED and
if (target[attribute_name] != resource[attribute_name]['default']): target[attribute_name] != resource[attribute_name]['default'])
return True
return False
def _build_target(action, original_target, plugin, context): def _build_target(action, original_target, plugin, context):

View File

@ -24,7 +24,6 @@ from quantum import context
from quantum.api.extensions import PluginAwareExtensionManager from quantum.api.extensions import PluginAwareExtensionManager
from quantum.api.extensions import ExtensionMiddleware from quantum.api.extensions import ExtensionMiddleware
from quantum.api.v2 import attributes from quantum.api.v2 import attributes
from quantum.api.v2.attributes import ATTR_NOT_SPECIFIED
from quantum.api.v2.router import APIRouter from quantum.api.v2.router import APIRouter
from quantum.common import config from quantum.common import config
from quantum.common import exceptions as q_exc from quantum.common import exceptions as q_exc

View File

@ -249,7 +249,8 @@ class QuantumDbPluginV2TestCase(unittest2.TestCase):
if arg in kwargs and kwargs[arg] is not None: if arg in kwargs and kwargs[arg] is not None:
data['subnet'][arg] = kwargs[arg] 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'] data['subnet']['gateway_ip'] = kwargs['gateway_ip']
subnet_req = self.new_create_request('subnets', data, fmt) subnet_req = self.new_create_request('subnets', data, fmt)