From 2394b580ca6820ffb4e0945dc3b61011d697518d Mon Sep 17 00:00:00 2001 From: shangxdy Date: Thu, 10 Aug 2017 21:26:10 +0800 Subject: [PATCH] Adding custom datatypes support in properties of capabilities Currently if there is costum datatype definition in capability, the parser will be error, the patch support custom datatype definition in property for capability. Change-Id: Ic899fd125840f817b06b05aacf0829aa2d82ffa0 Signed-off-by: shangxdy --- toscaparser/capabilities.py | 6 +++-- toscaparser/entity_template.py | 2 +- .../custom_cap_with_datatype.yaml | 27 +++++++++++++++++++ .../data/test_custom_caps_with_datatype.yaml | 19 +++++++++++++ toscaparser/tests/test_toscatpl.py | 6 +++++ 5 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 toscaparser/tests/data/custom_types/custom_cap_with_datatype.yaml create mode 100644 toscaparser/tests/data/test_custom_caps_with_datatype.yaml diff --git a/toscaparser/capabilities.py b/toscaparser/capabilities.py index c23ef725..ffd86220 100644 --- a/toscaparser/capabilities.py +++ b/toscaparser/capabilities.py @@ -16,10 +16,11 @@ from toscaparser.properties import Property class Capability(object): '''TOSCA built-in capabilities type.''' - def __init__(self, name, properties, definition): + def __init__(self, name, properties, definition, custom_def=None): self.name = name self._properties = properties self.definition = definition + self.custom_def = custom_def def get_properties_objects(self): '''Return a list of property objects.''' @@ -30,7 +31,8 @@ class Capability(object): props_def = self.definition.get_properties_def() if props_def and name in props_def: properties.append(Property(name, value, - props_def[name].schema)) + props_def[name].schema, + self.custom_def)) return properties def get_properties(self): diff --git a/toscaparser/entity_template.py b/toscaparser/entity_template.py index cc3d6206..d454ac0f 100644 --- a/toscaparser/entity_template.py +++ b/toscaparser/entity_template.py @@ -164,7 +164,7 @@ class EntityTemplate(object): if 'properties' in props and props['properties']: properties.update(props['properties']) - cap = Capability(name, properties, c) + cap = Capability(name, properties, c, self.custom_def) capability.append(cap) return capability diff --git a/toscaparser/tests/data/custom_types/custom_cap_with_datatype.yaml b/toscaparser/tests/data/custom_types/custom_cap_with_datatype.yaml new file mode 100644 index 00000000..6f2694b3 --- /dev/null +++ b/toscaparser/tests/data/custom_types/custom_cap_with_datatype.yaml @@ -0,0 +1,27 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +node_types: + tosca.nodes.SomeNode: + derived_from: tosca.nodes.Root + capabilities: + cap1: + type: tosca.capabilities.SomeCap + +capability_types: + tosca.capabilities.SomeCap: + derived_from: tosca.capabilities.Root + properties: + data: + type: tosca.datatypes.SomeData + +data_types: + tosca.datatypes.SomeData: + derived_from: tosca.datatypes.Root + properties: + name: + type: string + required: true + value: + type: integer + required: false + default: 100 diff --git a/toscaparser/tests/data/test_custom_caps_with_datatype.yaml b/toscaparser/tests/data/test_custom_caps_with_datatype.yaml new file mode 100644 index 00000000..47aeed7a --- /dev/null +++ b/toscaparser/tests/data/test_custom_caps_with_datatype.yaml @@ -0,0 +1,19 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +description: TOSCA simple profile to test a custom defined capability with custom datatype + +imports: + - custom_types/custom_cap_with_datatype.yaml + +topology_template: + + node_templates: + + TestNode: + type: tosca.nodes.SomeNode + capabilities: + cap1: + properties: + data: + name: test + value: 1 diff --git a/toscaparser/tests/test_toscatpl.py b/toscaparser/tests/test_toscatpl.py index 29c0f4e2..3ec01b80 100644 --- a/toscaparser/tests/test_toscatpl.py +++ b/toscaparser/tests/test_toscatpl.py @@ -779,6 +779,12 @@ class ToscaTemplateTest(TestCase): "data/test_custom_caps_def.yaml") ToscaTemplate(tosca_tpl) + def test_custom_caps_with_custom_datatype(self): + tosca_tpl = os.path.join( + os.path.dirname(os.path.abspath(__file__)), + "data/test_custom_caps_with_datatype.yaml") + ToscaTemplate(tosca_tpl) + def test_custom_rel_with_script(self): tosca_tpl = os.path.join( os.path.dirname(os.path.abspath(__file__)),