From: Anthony Lee <anthony.mic.lee@hp.com>
Date: Tue, 8 Sep 2015 22:29:17 +0000 (-0700)
Subject: LeftHand Add update_migrated_volume to drivers
X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=85a666a9af42825875c2731a9489d1adee1cc430;p=openstack-build%2Fcinder-build.git

LeftHand Add update_migrated_volume to drivers

The LeftHand drivers were missing the method
update_migrated_volume, which was causing the volume IDs
to no longer be accurate after a volume was migrated into
a LeftHand array.

Change-Id: I3718edc68c750856313557aebd212f64f7edc389
Closes-Bug: 1493546
---

diff --git a/cinder/tests/unit/test_hplefthand.py b/cinder/tests/unit/test_hplefthand.py
index 3a72f210d..632032d56 100644
--- a/cinder/tests/unit/test_hplefthand.py
+++ b/cinder/tests/unit/test_hplefthand.py
@@ -1629,6 +1629,49 @@ class TestHPLeftHandRESTISCSIDriver(HPLeftHandBaseDriver, test.TestCase):
                 len(expected),
                 len(mock_client.method_calls))
 
+    def test_update_migrated_volume(self):
+        mock_client = self.setup_driver()
+        volume_id = 'fake_vol_id'
+        clone_id = 'fake_clone_id'
+        fake_old_volume = {'id': volume_id}
+        provider_location = 'foo'
+        fake_new_volume = {'id': clone_id,
+                           '_name_id': clone_id,
+                           'provider_location': provider_location}
+        original_volume_status = 'available'
+        with mock.patch.object(hp_lefthand_rest_proxy.HPLeftHandRESTProxy,
+                               '_create_client') as mock_do_setup:
+            mock_do_setup.return_value = mock_client
+            actual_update = self.driver.update_migrated_volume(
+                context.get_admin_context(), fake_old_volume,
+                fake_new_volume, original_volume_status)
+
+            expected_update = {'_name_id': None,
+                               'provider_location': None}
+            self.assertEqual(expected_update, actual_update)
+
+    def test_update_migrated_volume_attached(self):
+        mock_client = self.setup_driver()
+        volume_id = 'fake_vol_id'
+        clone_id = 'fake_clone_id'
+        fake_old_volume = {'id': volume_id}
+        provider_location = 'foo'
+        fake_new_volume = {'id': clone_id,
+                           '_name_id': clone_id,
+                           'provider_location': provider_location}
+        original_volume_status = 'in-use'
+
+        with mock.patch.object(hp_lefthand_rest_proxy.HPLeftHandRESTProxy,
+                               '_create_client') as mock_do_setup:
+            mock_do_setup.return_value = mock_client
+            actual_update = self.driver.update_migrated_volume(
+                context.get_admin_context(), fake_old_volume,
+                fake_new_volume, original_volume_status)
+
+            expected_update = {'_name_id': fake_new_volume['_name_id'],
+                               'provider_location': provider_location}
+            self.assertEqual(expected_update, actual_update)
+
     @mock.patch.object(volume_types, 'get_volume_type',
                        return_value={'extra_specs': {'hplh:ao': 'true'}})
     def test_create_volume_with_ao_true(self, _mock_volume_type):
diff --git a/cinder/volume/drivers/san/hp/hp_lefthand_cliq_proxy.py b/cinder/volume/drivers/san/hp/hp_lefthand_cliq_proxy.py
index c4b635bea..2561ad1f0 100644
--- a/cinder/volume/drivers/san/hp/hp_lefthand_cliq_proxy.py
+++ b/cinder/volume/drivers/san/hp/hp_lefthand_cliq_proxy.py
@@ -74,9 +74,10 @@ class HPLeftHandCLIQProxy(san.SanISCSIDriver):
         1.2.1 - Fixed bug #1279897, HP LeftHand CLIQ proxy may return incorrect
                 capacity values.
         1.2.2 - Fixed driver with Paramiko 1.13.0, bug #1298608.
+        1.2.3 - Added update_migrated_volume #1493546
     """
 
-    VERSION = "1.2.2"
+    VERSION = "1.2.3"
 
     device_stats = {}
 
@@ -484,3 +485,7 @@ class HPLeftHandCLIQProxy(san.SanISCSIDriver):
                      dictionary of its reported capabilities.
         """
         return (False, None)
+
+    def update_migrated_volume(self, context, volume, new_volume,
+                               original_volume_status):
+        raise NotImplementedError()
diff --git a/cinder/volume/drivers/san/hp/hp_lefthand_iscsi.py b/cinder/volume/drivers/san/hp/hp_lefthand_iscsi.py
index 22a64d86a..21bcc3aa5 100644
--- a/cinder/volume/drivers/san/hp/hp_lefthand_iscsi.py
+++ b/cinder/volume/drivers/san/hp/hp_lefthand_iscsi.py
@@ -65,9 +65,10 @@ class HPLeftHandISCSIDriver(driver.TransferVD,
         1.0.6 - Updated minimum client version. bug #1432757
         1.0.7 - Update driver to use ABC metaclasses
         1.0.8 - Adds consistency group support
+        1.0.9 - Added update_migrated_volume #1493546
     """
 
-    VERSION = "1.0.8"
+    VERSION = "1.0.9"
 
     def __init__(self, *args, **kwargs):
         super(HPLeftHandISCSIDriver, self).__init__(*args, **kwargs)
@@ -196,6 +197,11 @@ class HPLeftHandISCSIDriver(driver.TransferVD,
         """Migrate directly if source and dest are managed by same storage."""
         return self.proxy.migrate_volume(ctxt, volume, host)
 
+    def update_migrated_volume(self, context, volume, new_volume,
+                               original_volume_status):
+        return self.proxy.update_migrated_volume(context, volume, new_volume,
+                                                 original_volume_status)
+
     def manage_existing(self, volume, existing_ref):
         return self.proxy.manage_existing(volume, existing_ref)
 
diff --git a/cinder/volume/drivers/san/hp/hp_lefthand_rest_proxy.py b/cinder/volume/drivers/san/hp/hp_lefthand_rest_proxy.py
index 0b722502a..0418766f8 100644
--- a/cinder/volume/drivers/san/hp/hp_lefthand_rest_proxy.py
+++ b/cinder/volume/drivers/san/hp/hp_lefthand_rest_proxy.py
@@ -109,9 +109,10 @@ class HPLeftHandRESTProxy(driver.ISCSIDriver):
         1.0.10 - Add stats for goodness_function and filter_function
         1.0.11 - Add over subscription support
         1.0.12 - Adds consistency group support
+        1.0.13 - Added update_migrated_volume #1493546
     """
 
-    VERSION = "1.0.12"
+    VERSION = "1.0.13"
 
     device_stats = {}
 
@@ -805,6 +806,45 @@ class HPLeftHandRESTProxy(driver.ISCSIDriver):
 
         return (True, None)
 
+    def update_migrated_volume(self, context, volume, new_volume,
+                               original_volume_status):
+        """Rename the new (temp) volume to it's original name.
+
+
+        This method tries to rename the new volume to it's original
+        name after the migration has completed.
+
+        """
+        LOG.debug("Update volume name for %(id)s.", {'id': new_volume['id']})
+        name_id = None
+        provider_location = None
+        if original_volume_status == 'available':
+            # volume isn't attached and can be updated
+            original_name = CONF.volume_name_template % volume['id']
+            current_name = CONF.volume_name_template % new_volume['id']
+            client = self._login()
+            try:
+                volume_info = client.getVolumeByName(current_name)
+                volumeMods = {'name': original_name}
+                client.modifyVolume(volume_info['id'], volumeMods)
+                LOG.info(_LI("Volume name changed from %(tmp)s to %(orig)s."),
+                         {'tmp': current_name, 'orig': original_name})
+            except Exception as e:
+                LOG.error(_LE("Changing the volume name from %(tmp)s to "
+                              "%(orig)s failed because %(reason)s."),
+                          {'tmp': current_name, 'orig': original_name,
+                           'reason': e})
+                name_id = new_volume['_name_id'] or new_volume['id']
+                provider_location = new_volume['provider_location']
+            finally:
+                self._logout(client)
+        else:
+            # the backend can't change the name.
+            name_id = new_volume['_name_id'] or new_volume['id']
+            provider_location = new_volume['provider_location']
+
+        return {'_name_id': name_id, 'provider_location': provider_location}
+
     def manage_existing(self, volume, existing_ref):
         """Manage an existing LeftHand volume.