Merge "cinder: add attachment_update method"

This commit is contained in:
Jenkins 2017-05-30 15:04:32 +00:00 committed by Gerrit Code Review
commit 717f110c44
2 changed files with 67 additions and 0 deletions

View File

@ -315,6 +315,49 @@ class CinderApiTestCase(test.NoDBTestCase):
self.assertRaises(exception.VolumeNotFound, self.api.attachment_create,
self.ctx, uuids.volume_id, uuids.instance_id)
@mock.patch('nova.volume.cinder.cinderclient')
def test_attachment_update(self, mock_cinderclient):
"""Tests the happy path for updating a volume attachment."""
values = {
'id': uuids.attachment_id,
'status': 'attached',
'instance': uuids.instance_id,
'volume_id': uuids.volume_id,
'attached_at': timeutils.utcnow(),
'detached_at': None,
'attach_mode': 'rw',
'connection_info': {'data': {'foo': 'bar'}}
}
fake_attachment = mock.Mock(
autospec='cinderclient.v3.attachments.VolumeAttachment', **values)
mock_cinderclient.return_value.attachments.update.return_value = (
fake_attachment)
result = self.api.attachment_update(
self.ctx, uuids.attachment_id, connector={'host': 'fake-host'})
self.assertEqual(fake_attachment, result)
@mock.patch('nova.volume.cinder.cinderclient')
def test_attachment_update_attachment_not_found(self, mock_cinderclient):
"""Tests that the translate_attachment_exception decorator is used."""
# fake out the volume not found error
mock_cinderclient.return_value.attachments.update.side_effect = (
cinder_exception.NotFound(404))
self.assertRaises(exception.VolumeAttachmentNotFound,
self.api.attachment_update,
self.ctx, uuids.attachment_id,
connector={'host': 'fake-host'})
@mock.patch('nova.volume.cinder.cinderclient')
def test_attachment_update_attachment_no_connector(self,
mock_cinderclient):
"""Tests that the translate_cinder_exception decorator is used."""
# fake out the volume bad request error
mock_cinderclient.return_value.attachments.update.side_effect = (
cinder_exception.BadRequest(400))
self.assertRaises(exception.InvalidInput,
self.api.attachment_update,
self.ctx, uuids.attachment_id, connector=None)
@mock.patch('nova.volume.cinder.cinderclient')
def test_attachment_delete(self, mock_cinderclient):
mock_attachments = mock.MagicMock()

View File

@ -531,6 +531,30 @@ class API(object):
'code': getattr(ex, 'code', None)},
instance_uuid=instance_id)
@translate_attachment_exception
def attachment_update(self, context, attachment_id, connector):
"""Updates the connector on the volume attachment. An attachment
without a connector is considered reserved but not fully attached.
:param context: The nova request context.
:param attachment_id: UUID of the volume attachment to update.
:param connector: host connector dict. This is required when updating
a volume attachment. To terminate a connection, the volume
attachment for that connection must be deleted.
:returns: cinderclient.v3.attachments.VolumeAttachment object
representing the updated volume attachment.
"""
try:
return cinderclient(context).attachments.update(
attachment_id, connector)
except cinder_exception.ClientException as ex:
with excutils.save_and_reraise_exception():
LOG.error(('Update attachment failed for attachment '
'%(id)s. Error: %(msg)s Code: %(code)s'),
{'id': attachment_id,
'msg': six.text_type(ex),
'code': getattr(ex, 'code', None)})
@translate_attachment_exception
def attachment_delete(self, context, attachment_id):
try: