From c657047d7e57fc651c33ee11021bb7b94f148551 Mon Sep 17 00:00:00 2001 From: Rajat Dhasmana Date: Wed, 6 Dec 2023 17:39:03 +0530 Subject: [PATCH] volume list: Don't call nova if no volume is attached Currently 'openstack volume list' calls nova to resolve server UUIDs to server names. This is not required if: 1. no volume is attached to an instance 2. no volume exists in deployment This patch fixes this by checking volume statuses and, if any volume has status 'in-use', we will call nova to resolve server names. Note that we don't check for 'reserved', 'attaching', 'detaching' states since those are transition states and doesn't guarantee that the volume is actually attached to the instance. Change-Id: Ic4d89db69244d3fba44d4b69c79b3e7632ee3d53 --- openstackclient/volume/v2/volume.py | 35 ++++++++++++++++++----------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/openstackclient/volume/v2/volume.py b/openstackclient/volume/v2/volume.py index 4127e23d49..a7bdb782fd 100644 --- a/openstackclient/volume/v2/volume.py +++ b/openstackclient/volume/v2/volume.py @@ -490,19 +490,6 @@ class ListVolume(command.Lister): column_headers = copy.deepcopy(columns) column_headers[4] = 'Attached to' - # Cache the server list - server_cache = {} - try: - compute_client = self.app.client_manager.compute - for s in compute_client.servers.list(): - server_cache[s.id] = s - except Exception: - # Just forget it if there's any trouble - pass - AttachmentsColumnWithCache = functools.partial( - AttachmentsColumn, server_cache=server_cache - ) - project_id = None if parsed_args.project: project_id = identity_common.find_project( @@ -533,6 +520,28 @@ class ListVolume(command.Lister): marker=parsed_args.marker, limit=parsed_args.limit, ) + + do_server_list = False + + for vol in data: + if vol.status == 'in-use': + do_server_list = True + break + + # Cache the server list + server_cache = {} + if do_server_list: + try: + compute_client = self.app.client_manager.compute + for s in compute_client.servers.list(): + server_cache[s.id] = s + except Exception: + # Just forget it if there's any trouble + pass + AttachmentsColumnWithCache = functools.partial( + AttachmentsColumn, server_cache=server_cache + ) + column_headers = utils.backward_compat_col_lister( column_headers, parsed_args.columns, {'Display Name': 'Name'} )