From: shu,xinxin Date: Thu, 21 Mar 2013 10:48:00 +0000 (+0800) Subject: use the fact that empty sequences are false X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=b1067c94c53e22cc9f8400cf7b2049e35d06877b;p=openstack-build%2Fneutron-build.git use the fact that empty sequences are false fixed bug #1156473 Change-Id: I43470eb884107111cbea958868f4cf8241c8d6e6 --- diff --git a/quantum/db/dhcp_rpc_base.py b/quantum/db/dhcp_rpc_base.py index 08b8c99c2..ea4a5a5f7 100644 --- a/quantum/db/dhcp_rpc_base.py +++ b/quantum/db/dhcp_rpc_base.py @@ -91,7 +91,7 @@ class DhcpRpcCallbackMixin(object): try: filters = dict(network_id=[network_id], device_id=[device_id]) ports = plugin.get_ports(context, filters=filters) - if len(ports): + if ports: # Ensure that fixed_ips cover all dhcp_enabled subnets. port = ports[0] for fixed_ip in port['fixed_ips']: @@ -144,7 +144,7 @@ class DhcpRpcCallbackMixin(object): filters = dict(network_id=[network_id], device_id=[device_id]) ports = plugin.get_ports(context, filters=filters) - if len(ports): + if ports: plugin.delete_port(context, ports[0]['id']) def release_port_fixed_ip(self, context, **kwargs): @@ -160,7 +160,7 @@ class DhcpRpcCallbackMixin(object): filters = dict(network_id=[network_id], device_id=[device_id]) ports = plugin.get_ports(context, filters=filters) - if len(ports): + if ports: port = ports[0] fixed_ips = port.get('fixed_ips', []) diff --git a/quantum/db/l3_db.py b/quantum/db/l3_db.py index e21e8b4dc..70d1ab4fd 100644 --- a/quantum/db/l3_db.py +++ b/quantum/db/l3_db.py @@ -221,7 +221,7 @@ class L3_NAT_db_mixin(l3.RouterPluginBase): 'admin_state_up': True, 'name': ''}}) - if not len(gw_port['fixed_ips']): + if not gw_port['fixed_ips']: self.delete_port(context.elevated(), gw_port['id'], l3_port_check=False) msg = (_('No IPs available for external network %s') % @@ -550,7 +550,7 @@ class L3_NAT_db_mixin(l3.RouterPluginBase): raise q_exc.BadRequest(resource='floatingip', msg=msg) else: ips = [ip['ip_address'] for ip in internal_port['fixed_ips']] - if len(ips) == 0: + if not ips: msg = (_('Cannot add floating IP to port %s that has' 'no fixed IP addresses') % internal_port['id']) raise q_exc.BadRequest(resource='floatingip', msg=msg) diff --git a/quantum/extensions/servicetype.py b/quantum/extensions/servicetype.py index 3d43f7f63..0468d1f88 100644 --- a/quantum/extensions/servicetype.py +++ b/quantum/extensions/servicetype.py @@ -85,7 +85,7 @@ def _validate_servicetype_ref(data, valid_values=None): def _validate_service_defs(data, valid_values=None): """ Validate the list of service definitions. """ try: - if len(data) == 0: + if not data: return _("No service type definition was provided. At least a " "service type definition must be provided") f_name = _validate_service_defs.__name__ @@ -137,7 +137,7 @@ def _validate_service_defs(data, valid_values=None): return msg del svc_def_copy[DRIVER_ATTR] # Anything left - it should be an error - if len(svc_def_copy): + if svc_def_copy: msg = (_("Unparseable attributes found in " "service definition %s") % svc_def) LOG.error(_("%(f_name)s: %(msg)s"), locals()) diff --git a/quantum/plugins/hyperv/agent/utils.py b/quantum/plugins/hyperv/agent/utils.py index a49248682..703b61765 100644 --- a/quantum/plugins/hyperv/agent/utils.py +++ b/quantum/plugins/hyperv/agent/utils.py @@ -72,7 +72,7 @@ class HyperVUtils(object): def _get_vnic_settings(self, vnic_name): vnic_settings = self._conn.Msvm_SyntheticEthernetPortSettingData( ElementName=vnic_name) - if not len(vnic_settings): + if not vnic_settings: raise HyperVException(msg=_('Vnic not found: %s') % vnic_name) return vnic_settings[0] @@ -185,7 +185,7 @@ class HyperVUtils(object): def _get_vswitch(self, vswitch_name): vswitch = self._conn.Msvm_VirtualSwitch(ElementName=vswitch_name) - if not len(vswitch): + if not vswitch: raise HyperVException(msg=_('VSwitch not found: %s') % vswitch_name) return vswitch[0] @@ -196,7 +196,7 @@ class HyperVUtils(object): for vswitch_port in vswitch_ports: lan_endpoints = vswitch_port.associators( wmi_result_class='Msvm_SwitchLanEndpoint') - if len(lan_endpoints): + if lan_endpoints: ext_port = lan_endpoints[0].associators( wmi_result_class='Msvm_ExternalEthernetPort') if ext_port: diff --git a/quantum/plugins/nec/nec_plugin.py b/quantum/plugins/nec/nec_plugin.py index d2cd6e6a9..5e230371f 100644 --- a/quantum/plugins/nec/nec_plugin.py +++ b/quantum/plugins/nec/nec_plugin.py @@ -354,7 +354,7 @@ class NECPluginV2(nec_plugin_base.NECPluginV2Base, # delete unnessary ofc_tenant filters = dict(tenant_id=[tenant_id]) nets = super(NECPluginV2, self).get_networks(context, filters=filters) - if len(nets) == 0: + if not nets: try: self.ofc.delete_ofc_tenant(context, tenant_id) except (nexc.OFCException, nexc.OFCConsistencyBroken) as exc: diff --git a/quantum/plugins/nicira/nicira_nvp_plugin/QuantumPlugin.py b/quantum/plugins/nicira/nicira_nvp_plugin/QuantumPlugin.py index abc9ac191..ea8017e23 100644 --- a/quantum/plugins/nicira/nicira_nvp_plugin/QuantumPlugin.py +++ b/quantum/plugins/nicira/nicira_nvp_plugin/QuantumPlugin.py @@ -1048,7 +1048,7 @@ class NvpPluginV2(db_base_plugin_v2.QuantumDbPluginV2, except q_exc.NetworkNotFound: continue pairs.append((c, lswitches)) - if len(pairs) == 0: + if not pairs: raise q_exc.NetworkNotFound(net_id=netw_id) LOG.debug(_("Returning pairs for network: %s"), pairs) return pairs @@ -1172,7 +1172,7 @@ class NvpPluginV2(db_base_plugin_v2.QuantumDbPluginV2, # do not make the case in which switches are found in NVP # but not in Quantum catastrophic. - if len(nvp_lswitches): + if nvp_lswitches: LOG.warning(_("Found %s logical switches not bound " "to Quantum networks. Quantum and NVP are " "potentially out of sync"), len(nvp_lswitches)) @@ -1304,7 +1304,7 @@ class NvpPluginV2(db_base_plugin_v2.QuantumDbPluginV2, lports.append(quantum_lport) # do not make the case in which ports are found in NVP # but not in Quantum catastrophic. - if len(nvp_lports): + if nvp_lports: LOG.warning(_("Found %s logical ports not bound " "to Quantum ports. Quantum and NVP are " "potentially out of sync"), len(nvp_lports)) @@ -1561,7 +1561,7 @@ class NvpPluginV2(db_base_plugin_v2.QuantumDbPluginV2, msg = (_("Network '%s' is not a valid external " "network") % network_id) raise q_exc.BadRequest(resource='router', msg=msg) - if len(ext_net.subnets): + if ext_net.subnets: ext_subnet = ext_net.subnets[0] nexthop = ext_subnet.gateway_ip cluster = self._find_target_cluster(router) @@ -1610,7 +1610,7 @@ class NvpPluginV2(db_base_plugin_v2.QuantumDbPluginV2, msg = (_("Network '%s' is not a valid external " "network") % network_id) raise q_exc.BadRequest(resource='router', msg=msg) - if len(ext_net.subnets): + if ext_net.subnets: ext_subnet = ext_net.subnets[0] nexthop = ext_subnet.gateway_ip cluster = self._find_target_cluster(router) @@ -1721,7 +1721,7 @@ class NvpPluginV2(db_base_plugin_v2.QuantumDbPluginV2, # do not make the case in which routers are found in NVP # but not in Quantum catastrophic. - if len(nvp_lrouters): + if nvp_lrouters: LOG.warning(_("Found %s logical routers not bound " "to Quantum routers. Quantum and NVP are " "potentially out of sync"), len(nvp_lrouters)) @@ -1748,7 +1748,7 @@ class NvpPluginV2(db_base_plugin_v2.QuantumDbPluginV2, results = nvplib.query_lswitch_lports( cluster, '*', filters={'tag': port_id, 'tag_scope': 'q_port_id'}) - if len(results): + if results: ls_port = results[0] else: raise nvp_exc.NvpPluginException( @@ -1825,7 +1825,7 @@ class NvpPluginV2(db_base_plugin_v2.QuantumDbPluginV2, cluster, '*', relations="LogicalPortAttachment", filters={'tag': port_id, 'tag_scope': 'q_port_id'}) lrouter_port_id = None - if len(results): + if results: lport = results[0] attachment_data = lport['_relations'].get('LogicalPortAttachment') lrouter_port_id = (attachment_data and diff --git a/quantum/plugins/nicira/nicira_nvp_plugin/check_nvp_config.py b/quantum/plugins/nicira/nicira_nvp_plugin/check_nvp_config.py index 16af11337..49e7f021c 100644 --- a/quantum/plugins/nicira/nicira_nvp_plugin/check_nvp_config.py +++ b/quantum/plugins/nicira/nicira_nvp_plugin/check_nvp_config.py @@ -95,7 +95,7 @@ def main(argv): print "NVP Default Cluster Name %s" % nvp_opts.default_cluster_name print "-----------Cluster Options--------------------" - if not len(clusters_opts): + if not clusters_opts: print "No NVP Clusters detected in nvp.ini!" exit(1) clusters, default_cluster = QuantumPlugin.parse_clusters_opts( diff --git a/quantum/plugins/nicira/nicira_nvp_plugin/nvplib.py b/quantum/plugins/nicira/nicira_nvp_plugin/nvplib.py index c07a7bc2d..d7f3cf5e2 100644 --- a/quantum/plugins/nicira/nicira_nvp_plugin/nvplib.py +++ b/quantum/plugins/nicira/nicira_nvp_plugin/nvplib.py @@ -137,7 +137,7 @@ def _build_uri_path(resource, params.extend(['%s=%s' % (k, v) for (k, v) in filters.iteritems()]) uri_path = "%s/%s" % (URI_PREFIX, res_path) non_empty_params = [x for x in params if x is not None] - if len(non_empty_params): + if non_empty_params: query_string = '&'.join(non_empty_params) if query_string: uri_path += "?%s" % query_string @@ -1127,7 +1127,7 @@ def update_security_group_rules(cluster, spid, rules): 'port_range_max': constants.DHCP_RESPONSE_PORT, 'ip_prefix': '0.0.0.0/0'}) # If there are no ingress rules add bunk rule to drop all ingress traffic - if not len(rules['logical_port_ingress_rules']): + if not rules['logical_port_ingress_rules']: rules['logical_port_ingress_rules'].append( {'ethertype': 'IPv4', 'ip_prefix': '127.0.0.1/32'}) try: diff --git a/quantum/tests/unit/nicira/fake_nvpapiclient.py b/quantum/tests/unit/nicira/fake_nvpapiclient.py index 0ddae7585..f9106fd16 100644 --- a/quantum/tests/unit/nicira/fake_nvpapiclient.py +++ b/quantum/tests/unit/nicira/fake_nvpapiclient.py @@ -437,7 +437,7 @@ class FakeClient: self.SECPROF_RESOURCE in res_type or 'gatewayservice' in res_type): LOG.debug("UUIDS:%s", uuids) - if len(uuids) > 0: + if uuids: return self._show(res_type, response_file, uuids[0], relations=relations) else: @@ -461,7 +461,7 @@ class FakeClient: if val_func: val_func(body_json) args = [body] - if len(uuids): + if uuids: args.append(uuids[0]) response = response_template % add_resource(*args) return response