Fix KeyError in get_attribute function

Fix KeyError is raised in get_attribute validation in case of
nested data types.
The data type schema was not correctly managed.

Closes-Bug: 1965089
Change-Id: Ic87a3c429105d6108507de05a31b788538ee2259
This commit is contained in:
Miguel Caballer 2022-03-16 08:52:02 +01:00
parent 4533792d10
commit 46141f95d2
4 changed files with 73 additions and 2 deletions

View File

@ -159,6 +159,7 @@ class GetAttribute(Function):
return
value_type = attr.schema['type']
value_schema = attr.schema
if len(self.args) > index:
for elem in self.args[index:]:
if value_type == "list":
@ -168,9 +169,9 @@ class GetAttribute(Function):
' "{0}". "{1}" Expected positive'
' integer argument'
).format(GET_ATTRIBUTE, elem)))
value_type = attr.schema['entry_schema']['type']
value_type = value_schema['entry_schema']['type']
elif value_type == "map":
value_type = attr.schema['entry_schema']['type']
value_type = value_schema['entry_schema']['type']
elif value_type in Schema.PROPERTY_TYPES:
ExceptionCollector.appendException(
ValueError(_('Illegal arguments for function'
@ -186,6 +187,7 @@ class GetAttribute(Function):
if found:
prop = found[0]
value_type = prop.schema['type']
value_schema = prop.schema
else:
ExceptionCollector.appendException(
KeyError(_('Illegal arguments for function'

View File

@ -0,0 +1,29 @@
tosca_definitions_version: tosca_simple_yaml_1_0
data_types:
tosca.datatypes.SomeTask:
derived_from: tosca.datatypes.Root
properties:
state:
type: string
output:
type: string
tosca.datatypes.OtherTask:
derived_from: tosca.datatypes.Root
properties:
tasks:
type: map
entry_schema:
type: tosca.datatypes.SomeTask
node_types:
tosca.nodes.SomeApp:
derived_from: tosca.nodes.SoftwareComponent
attributes:
some_new_att:
type: map
entry_schema:
type: tosca.datatypes.OtherTask

View File

@ -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_nested_data_types.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, output ]}

View File

@ -346,6 +346,10 @@ class GetAttributeTest(TestCase):
self.assertIsNotNone(self._load_template(
'functions/test_get_attribute_custom_data_type.yaml'))
def test_get_attribute_neste_data_types(self):
self.assertIsNotNone(self._load_template(
'functions/test_get_attribute_nested_data_types.yaml'))
class ConcatTest(TestCase):