Merge "Add access to update volume metadata."

This commit is contained in:
Jenkins 2013-01-09 07:57:09 +00:00 committed by Gerrit Code Review
commit 61e2a4237f
5 changed files with 96 additions and 2 deletions

View File

@ -270,6 +270,31 @@ def do_rename(cs, args):
_find_volume(cs, args.volume).update(**kwargs)
@utils.arg('volume',
metavar='<volume>',
help='ID of the volume to update metadata on.')
@utils.arg('action',
metavar='<action>',
choices=['set', 'unset'],
help="Actions: 'set' or 'unset'")
@utils.arg('metadata',
metavar='<key=value>',
nargs='+',
action='append',
default=[],
help='Metadata to set/unset (only key is necessary on unset)')
@utils.service_type('volume')
def do_metadata(cs, args):
"""Set or Delete metadata on a volume."""
volume = _find_volume(cs, args.volume)
metadata = _extract_metadata(args)
if args.action == 'set':
cs.volumes.set_metadata(volume, metadata)
elif args.action == 'unset':
cs.volumes.delete_metadata(volume, metadata.keys())
@utils.arg(
'--all-tenants',
dest='all_tenants',

View File

@ -95,6 +95,15 @@ class Volume(base.Resource):
"""
return self.manager.terminate_connection(self, connector)
def set_metadata(self, volume, metadata):
"""
Set or Append metadata to a volume.
:param type : The :class: `Volume` to set metadata on
:param metadata: A dict of key/value pairs to set
"""
return self.manager.set_metadata(self, metadata)
class VolumeManager(base.ManagerWithFind):
"""
@ -286,3 +295,24 @@ class VolumeManager(base.ManagerWithFind):
"""
self._action('os-terminate_connection', volume,
{'connector': connector})
def set_metadata(self, volume, metadata):
"""
Update/Set a volumes metadata.
:param volume: The :class:`Volume`.
:param metadata: A list of keys to be set.
"""
body = {'metadata': metadata}
return self._create("/volumes/%s/metadata" % base.getid(volume),
body, "metadata")
def delete_metadata(self, volume, keys):
"""
Delete specified keys from volumes metadata.
:param volume: The :class:`Volume`.
:param metadata: A list of keys to be removed.
"""
for k in keys:
self._delete("/volumes/%s/metadata/%s" % (base.getid(volume), k))

View File

@ -268,6 +268,21 @@ class FakeHTTPClient(base_client.HTTPClient):
def delete_types_1(self, **kw):
return (202, {}, None)
#
# Set/Unset metadata
#
def delete_volumes_1234_metadata_test_key(self, **kw):
return (204, {}, None)
def delete_volumes_1234_metadata_key1(self, **kw):
return (204, {}, None)
def delete_volumes_1234_metadata_key2(self, **kw):
return (204, {}, None)
def post_volumes_1234_metadata(self, **kw):
return (204, {}, {'metadata': {'test_key': 'test_value'}})
#
# List all extensions
#

View File

@ -146,3 +146,18 @@ class ShellTest(utils.TestCase):
# noop, the only all will be the lookup
self.run_command('snapshot-rename 1234')
self.assert_called('GET', '/snapshots/1234')
def test_set_metadata_set(self):
self.run_command('metadata 1234 set key1=val1 key2=val2')
self.assert_called('POST', '/volumes/1234/metadata',
{'metadata': {'key1': 'val1', 'key2': 'val2'}})
def test_set_metadata_delete_dict(self):
self.run_command('metadata 1234 unset key1=val1 key2=val2')
self.assert_called('DELETE', '/volumes/1234/metadata/key1')
self.assert_called('DELETE', '/volumes/1234/metadata/key2', pos=-2)
def test_set_metadata_delete_keys(self):
self.run_command('metadata 1234 unset key1 key2')
self.assert_called('DELETE', '/volumes/1234/metadata/key1')
self.assert_called('DELETE', '/volumes/1234/metadata/key2', pos=-2)

View File

@ -1,4 +1,3 @@
from cinderclient.v1 import volumes
from tests import utils
from tests.v1 import fakes
@ -18,7 +17,7 @@ class VolumesTest(utils.TestCase):
cs.assert_called('DELETE', '/volumes/1234')
def test_create_keypair(self):
kp = cs.volumes.create(1)
cs.volumes.create(1)
cs.assert_called('POST', '/volumes')
def test_attach(self):
@ -60,3 +59,13 @@ class VolumesTest(utils.TestCase):
v = cs.volumes.get('1234')
cs.volumes.terminate_connection(v, {})
cs.assert_called('POST', '/volumes/1234/action')
def test_set_metadata(self):
cs.volumes.set_metadata(1234, {'k1': 'v1'})
cs.assert_called('POST', '/volumes/1234/metadata',
{'metadata': {'k1': 'v1'}})
def test_delete_metadata(self):
keys = ['key1']
cs.volumes.delete_metadata(1234, keys)
cs.assert_called('DELETE', '/volumes/1234/metadata/key1')