Raise exception in command "plugin configs get"

In this patch, there are three changes.
1. Raise Exception if the config file already exists.
2. Avoid overriding the file of different version of a plugin.
3. stdout message should end with '\n' to start a newline.
4. execute remote API call only when file not exists.

Change-Id: I0c9eb2cb7fd1cafe4bea05c73811d6dea6b3e9a8
Closes-Bug: 1625990
This commit is contained in:
Shu Yingya
2016-09-25 23:58:38 +08:00
parent 559fa06ddc
commit 82cabbd3c9
2 changed files with 16 additions and 11 deletions

View File

@@ -154,7 +154,8 @@ class GetPluginConfigs(command.Command):
parser.add_argument(
'--file',
metavar="<file>",
help='Destination file (defaults to plugin name)',
help="Destination file (defaults to a combination of "
"plugin name and plugin version)",
)
return parser
@@ -163,21 +164,25 @@ class GetPluginConfigs(command.Command):
client = self.app.client_manager.data_processing
if not parsed_args.file:
parsed_args.file = parsed_args.plugin
data = client.plugins.get_version_details(
parsed_args.plugin, parsed_args.plugin_version).to_dict()
parsed_args.file = (parsed_args.plugin + '-' +
parsed_args.plugin_version)
if path.exists(parsed_args.file):
self.log.error('File "%s" already exists. Chose another one with '
'--file argument.' % parsed_args.file)
msg = ('File "%s" already exists. Choose another one with '
'--file argument.' % parsed_args.file)
raise exceptions.CommandError(msg)
else:
data = client.plugins.get_version_details(
parsed_args.plugin, parsed_args.plugin_version).to_dict()
with open(parsed_args.file, 'w') as f:
jsonutils.dump(data, f, indent=4)
sys.stdout.write(
'"%(plugin)s" plugin configs was saved in "%(file)s"'
'file' % {'plugin': parsed_args.plugin,
'file': parsed_args.file})
'"%(plugin)s" plugin "%(version)s" version configs '
'was saved in "%(file)s" file\n' % {
'plugin': parsed_args.plugin,
'version': parsed_args.plugin_version,
'file': parsed_args.file})
class UpdatePlugin(command.ShowOne):

View File

@@ -171,7 +171,7 @@ class TestGetPluginConfigs(TestPlugins):
self.assertEqual(PLUGIN_INFO, args_to_dump[0])
# Check that data will be saved to the right file
self.assertEqual('fake', m_open.call_args[0][0])
self.assertEqual('fake-0.1', m_open.call_args[0][0])
@mock.patch('oslo_serialization.jsonutils.dump')
def test_get_plugin_configs_specified_file(self, p_dump):