netsted template validate type error
[parser.git] / tosca2heat / tosca-parser / toscaparser / properties.py
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
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
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
11 #    under the License.
12
13 from toscaparser.dataentity import DataEntity
14 from toscaparser.elements.constraints import Schema
15 from toscaparser.functions import is_function
16
17
18 class Property(object):
19     '''TOSCA built-in Property type.'''
20
21     PROPERTY_KEYS = (
22         TYPE, REQUIRED, DESCRIPTION, DEFAULT, CONSTRAINTS
23     ) = (
24         'type', 'required', 'description', 'default', 'constraints'
25     )
26
27     ENTRY_SCHEMA_KEYS = (
28         ENTRYTYPE, ENTRYPROPERTIES
29     ) = (
30         'type', 'properties'
31     )
32
33     def __init__(self, property_name, value, schema_dict, custom_def=None):
34         self.name = property_name
35         self.value = value
36         self.custom_def = custom_def
37         self.schema = Schema(property_name, schema_dict)
38
39     @property
40     def type(self):
41         return self.schema.type
42
43     @property
44     def required(self):
45         return self.schema.required
46
47     @property
48     def description(self):
49         return self.schema.description
50
51     @property
52     def default(self):
53         return self.schema.default
54
55     @property
56     def constraints(self):
57         return self.schema.constraints
58
59     @property
60     def entry_schema(self):
61         return self.schema.entry_schema
62
63     def validate(self):
64         '''Validate if not a reference property.'''
65         if not is_function(self.value):
66             if self.type == Schema.STRING:
67                 self.value = str(self.value)
68             self.value = DataEntity.validate_datatype(self.type, self.value,
69                                                       self.entry_schema,
70                                                       self.custom_def)
71             self._validate_constraints()
72
73     def _validate_constraints(self):
74         if self.constraints:
75             for constraint in self.constraints:
76                 constraint.validate(self.value)