if vol.get('volume_metadata'):
metadata = vol.get('volume_metadata')
d['metadata'] = dict((item['key'], item['value']) for item in metadata)
+ # avoid circular ref when vol is a Volume instance
+ elif vol.get('metadata') and isinstance(vol.get('metadata'), dict):
+ d['metadata'] = vol['metadata']
else:
d['metadata'] = {}
valid_update_keys = (
'display_name',
'display_description',
+ 'metadata',
)
for key in valid_update_keys:
metadata = values.get('metadata')
if metadata is not None:
volume_metadata_update(context,
- volume_id,
- values.pop('metadata'),
- delete=True)
+ volume_id,
+ values.pop('metadata'),
+ delete=True)
with session.begin():
volume_ref = volume_get(context, volume_id, session=session)
volume_ref.update(values)
meta_ref = None
# Now update all existing items with new values, or create new meta objects
- for meta_key, meta_value in metadata.iteritems():
+ for meta_key, meta_value in metadata.items():
# update the value whether it exists or not
item = {"value": meta_value}
try:
meta_ref = volume_metadata_get_item(context, volume_id,
- meta_key, session)
- except exception.VolumeMetadataNotFound, e:
+ meta_key, session)
+ except exception.VolumeMetadataNotFound as e:
meta_ref = models.VolumeMetadata()
item.update({"key": meta_key, "volume_id": volume_id})
}}
self.assertEquals(res_dict, expected)
+ def test_volume_update_metadata(self):
+ self.stubs.Set(volume_api.API, "update", fakes.stub_volume_update)
+ updates = {
+ "metadata": {"qos_max_iops": 2000}
+ }
+ body = {"volume": updates}
+ req = fakes.HTTPRequest.blank('/v1/volumes/1')
+ res_dict = self.controller.update(req, '1', body)
+ expected = {'volume': {
+ 'status': 'fakestatus',
+ 'display_description': 'displaydesc',
+ 'availability_zone': 'fakeaz',
+ 'display_name': 'displayname',
+ 'attachments': [{
+ 'id': '1',
+ 'volume_id': '1',
+ 'server_id': 'fakeuuid',
+ 'device': '/',
+ }],
+ 'volume_type': 'vol_type_name',
+ 'snapshot_id': None,
+ 'metadata': {"qos_max_iops": 2000},
+ 'id': '1',
+ 'created_at': datetime.datetime(1, 1, 1, 1, 1, 1),
+ 'size': 1,
+ }}
+ self.assertEquals(res_dict, expected)
+
def test_update_empty_body(self):
body = {}
req = fakes.HTTPRequest.blank('/v1/volumes/1')