attach_volume first check for existing attachment

Attach_volume has logic to check for an existing
attachment and return that attachment. Moved this
logic before checking to see if the status of the
volume is 'is-use'.

If a first call to attach fails with a time out,
cinderclient will retry the operation therefore
attach_volume needs to return the already existing
attachment if it exists for this instance.

Updated testcase.

Change-Id: I86d7f01b6aca376f9d8cf9de6cdfecd436726a0e
Closes-bug: 1681543
This commit is contained in:
Gerald McBrearty 2017-04-14 10:10:23 -05:00
parent 9eca9e3e1d
commit a4305884e5
2 changed files with 11 additions and 5 deletions

View File

@ -220,6 +220,12 @@ class VolumeAttachDetachTestCase(base.BaseVolumeTestCase):
instance_uuid, None,
mountpoint, 'ro',
volume=volume_passed)
attachment2 = self.volume.attach_volume(self.user_context,
volume_id,
instance_uuid, None,
mountpoint, 'ro',
volume=volume_passed)
self.assertEqual(attachment.id, attachment2.id)
vol = objects.Volume.get_by_id(self.context, volume_id)
self.assertEqual("in-use", vol.status)
self.assertEqual(fields.VolumeAttachStatus.ATTACHED,

View File

@ -1005,11 +1005,6 @@ class VolumeManager(manager.CleanableManager,
raise exception.InvalidVolume(
reason=_("being attached by different mode"))
if (volume.status == 'in-use' and not volume.multiattach
and not volume.migration_status):
raise exception.InvalidVolume(
reason=_("volume is already attached"))
host_name_sanitized = utils.sanitize_hostname(
host_name) if host_name else None
if instance_uuid:
@ -1028,6 +1023,11 @@ class VolumeManager(manager.CleanableManager,
volume.save()
return attachment
if (volume.status == 'in-use' and not volume.multiattach
and not volume.migration_status):
raise exception.InvalidVolume(
reason=_("volume is already attached"))
self._notify_about_volume_usage(context, volume,
"attach.start")