Adding '--params' to cron-trigger CLI

Closes-Bug: #1449522

Depends-On: I68cc1121f8b1b7c211644a032c788b1361e831db
Change-Id: I1767961697fb995d9821543d2add23d8191662a4
This commit is contained in:
Nikolay Mahotkin 2015-04-30 16:43:00 +03:00
parent 74a3e03008
commit 63a41d657d
3 changed files with 42 additions and 18 deletions

View File

@ -24,7 +24,8 @@ class CronTrigger(base.Resource):
class CronTriggerManager(base.ResourceManager): class CronTriggerManager(base.ResourceManager):
resource_class = CronTrigger resource_class = CronTrigger
def create(self, name, workflow_name, workflow_input=None, pattern=None, def create(self, name, workflow_name, workflow_input=None,
workflow_params=None, pattern=None,
first_time=None, count=None): first_time=None, count=None):
self._ensure_not_empty( self._ensure_not_empty(
name=name, name=name,
@ -42,6 +43,9 @@ class CronTriggerManager(base.ResourceManager):
if workflow_input: if workflow_input:
data.update({'workflow_input': json.dumps(workflow_input)}) data.update({'workflow_input': json.dumps(workflow_input)})
if workflow_params:
data.update({'workflow_params': json.dumps(workflow_params)})
return self._create('/cron_triggers', data) return self._create('/cron_triggers', data)
def list(self): def list(self):

View File

@ -35,6 +35,7 @@ def format(trigger=None, lister=False):
columns = ( columns = (
'Name', 'Name',
'Workflow', 'Workflow',
'Params',
'Pattern', 'Pattern',
# TODO(rakhmerov): Uncomment when passwords are handled properly. # TODO(rakhmerov): Uncomment when passwords are handled properly.
# TODO(rakhmerov): Add 'Workflow input' column. # TODO(rakhmerov): Add 'Workflow input' column.
@ -52,6 +53,7 @@ def format(trigger=None, lister=False):
data = ( data = (
trigger.name, trigger.name,
trigger.workflow_name, trigger.workflow_name,
trigger.workflow_params,
trigger.pattern, trigger.pattern,
# TODO(rakhmerov): Uncomment when passwords are handled properly. # TODO(rakhmerov): Uncomment when passwords are handled properly.
# TODo(rakhmerov): Add 'wf_input' here. # TODo(rakhmerov): Add 'wf_input' here.
@ -111,6 +113,11 @@ class Create(show.ShowOne):
help='Workflow input' help='Workflow input'
) )
parser.add_argument(
'--params',
help='Workflow params',
)
parser.add_argument( parser.add_argument(
'--pattern', '--pattern',
type=str, type=str,
@ -132,21 +139,27 @@ class Create(show.ShowOne):
return parser return parser
@staticmethod
def _get_file_content_or_dict(string):
if string:
try:
return json.loads(string)
except:
return json.load(open(string))
else:
return {}
def take_action(self, parsed_args): def take_action(self, parsed_args):
mgr = cron_triggers.CronTriggerManager(self.app.client) mgr = cron_triggers.CronTriggerManager(self.app.client)
if parsed_args.workflow_input: wf_input = self._get_file_content_or_dict(parsed_args.workflow_input)
try: wf_params = self._get_file_content_or_dict(parsed_args.params)
wf_input = json.loads(parsed_args.workflow_input)
except:
wf_input = json.load(open(parsed_args.workflow_input))
else:
wf_input = {}
trigger = mgr.create( trigger = mgr.create(
parsed_args.name, parsed_args.name,
parsed_args.workflow_name, parsed_args.workflow_name,
wf_input, wf_input,
wf_params,
parsed_args.pattern, parsed_args.pattern,
parsed_args.first_time, parsed_args.first_time,
parsed_args.count parsed_args.count

View File

@ -1,7 +1,7 @@
# Copyright 2014 Mirantis, Inc. # Copyright 2014 Mirantis, Inc.
# All Rights Reserved # All Rights Reserved
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may # Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain # not use this file except in compliance with the License. You may obtain
# a copy of the License at # a copy of the License at
# #
@ -25,6 +25,7 @@ TRIGGER_DICT = {
'name': 'my_trigger', 'name': 'my_trigger',
'workflow_name': 'flow1', 'workflow_name': 'flow1',
'workflow_input': {}, 'workflow_input': {},
'workflow_params': {},
'pattern': '* * * * *', 'pattern': '* * * * *',
'next_execution_time': '4242-12-20 13:37', 'next_execution_time': '4242-12-20 13:37',
'remaining_executions': 5, 'remaining_executions': 5,
@ -32,11 +33,10 @@ TRIGGER_DICT = {
'updated_at': '1' 'updated_at': '1'
} }
TRIGGER = cron_triggers.CronTrigger(mock, TRIGGER_DICT) TRIGGER = cron_triggers.CronTrigger(mock, TRIGGER_DICT)
class TestCLIWorkbooksV2(base.BaseCommandTest): class TestCLITriggersV2(base.BaseCommandTest):
@mock.patch('argparse.open', create=True) @mock.patch('argparse.open', create=True)
@mock.patch('mistralclient.api.v2.cron_triggers.CronTriggerManager.create') @mock.patch('mistralclient.api.v2.cron_triggers.CronTriggerManager.create')
def test_create(self, mock, mock_open): def test_create(self, mock, mock_open):
@ -46,12 +46,15 @@ class TestCLIWorkbooksV2(base.BaseCommandTest):
result = self.call( result = self.call(
cron_triggers_cmd.Create, cron_triggers_cmd.Create,
app_args=['my_trigger', 'flow1', '--pattern', '* * * * *', app_args=['my_trigger', 'flow1', '--pattern', '* * * * *',
'--count', '5', '--first-time', '4242-12-20 13:37'] '--params', '{}', '--count', '5', '--first-time',
'4242-12-20 13:37']
) )
self.assertEqual( self.assertEqual(
('my_trigger', 'flow1', '* * * * *', '4242-12-20 13:37', 5, '1', (
'1'), 'my_trigger', 'flow1', {}, '* * * * *',
'4242-12-20 13:37', 5, '1', '1'
),
result[1] result[1]
) )
@ -62,8 +65,10 @@ class TestCLIWorkbooksV2(base.BaseCommandTest):
result = self.call(cron_triggers_cmd.List) result = self.call(cron_triggers_cmd.List)
self.assertEqual( self.assertEqual(
[('my_trigger', 'flow1', '* * * * *', '4242-12-20 13:37', 5, '1', [(
'1')], 'my_trigger', 'flow1', {}, '* * * * *',
'4242-12-20 13:37', 5, '1', '1'
)],
result[1] result[1]
) )
@ -74,8 +79,10 @@ class TestCLIWorkbooksV2(base.BaseCommandTest):
result = self.call(cron_triggers_cmd.Get, app_args=['name']) result = self.call(cron_triggers_cmd.Get, app_args=['name'])
self.assertEqual( self.assertEqual(
('my_trigger', 'flow1', '* * * * *', '4242-12-20 13:37', 5, '1', (
'1'), 'my_trigger', 'flow1', {}, '* * * * *',
'4242-12-20 13:37', 5, '1', '1'
),
result[1] result[1]
) )