From 824d9620b0b90483baf45981d2cb328855943e06 Mon Sep 17 00:00:00 2001 From: Wayne Okuma Date: Thu, 11 Sep 2014 13:59:29 -0700 Subject: [PATCH] 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 --- glance/api/v2/metadef_namespaces.py | 3 +-- glance/api/v2/metadef_properties.py | 10 +++++----- glance/api/v2/model/metadef_namespace.py | 6 ++---- glance/db/__init__.py | 6 ++---- .../migrate_repo/versions/035_add_metadef_tables.py | 4 ++-- glance/db/sqlalchemy/models_metadef.py | 6 ++++-- glance/tests/unit/test_db_metadef.py | 4 ++-- glance/tests/unit/v2/test_metadef_resources.py | 4 ++-- 8 files changed, 20 insertions(+), 23 deletions(-) diff --git a/glance/api/v2/metadef_namespaces.py b/glance/api/v2/metadef_namespaces.py index 5a7fa035c4..4db5a17240 100644 --- a/glance/api/v2/metadef_namespaces.py +++ b/glance/api/v2/metadef_namespaces.py @@ -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 diff --git a/glance/api/v2/metadef_properties.py b/glance/api/v2/metadef_properties.py index 3f8fdd79cb..ce7fe7f761 100644 --- a/glance/api/v2/metadef_properties.py +++ b/glance/api/v2/metadef_properties.py @@ -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 diff --git a/glance/api/v2/model/metadef_namespace.py b/glance/api/v2/model/metadef_namespace.py index c8c23840dc..041ce35dd8 100644 --- a/glance/api/v2/model/metadef_namespace.py +++ b/glance/api/v2/model/metadef_namespace.py @@ -13,7 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from oslo.serialization import jsonutils as json import wsme from wsme.rest.json import fromjson from wsme import types @@ -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 diff --git a/glance/db/__init__.py b/glance/db/__init__.py index 3754972b4d..a00c4e8885 100644 --- a/glance/db/__init__.py +++ b/glance/db/__init__.py @@ -17,7 +17,6 @@ # under the License. from oslo.config import cfg -from oslo.serialization import jsonutils as json from oslo.utils import importutils from wsme.rest.json import fromjson from wsme.rest.json import tojson @@ -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 diff --git a/glance/db/sqlalchemy/migrate_repo/versions/035_add_metadef_tables.py b/glance/db/sqlalchemy/migrate_repo/versions/035_add_metadef_tables.py index 6cc69ba245..8053227bc1 100644 --- a/glance/db/sqlalchemy/migrate_repo/versions/035_add_metadef_tables.py +++ b/glance/db/sqlalchemy/migrate_repo/versions/035_add_metadef_tables.py @@ -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), diff --git a/glance/db/sqlalchemy/models_metadef.py b/glance/db/sqlalchemy/models_metadef.py index 9c6b9547ab..ced963d85f 100644 --- a/glance/db/sqlalchemy/models_metadef.py +++ b/glance/db/sqlalchemy/models_metadef.py @@ -29,6 +29,8 @@ from sqlalchemy.orm import relationship from sqlalchemy import String from sqlalchemy import Text +from glance.db.sqlalchemy.models import JSONEncodedDict + class DictionaryBase(models.ModelBase): metadata = None @@ -87,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): @@ -101,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): diff --git a/glance/tests/unit/test_db_metadef.py b/glance/tests/unit/test_db_metadef.py index 469d7879f3..81226aaf68 100644 --- a/glance/tests/unit/test_db_metadef.py +++ b/glance/tests/unit/test_db_metadef.py @@ -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) diff --git a/glance/tests/unit/v2/test_metadef_resources.py b/glance/tests/unit/v2/test_metadef_resources.py index 9bd1599e50..87aff8a084 100644 --- a/glance/tests/unit/v2/test_metadef_resources.py +++ b/glance/tests/unit/v2/test_metadef_resources.py @@ -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)