Debian Codegraph @

glanceclient/v2/schemas.py

raw symbols SCIP
1 # Copyright 2012 OpenStack Foundation
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import copy
17 import json
18 import jsonpatch
19 import warlock.model as warlock
20
21
22 class SchemaBasedModel(warlock.Model):
23 """Glance specific subclass of the warlock Model.
24
25 This implementation alters the function of the patch property
26 to take into account the schema's core properties. With this version
27 undefined properties which are core will generated 'replace'
28 operations rather than 'add' since this is what the Glance API
29 expects.
30 """
31
32 def _make_custom_patch(self, new, original):
33 if not self.get('tags'):
34 tags_patch = []
35 else:
36 tags_patch = [{"path": "/tags",
37 "value": self.get('tags'),
38 "op": "replace"}]
39
40 patch_string = jsonpatch.make_patch(original, new).to_string()
41 patch = json.loads(patch_string)
42 if not patch:
43 return json.dumps(tags_patch)
44 else:
45 return json.dumps(patch + tags_patch)
46
47 @warlock.Model.patch.getter
48 def patch(self):
49 """Return a jsonpatch object representing the delta."""
50 original = copy.deepcopy(self.__dict__['__original__'])
51 new = dict(self)
52 if self.schema:
53 for (name, prop) in self.schema['properties'].items():
54 if (name not in original and name in new and
55 prop.get('is_base', True)):
56 original[name] = None
57
58 original['tags'] = None
59 new['tags'] = None
60 return self._make_custom_patch(new, original)
61
62
63 class SchemaProperty(object):
64 def __init__(self, name, **kwargs):
65 self.name = name
66 self.description = kwargs.get('description')
67 self.is_base = kwargs.get('is_base', True)
68
69
70 def translate_schema_properties(schema_properties):
71 """Parse the properties dictionary of a schema document.
72
73 :returns: list of SchemaProperty objects
74 """
75 properties = []
76 for (name, prop) in schema_properties.items():
77 properties.append(SchemaProperty(name, **prop))
78 return properties
79
80
81 class Schema(object):
82 def __init__(self, raw_schema):
83 self._raw_schema = raw_schema
84 self.name = raw_schema['name']
85 raw_properties = raw_schema['properties']
86 self.properties = translate_schema_properties(raw_properties)
87
88 def is_core_property(self, property_name):
89 """Check if a property with a given name is known to the schema.
90
91 Determines if it is either a base property or a custom one
92 registered in schema-image.json file
93
94 :param property_name: name of the property
95 :returns: True if the property is known, False otherwise
96 """
97 return self._check_property(property_name, True)
98
99 def is_base_property(self, property_name):
100 """Checks if a property with a given name is a base property.
101
102 :param property_name: name of the property
103 :returns: True if the property is base, False otherwise
104 """
105 return self._check_property(property_name, False)
106
107 def _check_property(self, property_name, allow_non_base):
108 for prop in self.properties:
109 if property_name == prop.name:
110 return prop.is_base or allow_non_base
111 return False
112
113 def raw(self):
114 return copy.deepcopy(self._raw_schema)
115
116
117 class Controller(object):
118 def __init__(self, http_client):
119 self.http_client = http_client
120
121 def get(self, schema_name):
122 uri = '/v2/schemas/%s' % schema_name
123 _, raw_schema = self.http_client.get(uri)
124 return Schema(raw_schema)