diff --git a/translator/hot/translate_node_templates.py b/translator/hot/translate_node_templates.py index e603a14d..2d8e38f5 100644 --- a/translator/hot/translate_node_templates.py +++ b/translator/hot/translate_node_templates.py @@ -395,9 +395,43 @@ class TranslateNodeTemplates(object): else: return {'get_param': self.translate_param_value( get_input_args, resource)} + elif isinstance(param_value, dict) \ + and 'get_operation_output' in param_value: + res = self._translate_get_operation_output_function( + param_value['get_operation_output'], tosca_template) + if res: + return res return param_value + def _translate_get_operation_output_function(self, args, tosca_template): + tosca_target = self._find_tosca_node(args[0], + tosca_template) + if tosca_target and len(args) >= 4: + operations = HotResource.get_all_operations(tosca_target) + # ignore Standard interface name, + # it is the only one supported in the translator anyway + op_name = args[2] + output_name = args[3] + if op_name in operations: + operation = operations[op_name] + if operation in self.hot_lookup: + matching_deploy = self.hot_lookup[operation] + matching_config_name = matching_deploy.\ + properties['config']['get_resource'] + matching_config = self.find_hot_resource( + matching_config_name) + if matching_config: + outputs = matching_config.properties.get('outputs') + if outputs is None: + outputs = [] + outputs.append({'name': output_name}) + matching_config.properties['outputs'] = outputs + return {'get_attr': [ + matching_deploy.name, + output_name + ]} + @staticmethod def _unfold_value(value, value_arg): if value_arg is not None: diff --git a/translator/tests/data/artifacts/ssh/ssh_generate_keys.sh b/translator/tests/data/artifacts/ssh/ssh_generate_keys.sh new file mode 100644 index 00000000..9dc8e16f --- /dev/null +++ b/translator/tests/data/artifacts/ssh/ssh_generate_keys.sh @@ -0,0 +1,4 @@ +#!/bin/bash +test -d /root/.ssh || mkdir /root/.ssh +test -f /root/.ssh/id_rsa.pub || ssh-keygen -q -t rsa -N "" -f /root/.ssh/id_rsa +cat /root/.ssh/id_rsa.pub > ${heat_outputs_path}.public_key diff --git a/translator/tests/data/artifacts/ssh/ssh_import_public_key.sh b/translator/tests/data/artifacts/ssh/ssh_import_public_key.sh new file mode 100644 index 00000000..1ef94e04 --- /dev/null +++ b/translator/tests/data/artifacts/ssh/ssh_import_public_key.sh @@ -0,0 +1,3 @@ +#!/bin/bash +test -d /root/.ssh || mkdir /root/.ssh +echo "$public_key" >> /root/.ssh/authorized_keys diff --git a/translator/tests/data/hot_output/hot_exchange_public_ssh_key.yaml b/translator/tests/data/hot_output/hot_exchange_public_ssh_key.yaml new file mode 100644 index 00000000..1dfa1250 --- /dev/null +++ b/translator/tests/data/hot_output/hot_exchange_public_ssh_key.yaml @@ -0,0 +1,55 @@ +heat_template_version: 2013-05-23 + +description: > + TOSCA template to test get_operation_output by exchanging ssh public key + +parameters: {} +resources: + generate_ssh_key_create_deploy: + type: OS::Heat::SoftwareDeployment + properties: + config: + get_resource: generate_ssh_key_create_config + server: + get_resource: server1 + import_public_key_create_deploy: + type: OS::Heat::SoftwareDeployment + properties: + config: + get_resource: import_public_key_create_config + input_values: + public_key: + get_attr: + - generate_ssh_key_create_deploy + - public_key + server: + get_resource: server2 + depends_on: + - generate_ssh_key_create_deploy + server1: + type: OS::Nova::Server + properties: + flavor: m1.small + image: ubuntu-12.04-software-config-os-init + user_data_format: SOFTWARE_CONFIG + server2: + type: OS::Nova::Server + properties: + flavor: m1.small + image: ubuntu-12.04-software-config-os-init + user_data_format: SOFTWARE_CONFIG + generate_ssh_key_create_config: + type: OS::Heat::SoftwareConfig + properties: + config: + get_file: artifacts/ssh/ssh_generate_keys.sh + group: script + outputs: + - name: public_key + import_public_key_create_config: + type: OS::Heat::SoftwareConfig + properties: + config: + get_file: artifacts/ssh/ssh_import_public_key.sh + group: script +outputs: {} \ No newline at end of file diff --git a/translator/tests/data/tosca_exchange_public_ssh_key.yaml b/translator/tests/data/tosca_exchange_public_ssh_key.yaml new file mode 100644 index 00000000..7decb337 --- /dev/null +++ b/translator/tests/data/tosca_exchange_public_ssh_key.yaml @@ -0,0 +1,54 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +description: TOSCA template to test get_operation_output by exchanging ssh public key + +topology_template: + + node_templates: + server1: + type: tosca.nodes.Compute + capabilities: + host: + properties: + num_cpus: 1 + mem_size: 1 GB + os: + properties: + type: Linux + distribution: Ubuntu + version: 12.04 + architecture: x86_64 + + server2: + type: tosca.nodes.Compute + capabilities: + host: + properties: + num_cpus: 1 + mem_size: 1 GB + os: + properties: + type: Linux + distribution: Ubuntu + version: 12.04 + architecture: x86_64 + + generate_ssh_key: + type: tosca.nodes.SoftwareComponent + interfaces: + Standard: + create: artifacts/ssh/ssh_generate_keys.sh + requirements: + - host: server1 + + import_public_key: + type: tosca.nodes.SoftwareComponent + interfaces: + Standard: + create: + implementation: artifacts/ssh/ssh_import_public_key.sh + inputs: + public_key: { get_operation_output: [generate_ssh_key, Standard, create, public_key] } + requirements: + - host: server2 + - dependency: generate_ssh_key diff --git a/translator/tests/test_tosca_hot_translation.py b/translator/tests/test_tosca_hot_translation.py index a746153b..7a898271 100644 --- a/translator/tests/test_tosca_hot_translation.py +++ b/translator/tests/test_tosca_hot_translation.py @@ -483,3 +483,9 @@ class ToscaHotTranslationTest(TestCase): hot_file = '../tests/data/hot_output/hot_get_functions_semantic.yaml' params = {} self._test_successful_translation(tosca_file, hot_file, params) + + def test_hot_exchange_public_ssh_key(self): + tosca_file = '../tests/data/tosca_exchange_public_ssh_key.yaml' + hot_file = '../tests/data/hot_output/hot_exchange_public_ssh_key.yaml' + params = {} + self._test_successful_translation(tosca_file, hot_file, params)