LogoopenSUSE Build Service > Projects
Sign Up | Log In

View File add-volume-list.diff of Package python-openstackclient (Project home:garloff:OTC:python-save)

Index: python-openstackclient-1.5.0/openstackclient/volume/v2/volume.py
===================================================================
--- python-openstackclient-1.5.0.orig/openstackclient/volume/v2/volume.py
+++ python-openstackclient-1.5.0/openstackclient/volume/v2/volume.py
@@ -13,11 +13,14 @@
 #
 
 """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
 
 from openstackclient.common import utils
@@ -80,4 +83,112 @@ class ShowVolume(show.ShowOne):
 
         # Remove key links from being displayed
         volume._info.pop("links", None)
         return zip(*sorted(six.iteritems(volume._info)))
+
+
+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['server_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))
+
+
Index: python-openstackclient-1.5.0/python_openstackclient.egg-info/entry_points.txt
===================================================================
--- python-openstackclient-1.5.0.orig/python_openstackclient.egg-info/entry_points.txt
+++ python-openstackclient-1.5.0/python_openstackclient.egg-info/entry_points.txt
@@ -304,5 +304,6 @@ snapshot_unset = openstackclient.volume.
 volume_delete = openstackclient.volume.v2.volume:DeleteVolume
 volume_show = openstackclient.volume.v2.volume:ShowVolume
 volume_type_delete = openstackclient.volume.v2.volume_type:DeleteVolumeType
 volume_type_show = openstackclient.volume.v2.volume_type:ShowVolumeType
+volume_list = openstackclient.volume.v2.volume:ListVolume
 
Index: python-openstackclient-1.5.0/setup.cfg
===================================================================
--- python-openstackclient-1.5.0.orig/setup.cfg
+++ python-openstackclient-1.5.0/setup.cfg
@@ -317,8 +317,9 @@ openstack.volume.v2 =
 	volume_delete = openstackclient.volume.v2.volume:DeleteVolume
 	volume_show = openstackclient.volume.v2.volume:ShowVolume
 	volume_type_delete = openstackclient.volume.v2.volume_type:DeleteVolumeType
 	volume_type_show = openstackclient.volume.v2.volume_type:ShowVolumeType
+	volume_list = openstackclient.volume.v2.volume:ListVolume
 
 [build_sphinx]
 source-dir = doc/source
 build-dir = doc/build