diff options
| author | Jenkins <jenkins@review.openstack.org> | 2015-08-07 04:56:16 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2015-08-07 04:56:16 +0000 |
| commit | 8affa0d1c331077a35e6de6fffa154a684184bac (patch) | |
| tree | 6409fa0a34d6af5eb7896b44e278076251402d04 /openstackclient/volume | |
| parent | d87403a7bf868d96c6c3185a59ac9bac4ba264fa (diff) | |
| parent | dc6fe04895287647f6bf5a977b398659edfda822 (diff) | |
| download | python-openstackclient-8affa0d1c331077a35e6de6fffa154a684184bac.tar.gz | |
Merge "Add list feature to volume v2"
Diffstat (limited to 'openstackclient/volume')
| -rw-r--r-- | openstackclient/volume/v2/volume.py | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/openstackclient/volume/v2/volume.py b/openstackclient/volume/v2/volume.py index d4536f51..fe4a3ff6 100644 --- a/openstackclient/volume/v2/volume.py +++ b/openstackclient/volume/v2/volume.py @@ -14,9 +14,12 @@ """Volume V2 Volume action implementations""" +import copy import logging +import os from cliff import command +from cliff import lister from cliff import show import six @@ -189,6 +192,112 @@ class DeleteVolume(command.Command): return +class ListVolume(lister.Lister): + """List volumes""" + + log = logging.getLogger(__name__ + '.ListVolume') + + def get_parser(self, prog_name): + parser = super(ListVolume, self).get_parser(prog_name) + parser.add_argument( + '--all-projects', + action='store_true', + default=bool(int(os.environ.get("ALL_PROJECTS", 0))), + help='Include all projects (admin only)', + ) + parser.add_argument( + '--long', + action='store_true', + default=False, + help='List additional fields in output', + ) + parser.add_argument( + '--name', + metavar='<name>', + help='Filter results by name', + ) + parser.add_argument( + '--status', + metavar='<status>', + help='Filter results by status', + ) + return parser + + def take_action(self, parsed_args): + self.log.debug('take_action(%s)', parsed_args) + + volume_client = self.app.client_manager.volume + compute_client = self.app.client_manager.compute + + def _format_attach(attachments): + """Return a formatted string of a volume's attached instances + + :param volume: a volume.attachments field + :rtype: a string of formatted instances + """ + + msg = '' + for attachment in attachments: + server = attachment['id'] + if server in server_cache: + server = server_cache[server].name + device = attachment['device'] + msg += 'Attached to %s on %s ' % (server, device) + return msg + + if parsed_args.long: + columns = [ + 'ID', + 'Name', + 'Status', + 'Size', + 'Volume Type', + 'Bootable', + 'Attachments', + 'Metadata', + ] + column_headers = copy.deepcopy(columns) + column_headers[1] = 'Display Name' + column_headers[4] = 'Type' + column_headers[6] = 'Attached to' + column_headers[7] = 'Properties' + else: + columns = [ + 'ID', + 'Name', + 'Status', + 'Size', + 'Attachments', + ] + column_headers = copy.deepcopy(columns) + column_headers[1] = 'Display Name' + column_headers[4] = 'Attached to' + + # Cache the server list + server_cache = {} + try: + for s in compute_client.servers.list(): + server_cache[s.id] = s + except Exception: + # Just forget it if there's any trouble + pass + + search_opts = { + 'all_projects': parsed_args.all_projects, + 'display_name': parsed_args.name, + 'status': parsed_args.status, + } + + data = volume_client.volumes.list(search_opts=search_opts) + + return (column_headers, + (utils.get_item_properties( + s, columns, + formatters={'Metadata': utils.format_dict, + 'Attachments': _format_attach}, + ) for s in data)) + + class SetVolume(show.ShowOne): """Set volume properties""" |
