Merge "Add Token class to manage token function"

This commit is contained in:
Jenkins
2016-07-03 01:29:06 +00:00
committed by Gerrit Code Review
4 changed files with 112 additions and 1 deletions

View File

@@ -29,6 +29,7 @@ GET_PROPERTY = 'get_property'
GET_ATTRIBUTE = 'get_attribute'
GET_INPUT = 'get_input'
CONCAT = 'concat'
TOKEN = 'token'
SELF = 'SELF'
HOST = 'HOST'
@@ -636,11 +637,58 @@ class Concat(Function):
def result(self):
return self
class Token(Function):
"""Validate the function and provide an instance of the function
The token function is used within a TOSCA service template on a string to
parse out (tokenize) substrings separated by one or more token characters
within a larger string.
Arguments:
* The composite string that contains one or more substrings separated by
token characters.
* The string that contains one or more token characters that separate
substrings within the composite string.
* The integer indicates the index of the substring to return from the
composite string. Note that the first substring is denoted by using
the '0' (zero) integer value.
Example:
[ get_attribute: [ my_server, data_endpoint, ip_address ], ':', 1 ]
"""
def validate(self):
if len(self.args) < 3:
ExceptionCollector.appendException(
ValueError(_('Invalid arguments for function "{0}". Expected '
'at least three arguments.').format(TOKEN)))
else:
if not isinstance(self.args[1], str) or len(self.args[1]) != 1:
ExceptionCollector.appendException(
ValueError(_('Invalid arguments for function "{0}". '
'Expected single char value as second '
'argument.').format(TOKEN)))
if not isinstance(self.args[2], int):
ExceptionCollector.appendException(
ValueError(_('Invalid arguments for function "{0}". '
'Expected integer value as third '
'argument.').format(TOKEN)))
def result(self):
return self
function_mappings = {
GET_PROPERTY: GetProperty,
GET_INPUT: GetInput,
GET_ATTRIBUTE: GetAttribute,
CONCAT: Concat
CONCAT: Concat,
TOKEN: Token
}

View File

@@ -0,0 +1,15 @@
tosca_definitions_version: tosca_simple_yaml_1_0
description: Template for deploying a single server with token function.
topology_template:
node_templates:
server:
type: tosca.nodes.Compute
outputs:
url:
description: Get the first part of the ip
value: { token: [ get_attribute: [ server, public_address ],
'.' ,
0 ] }

View File

@@ -0,0 +1,17 @@
tosca_definitions_version: tosca_simple_yaml_1_0
description: Template for deploying a single server with invalid token function.
topology_template:
outputs:
invalid_token_syntax_1:
description: test token with only two paremeters.
value: { token: ["some_string", "_"]}
invalid_token_syntax_2:
description: test token with invalid string as third argument.
value: { token: ["some_string", "_", "1"]}
invalid_token_syntax_3:
description: test token with invalid string as second argument.
value: { token: ["some_string", "aa", "1"]}

View File

@@ -323,3 +323,34 @@ class ConcatTest(TestCase):
ValueError,
_('Invalid arguments for function "concat". Expected at least '
'one arguments.'))
class TokenTest(TestCase):
def _load_template(self, filename):
return ToscaTemplate(os.path.join(
os.path.dirname(os.path.abspath(__file__)),
filename))
def test_validate_token(self):
tosca = self._load_template("data/functions/test_token.yaml")
server_url_output = [
output for output in tosca.outputs if output.name == 'url'][0]
func = functions.get_function(self, tosca.outputs,
server_url_output.value)
self.assertIsInstance(func, functions.Token)
self.assertRaises(exception.ValidationError, self._load_template,
'data/functions/test_token_invalid.yaml')
exception.ExceptionCollector.assertExceptionMessage(
ValueError,
_('Invalid arguments for function "token". Expected at least '
'three arguments.'))
exception.ExceptionCollector.assertExceptionMessage(
ValueError,
_('Invalid arguments for function "token". Expected '
'integer value as third argument.'))
exception.ExceptionCollector.assertExceptionMessage(
ValueError,
_('Invalid arguments for function "token". Expected '
'single char value as second argument.'))