Skip to content

Commit 457f4c8

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Properly format 'attached to' column list when listing volumes"
2 parents 8593b0a + 71d9c8b commit 457f4c8

1 file changed

Lines changed: 35 additions & 6 deletions

File tree

‎openstackclient/volume/v1/volume.py‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,25 @@ def get_parser(self, prog_name):
223223
def take_action(self, parsed_args):
224224
self.log.debug('take_action(%s)', parsed_args)
225225

226+
volume_client = self.app.client_manager.volume
227+
compute_client = self.app.client_manager.compute
228+
229+
def _format_attach(attachments):
230+
"""Return a formatted string of a volume's attached instances
231+
232+
:param volume: a volume.attachments field
233+
:rtype: a string of formatted instances
234+
"""
235+
236+
msg = ''
237+
for attachment in attachments:
238+
server = attachment['server_id']
239+
if server in server_cache.keys():
240+
server = server_cache[server].name
241+
device = attachment['device']
242+
msg += 'Attached to %s on %s ' % (server, device)
243+
return msg
244+
226245
if parsed_args.long:
227246
columns = (
228247
'ID',
@@ -231,7 +250,7 @@ def take_action(self, parsed_args):
231250
'Size',
232251
'Volume Type',
233252
'Bootable',
234-
'Attached to',
253+
'Attachments',
235254
'Metadata',
236255
)
237256
column_headers = (
@@ -241,7 +260,7 @@ def take_action(self, parsed_args):
241260
'Size',
242261
'Type',
243262
'Bootable',
244-
'Attached',
263+
'Attached to',
245264
'Properties',
246265
)
247266
else:
@@ -250,28 +269,38 @@ def take_action(self, parsed_args):
250269
'Display Name',
251270
'Status',
252271
'Size',
253-
'Attached to',
272+
'Attachments',
254273
)
255274
column_headers = (
256275
'ID',
257276
'Display Name',
258277
'Status',
259278
'Size',
260-
'Attached',
279+
'Attached to',
261280
)
281+
282+
# Cache the server list
283+
server_cache = {}
284+
try:
285+
for s in compute_client.servers.list():
286+
server_cache[s.id] = s
287+
except Exception:
288+
# Just forget it if there's any trouble
289+
pass
290+
262291
search_opts = {
263292
'all_tenants': parsed_args.all_projects,
264293
'display_name': parsed_args.name,
265294
'status': parsed_args.status,
266295
}
267296

268-
volume_client = self.app.client_manager.volume
269297
data = volume_client.volumes.list(search_opts=search_opts)
270298

271299
return (column_headers,
272300
(utils.get_item_properties(
273301
s, columns,
274-
formatters={'Metadata': utils.format_dict},
302+
formatters={'Metadata': utils.format_dict,
303+
'Attachments': _format_attach},
275304
) for s in data))
276305

277306

0 commit comments

Comments
 (0)