Make graph download command more predictable
Now if we try to download graph with any task level parameters without specifying any filename the result (by default) will be stored in 'cluster_graph.yaml'. This patch: * makes default filename more accurate based on input parameters (environment id, task level graph value and format) * adds support to download graph in JSON format fuel2 graph download [-h] -e ENV (-a | -c | -p | -r) [-t GRAPH_TYPE][-f FILE] [--format {json,yaml}] DocImpact Change-Id: Ia7aa03cbbcde3d63e4ade5e76ea1242a2939b71e
This commit is contained in:
parent
735ec78c4c
commit
13acdfba37
|
@ -200,6 +200,7 @@ class GraphExecute(base.BaseTasksExecuteCommand):
|
||||||
class GraphDownload(base.BaseCommand):
|
class GraphDownload(base.BaseCommand):
|
||||||
"""Download deployment graph configuration."""
|
"""Download deployment graph configuration."""
|
||||||
entity_name = 'graph'
|
entity_name = 'graph'
|
||||||
|
supported_file_formats = ('json', 'yaml')
|
||||||
|
|
||||||
def get_parser(self, prog_name):
|
def get_parser(self, prog_name):
|
||||||
parser = super(GraphDownload, self).get_parser(prog_name)
|
parser = super(GraphDownload, self).get_parser(prog_name)
|
||||||
|
@ -247,32 +248,30 @@ class GraphDownload(base.BaseCommand):
|
||||||
type=str,
|
type=str,
|
||||||
required=False,
|
required=False,
|
||||||
default=None,
|
default=None,
|
||||||
help='YAML file that contains tasks data.')
|
help='File in {} format that contains tasks '
|
||||||
|
'data.'.format(self.supported_file_formats))
|
||||||
|
parser.add_argument('--format',
|
||||||
|
required=False,
|
||||||
|
choices=self.supported_file_formats,
|
||||||
|
default='yaml',
|
||||||
|
help='Format of serialized tasks data. '
|
||||||
|
'Defaults to YAML.')
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_default_tasks_data_path(cls):
|
def get_default_tasks_data_path(cls, env_id, task_level_name, file_format):
|
||||||
return os.path.join(
|
return os.path.join(
|
||||||
os.path.abspath(os.curdir),
|
os.path.abspath(os.curdir),
|
||||||
"cluster_graph"
|
'{}_graph_{}.{}'.format(task_level_name, env_id, file_format)
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def write_tasks_to_file(cls, tasks_data, serializer=None, file_path=None):
|
def write_tasks_to_file(cls, tasks_data, serializer, file_path):
|
||||||
serializer = serializer or Serializer()
|
return serializer.write_to_full_path(file_path, tasks_data)
|
||||||
if file_path:
|
|
||||||
return serializer.write_to_full_path(
|
|
||||||
file_path,
|
|
||||||
tasks_data
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
return serializer.write_to_path(
|
|
||||||
cls.get_default_tasks_data_path(),
|
|
||||||
tasks_data
|
|
||||||
)
|
|
||||||
|
|
||||||
def take_action(self, args):
|
def take_action(self, args):
|
||||||
tasks_data = []
|
tasks_data = []
|
||||||
|
tasks_level_name = ''
|
||||||
for tasks_level_name in ('all', 'cluster', 'release', 'plugins'):
|
for tasks_level_name in ('all', 'cluster', 'release', 'plugins'):
|
||||||
if getattr(args, tasks_level_name):
|
if getattr(args, tasks_level_name):
|
||||||
tasks_data = self.client.download(
|
tasks_data = self.client.download(
|
||||||
|
@ -283,10 +282,12 @@ class GraphDownload(base.BaseCommand):
|
||||||
break
|
break
|
||||||
|
|
||||||
# write to file
|
# write to file
|
||||||
|
file_path = args.file or self.get_default_tasks_data_path(
|
||||||
|
args.env, tasks_level_name, args.format)
|
||||||
graph_data_file_path = self.write_tasks_to_file(
|
graph_data_file_path = self.write_tasks_to_file(
|
||||||
tasks_data=tasks_data,
|
tasks_data=tasks_data,
|
||||||
serializer=Serializer(),
|
serializer=Serializer(format=args.format),
|
||||||
file_path=args.file)
|
file_path=file_path)
|
||||||
|
|
||||||
self.app.stdout.write(
|
self.app.stdout.write(
|
||||||
"Tasks were downloaded to {0}\n".format(graph_data_file_path)
|
"Tasks were downloaded to {0}\n".format(graph_data_file_path)
|
||||||
|
|
|
@ -285,6 +285,25 @@ class TestGraphActions(test_engine.BaseCLITest):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@mock.patch('json.dumps')
|
||||||
|
def test_download_json(self, m_dumps):
|
||||||
|
env_id = 1
|
||||||
|
graph_data = [{'id': 1}]
|
||||||
|
args = 'graph download --env {0} --all --format json'.format(env_id)
|
||||||
|
expected_path = '/tmp/all_graph_{0}.json'.format(env_id)
|
||||||
|
|
||||||
|
self.m_client.download.return_value = graph_data
|
||||||
|
|
||||||
|
m_open = mock.mock_open()
|
||||||
|
with mock.patch('os.path.abspath', return_value='/tmp'):
|
||||||
|
with mock.patch(
|
||||||
|
'fuelclient.cli.serializers.open', m_open, create=True):
|
||||||
|
self.exec_command(args)
|
||||||
|
|
||||||
|
m_open.assert_called_once_with(expected_path, 'w')
|
||||||
|
m_dumps.assert_called_once_with(graph_data, indent=4)
|
||||||
|
self.m_get_client.assert_called_once_with('graph', mock.ANY)
|
||||||
|
|
||||||
@mock.patch('sys.stderr')
|
@mock.patch('sys.stderr')
|
||||||
def test_download_fail(self, mocked_stderr):
|
def test_download_fail(self, mocked_stderr):
|
||||||
cmd = 'graph download --env 1'
|
cmd = 'graph download --env 1'
|
||||||
|
|
Loading…
Reference in New Issue