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
14 from toscaparser.common.exception import ExceptionCollector
15 from toscaparser.common.exception import InvalidTypeError
16 from toscaparser.common.exception import UnknownFieldError
17 from toscaparser.elements.statefulentitytype import StatefulEntityType
20 class GroupType(StatefulEntityType):
21 '''TOSCA built-in group type.'''
23 SECTIONS = (DERIVED_FROM, VERSION, METADATA, DESCRIPTION, PROPERTIES,
24 MEMBERS, INTERFACES) = \
25 ("derived_from", "version", "metadata", "description",
26 "properties", "members", "interfaces")
28 def __init__(self, grouptype, custom_def=None):
29 super(GroupType, self).__init__(grouptype, self.GROUP_PREFIX,
31 self.custom_def = custom_def
32 self.grouptype = grouptype
33 self._validate_fields()
34 self.group_description = None
35 if self.DESCRIPTION in self.defs:
36 self.group_description = self.defs[self.DESCRIPTION]
38 self.group_version = None
39 if self.VERSION in self.defs:
40 self.group_version = self.defs[self.VERSION]
42 self.group_properties = None
43 if self.PROPERTIES in self.defs:
44 self.group_properties = self.defs[self.PROPERTIES]
46 self.group_members = None
47 if self.MEMBERS in self.defs:
48 self.group_members = self.defs[self.MEMBERS]
50 if self.METADATA in self.defs:
51 self.meta_data = self.defs[self.METADATA]
52 self._validate_metadata(self.meta_data)
55 def parent_type(self):
56 '''Return a group statefulentity of this entity is derived from.'''
57 if not hasattr(self, 'defs'):
59 pgroup_entity = self.derived_from(self.defs)
61 return GroupType(pgroup_entity, self.custom_def)
64 def description(self):
65 return self.group_description
69 return self.group_version
73 return self.get_value(self.INTERFACES)
75 def _validate_fields(self):
77 for name in self.defs.keys():
78 if name not in self.SECTIONS:
79 ExceptionCollector.appendException(
80 UnknownFieldError(what='Group Type %s'
81 % self.grouptype, field=name))
83 def _validate_metadata(self, meta_data):
84 if not meta_data.get('type') in ['map', 'tosca:map']:
85 ExceptionCollector.appendException(
86 InvalidTypeError(what='"%s" defined in group for '
87 'metadata' % (meta_data.get('type'))))
88 for entry_schema, entry_schema_type in meta_data.items():
89 if isinstance(entry_schema_type, dict) and not \
90 entry_schema_type.get('type') == 'string':
91 ExceptionCollector.appendException(
92 InvalidTypeError(what='"%s" defined in group for '
94 % (entry_schema_type.get('type'),