|
15 | 15 |
|
16 | 16 | """Volume v1 Backup action implementations""" |
17 | 17 |
|
| 18 | +import copy |
18 | 19 | import logging |
19 | 20 | import six |
20 | 21 |
|
@@ -102,20 +103,55 @@ class ListBackup(lister.Lister): |
102 | 103 |
|
103 | 104 | log = logging.getLogger(__name__ + '.ListBackup') |
104 | 105 |
|
| 106 | + def get_parser(self, prog_name): |
| 107 | + parser = super(ListBackup, self).get_parser(prog_name) |
| 108 | + parser.add_argument( |
| 109 | + '--long', |
| 110 | + action='store_true', |
| 111 | + default=False, |
| 112 | + help='List additional fields in output', |
| 113 | + ) |
| 114 | + return parser |
| 115 | + |
105 | 116 | def take_action(self, parsed_args): |
106 | 117 | self.log.debug('take_action(%s)', parsed_args) |
107 | | - columns = ( |
108 | | - 'ID', |
109 | | - 'Display Name', |
110 | | - 'Display Description', |
111 | | - 'Status', |
112 | | - 'Size' |
113 | | - ) |
| 118 | + |
| 119 | + def _format_volume_id(volume_id): |
| 120 | + """Return a volume name if available |
| 121 | +
|
| 122 | + :param volume_id: a volume ID |
| 123 | + :rtype: either the volume ID or name |
| 124 | + """ |
| 125 | + |
| 126 | + volume = volume_id |
| 127 | + if volume_id in volume_cache.keys(): |
| 128 | + volume = volume_cache[volume_id].display_name |
| 129 | + return volume |
| 130 | + |
| 131 | + if parsed_args.long: |
| 132 | + columns = ['ID', 'Name', 'Description', 'Status', 'Size', |
| 133 | + 'Availability Zone', 'Volume ID', 'Container'] |
| 134 | + column_headers = copy.deepcopy(columns) |
| 135 | + column_headers[6] = 'Volume' |
| 136 | + else: |
| 137 | + columns = ['ID', 'Name', 'Description', 'Status', 'Size'] |
| 138 | + column_headers = columns |
| 139 | + |
| 140 | + # Cache the volume list |
| 141 | + volume_cache = {} |
| 142 | + try: |
| 143 | + for s in self.app.client_manager.volume.volumes.list(): |
| 144 | + volume_cache[s.id] = s |
| 145 | + except Exception: |
| 146 | + # Just forget it if there's any trouble |
| 147 | + pass |
| 148 | + |
114 | 149 | data = self.app.client_manager.volume.backups.list() |
115 | | - return (columns, |
| 150 | + |
| 151 | + return (column_headers, |
116 | 152 | (utils.get_item_properties( |
117 | 153 | s, columns, |
118 | | - formatters={}, |
| 154 | + formatters={'Volume ID': _format_volume_id}, |
119 | 155 | ) for s in data)) |
120 | 156 |
|
121 | 157 |
|
|
0 commit comments