Merge "Implementing get_operation_output function"

This commit is contained in:
Jenkins 2016-10-16 23:41:43 +00:00 committed by Gerrit Code Review
commit a243997d9d
3 changed files with 211 additions and 0 deletions

View File

@ -14,6 +14,7 @@
import abc
import six
import toscaparser.elements.interfaces
from toscaparser.common.exception import ExceptionCollector
from toscaparser.common.exception import UnknownInputError
@ -22,12 +23,14 @@ from toscaparser.elements.constraints import Schema
from toscaparser.elements.datatype import DataType
from toscaparser.elements.entity_type import EntityType
from toscaparser.elements.relationshiptype import RelationshipType
from toscaparser.elements.statefulentitytype import StatefulEntityType
from toscaparser.utils.gettextutils import _
GET_PROPERTY = 'get_property'
GET_ATTRIBUTE = 'get_attribute'
GET_INPUT = 'get_input'
GET_OPERATION_OUTPUT = 'get_operation_output'
CONCAT = 'concat'
TOKEN = 'token'
@ -609,6 +612,88 @@ class GetProperty(Function):
return None
class GetOperationOutput(Function):
def validate(self):
if len(self.args) == 4:
self._find_node_template(self.args[0])
interface_name = self._find_interface_name(self.args[1])
self._find_operation_name(interface_name, self.args[2])
else:
ExceptionCollector.appendException(
ValueError(_('Illegal arguments for function "{0}". Expected '
'arguments: "template_name","interface_name",'
'"operation_name","output_variable_name"'
).format(GET_OPERATION_OUTPUT)))
return
def _find_interface_name(self, interface_name):
if interface_name in toscaparser.elements.interfaces.SECTIONS:
return interface_name
else:
ExceptionCollector.appendException(
ValueError(_('Enter a valid interface name'
).format(GET_OPERATION_OUTPUT)))
return
def _find_operation_name(self, interface_name, operation_name):
if(interface_name == 'Configure' or
interface_name == 'tosca.interfaces.node.relationship.Configure'):
if(operation_name in
StatefulEntityType.
interfaces_relationship_configure_operations):
return operation_name
else:
ExceptionCollector.appendException(
ValueError(_('Enter an operation of Configure interface'
).format(GET_OPERATION_OUTPUT)))
return
elif(interface_name == 'Standard' or
interface_name == 'tosca.interfaces.node.lifecycle.Standard'):
if(operation_name in
StatefulEntityType.interfaces_node_lifecycle_operations):
return operation_name
else:
ExceptionCollector.appendException(
ValueError(_('Enter an operation of Standard interface'
).format(GET_OPERATION_OUTPUT)))
return
else:
ExceptionCollector.appendException(
ValueError(_('Enter a valid operation name'
).format(GET_OPERATION_OUTPUT)))
return
def _find_node_template(self, node_template_name):
if node_template_name == TARGET:
if not isinstance(self.context.type_definition, RelationshipType):
ExceptionCollector.appendException(
KeyError(_('"TARGET" keyword can only be used in context'
' to "Relationships" target node')))
return
return self.context.target
if node_template_name == SOURCE:
if not isinstance(self.context.type_definition, RelationshipType):
ExceptionCollector.appendException(
KeyError(_('"SOURCE" keyword can only be used in context'
' to "Relationships" source node')))
return
return self.context.source
name = self.context.name \
if node_template_name == SELF and \
not isinstance(self.context, list) \
else node_template_name
for node_template in self.tosca_tpl.nodetemplates:
if node_template.name == name:
return node_template
ExceptionCollector.appendException(
KeyError(_(
'Node template "{0}" was not found.'
).format(node_template_name)))
def result(self):
return self
class Concat(Function):
"""Validate the function and provide an instance of the function
@ -687,6 +772,7 @@ function_mappings = {
GET_PROPERTY: GetProperty,
GET_INPUT: GetInput,
GET_ATTRIBUTE: GetAttribute,
GET_OPERATION_OUTPUT: GetOperationOutput,
CONCAT: Concat,
TOKEN: Token
}

View File

@ -0,0 +1,19 @@
tosca_definitions_version: tosca_simple_yaml_1_0
description: TOSCA simple profile to test the GET OPERATION OUTPUT functionality
imports:
- custom_types/compute_with_prop.yaml
topology_template:
node_templates:
front_end:
type: tosca.nodes.ComputeWithProp
interfaces:
Standard:
create:
implementation: nodejs/create.sh
inputs:
data_dir: {get_operation_output: [front_end,Standard,create,data_dir]}

View File

@ -105,6 +105,112 @@ class ToscaTemplateValidationTest(TestCase):
'field "derived_from4". Refer to the definition to '
'verify valid values.'))
def test_getoperation_IncorrectValue(self):
# test case 1
tpl_snippet = '''
node_templates:
front_end:
type: tosca.nodes.Compute
interfaces:
Standard:
create:
implementation: scripts/frontend/create.sh
configure:
implementation: scripts/frontend/configure.sh
inputs:
data_dir: {get_operation_output: [front_end,Standard1,
create,data_dir]}
'''
tpl = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
err = self.assertRaises(ValueError,
TopologyTemplate, tpl, None)
expectedmessage = _('Enter a valid interface name')
self.assertEqual(expectedmessage, err.__str__())
# test case 2
tpl_snippet2 = '''
node_templates:
front_end:
type: tosca.nodes.Compute
interfaces:
Standard:
create:
implementation: scripts/frontend/create.sh
configure:
implementation: scripts/frontend/configure.sh
inputs:
data_dir: {get_operation_output: [front_end1,Standard,
create,data_dir]}
'''
tpl2 = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet2))
err2 = self.assertRaises(KeyError,
TopologyTemplate, tpl2, None)
expectedmessage2 = _('\'Node template "front_end1" was not found.\'')
self.assertEqual(expectedmessage2, err2.__str__())
# test case 3
tpl_snippet3 = '''
node_templates:
front_end:
type: tosca.nodes.Compute
interfaces:
Standard:
create:
implementation: scripts/frontend/create.sh
configure:
implementation: scripts/frontend/configure.sh
inputs:
data_dir: {get_operation_output: [front_end,Standard,
get_target,data_dir]}
'''
tpl3 = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet3))
err3 = self.assertRaises(ValueError,
TopologyTemplate, tpl3, None)
expectedmessage3 = _('Enter an operation of Standard interface')
self.assertEqual(expectedmessage3, err3.__str__())
# test case 4
tpl_snippet4 = '''
node_templates:
front_end:
type: tosca.nodes.Compute
interfaces:
Standard:
create:
implementation: scripts/frontend/create.sh
configure:
implementation: scripts/frontend/configure.sh
inputs:
data_dir: {get_operation_output: [front_end,Configure,
create,data_dir]}
'''
tpl4 = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet4))
err4 = self.assertRaises(ValueError,
TopologyTemplate, tpl4, None)
expectedmessage4 = _('Enter an operation of Configure interface')
self.assertEqual(expectedmessage4, err4.__str__())
# test case 5
tpl_snippet5 = '''
node_templates:
front_end:
type: tosca.nodes.Compute
interfaces:
Standard:
create:
implementation: scripts/frontend/create.sh
configure:
implementation: scripts/frontend/configure.sh
inputs:
data_dir: {get_operation_output: [front_end,Standard,
create]}
'''
tpl5 = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet5))
err5 = self.assertRaises(ValueError,
TopologyTemplate, tpl5, None)
expectedmessage5 = _('Illegal arguments for function'
' "get_operation_output".'
' Expected arguments: "template_name",'
'"interface_name",'
'"operation_name","output_variable_name"')
self.assertEqual(expectedmessage5, err5.__str__())
def test_unsupported_type(self):
tpl_snippet = '''
node_templates: