Added Support for UnsupportedTypes
Checks for the unsupported types in the template, and raises unsupportedtype error while parsing. Change-Id: I36b6eccce1e695d74806b09fc2a3f7750628bb1d Implements: blueprint unsupported-type-validation
This commit is contained in:
@@ -67,6 +67,11 @@ class TOSCAException(Exception):
|
|||||||
TOSCAException._FATAL_EXCEPTION_FORMAT_ERRORS = flag
|
TOSCAException._FATAL_EXCEPTION_FORMAT_ERRORS = flag
|
||||||
|
|
||||||
|
|
||||||
|
class UnsupportedTypeError(TOSCAException):
|
||||||
|
msg_fmt = _('Type "%(what)s" is valid TOSCA type'
|
||||||
|
' but not supported at this time.')
|
||||||
|
|
||||||
|
|
||||||
class MissingRequiredFieldError(TOSCAException):
|
class MissingRequiredFieldError(TOSCAException):
|
||||||
msg_fmt = _('%(what)s is missing required field "%(required)s".')
|
msg_fmt = _('%(what)s is missing required field "%(required)s".')
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ from toscaparser.common.exception import InvalidTypeError
|
|||||||
from toscaparser.elements.attribute_definition import AttributeDef
|
from toscaparser.elements.attribute_definition import AttributeDef
|
||||||
from toscaparser.elements.entity_type import EntityType
|
from toscaparser.elements.entity_type import EntityType
|
||||||
from toscaparser.elements.property_definition import PropertyDef
|
from toscaparser.elements.property_definition import PropertyDef
|
||||||
|
from toscaparser.unsupportedtype import UnsupportedType
|
||||||
|
|
||||||
|
|
||||||
class StatefulEntityType(EntityType):
|
class StatefulEntityType(EntityType):
|
||||||
@@ -31,17 +32,20 @@ class StatefulEntityType(EntityType):
|
|||||||
|
|
||||||
def __init__(self, entitytype, prefix, custom_def=None):
|
def __init__(self, entitytype, prefix, custom_def=None):
|
||||||
entire_entitytype = entitytype
|
entire_entitytype = entitytype
|
||||||
if not entitytype.startswith(self.TOSCA):
|
if UnsupportedType.validate_type(entire_entitytype):
|
||||||
entire_entitytype = prefix + entitytype
|
|
||||||
if entire_entitytype in list(self.TOSCA_DEF.keys()):
|
|
||||||
self.defs = self.TOSCA_DEF[entire_entitytype]
|
|
||||||
entitytype = entire_entitytype
|
|
||||||
elif custom_def and entitytype in list(custom_def.keys()):
|
|
||||||
self.defs = custom_def[entitytype]
|
|
||||||
else:
|
|
||||||
self.defs = None
|
self.defs = None
|
||||||
ExceptionCollector.appendException(
|
else:
|
||||||
InvalidTypeError(what=entitytype))
|
if not entitytype.startswith(self.TOSCA):
|
||||||
|
entire_entitytype = prefix + entitytype
|
||||||
|
if entire_entitytype in list(self.TOSCA_DEF.keys()):
|
||||||
|
self.defs = self.TOSCA_DEF[entire_entitytype]
|
||||||
|
entitytype = entire_entitytype
|
||||||
|
elif custom_def and entitytype in list(custom_def.keys()):
|
||||||
|
self.defs = custom_def[entitytype]
|
||||||
|
else:
|
||||||
|
self.defs = None
|
||||||
|
ExceptionCollector.appendException(
|
||||||
|
InvalidTypeError(what=entitytype))
|
||||||
self.type = entitytype
|
self.type = entitytype
|
||||||
|
|
||||||
def get_properties_def_objects(self):
|
def get_properties_def_objects(self):
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ from toscaparser.elements.nodetype import NodeType
|
|||||||
from toscaparser.elements.policytype import PolicyType
|
from toscaparser.elements.policytype import PolicyType
|
||||||
from toscaparser.elements.relationshiptype import RelationshipType
|
from toscaparser.elements.relationshiptype import RelationshipType
|
||||||
from toscaparser.properties import Property
|
from toscaparser.properties import Property
|
||||||
|
from toscaparser.unsupportedtype import UnsupportedType
|
||||||
from toscaparser.utils.gettextutils import _
|
from toscaparser.utils.gettextutils import _
|
||||||
|
|
||||||
|
|
||||||
@@ -44,8 +45,9 @@ class EntityTemplate(object):
|
|||||||
self.entity_tpl = template
|
self.entity_tpl = template
|
||||||
self.custom_def = custom_def
|
self.custom_def = custom_def
|
||||||
self._validate_field(self.entity_tpl)
|
self._validate_field(self.entity_tpl)
|
||||||
|
type = self.entity_tpl.get('type')
|
||||||
|
UnsupportedType.validate_type(type)
|
||||||
if entity_name == 'node_type':
|
if entity_name == 'node_type':
|
||||||
type = self.entity_tpl.get('type')
|
|
||||||
self.type_definition = NodeType(type, custom_def) \
|
self.type_definition = NodeType(type, custom_def) \
|
||||||
if type is not None else None
|
if type is not None else None
|
||||||
if entity_name == 'relationship_type':
|
if entity_name == 'relationship_type':
|
||||||
@@ -57,10 +59,10 @@ class EntityTemplate(object):
|
|||||||
type = self.entity_tpl['relationship']
|
type = self.entity_tpl['relationship']
|
||||||
else:
|
else:
|
||||||
type = self.entity_tpl['type']
|
type = self.entity_tpl['type']
|
||||||
|
UnsupportedType.validate_type(type)
|
||||||
self.type_definition = RelationshipType(type,
|
self.type_definition = RelationshipType(type,
|
||||||
None, custom_def)
|
None, custom_def)
|
||||||
if entity_name == 'policy_type':
|
if entity_name == 'policy_type':
|
||||||
type = self.entity_tpl.get('type')
|
|
||||||
if not type:
|
if not type:
|
||||||
msg = (_('Policy definition of "%(pname)s" must have'
|
msg = (_('Policy definition of "%(pname)s" must have'
|
||||||
' a "type" ''attribute.') % dict(pname=name))
|
' a "type" ''attribute.') % dict(pname=name))
|
||||||
@@ -69,7 +71,6 @@ class EntityTemplate(object):
|
|||||||
|
|
||||||
self.type_definition = PolicyType(type, custom_def)
|
self.type_definition = PolicyType(type, custom_def)
|
||||||
if entity_name == 'group_type':
|
if entity_name == 'group_type':
|
||||||
type = self.entity_tpl.get('type')
|
|
||||||
self.type_definition = GroupType(type, custom_def) \
|
self.type_definition = GroupType(type, custom_def) \
|
||||||
if type is not None else None
|
if type is not None else None
|
||||||
self._properties = None
|
self._properties = None
|
||||||
|
|||||||
@@ -98,6 +98,22 @@ class ToscaTemplateValidationTest(TestCase):
|
|||||||
'field "derived1_from". Refer to the definition to '
|
'field "derived1_from". Refer to the definition to '
|
||||||
'verify valid values.'))
|
'verify valid values.'))
|
||||||
|
|
||||||
|
def test_unsupported_type(self):
|
||||||
|
tpl_snippet = '''
|
||||||
|
node_templates:
|
||||||
|
invalid_type:
|
||||||
|
type: tosca.test.invalidtype
|
||||||
|
properties:
|
||||||
|
size: { get_input: storage_size }
|
||||||
|
snapshot_id: { get_input: storage_snapshot_id }
|
||||||
|
'''
|
||||||
|
tpl = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
|
||||||
|
err = self.assertRaises(exception.UnsupportedTypeError,
|
||||||
|
TopologyTemplate, tpl, None)
|
||||||
|
expectedmessage = _('Type "tosca.test.invalidtype" is valid'
|
||||||
|
' TOSCA type but not supported at this time.')
|
||||||
|
self.assertEqual(expectedmessage, err.__str__())
|
||||||
|
|
||||||
def test_inputs(self):
|
def test_inputs(self):
|
||||||
tpl_snippet = '''
|
tpl_snippet = '''
|
||||||
inputs:
|
inputs:
|
||||||
|
|||||||
38
toscaparser/unsupportedtype.py
Normal file
38
toscaparser/unsupportedtype.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from toscaparser.common.exception import ExceptionCollector
|
||||||
|
from toscaparser.common.exception import UnsupportedTypeError
|
||||||
|
from toscaparser.utils.gettextutils import _
|
||||||
|
|
||||||
|
log = logging.getLogger('tosca')
|
||||||
|
|
||||||
|
|
||||||
|
class UnsupportedType(object):
|
||||||
|
|
||||||
|
un_supported_types = ['tosca.test.invalidtype',
|
||||||
|
'tosca.nodes.Storage.ObjectStorage',
|
||||||
|
'tosca.nodes.Storage.BlockStorage']
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def validate_type(entitytype):
|
||||||
|
if entitytype in UnsupportedType.un_supported_types:
|
||||||
|
ExceptionCollector.appendException(UnsupportedTypeError(
|
||||||
|
what=_('%s')
|
||||||
|
% entitytype))
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
Reference in New Issue
Block a user