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.

Closes-Bug: 1368479

Conflicts:
	glance/db/__init__.py
	glance/db/sqlalchemy/models_metadef.py

Change-Id: I2c574210f8d62c77a438afab83ff80f3e5bd2fe7
(cherry picked from commit 824d9620b0)
This commit is contained in:
Wayne Okuma 2014-09-11 13:59:29 -07:00 committed by Thierry Carrez
parent cecc9497c1
commit da93f408dd
8 changed files with 19 additions and 23 deletions

View File

@ -171,9 +171,8 @@ class NamespaceController(object):
def _to_property_dict(self, name, value):
# Convert the model PropertyTypes dict to a JSON string
json_data = tojson(PropertyType, value)
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
return db_property_type_dict

View File

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

View File

@ -21,7 +21,6 @@ from glance.api.v2.model.metadef_object import MetadefObject
from glance.api.v2.model.metadef_property_type import PropertyType
from glance.api.v2.model.metadef_resource_type import ResourceTypeAssociation
from glance.common.wsme_utils import WSMEModelTransformer
from glance.openstack.common import jsonutils as json
class Namespace(types.Base, WSMEModelTransformer):
@ -57,9 +56,8 @@ class Namespace(types.Base, WSMEModelTransformer):
property_types = {}
for db_property_type in db_property_types:
# Convert the persisted json schema to a dict of PropertyTypes
json_props = json.loads(db_property_type.schema)
property_type = fromjson(PropertyType, json_props)
property_type = fromjson(
PropertyType, db_property_type.schema)
property_type_name = db_property_type.name
property_types[property_type_name] = property_type

View File

@ -27,7 +27,6 @@ from glance.common import location_strategy
import glance.domain
import glance.domain.proxy
from glance.openstack.common import importutils
from glance.openstack.common import jsonutils as json
CONF = cfg.CONF
CONF.import_opt('image_size_cap', 'glance.common.config')
@ -508,7 +507,7 @@ class MetadefObjectRepo(object):
# Convert the persisted json schema to a dict of PropertyTypes
property_types = {}
json_props = json.loads(metadata_object['schema'])
json_props = metadata_object['schema']
for id in json_props:
property_types[id] = fromjson(PropertyType, json_props[id])
@ -535,13 +534,12 @@ class MetadefObjectRepo(object):
for k, v in properties.items():
json_data = tojson(PropertyType, v)
db_schema[k] = json_data
property_schema = json.dumps(db_schema)
db_metadata_object = {
'name': metadata_object.name,
'required': required_str,
'description': metadata_object.description,
'schema': property_schema
'schema': db_schema
}
return db_metadata_object

View File

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

View File

@ -28,6 +28,7 @@ from sqlalchemy.orm import relationship
from sqlalchemy import String
from sqlalchemy import Text
from glance.db.sqlalchemy.models import JSONEncodedDict
from glance.openstack.common import timeutils
@ -88,7 +89,7 @@ class MetadefObject(BASE_DICT, GlanceMetadefBase):
name = Column(String(80), nullable=False)
description = Column(Text())
required = Column(Text())
schema = Column(Text(), default={})
schema = Column(JSONEncodedDict(), default={})
class MetadefProperty(BASE_DICT, GlanceMetadefBase):
@ -102,7 +103,7 @@ class MetadefProperty(BASE_DICT, GlanceMetadefBase):
namespace_id = Column(Integer(), ForeignKey('metadef_namespaces.id'),
nullable=False)
name = Column(String(80), nullable=False)
schema = Column(Text(), default={})
schema = Column(JSONEncodedDict(), default={})
class MetadefNamespaceResourceType(BASE_DICT, GlanceMetadefBase):

View File

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

View File

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