1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
13 from toscaparser.common.exception import ExceptionCollector
14 from toscaparser.common.exception import InvalidSchemaError
15 from toscaparser.common.exception import TOSCAException
16 from toscaparser.utils.gettextutils import _
19 class PropertyDef(object):
20 '''TOSCA built-in Property type.'''
22 VALID_PROPERTY_KEYNAMES = (PROPERTY_KEYNAME_DEFAULT,
23 PROPERTY_KEYNAME_REQUIRED,
24 PROPERTY_KEYNAME_STATUS) = \
25 ('default', 'required', 'status')
27 PROPERTY_REQUIRED_DEFAULT = True
29 VALID_REQUIRED_VALUES = ['true', 'false']
30 VALID_STATUS_VALUES = (PROPERTY_STATUS_SUPPORTED,
31 PROPERTY_STATUS_EXPERIMENTAL) = \
32 ('supported', 'experimental')
34 PROPERTY_STATUS_DEFAULT = PROPERTY_STATUS_SUPPORTED
36 def __init__(self, name, value=None, schema=None):
40 self._status = self.PROPERTY_STATUS_DEFAULT
41 self._required = self.PROPERTY_REQUIRED_DEFAULT
43 # Validate required 'type' property exists
47 msg = (_('Schema definition of "%(pname)s" must have a "type" '
48 'attribute.') % dict(pname=self.name))
49 ExceptionCollector.appendException(
50 InvalidSchemaError(message=msg))
53 self._load_required_attr_from_schema()
54 self._load_status_attr_from_schema()
59 for prop_key, prop_value in self.schema.items():
60 if prop_key == self.PROPERTY_KEYNAME_DEFAULT:
68 def _load_required_attr_from_schema(self):
69 # IF 'required' keyname exists verify it's a boolean,
70 # if so override default
71 if self.PROPERTY_KEYNAME_REQUIRED in self.schema:
72 value = self.schema[self.PROPERTY_KEYNAME_REQUIRED]
73 if isinstance(value, bool):
74 self._required = value
76 valid_values = ', '.join(self.VALID_REQUIRED_VALUES)
77 attr = self.PROPERTY_KEYNAME_REQUIRED
78 TOSCAException.generate_inv_schema_property_error(self,
87 def _load_status_attr_from_schema(self):
88 # IF 'status' keyname exists verify it's a valid value,
89 # if so override default
90 if self.PROPERTY_KEYNAME_STATUS in self.schema:
91 value = self.schema[self.PROPERTY_KEYNAME_STATUS]
92 if value in self.VALID_STATUS_VALUES:
95 valid_values = ', '.join(self.VALID_STATUS_VALUES)
96 attr = self.PROPERTY_KEYNAME_STATUS
97 TOSCAException.generate_inv_schema_property_error(self,