Metadef Property and Object schema columns should use JSONEncodedDict

The MetadefProperty and MetadefObject ORM classes currently specify the
JSON schema columns as type Text. It is preferred to use the
JSONEncodedDict Type Decorator instead. This fix also includes necessary
code changes to remove JSON encoding/decoding that was previously done
in other layers. Fixes for unit tests involving the schema columns are
also included.

Change-Id: I2c574210f8d62c77a438afab83ff80f3e5bd2fe7
Closes-Bug: 1368479
This commit is contained in:
Wayne Okuma 2014-09-11 13:59:29 -07:00
parent bcaea50bac
commit 824d9620b0
8 changed files with 20 additions and 23 deletions

View File

@ -171,9 +171,8 @@ class NamespaceController(object):
def _to_property_dict(self, name, value): def _to_property_dict(self, name, value):
# Convert the model PropertyTypes dict to a JSON string # Convert the model PropertyTypes dict to a JSON string
json_data = tojson(PropertyType, value)
db_property_type_dict = dict() db_property_type_dict = dict()
db_property_type_dict['schema'] = json.dumps(json_data) db_property_type_dict['schema'] = tojson(PropertyType, value)
db_property_type_dict['name'] = name db_property_type_dict['name'] = name
return db_property_type_dict return db_property_type_dict

View File

@ -47,17 +47,17 @@ class NamespacePropertiesController(object):
policy_enforcer=self.policy) policy_enforcer=self.policy)
def _to_dict(self, model_property_type): def _to_dict(self, model_property_type):
# Convert the model PropertyTypes dict to a JSON string # Convert the model PropertyTypes dict to a JSON encoding
json_data = tojson(PropertyType, model_property_type)
db_property_type_dict = dict() db_property_type_dict = dict()
db_property_type_dict['schema'] = json.dumps(json_data) db_property_type_dict['schema'] = tojson(
PropertyType, model_property_type)
db_property_type_dict['name'] = model_property_type.name db_property_type_dict['name'] = model_property_type.name
return db_property_type_dict return db_property_type_dict
def _to_model(self, db_property_type): def _to_model(self, db_property_type):
# Convert the persisted json schema to a dict of PropertyTypes # Convert the persisted json schema to a dict of PropertyTypes
json_props = json.loads(db_property_type.schema) property_type = fromjson(
property_type = fromjson(PropertyType, json_props) PropertyType, db_property_type.schema)
property_type.name = db_property_type.name property_type.name = db_property_type.name
return property_type return property_type

View File

@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from oslo.serialization import jsonutils as json
import wsme import wsme
from wsme.rest.json import fromjson from wsme.rest.json import fromjson
from wsme import types from wsme import types
@ -57,9 +56,8 @@ class Namespace(types.Base, WSMEModelTransformer):
property_types = {} property_types = {}
for db_property_type in db_property_types: for db_property_type in db_property_types:
# Convert the persisted json schema to a dict of PropertyTypes # Convert the persisted json schema to a dict of PropertyTypes
json_props = json.loads(db_property_type.schema) property_type = fromjson(
property_type = fromjson(PropertyType, json_props) PropertyType, db_property_type.schema)
property_type_name = db_property_type.name property_type_name = db_property_type.name
property_types[property_type_name] = property_type property_types[property_type_name] = property_type

View File

@ -17,7 +17,6 @@
# under the License. # under the License.
from oslo.config import cfg from oslo.config import cfg
from oslo.serialization import jsonutils as json
from oslo.utils import importutils from oslo.utils import importutils
from wsme.rest.json import fromjson from wsme.rest.json import fromjson
from wsme.rest.json import tojson from wsme.rest.json import tojson
@ -508,7 +507,7 @@ class MetadefObjectRepo(object):
# Convert the persisted json schema to a dict of PropertyTypes # Convert the persisted json schema to a dict of PropertyTypes
property_types = {} property_types = {}
json_props = json.loads(metadata_object['schema']) json_props = metadata_object['schema']
for id in json_props: for id in json_props:
property_types[id] = fromjson(PropertyType, json_props[id]) property_types[id] = fromjson(PropertyType, json_props[id])
@ -535,13 +534,12 @@ class MetadefObjectRepo(object):
for k, v in properties.items(): for k, v in properties.items():
json_data = tojson(PropertyType, v) json_data = tojson(PropertyType, v)
db_schema[k] = json_data db_schema[k] = json_data
property_schema = json.dumps(db_schema)
db_metadata_object = { db_metadata_object = {
'name': metadata_object.name, 'name': metadata_object.name,
'required': required_str, 'required': required_str,
'description': metadata_object.description, 'description': metadata_object.description,
'schema': property_schema 'schema': db_schema
} }
return db_metadata_object return db_metadata_object

View File

@ -89,7 +89,7 @@ def define_metadef_objects_table(meta):
Column('name', String(80), nullable=False), Column('name', String(80), nullable=False),
Column('description', Text()), Column('description', Text()),
Column('required', Text()), Column('required', Text()),
Column('schema', Text()), Column('schema', Text(), nullable=False),
Column('created_at', DateTime(), nullable=False), Column('created_at', DateTime(), nullable=False),
Column('updated_at', DateTime()), Column('updated_at', DateTime()),
UniqueConstraint('namespace_id', 'name', UniqueConstraint('namespace_id', 'name',
@ -118,7 +118,7 @@ def define_metadef_properties_table(meta):
Column('namespace_id', Integer(), ForeignKey('metadef_namespaces.id'), Column('namespace_id', Integer(), ForeignKey('metadef_namespaces.id'),
nullable=False), nullable=False),
Column('name', String(80), nullable=False), Column('name', String(80), nullable=False),
Column('schema', Text()), Column('schema', Text(), nullable=False),
Column('created_at', DateTime(), nullable=False), Column('created_at', DateTime(), nullable=False),
Column('updated_at', DateTime()), Column('updated_at', DateTime()),
UniqueConstraint('namespace_id', 'name', **_constr_kwargs), UniqueConstraint('namespace_id', 'name', **_constr_kwargs),

View File

@ -29,6 +29,8 @@ from sqlalchemy.orm import relationship
from sqlalchemy import String from sqlalchemy import String
from sqlalchemy import Text from sqlalchemy import Text
from glance.db.sqlalchemy.models import JSONEncodedDict
class DictionaryBase(models.ModelBase): class DictionaryBase(models.ModelBase):
metadata = None metadata = None
@ -87,7 +89,7 @@ class MetadefObject(BASE_DICT, GlanceMetadefBase):
name = Column(String(80), nullable=False) name = Column(String(80), nullable=False)
description = Column(Text()) description = Column(Text())
required = Column(Text()) required = Column(Text())
schema = Column(Text(), default={}) schema = Column(JSONEncodedDict(), default={})
class MetadefProperty(BASE_DICT, GlanceMetadefBase): class MetadefProperty(BASE_DICT, GlanceMetadefBase):
@ -101,7 +103,7 @@ class MetadefProperty(BASE_DICT, GlanceMetadefBase):
namespace_id = Column(Integer(), ForeignKey('metadef_namespaces.id'), namespace_id = Column(Integer(), ForeignKey('metadef_namespaces.id'),
nullable=False) nullable=False)
name = Column(String(80), nullable=False) name = Column(String(80), nullable=False)
schema = Column(Text(), default={}) schema = Column(JSONEncodedDict(), default={})
class MetadefNamespaceResourceType(BASE_DICT, GlanceMetadefBase): class MetadefNamespaceResourceType(BASE_DICT, GlanceMetadefBase):

View File

@ -62,7 +62,7 @@ def _db_namespace_fixture(**kwargs):
def _db_property_fixture(name, **kwargs): def _db_property_fixture(name, **kwargs):
property = { property = {
'name': name, 'name': name,
'schema': '{"type": "string", "title": "title"}', 'schema': {"type": "string", "title": "title"},
} }
property.update(kwargs) property.update(kwargs)
return property return property
@ -72,7 +72,7 @@ def _db_object_fixture(name, **kwargs):
obj = { obj = {
'name': name, 'name': name,
'description': None, 'description': None,
'schema': '{}', 'schema': {},
'required': '[]', 'required': '[]',
} }
obj.update(kwargs) obj.update(kwargs)

View File

@ -69,7 +69,7 @@ def _db_namespace_fixture(namespace, **kwargs):
def _db_property_fixture(name, **kwargs): def _db_property_fixture(name, **kwargs):
obj = { obj = {
'name': name, 'name': name,
'schema': '{"type": "string", "title": "title"}', 'schema': {"type": "string", "title": "title"},
} }
obj.update(kwargs) obj.update(kwargs)
return obj return obj
@ -79,7 +79,7 @@ def _db_object_fixture(name, **kwargs):
obj = { obj = {
'name': name, 'name': name,
'description': None, 'description': None,
'schema': '{}', 'schema': {},
'required': '[]', 'required': '[]',
} }
obj.update(kwargs) obj.update(kwargs)