From 985a82eaefcb7f3ffc1a40534eb9777fb8faf441 Mon Sep 17 00:00:00 2001 From: Ruslan Aliev Date: Thu, 1 Dec 2016 21:56:18 +0300 Subject: [PATCH] Fix freezer backup support Since backup_id is UUID in freezer-api, there is need to change some methods: 1. Removed workaround to get backup data and use standart API call get() 2. Fixed backup-list and backup-show output according to new backup structure 3. Added backup-delete feature based on backup_id query Change-Id: I62f312ec3a0a56f5f2e6f8915023b34f87fa42de Closes-Bug: #1646575 Closes-Bug: #1642151 Depends-On: I50ae24b11b58bcea3d2c9f117957300a926cd3ab Signed-off-by: Ruslan Aliev --- freezerclient/shell.py | 1 + freezerclient/v1/backups.py | 64 ++++++++++++++++++++----------------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/freezerclient/shell.py b/freezerclient/shell.py index 7a4a98f..fdb63ad 100644 --- a/freezerclient/shell.py +++ b/freezerclient/shell.py @@ -50,6 +50,7 @@ class FreezerCommandManager(commandmanager.CommandManager): 'client-delete': clients.ClientDelete, 'backup-list': backups.BackupList, 'backup-show': backups.BackupShow, + 'backup-delete': backups.BackupDelete, 'session-list': sessions.SessionList, 'session-show': sessions.SessionShow, 'session-create': sessions.SessionCreate, diff --git a/freezerclient/v1/backups.py b/freezerclient/v1/backups.py index 1742fab..7ff4b50 100644 --- a/freezerclient/v1/backups.py +++ b/freezerclient/v1/backups.py @@ -16,6 +16,7 @@ import datetime import logging import pprint +from cliff import command from cliff import lister from cliff import show @@ -30,33 +31,21 @@ class BackupShow(show.ShowOne): """Show the metadata of a single backup""" def get_parser(self, prog_name): parser = super(BackupShow, self).get_parser(prog_name) - parser.add_argument(dest='backup_uuid', - help='UUID of the backup') + parser.add_argument(dest='backup_id', + help='ID of the backup') return parser def take_action(self, parsed_args): - # due to the fact that a backup_id is composed of several strings - # some of them may include a slash "/" so it will never find the - # correct backup, so the workaround for this version is to use the - # backup_uuid as a filter for the search. this won't work when the - # user wants to delete a backup, but that functionality is yet to be - # provided by the api. - search = {"match": [{"backup_uuid": parsed_args.backup_uuid}, ], } - backup = self.app.client.backups.list(search=search) - + backup = self.app.client.backups.get(parsed_args.backup_id) if not backup: raise exceptions.ApiClientException('Backup not found') - backup = backup[0] - column = ( 'Backup ID', - 'Backup UUID', 'Metadata' ) data = ( backup.get('backup_id'), - backup.get('backup_uuid'), pprint.pformat(backup.get('backup_metadata')) ) return column, data @@ -96,24 +85,39 @@ class BackupList(lister.Lister): offset=parsed_args.offset, search=search) - columns = ('Backup UUID', 'Hostname', 'Path', 'Created at', 'Level') + columns = ('Backup ID', 'Hostname', 'Path', 'Created at', 'Level') # Print empty table if no backups found if not backups: backups = [{}] - data = ((b.get('backup_uuid', ''), - b.get('backup_metadata', {}).get('hostname', ''), - b.get('backup_metadata', {}).get('path_to_backup', ''), - b.get('backup_metadata', {}).get('time_stamp', ''), - b.get('backup_metadata', {}).get('curr_backup_level', '') - ) for b in backups) - else: - data = ((b.get('backup_uuid'), - b.get('backup_metadata', {}).get('hostname'), - b.get('backup_metadata', {}).get('path_to_backup'), - datetime.datetime.fromtimestamp( - int(b.get('backup_metadata', {}).get('time_stamp'))), - b.get('backup_metadata', {}).get('curr_backup_level') - ) for b in backups) + + data = ((b.get('backup_id', ''), + b.get('backup_metadata', {}).get('hostname', ''), + b.get('backup_metadata', {}).get('path_to_backup', ''), + datetime.datetime.fromtimestamp( + int(b.get('backup_metadata', {}).get( + 'time_stamp', ''))) if b.get( + 'backup_metadata') else '', + b.get('backup_metadata', {}).get('curr_backup_level', '') + ) for b in backups) return columns, data + + +class BackupDelete(command.Command): + """Delete a backup from the api""" + def get_parser(self, prog_name): + parser = super(BackupDelete, self).get_parser(prog_name) + parser.add_argument(dest='backup_id', + help='ID of the backup') + return parser + + def take_action(self, parsed_args): + # Need to check that backup exists + backup = self.app.client.backups.get(parsed_args.backup_id) + if not backup: + logging.info("Unable to delete specified backup.") + raise exceptions.ApiClientException('Backup not found') + + self.app.client.backups.delete(parsed_args.backup_id) + logging.info('Backup {0} deleted'.format(parsed_args.backup_id))