Added option to command execute for specifying output format

New option 'format' for commands 'graph execute' and
'sequence execute' will allow to specify the expected
format of output: json or yaml.
By default the text message will be produced.

Change-Id: I68be70bf317ea9fe4c0197073bd5dd41c3ba9d32
Closes-Bug: 1623014
This commit is contained in:
Bulat Gaifullin 2016-09-14 14:15:12 +03:00
parent 29cf9b2a0a
commit 1c6cd3524e
5 changed files with 59 additions and 29 deletions

View File

@ -21,9 +21,9 @@ from cliff import show
import six
import fuelclient
from fuelclient.cli.serializers import Serializer
from fuelclient.common import data_utils
VERSION = 'v1'
@ -176,6 +176,7 @@ class BaseDeleteCommand(BaseCommand):
@six.add_metaclass(abc.ABCMeta)
class BaseTasksExecuteCommand(BaseCommand):
def get_parser(self, prog_name):
parser = super(BaseTasksExecuteCommand, self).get_parser(prog_name)
@ -197,6 +198,11 @@ class BaseTasksExecuteCommand(BaseCommand):
default=False,
help='Enable debugging mode in tasks executor.'
)
parser.add_argument(
'--format',
choices=['json', 'yaml'],
help='Select output format, by default text message will produce.'
)
mode_group = parser.add_mutually_exclusive_group()
mode_group.add_argument(
@ -215,3 +221,26 @@ class BaseTasksExecuteCommand(BaseCommand):
'Execution result summary can be got via history of tasks.')
return parser
def take_action(self, parsed_args):
task = self.client.execute(
env_id=parsed_args.env,
dry_run=parsed_args.dry_run,
noop_run=parsed_args.noop,
force=parsed_args.force,
debug=parsed_args.trace,
**self.get_options(parsed_args)
)
if parsed_args.format:
msg = Serializer(parsed_args.format).serialize(task.data) + '\n'
else:
msg = (
'Deployment task with id {0} for the environment {1} '
'has been started.\n'
.format(task.data['id'], task.data['cluster'])
)
self.app.stdout.write(msg)
def get_options(self, parsed_args):
"""Produce additional options from cmdline arguments."""
raise NotImplementedError

View File

@ -180,21 +180,13 @@ class GraphExecute(base.BaseTasksExecuteCommand):
)
return parser
def take_action(self, args):
result = self.client.execute(
env_id=args.env,
graph_types=args.graph_types,
nodes=args.nodes,
task_names=args.task_names,
dry_run=args.dry_run,
noop_run=args.noop,
force=args.force,
debug=args.trace
)
msg = 'Deployment task with id {t} for the environment {e} ' \
'has been started.\n'.format(t=result.data['id'],
e=result.data['cluster'])
self.app.stdout.write(msg)
def get_options(self, parsed_args):
return {
'graph_types': parsed_args.graph_types,
'nodes': parsed_args.nodes,
'task_names': parsed_args.task_names,
}
class GraphDownload(base.BaseCommand):

View File

@ -197,16 +197,7 @@ class SequenceExecute(SequenceMixIn, base.BaseTasksExecuteCommand):
)
return parser
def take_action(self, args):
result = self.client.execute(
sequence_id=args.id,
env_id=args.env,
dry_run=args.dry_run,
noop_run=args.noop,
force=args.force,
debug=args.trace
)
msg = 'Deployment task with id {t} for the environment {e} ' \
'has been started.\n'.format(t=result.data['id'],
e=result.data['cluster'])
self.app.stdout.write(msg)
def get_options(self, parsed_args):
return {
'sequence_id': parsed_args.id,
}

View File

@ -249,6 +249,16 @@ class TestGraphActions(test_engine.BaseCLITest):
)
)
def test_execute_with_json_output(self):
self.m_client.execute.return_value = mock.MagicMock(
data={'id': 1}
)
with mock.patch('sys.stdout') as stdout_mock:
self.exec_command(
'graph execute --env 1 --graph-types graph1 --format=json'
)
stdout_mock.write.assert_called_with('{\n "id": 1\n}\n')
def test_download(self):
self.m_client.download.return_value = yaml.safe_load(TASKS_YAML)

View File

@ -90,3 +90,11 @@ class TestSequenceActions(test_engine.BaseCLITest):
sequence_id=1, env_id=2,
dry_run=False, noop_run=True, force=False, debug=True
)
def test_execute_with_json_output(self):
self.m_client.execute.return_value = mock.MagicMock(
data={'id': 1}
)
with mock.patch('sys.stdout') as stdout_mock:
self.exec_command('sequence execute 1 -e 2 --format=json')
stdout_mock.write.assert_called_with('{\n "id": 1\n}\n')