Skip to content

Commit 71d9c8b

Browse files
author
Steve Martinelli
committed
Properly format 'attached to' column list when listing volumes
Previously, no data was being returned for the 'attached to' field when listing volumes. Dig into the the returned array to format the column. Change-Id: Iebd79e5ddcb4a335703d9b2675aa7128995de918 Closes-Bug: #1404931
1 parent 52d2235 commit 71d9c8b

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
@@ -221,6 +221,25 @@ def get_parser(self, prog_name):
221221
def take_action(self, parsed_args):
222222
self.log.debug('take_action(%s)', parsed_args)
223223

224+
volume_client = self.app.client_manager.volume
225+
compute_client = self.app.client_manager.compute
226+
227+
def _format_attach(attachments):
228+
"""Return a formatted string of a volume's attached instances
229+
230+
:param volume: a volume.attachments field
231+
:rtype: a string of formatted instances
232+
"""
233+
234+
msg = ''
235+
for attachment in attachments:
236+
server = attachment['server_id']
237+
if server in server_cache.keys():
238+
server = server_cache[server].name
239+
device = attachment['device']
240+
msg += 'Attached to %s on %s ' % (server, device)
241+
return msg
242+
224243
if parsed_args.long:
225244
columns = (
226245
'ID',
@@ -229,7 +248,7 @@ def take_action(self, parsed_args):
229248
'Size',
230249
'Volume Type',
231250
'Bootable',
232-
'Attached to',
251+
'Attachments',
233252
'Metadata',
234253
)
235254
column_headers = (
@@ -239,7 +258,7 @@ def take_action(self, parsed_args):
239258
'Size',
240259
'Type',
241260
'Bootable',
242-
'Attached',
261+
'Attached to',
243262
'Properties',
244263
)
245264
else:
@@ -248,28 +267,38 @@ def take_action(self, parsed_args):
248267
'Display Name',
249268
'Status',
250269
'Size',
251-
'Attached to',
270+
'Attachments',
252271
)
253272
column_headers = (
254273
'ID',
255274
'Display Name',
256275
'Status',
257276
'Size',
258-
'Attached',
277+
'Attached to',
259278
)
279+
280+
# Cache the server list
281+
server_cache = {}
282+
try:
283+
for s in compute_client.servers.list():
284+
server_cache[s.id] = s
285+
except Exception:
286+
# Just forget it if there's any trouble
287+
pass
288+
260289
search_opts = {
261290
'all_tenants': parsed_args.all_projects,
262291
'display_name': parsed_args.name,
263292
'status': parsed_args.status,
264293
}
265294

266-
volume_client = self.app.client_manager.volume
267295
data = volume_client.volumes.list(search_opts=search_opts)
268296

269297
return (column_headers,
270298
(utils.get_item_properties(
271299
s, columns,
272-
formatters={'Metadata': utils.format_dict},
300+
formatters={'Metadata': utils.format_dict,
301+
'Attachments': _format_attach},
273302
) for s in data))
274303

275304

0 commit comments

Comments
 (0)