Explicitly compare IDs to None
Many database IDs (Resource, WatchRule) are integers, so 0 is a valid ID and evaluating it in a boolean context may give the wrong result for the first entry in the table. Even for the ones that aren't (e.g. Stack) it's better to explicitly compare to None. Change-Id: Id95eeb5cd0f9cac82937fae48122ce5fa90ca48d
This commit is contained in:
@@ -623,8 +623,8 @@ class Resource(status.ResourceStatus):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
class_name = reflection.get_class_name(self, fully_qualified=False)
|
class_name = reflection.get_class_name(self, fully_qualified=False)
|
||||||
if self.stack.id:
|
if self.stack.id is not None:
|
||||||
if self.resource_id:
|
if self.resource_id is not None:
|
||||||
text = '%s "%s" [%s] %s' % (class_name, self.name,
|
text = '%s "%s" [%s] %s' % (class_name, self.name,
|
||||||
self.resource_id,
|
self.resource_id,
|
||||||
six.text_type(self.stack))
|
six.text_type(self.stack))
|
||||||
@@ -2309,7 +2309,7 @@ class Resource(status.ResourceStatus):
|
|||||||
|
|
||||||
:returns: a dict representing the resource data for this resource.
|
:returns: a dict representing the resource data for this resource.
|
||||||
"""
|
"""
|
||||||
if self._data is None and self.id:
|
if self._data is None and self.id is not None:
|
||||||
try:
|
try:
|
||||||
self._data = resource_data_objects.ResourceData.get_all(self)
|
self._data = resource_data_objects.ResourceData.get_all(self)
|
||||||
except exception.NotFound:
|
except exception.NotFound:
|
||||||
|
|||||||
@@ -360,7 +360,7 @@ class Stack(collections.Mapping):
|
|||||||
return resources or None
|
return resources or None
|
||||||
|
|
||||||
def db_resource_get(self, name):
|
def db_resource_get(self, name):
|
||||||
if not self.id:
|
if self.id is None:
|
||||||
return None
|
return None
|
||||||
return self._db_resources_get().get(name)
|
return self._db_resources_get().get(name)
|
||||||
|
|
||||||
@@ -461,7 +461,7 @@ class Stack(collections.Mapping):
|
|||||||
Includes nested stacks below.
|
Includes nested stacks below.
|
||||||
"""
|
"""
|
||||||
if not stack_id:
|
if not stack_id:
|
||||||
if not self.id:
|
if self.id is None:
|
||||||
# We're not stored yet, so we don't have anything to count
|
# We're not stored yet, so we don't have anything to count
|
||||||
return 0
|
return 0
|
||||||
stack_id = self.id
|
stack_id = self.id
|
||||||
@@ -653,7 +653,7 @@ class Stack(collections.Mapping):
|
|||||||
else:
|
else:
|
||||||
s['raw_template_id'] = self.t.id
|
s['raw_template_id'] = self.t.id
|
||||||
|
|
||||||
if self.id:
|
if self.id is not None:
|
||||||
if exp_trvsl is None and not ignore_traversal_check:
|
if exp_trvsl is None and not ignore_traversal_check:
|
||||||
exp_trvsl = self.current_traversal
|
exp_trvsl = self.current_traversal
|
||||||
|
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ class WatchRule(object):
|
|||||||
'stack_id': self.stack_id
|
'stack_id': self.stack_id
|
||||||
}
|
}
|
||||||
|
|
||||||
if not self.id:
|
if self.id is None:
|
||||||
wr = watch_rule_objects.WatchRule.create(self.context, wr_values)
|
wr = watch_rule_objects.WatchRule.create(self.context, wr_values)
|
||||||
self.id = wr.id
|
self.id = wr.id
|
||||||
else:
|
else:
|
||||||
@@ -120,7 +120,7 @@ class WatchRule(object):
|
|||||||
|
|
||||||
def destroy(self):
|
def destroy(self):
|
||||||
"""Delete the watchrule from the database."""
|
"""Delete the watchrule from the database."""
|
||||||
if self.id:
|
if self.id is not None:
|
||||||
watch_rule_objects.WatchRule.delete(self.context, self.id)
|
watch_rule_objects.WatchRule.delete(self.context, self.id)
|
||||||
|
|
||||||
def do_data_cmp(self, data, threshold):
|
def do_data_cmp(self, data, threshold):
|
||||||
|
|||||||
Reference in New Issue
Block a user