From 4533792d101fc58323f11e48f1c90b079881db2f Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Tue, 15 Mar 2022 16:15:26 +0100 Subject: [PATCH] Fix InvalidTypeError in get_attribute function Fix InvalidTypeError is raised when a custom def datatype attribute is requested in a function. It only required to add custom defs in the DataType validation. Closes-Bug: 1964972 Change-Id: Ie3413c6d6ba86d0fd5eb819dfc9770b8211badc6 --- toscaparser/functions.py | 3 +- .../data/custom_types/custom_data_type.yaml | 21 +++++++++++ .../test_get_attribute_custom_data_type.yaml | 36 +++++++++++++++++++ toscaparser/tests/test_functions.py | 4 +++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 toscaparser/tests/data/custom_types/custom_data_type.yaml create mode 100644 toscaparser/tests/data/functions/test_get_attribute_custom_data_type.yaml diff --git a/toscaparser/functions.py b/toscaparser/functions.py index 58c9c2f6..1495e05d 100644 --- a/toscaparser/functions.py +++ b/toscaparser/functions.py @@ -179,7 +179,8 @@ class GetAttribute(Function): ).format(GET_ATTRIBUTE, elem))) return else: # It is a complex type - data_type = DataType(value_type) + data_type = DataType(value_type, + self.tosca_tpl.custom_defs) props = data_type.get_all_properties() found = [props[elem]] if elem in props else [] if found: diff --git a/toscaparser/tests/data/custom_types/custom_data_type.yaml b/toscaparser/tests/data/custom_types/custom_data_type.yaml new file mode 100644 index 00000000..8a4c481f --- /dev/null +++ b/toscaparser/tests/data/custom_types/custom_data_type.yaml @@ -0,0 +1,21 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +data_types: + + tosca.datatypes.SomeType: + derived_from: tosca.datatypes.Root + properties: + tasks: + type: map + entry_schema: + type: string + +node_types: + + tosca.nodes.SomeApp: + derived_from: tosca.nodes.SoftwareComponent + attributes: + some_new_att: + type: map + entry_schema: + type: tosca.datatypes.SomeType diff --git a/toscaparser/tests/data/functions/test_get_attribute_custom_data_type.yaml b/toscaparser/tests/data/functions/test_get_attribute_custom_data_type.yaml new file mode 100644 index 00000000..8484f751 --- /dev/null +++ b/toscaparser/tests/data/functions/test_get_attribute_custom_data_type.yaml @@ -0,0 +1,36 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +description: Template for testing get_attribute with a custom data type + +imports: + - ../custom_types/custom_data_type.yaml + +topology_template: + + node_templates: + + some_app: + type: tosca.nodes.SomeApp + requirements: + - host: server + + server: + type: tosca.nodes.Compute + capabilities: + # Host container properties + host: + properties: + num_cpus: 2 + disk_size: 10 GB + mem_size: 512 MB + # Guest Operating System properties + os: + properties: + # host Operating System image properties + architecture: x86_64 + type: Linux + distribution: RHEL + version: 6.5 + outputs: + url: + value: { get_attribute: [ some_app, some_new_att, map_value, tasks, other_map ]} diff --git a/toscaparser/tests/test_functions.py b/toscaparser/tests/test_functions.py index 93973fb1..e3bdab47 100644 --- a/toscaparser/tests/test_functions.py +++ b/toscaparser/tests/test_functions.py @@ -342,6 +342,10 @@ class GetAttributeTest(TestCase): self.assertIsNotNone(self._load_template( 'functions/test_container_cap_child.yaml')) + def test_get_attribute_custom_data_type(self): + self.assertIsNotNone(self._load_template( + 'functions/test_get_attribute_custom_data_type.yaml')) + class ConcatTest(TestCase):