Skip to content

Commit 95bf187

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add metadata support for volume"
2 parents e11a703 + 04af8fa commit 95bf187

2 files changed

Lines changed: 82 additions & 7 deletions

File tree

openstackclient/volume/v1/volume.py

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ def get_parser(self, prog_name):
7676
help='Availability Zone to use',
7777
)
7878
parser.add_argument(
79-
'--metadata',
80-
metavar='<metadata>',
79+
'--meta-data',
80+
metavar='<key=value>',
8181
help='Optional metadata to set on volume creation',
8282
)
8383
parser.add_argument(
@@ -97,6 +97,11 @@ def take_action(self, parsed_args):
9797
self.log.debug('take_action(%s)' % parsed_args)
9898

9999
volume_client = self.app.client_manager.volume
100+
101+
meta = None
102+
if parsed_args.meta_data:
103+
meta = dict(v.split('=') for v in parsed_args.meta_data.split(' '))
104+
100105
volume = volume_client.volumes.create(
101106
parsed_args.size,
102107
parsed_args.snapshot_id,
@@ -107,7 +112,7 @@ def take_action(self, parsed_args):
107112
parsed_args.user_id,
108113
parsed_args.project_id,
109114
parsed_args.availability_zone,
110-
parsed_args.metadata,
115+
meta,
111116
parsed_args.image_ref
112117
)
113118

@@ -172,12 +177,22 @@ def get_parser(self, prog_name):
172177
default=False,
173178
help='Display information from all tenants (Admin-only)',
174179
)
180+
parser.add_argument(
181+
'--long',
182+
action='store_true',
183+
default=False,
184+
help='Display meta-data',
185+
)
175186
return parser
176187

177188
def take_action(self, parsed_args):
178189
self.log.debug('take_action(%s)' % parsed_args)
190+
179191
columns = ('ID', 'Status', 'Display Name', 'Size',
180192
'Volume Type', 'Bootable', 'Attached to')
193+
if parsed_args.long:
194+
columns = ('ID', 'Status', 'Display Name', 'Size',
195+
'Volume Type', 'Bootable', 'Attached to', 'Meta-data')
181196

182197
search_opts = {
183198
'all_tenants': parsed_args.all_tenants,
@@ -191,7 +206,7 @@ def take_action(self, parsed_args):
191206
return (columns,
192207
(utils.get_item_properties(
193208
s, columns,
194-
formatters={},
209+
formatters={'Meta-data': _format_meta_data},
195210
) for s in data))
196211

197212

@@ -206,7 +221,7 @@ def get_parser(self, prog_name):
206221
parser.add_argument(
207222
'volume',
208223
metavar='<volume>',
209-
help='ID of volume to change')
224+
help='Name or ID of volume to change')
210225
parser.add_argument(
211226
'--name',
212227
metavar='<new-volume-name>',
@@ -215,19 +230,29 @@ def get_parser(self, prog_name):
215230
'--description',
216231
metavar='<volume-description>',
217232
help='New volume description')
233+
parser.add_argument(
234+
'--meta-data',
235+
metavar='<key=value>',
236+
help='meta-data to add to volume')
218237
return parser
219238

220239
def take_action(self, parsed_args):
221240
self.log.debug('take_action(%s)' % parsed_args)
222241
volume_client = self.app.client_manager.volume
223242
volume = utils.find_resource(volume_client.volumes, parsed_args.volume)
243+
244+
meta = None
245+
if parsed_args.meta_data:
246+
meta = dict(v.split('=') for v in parsed_args.meta_data.split(' '))
247+
volume_client.volumes.set_metadata(volume.id, meta)
248+
224249
kwargs = {}
225250
if parsed_args.name:
226251
kwargs['display_name'] = parsed_args.name
227252
if parsed_args.description:
228253
kwargs['display_description'] = parsed_args.description
229254

230-
if not kwargs:
255+
if not kwargs and not meta:
231256
sys.stdout.write("Volume not updated, no arguments present \n")
232257
return
233258
volume_client.volumes.update(volume.id, **kwargs)
@@ -245,7 +270,7 @@ def get_parser(self, prog_name):
245270
parser.add_argument(
246271
'volume',
247272
metavar='<volume>',
248-
help='ID of volume to display')
273+
help='Name or ID of volume to display')
249274
return parser
250275

251276
def take_action(self, parsed_args):
@@ -254,3 +279,52 @@ def take_action(self, parsed_args):
254279
volume = utils.find_resource(volume_client.volumes, parsed_args.volume)
255280

256281
return zip(*sorted(volume._info.iteritems()))
282+
283+
284+
class UnsetVolume(command.Command):
285+
"""Unset volume command"""
286+
287+
api = 'volume'
288+
log = logging.getLogger(__name__ + '.UnsetVolume')
289+
290+
def get_parser(self, prog_name):
291+
parser = super(UnsetVolume, self).get_parser(prog_name)
292+
parser.add_argument(
293+
'volume',
294+
metavar='<volume>',
295+
help='Name or ID of volume to change')
296+
parser.add_argument(
297+
'--meta-data',
298+
metavar='<key>',
299+
help='meta-data to remove from volume (key only)')
300+
return parser
301+
302+
def take_action(self, parsed_args):
303+
self.log.debug('take_action(%s)' % parsed_args)
304+
volume_client = self.app.client_manager.volume
305+
volume = utils.find_resource(
306+
volume_client.volumes, parsed_args.volume)
307+
308+
if not parsed_args.meta_data:
309+
sys.stdout.write("Volume not updated, no arguments present \n")
310+
return
311+
312+
key_list = []
313+
key_list.append(parsed_args.meta_data)
314+
volume_client.volumes.delete_metadata(volume.id, key_list)
315+
316+
return
317+
318+
319+
def _format_meta_data(volume):
320+
"""Return a string containing the key value pairs
321+
322+
:param server: a single volume resource
323+
:rtype: a string formatted to key=value
324+
"""
325+
326+
keys = volume.metadata
327+
output = ""
328+
for s in keys:
329+
output = output + s + "=" + keys[s] + "; "
330+
return output

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ def read(fname):
231231
'list_volume=openstackclient.volume.v1.volume:ListVolume',
232232
'set_volume=openstackclient.volume.v1.volume:SetVolume',
233233
'show_volume=openstackclient.volume.v1.volume:ShowVolume',
234+
'unset_volume=openstackclient.volume.v1.volume:UnsetVolume',
234235

235236
'create_volume-type='
236237
'openstackclient.volume.v1.type:CreateVolumeType',

0 commit comments

Comments
 (0)