From 793c1cdea641fc90bea30eb91cd98fd3b69ff9ad Mon Sep 17 00:00:00 2001 From: tengqm Date: Sat, 15 Nov 2014 01:35:21 +0800 Subject: [PATCH] Enable script hook to handle Json input This patch adds conversion of Json type input into a string so that it can be consumed by scripts (e.g. Python or Bash). Change-Id: I715819dc8e0c1c9119b544330d5b0c77e9c2f924 Closes-Bug: #1392802 --- .../elements/heat-config-script/install.d/hook-script.py | 6 +++++- tests/software_config/test_hook_script.py | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/hot/software-config/elements/heat-config-script/install.d/hook-script.py b/hot/software-config/elements/heat-config-script/install.d/hook-script.py index 94b3a178..5c9cb4dc 100755 --- a/hot/software-config/elements/heat-config-script/install.d/hook-script.py +++ b/hot/software-config/elements/heat-config-script/install.d/hook-script.py @@ -47,7 +47,11 @@ def main(argv=sys.argv): env = os.environ.copy() for input in c['inputs']: input_name = input['name'] - env[input_name] = input.get('value', '') + value = input.get('value', '') + if isinstance(value, dict) or isinstance(value, list): + env[input_name] = json.dumps(value) + else: + env[input_name] = value log.info('%s=%s' % (input_name, env[input_name])) fn = os.path.join(WORKING_DIR, c['id']) diff --git a/tests/software_config/test_hook_script.py b/tests/software_config/test_hook_script.py index 80b25af2..2e0d5e8c 100644 --- a/tests/software_config/test_hook_script.py +++ b/tests/software_config/test_hook_script.py @@ -41,7 +41,9 @@ class HookScriptTest(common.RunScriptTest): 'group': 'script', 'inputs': [ {'name': 'foo', 'value': 'bar'}, - {'name': 'another', 'value': 'input'} + {'name': 'another', 'value': 'input'}, + {'name': 'a_dict', 'value': '{"key": "value"}'}, + {'name': 'a_list', 'value': '["v1", 12]'}, ], 'outputs': [ {'name': 'first_output'}, @@ -93,6 +95,8 @@ class HookScriptTest(common.RunScriptTest): self.assertEqual([script], state['args']) self.assertEqual('bar', state['env']['foo']) self.assertEqual('input', state['env']['another']) + self.assertEqual('{"key": "value"}', state['env']['a_dict']) + self.assertEqual('["v1", 12]', state['env']['a_list']) self.assertEqual(self.outputs_dir.join('1234'), state['env']['heat_outputs_path']) @@ -123,5 +127,7 @@ class HookScriptTest(common.RunScriptTest): self.assertEqual([script], state['args']) self.assertEqual('bar', state['env']['foo']) self.assertEqual('input', state['env']['another']) + self.assertEqual('{"key": "value"}', state['env']['a_dict']) + self.assertEqual('["v1", 12]', state['env']['a_list']) self.assertEqual(self.outputs_dir.join('1234'), state['env']['heat_outputs_path'])