Shangzhong Zhu 0968fdf606 Achieve a consistent style output
When the list is empty, the different style output is being used for
the freezer action-list, backup-list, job-list, session-list and
client-list.

Achieve a consistent style output for these commands in this patch.

Change-Id: I17f65fc3a836357f2c4857b4e5f7de7c8e8fb116
Closes-Bug: #1642101
2016-11-19 15:07:54 +08:00

121 lines
4.0 KiB
Python

# (c) Copyright 2014-2016 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import logging
import pprint
from cliff import lister
from cliff import show
from freezerclient import exceptions
from freezerclient import utils
logging = logging.getLogger(__name__)
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')
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)
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
class BackupList(lister.Lister):
"""List all backups for your user"""
def get_parser(self, prog_name):
parser = super(BackupList, self).get_parser(prog_name)
parser.add_argument(
'--limit',
dest='limit',
default=100,
help='Specify a limit for search query',
)
parser.add_argument(
'--offset',
dest='offset',
default=0,
help='',
)
parser.add_argument(
'--search',
dest='search',
default='',
help='Define a filter for the query',
)
return parser
def take_action(self, parsed_args):
search = utils.prepare_search(parsed_args.search)
backups = self.app.client.backups.list(limit=parsed_args.limit,
offset=parsed_args.offset,
search=search)
columns = ('Backup UUID', '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)
return columns, data