2013-09-20 04:05:51 +08:00
|
|
|
# Copyright 2012 OpenStack Foundation
|
2012-06-07 14:01:50 -07:00
|
|
|
# All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
2012-07-14 01:11:22 +00:00
|
|
|
import copy
|
2015-03-10 13:10:37 -07:00
|
|
|
import json
|
2013-08-19 17:27:07 -04:00
|
|
|
import jsonpatch
|
|
|
|
import warlock.model as warlock
|
|
|
|
|
|
|
|
|
|
|
|
class SchemaBasedModel(warlock.Model):
|
2015-07-20 17:29:49 +03:00
|
|
|
"""Glance specific subclass of the warlock Model.
|
2013-08-19 17:27:07 -04:00
|
|
|
|
|
|
|
This implementation alters the function of the patch property
|
|
|
|
to take into account the schema's core properties. With this version
|
|
|
|
undefined properties which are core will generated 'replace'
|
|
|
|
operations rather than 'add' since this is what the Glance API
|
|
|
|
expects.
|
|
|
|
"""
|
|
|
|
|
2015-03-10 13:10:37 -07:00
|
|
|
def _make_custom_patch(self, new, original):
|
2016-08-19 06:10:14 +00:00
|
|
|
if not self.get('tags'):
|
|
|
|
tags_patch = []
|
|
|
|
else:
|
2015-03-10 13:10:37 -07:00
|
|
|
tags_patch = [{"path": "/tags",
|
2015-07-20 17:29:49 +03:00
|
|
|
"value": self.get('tags'),
|
|
|
|
"op": "replace"}]
|
2015-03-10 13:10:37 -07:00
|
|
|
|
|
|
|
patch_string = jsonpatch.make_patch(original, new).to_string()
|
|
|
|
patch = json.loads(patch_string)
|
|
|
|
if not patch:
|
|
|
|
return json.dumps(tags_patch)
|
|
|
|
else:
|
|
|
|
return json.dumps(patch + tags_patch)
|
|
|
|
|
2013-08-19 17:27:07 -04:00
|
|
|
@warlock.Model.patch.getter
|
|
|
|
def patch(self):
|
|
|
|
"""Return a jsonpatch object representing the delta."""
|
|
|
|
original = copy.deepcopy(self.__dict__['__original__'])
|
|
|
|
new = dict(self)
|
2015-03-10 13:10:37 -07:00
|
|
|
if self.schema:
|
2017-02-08 16:27:54 +08:00
|
|
|
for (name, prop) in self.schema['properties'].items():
|
2014-09-24 14:25:26 +02:00
|
|
|
if (name not in original and name in new and
|
|
|
|
prop.get('is_base', True)):
|
|
|
|
original[name] = None
|
2016-08-19 06:10:14 +00:00
|
|
|
|
|
|
|
original['tags'] = None
|
|
|
|
new['tags'] = None
|
2015-03-10 13:10:37 -07:00
|
|
|
return self._make_custom_patch(new, original)
|
2012-07-14 01:11:22 +00:00
|
|
|
|
2012-06-07 14:01:50 -07:00
|
|
|
|
|
|
|
class SchemaProperty(object):
|
|
|
|
def __init__(self, name, **kwargs):
|
|
|
|
self.name = name
|
|
|
|
self.description = kwargs.get('description')
|
2015-03-27 17:53:46 +03:00
|
|
|
self.is_base = kwargs.get('is_base', True)
|
2012-06-07 14:01:50 -07:00
|
|
|
|
|
|
|
|
|
|
|
def translate_schema_properties(schema_properties):
|
2015-07-20 17:29:49 +03:00
|
|
|
"""Parse the properties dictionary of a schema document.
|
2012-06-07 14:01:50 -07:00
|
|
|
|
2016-03-16 16:34:46 -05:00
|
|
|
:returns: list of SchemaProperty objects
|
2012-06-07 14:01:50 -07:00
|
|
|
"""
|
|
|
|
properties = []
|
|
|
|
for (name, prop) in schema_properties.items():
|
|
|
|
properties.append(SchemaProperty(name, **prop))
|
|
|
|
return properties
|
|
|
|
|
|
|
|
|
|
|
|
class Schema(object):
|
|
|
|
def __init__(self, raw_schema):
|
|
|
|
self._raw_schema = raw_schema
|
|
|
|
self.name = raw_schema['name']
|
|
|
|
raw_properties = raw_schema['properties']
|
|
|
|
self.properties = translate_schema_properties(raw_properties)
|
|
|
|
|
2013-08-19 17:27:07 -04:00
|
|
|
def is_core_property(self, property_name):
|
2016-03-25 03:07:45 -05:00
|
|
|
"""Check if a property with a given name is known to the schema.
|
2015-07-20 17:29:49 +03:00
|
|
|
|
2016-03-25 03:07:45 -05:00
|
|
|
Determines if it is either a base property or a custom one
|
|
|
|
registered in schema-image.json file
|
2015-03-27 17:53:46 +03:00
|
|
|
|
|
|
|
:param property_name: name of the property
|
|
|
|
:returns: True if the property is known, False otherwise
|
|
|
|
"""
|
|
|
|
return self._check_property(property_name, True)
|
|
|
|
|
|
|
|
def is_base_property(self, property_name):
|
2015-07-20 17:29:49 +03:00
|
|
|
"""Checks if a property with a given name is a base property.
|
2015-03-27 17:53:46 +03:00
|
|
|
|
|
|
|
:param property_name: name of the property
|
|
|
|
:returns: True if the property is base, False otherwise
|
|
|
|
"""
|
|
|
|
return self._check_property(property_name, False)
|
|
|
|
|
|
|
|
def _check_property(self, property_name, allow_non_base):
|
2013-08-19 17:27:07 -04:00
|
|
|
for prop in self.properties:
|
|
|
|
if property_name == prop.name:
|
2015-03-27 17:53:46 +03:00
|
|
|
return prop.is_base or allow_non_base
|
2013-08-19 17:27:07 -04:00
|
|
|
return False
|
|
|
|
|
2012-07-14 01:11:22 +00:00
|
|
|
def raw(self):
|
|
|
|
return copy.deepcopy(self._raw_schema)
|
|
|
|
|
2012-06-07 14:01:50 -07:00
|
|
|
|
|
|
|
class Controller(object):
|
|
|
|
def __init__(self, http_client):
|
|
|
|
self.http_client = http_client
|
|
|
|
|
|
|
|
def get(self, schema_name):
|
2012-08-08 10:08:33 -07:00
|
|
|
uri = '/v2/schemas/%s' % schema_name
|
2014-07-01 14:45:12 +05:30
|
|
|
_, raw_schema = self.http_client.get(uri)
|
2012-06-07 14:01:50 -07:00
|
|
|
return Schema(raw_schema)
|