Adds plan-templates command to CLI

Adds plan-templates command to command line interface.
This commands needs Plan UUID and output directory parameters
and it will retrieve templates of one particular Plan
and writes one file for each template to output directory.
Adds tests for this behavior.

Change-Id: Ie9a7418d4f2abc0fabc8642c0daf60cd4508ca84
This commit is contained in:
Petr Blaho 2014-09-11 15:08:27 +02:00
parent f2799bfc78
commit dc170a3b22
2 changed files with 63 additions and 0 deletions

View File

@ -130,3 +130,40 @@ class PlansShellTest(BasePlansShellTest):
sorted(attributes, key=lambda k: k['name']),
sorted(self.tuskar.plans.patch.call_args[0][1],
key=lambda k: k['name']))
@mock.patch('tuskarclient.v2.plans_shell.print', create=True)
@mock.patch('tuskarclient.v2.plans_shell.os.mkdir', create=True)
@mock.patch('tuskarclient.v2.plans_shell.os.path.isdir', create=True)
@mock.patch('tuskarclient.v2.plans_shell.open', create=True)
def test_plan_templates(
self, mock_open, mock_isdir, mock_mkdir, mock_print):
args = empty_args()
args.plan_uuid = 'plan_uuid'
args.output_dir = 'outdir/subdir'
mock_isdir.return_value = False
self.tuskar.plans.templates.return_value = {
'name_foo': 'value_foo',
'name_bar': 'value_bar'
}
self.shell.do_plan_templates(self.tuskar, args, outfile=self.outfile)
mock_isdir.assert_called_with('outdir/subdir')
mock_mkdir.assert_called_with('outdir/subdir')
self.tuskar.plans.templates.assert_called_with('plan_uuid')
mock_open.assert_any_call('outdir/subdir/name_foo', 'w+')
mock_open.assert_any_call('outdir/subdir/name_bar', 'w+')
self.assertEqual(mock_open.call_count, 2)
mock_opened_file = mock_open.return_value.__enter__.return_value
mock_opened_file.write.assert_any_call('value_foo')
mock_opened_file.write.assert_any_call('value_bar')
self.assertEqual(mock_opened_file.write.call_count, 2)
mock_print.assert_any_call('Following templates has been written:')
mock_print.assert_any_call('outdir/subdir/name_foo')
mock_print.assert_any_call('outdir/subdir/name_bar')
self.assertEqual(mock_print.call_count, 3)

View File

@ -12,6 +12,7 @@
from __future__ import print_function
import os
import sys
import tuskarclient.common.formatting as fmt
@ -103,3 +104,28 @@ def do_plan_patch(tuskar, args, outfile=sys.stdout):
for pair
in utils.format_attributes(args.attributes).items()]
return tuskar.plans.patch(args.plan_uuid, attributes)
@utils.arg('plan_uuid',
help="UUID of the Plan whose Templates will be retrieved.")
@utils.arg('-O', '--output-dir', metavar='<OUTPUT DIR>',
required=True,
help='Directory to write template files into. ' +
'It will be created if it does not exist.')
def do_plan_templates(tuskar, args, outfile=sys.stdout):
# check that output directory exists and we can write into it
output_dir = args.output_dir
if not os.path.isdir(output_dir):
os.mkdir(output_dir)
# retrieve templates
templates = tuskar.plans.templates(args.plan_uuid)
# write file for each key-value in templates
print("Following templates has been written:")
for template_name, template_content in templates.items():
filename = os.path.join(output_dir, template_name)
with open(filename, 'w+') as template_file:
template_file.write(template_content)
print(filename)