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
This commit is contained in:
Rajat Dhasmana 2023-12-06 17:39:03 +05:30
parent 2642b070db
commit c657047d7e

View File

@ -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'}
)