Remove multiple plan support from plan export command

Multiple plan support has little value while unnecessarily complicating
the command UX. Being able to do one export at a time is enough. Operators
are encouraged to script the command if they wish to achieve the effect
of exporting multiple plans.

Partially implements: blueprint plan-export-command
Change-Id: I99cf5dfde82a8b4b9dfb7b3f61f4f4f1d31b58f7
This commit is contained in:
Ana Krivokapic 2017-04-18 17:14:35 +02:00
parent 41bf090d01
commit d0345f6b69
3 changed files with 46 additions and 40 deletions

View File

@ -74,3 +74,7 @@ class ProfileMatchingError(Exception):
class PlanCreationError(Exception):
"""Plan creation failed"""
class PlanExportError(Exception):
"""Plan export failed"""

View File

@ -387,7 +387,7 @@ class TestOvercloudExportPlan(utils.TestCommand):
autospec=True)
def test_export_plan(self, export_deployment_plan_mock):
parsed_args = self.check_parser(self.cmd, ['test-plan'],
[('plans', ['test-plan'])])
[('plan', 'test-plan')])
export_deployment_plan_mock.return_value = 'http://fake-url.com'
@ -397,21 +397,34 @@ class TestOvercloudExportPlan(utils.TestCommand):
export_deployment_plan_mock.assert_called_once_with(
self.clients, plan='test-plan', queue_name='UUID4')
@mock.patch('os.path.exists')
def test_export_plan_outfile_exists(self, exists_mock):
parsed_args = self.check_parser(self.cmd, ['test-plan'],
[('plan', 'test-plan')])
exists_mock.return_value = True
self.assertRaises(exceptions.PlanExportError,
self.cmd.take_action, parsed_args)
@mock.patch(
'tripleoclient.workflows.plan_management.export_deployment_plan',
autospec=True)
def test_export_multiple_plans(self, export_deployment_plan_mock):
argslist = ['test-plan1', 'test-plan2']
verifylist = [('plans', ['test-plan1', 'test-plan2'])]
parsed_args = self.check_parser(self.cmd, argslist, verifylist)
@mock.patch('os.path.exists')
def test_export_plan_outfile_exists_with_overwrite(
self, exists_mock, export_deployment_plan_mock):
arglist = ['-f', 'test-plan']
verifylist = [
('plan', 'test-plan'),
('force_overwrite', True)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
exists_mock.return_value = True
export_deployment_plan_mock.return_value = 'http://fake-url.com'
with mock.patch('six.moves.builtins.open', mock.mock_open()):
self.cmd.take_action(parsed_args)
expected = [
mock.call(self.clients, plan='test-plan1', queue_name='UUID4'),
mock.call(self.clients, plan='test-plan2', queue_name='UUID4'),
]
self.assertEqual(export_deployment_plan_mock.call_args_list, expected)
export_deployment_plan_mock.assert_called_once_with(
self.clients, plan='test-plan', queue_name='UUID4')

View File

@ -19,7 +19,7 @@ from osc_lib.command import command
from osc_lib.i18n import _
from six.moves.urllib import request
from tripleoclient import exceptions
from tripleoclient import utils
from tripleoclient.workflows import deployment
from tripleoclient.workflows import plan_management
@ -158,42 +158,31 @@ class ExportPlan(command.Command):
def get_parser(self, prog_name):
parser = super(ExportPlan, self).get_parser(prog_name)
parser.add_argument('plans', metavar='<name>', nargs='+',
help=_('Name of the plan(s) to export.'))
parser.add_argument('--output-files', '-o', metavar='<output file>',
nargs='+', default=[],
help=_('Name of the output file(s) for exports. '
'Each will default to "<name>.tar.gz".')
)
parser.add_argument('plan', metavar='<name>',
help=_('Name of the plan to export.'))
parser.add_argument('--output-file', '-o', metavar='<output file>',
help=_('Name of the output file for export. '
'It will default to "<name>.tar.gz".'))
parser.add_argument('--force-overwrite', '-f', action='store_true',
default=False,
help=_('Overwrite output files if they exist.'))
help=_('Overwrite output file if it exists.'))
return parser
def take_action(self, parsed_args):
self.log.debug("take_action(%s)" % parsed_args)
clients = self.app.client_manager
plans = parsed_args.plans
outfiles = parsed_args.output_files
overwrite = parsed_args.force_overwrite
plan = parsed_args.plan
outfile = parsed_args.output_file or '%s.tar.gz' % plan
for i, plan in enumerate(plans):
print("Exporting plan %s..." % plan)
if os.path.exists(outfile) and not parsed_args.force_overwrite:
raise exceptions.PlanExportError(
"File '%s' already exists, not exporting." % outfile)
try:
tarball_name = outfiles[i]
except IndexError:
tarball_name = '%s.tar.gz' % plan
print("Exporting plan %s..." % plan)
if os.path.exists(tarball_name) and not overwrite:
print("File '%s' already exists, not exporting."
% tarball_name)
continue
tempurl = plan_management.export_deployment_plan(
self.app.client_manager, plan=plan, queue_name=str(uuid.uuid4()))
f = request.urlopen(tempurl)
tarball_contents = f.read()
tempurl = plan_management.export_deployment_plan(
clients, plan=plan, queue_name=str(uuid.uuid4()))
f = request.urlopen(tempurl)
tarball_contents = f.read()
with open(tarball_name, 'w') as f:
f.write(tarball_contents)
with open(outfile, 'w') as f:
f.write(tarball_contents)