From dc777f1dc736b01b8f8eec58972230d30b3074f6 Mon Sep 17 00:00:00 2001
From: Ryan McNair <rdmcnair@us.ibm.com>
Date: Thu, 19 Nov 2015 19:48:37 +0000
Subject: [PATCH] Fix swap_volume for case without migration

Fix swap_volume so that even when a volume-migration does not
occur, the volumes don't get stuck in "attaching" and "detaching".
Previously the swap_volume was changed in
https://review.openstack.org/#/c/101932 and
https://review.openstack.org/#/c/101933 to move the final
attach and detach logic to Cinder, however those changes only fix
the migration path.  This change calls Cinder's "attach" and "detach"
for the non-migration path to update the volumes' states.

Change-Id: I242c2c7c4a7197bbce04d0b3d75688f989ea1fd5
Closes-Bug: #1489744
---
 cinder/volume/api.py | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/cinder/volume/api.py b/cinder/volume/api.py
index 2031c0c78..8dfcf5e3d 100644
--- a/cinder/volume/api.py
+++ b/cinder/volume/api.py
@@ -1405,6 +1405,19 @@ class API(base.Base):
         # This is a volume swap initiated by Nova, not Cinder. Nova expects
         # us to return the new_volume_id.
         if not (volume['migration_status'] or new_volume['migration_status']):
+            # Don't need to do migration, but still need to finish the
+            # volume attach and detach so volumes don't end in 'attaching'
+            # and 'detaching' state
+            attachments = volume['volume_attachment']
+            for attachment in attachments:
+                self.detach(context, volume, attachment['id'])
+
+                self.attach(context, new_volume,
+                            attachment['instance_uuid'],
+                            attachment['attached_host'],
+                            attachment['mountpoint'],
+                            'rw')
+
             return new_volume['id']
 
         if not volume['migration_status']:
-- 
2.45.2