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
This commit is contained in:
tengqm 2014-11-15 01:35:21 +08:00
parent 834368b096
commit 793c1cdea6
2 changed files with 12 additions and 2 deletions

View File

@ -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'])

View File

@ -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'])