Make the objects behave more consistently

1. don't return DB models from the objects API
   (only return Objects)
2. delete shouldn't return anything
3. update_and_save should return the refreshed object.

Note: there is still some inconsistency in what is returned by
update_by_id() some return an object and some return a bool.

Related-bug: #1432936
Change-Id: I1a0a38773d4fc4a62af5e0a98076396f39187b6c
This commit is contained in:
Angus Salkeld 2015-06-19 11:42:04 +10:00
parent 3fd6b53079
commit bbe08b18eb
7 changed files with 43 additions and 26 deletions

View File

@ -69,8 +69,7 @@ class RawTemplate(
@classmethod
def get_by_id(cls, context, template_id):
raw_template_db = db_api.raw_template_get(context, template_id)
raw_template = cls._from_db_object(context, cls(), raw_template_db)
return raw_template
return cls._from_db_object(context, cls(), raw_template_db)
@classmethod
def encrypt_hidden_parameters(cls, tmpl):
@ -85,12 +84,15 @@ class RawTemplate(
@classmethod
def create(cls, context, values):
return db_api.raw_template_create(context, values)
return cls._from_db_object(context, cls(),
db_api.raw_template_create(context, values))
@classmethod
def update_by_id(cls, context, template_id, values):
return db_api.raw_template_update(context, template_id, values)
return cls._from_db_object(
context, cls(),
db_api.raw_template_update(context, template_id, values))
@classmethod
def delete(cls, context, template_id):
return db_api.raw_template_delete(context, template_id)
db_api.raw_template_delete(context, template_id)

View File

@ -99,8 +99,7 @@ class Resource(
@classmethod
def get_obj(cls, context, resource_id):
resource_db = db_api.resource_get(context, resource_id)
resource = cls._from_db_object(cls(context), context, resource_db)
return resource
return cls._from_db_object(cls(context), context, resource_db)
@classmethod
def get_all(cls, context):
@ -116,7 +115,8 @@ class Resource(
@classmethod
def create(cls, context, values):
return db_api.resource_create(context, values)
return cls._from_db_object(cls(context), context,
db_api.resource_create(context, values))
@classmethod
def delete(cls, context, resource_id):
@ -148,22 +148,19 @@ class Resource(
context,
resource_name,
stack_id)
resource = cls._from_db_object(cls(context), context, resource_db)
return resource
return cls._from_db_object(cls(context), context, resource_db)
@classmethod
def get_by_physical_resource_id(cls, context, physical_resource_id):
resource_db = db_api.resource_get_by_physical_resource_id(
context,
physical_resource_id)
resource = cls._from_db_object(cls(context), context, resource_db)
return resource
return cls._from_db_object(cls(context), context, resource_db)
def update_and_save(self, values):
resource_db = db_api.resource_get(self._context, self.id)
resource_db.update_and_save(values)
self._refresh()
return resource_db
return self._refresh()
def _refresh(self):
return self.__class__._from_db_object(

View File

@ -80,4 +80,4 @@ class ResourceData(
@classmethod
def delete(cls, resource, key):
return db_api.resource_data_delete(resource, key)
db_api.resource_data_delete(resource, key)

View File

@ -75,7 +75,7 @@ class Service(base.VersionedObject,
@classmethod
def delete(cls, context, service_id, soft_delete=True):
return db_api.service_delete(context, service_id, soft_delete)
db_api.service_delete(context, service_id, soft_delete)
@classmethod
def get_all(cls, context):

View File

@ -79,6 +79,10 @@ class SoftwareDeployment(base.VersionedObject,
@classmethod
def update_by_id(cls, context, deployment_id, values):
"""Note this is a bit unusual as it returns the object.
Other update_by_id methods return a bool (was it updated).
"""
return cls._from_db_object(
context, cls(),
db_api.software_deployment_update(context, deployment_id, values))

View File

@ -20,7 +20,8 @@ Stack object
from oslo_versionedobjects import base
from oslo_versionedobjects import fields
from heat.common import exception
from heat.common.i18n import _
from heat.db import api as db_api
from heat.objects import fields as heat_fields
from heat.objects import raw_template
@ -88,7 +89,7 @@ class Stack(
def get_by_id(cls, context, stack_id, **kwargs):
db_stack = db_api.stack_get(context, stack_id, **kwargs)
if not db_stack:
return db_stack
return None
stack = cls._from_db_object(context, cls(context), db_stack)
return stack
@ -100,7 +101,7 @@ class Stack(
owner_id
)
if not db_stack:
return db_stack
return None
stack = cls._from_db_object(context, cls(context), db_stack)
return stack
@ -108,7 +109,7 @@ class Stack(
def get_by_name(cls, context, stack_name):
db_stack = db_api.stack_get_by_name(context, stack_name)
if not db_stack:
return db_stack
return None
stack = cls._from_db_object(context, cls(context), db_stack)
return stack
@ -144,20 +145,33 @@ class Stack(
@classmethod
def create(cls, context, values):
return db_api.stack_create(context, values)
return cls._from_db_object(context, cls(context),
db_api.stack_create(context, values))
@classmethod
def update_by_id(cls, context, stack_id, values):
"""Update and return (boolean) if it was updated.
Note: the underlying stack_update filters by current_traversal
and stack_id.
"""
return db_api.stack_update(context, stack_id, values)
@classmethod
def delete(cls, context, stack_id):
return db_api.stack_delete(context, stack_id)
db_api.stack_delete(context, stack_id)
def update_and_save(self, values):
db_stack = self.__class__.update_by_id(self._context, self.id, values)
self.refresh()
return db_stack
has_updated = self.__class__.update_by_id(self._context,
self.id, values)
if not has_updated:
raise exception.NotFound(_('Attempt to update a stack with id: '
'%(id)s %(traversal) %(msg)s') % {
'id': self.id,
'traversal': self.current_traversal,
'msg': 'that does not exist'})
return self.refresh()
def __eq__(self, another):
self.refresh() # to make test object comparison work well

View File

@ -64,7 +64,7 @@ class UserCreds(base.VersionedObject,
@classmethod
def delete(cls, context, user_creds_id):
return db_api.user_creds_delete(context, user_creds_id)
db_api.user_creds_delete(context, user_creds_id)
@classmethod
def get_by_id(cls, context_id):