netsted template validate type error
[parser.git] / tosca2heat / tosca-parser / toscaparser / parameters.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
14 import logging
15
16 from toscaparser.common.exception import ExceptionCollector
17 from toscaparser.common.exception import MissingRequiredFieldError
18 from toscaparser.common.exception import UnknownFieldError
19 from toscaparser.dataentity import DataEntity
20 from toscaparser.elements.constraints import Schema
21 from toscaparser.elements.entity_type import EntityType
22 from toscaparser.utils.gettextutils import _
23
24
25 log = logging.getLogger('tosca')
26
27
28 class Input(object):
29
30     INPUTFIELD = (TYPE, DESCRIPTION, DEFAULT, CONSTRAINTS) = \
31         ('type', 'description', 'default', 'constraints')
32
33     def __init__(self, name, schema_dict):
34         self.name = name
35         self.schema = Schema(name, schema_dict)
36
37     @property
38     def type(self):
39         return self.schema.type
40
41     @property
42     def description(self):
43         return self.schema.description
44
45     @property
46     def default(self):
47         return self.schema.default
48
49     @property
50     def constraints(self):
51         return self.schema.constraints
52
53     def validate(self, value=None):
54         self._validate_field()
55         self.validate_type(self.type)
56         if value:
57             self._validate_value(value)
58
59     def _validate_field(self):
60         for name in self.schema.schema:
61             if name not in self.INPUTFIELD:
62                 ExceptionCollector.appendException(
63                     UnknownFieldError(what='Input "%s"' % self.name,
64                                       field=name))
65
66     def validate_type(self, input_type):
67         if input_type not in Schema.PROPERTY_TYPES:
68             ExceptionCollector.appendException(
69                 ValueError(_('Invalid type "%s".') % type))
70
71     def _validate_value(self, value):
72         tosca = EntityType.TOSCA_DEF
73         datatype = None
74         if self.type in tosca:
75             datatype = tosca[self.type]
76         elif EntityType.DATATYPE_PREFIX + self.type in tosca:
77             datatype = tosca[EntityType.DATATYPE_PREFIX + self.type]
78
79         DataEntity.validate_datatype(self.type, value, None, datatype)
80
81
82 class Output(object):
83
84     OUTPUTFIELD = (DESCRIPTION, VALUE) = ('description', 'value')
85
86     def __init__(self, name, attrs):
87         self.name = name
88         self.attrs = attrs
89
90     @property
91     def description(self):
92         return self.attrs.get(self.DESCRIPTION)
93
94     @property
95     def value(self):
96         return self.attrs.get(self.VALUE)
97
98     def validate(self):
99         self._validate_field()
100
101     def _validate_field(self):
102         if not isinstance(self.attrs, dict):
103             ExceptionCollector.appendException(
104                 MissingRequiredFieldError(what='Output "%s"' % self.name,
105                                           required=self.VALUE))
106         if self.value is None:
107             ExceptionCollector.appendException(
108                 MissingRequiredFieldError(what='Output "%s"' % self.name,
109                                           required=self.VALUE))
110         for name in self.attrs:
111             if name not in self.OUTPUTFIELD:
112                 ExceptionCollector.appendException(
113                     UnknownFieldError(what='Output "%s"' % self.name,
114                                       field=name))