diff options
| author | Huanxuan Ao <huanxuan.ao@easystack.cn> | 2016-09-22 17:10:32 +0800 |
|---|---|---|
| committer | Stephen Finucane <sfinucan@redhat.com> | 2022-03-10 12:04:11 +0000 |
| commit | de4a69a29ff4657d0c3cd95ca9f35ff24f653b5f (patch) | |
| tree | bf53a4f91a0afeddf809297d6f54effe94a6cfa0 /openstackclient/volume | |
| parent | 8c975ba09790f9fe57c32293fb4320bf5c56e012 (diff) | |
| download | python-openstackclient-de4a69a29ff4657d0c3cd95ca9f35ff24f653b5f.tar.gz | |
Refactor "volume backup restore" command
Make the positional argument "volume" optional and add a "--force"
option (volume v2 only) to the "volume backup restore" command.
Closes-Bug: #1597189
Change-Id: If944e10158bd18e8331be63e96187a23e23095d7
Diffstat (limited to 'openstackclient/volume')
| -rw-r--r-- | openstackclient/volume/v1/volume_backup.py | 19 | ||||
| -rw-r--r-- | openstackclient/volume/v2/volume_backup.py | 44 |
2 files changed, 50 insertions, 13 deletions
diff --git a/openstackclient/volume/v1/volume_backup.py b/openstackclient/volume/v1/volume_backup.py index 1a83a3c0..790cb463 100644 --- a/openstackclient/volume/v1/volume_backup.py +++ b/openstackclient/volume/v1/volume_backup.py @@ -231,18 +231,23 @@ class RestoreVolumeBackup(command.Command): parser.add_argument( 'volume', metavar='<volume>', - help=_('Volume to restore to (name or ID)') + nargs='?', + help=_('Volume to restore to (name or ID) (default to None)') ) return parser def take_action(self, parsed_args): volume_client = self.app.client_manager.volume - backup = utils.find_resource(volume_client.backups, - parsed_args.backup) - destination_volume = utils.find_resource(volume_client.volumes, - parsed_args.volume) - return volume_client.restores.restore(backup.id, - destination_volume.id) + backup = utils.find_resource( + volume_client.backups, parsed_args.backup, + ) + volume_id = None + if parsed_args.volume is not None: + volume_id = utils.find_resource( + volume_client.volumes, + parsed_args.volume, + ).id + return volume_client.restores.restore(backup.id, volume_id) class ShowVolumeBackup(command.ShowOne): diff --git a/openstackclient/volume/v2/volume_backup.py b/openstackclient/volume/v2/volume_backup.py index 96b22a68..0c0f3fa4 100644 --- a/openstackclient/volume/v2/volume_backup.py +++ b/openstackclient/volume/v2/volume_backup.py @@ -355,18 +355,50 @@ class RestoreVolumeBackup(command.ShowOne): parser.add_argument( "volume", metavar="<volume>", - help=_("Volume to restore to (name or ID)") + nargs="?", + help=_( + "Volume to restore to " + "(name or ID for existing volume, name only for new volume) " + "(default to None)" + ) + ) + parser.add_argument( + "--force", + action="store_true", + help=_( + "Restore the backup to an existing volume " + "(default to False)" + ) ) return parser def take_action(self, parsed_args): volume_client = self.app.client_manager.volume + backup = utils.find_resource(volume_client.backups, parsed_args.backup) - destination_volume = utils.find_resource(volume_client.volumes, - parsed_args.volume) - backup = volume_client.restores.restore(backup.id, - destination_volume.id) - return zip(*sorted(backup._info.items())) + + volume_name = None + volume_id = None + try: + volume_id = utils.find_resource( + volume_client.volumes, + parsed_args.volume, + ).id + except Exception: + volume_name = parsed_args.volume + else: + # If we didn't fail, the volume must already exist. We only allow + # this to work if the user forced things + if not parsed_args.force: + msg = _( + "Volume '%s' already exists; if you want to restore the " + "backup to it you need to specify the '--force' option" + ) % parsed_args.volume + raise exceptions.CommandError(msg) + + return volume_client.restores.restore( + backup.id, volume_id, volume_name, + ) class SetVolumeBackup(command.Command): |
