]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Prevent volume already in CG to be added to another
authorXing Yang <xing.yang@emc.com>
Wed, 12 Aug 2015 03:09:30 +0000 (23:09 -0400)
committerXing Yang <xing.yang@emc.com>
Wed, 12 Aug 2015 17:08:15 +0000 (13:08 -0400)
If a volume is already in a CG, currently it can still be added to
another CG without any warning. We should add a check to see if the
volume is already in a CG and prevent it from being added to another
if that check returns True. It has to be removed from the previous
CG in order to be added to a new one.

Change-Id: I6ec92cbf4ce60e7afbfa5a57edef18ddba554a74
Closes-Bug: #1487151

cinder/consistencygroup/api.py
cinder/tests/unit/api/contrib/test_consistencygroups.py

index 52cc19162a6730d1e96c17e9072ccf59c072e5d9..62b3b402e6442a3034d20db248362327763523e1 100644 (file)
@@ -611,6 +611,19 @@ class API(base.Base):
                        {'volume_id': add_vol,
                         'group_id': group.id})
                 raise exception.InvalidVolume(reason=msg)
+            orig_group = add_vol_ref.get('consistencygroup_id', None)
+            if orig_group:
+                # If volume to be added is already in the group to be updated,
+                # it should have been removed from the add_volumes_list in the
+                # beginning of this function. If we are here, it means it is
+                # in a different group.
+                msg = (_("Cannot add volume %(volume_id)s to consistency "
+                         "group %(group_id)s because it is already in "
+                         "consistency group %(orig_group)s.") %
+                       {'volume_id': add_vol_ref['id'],
+                        'group_id': group.id,
+                        'orig_group': orig_group})
+                raise exception.InvalidVolume(reason=msg)
             if add_vol_ref:
                 add_vol_type_id = add_vol_ref.get('volume_type_id', None)
                 if not add_vol_type_id:
index f37164585e6ac48fb3ffc1a464d3591ab5c3fdc1..07e3e4a3c6b15c5d9e17ac886572e3565e05908a 100644 (file)
@@ -671,6 +671,31 @@ class ConsistencyGroupsAPITestCase(test.TestCase):
 
         consistencygroup.destroy()
 
+    def test_update_consistencygroup_add_volume_already_in_cg(self):
+        consistencygroup = self._create_consistencygroup(ctxt=self.ctxt,
+                                                         status='available')
+        add_volume_id = utils.create_volume(
+            self.ctxt,
+            consistencygroup_id='some_other_cg')['id']
+        req = webob.Request.blank('/v2/fake/consistencygroups/%s/update' %
+                                  consistencygroup.id)
+        req.method = 'PUT'
+        req.headers['Content-Type'] = 'application/json'
+        add_volumes = add_volume_id
+        body = {"consistencygroup": {"name": "cg1",
+                                     "description": "",
+                                     "add_volumes": add_volumes,
+                                     "remove_volumes": None, }}
+        req.body = json.dumps(body)
+        res = req.get_response(fakes.wsgi_app())
+        res_dict = json.loads(res.body)
+
+        self.assertEqual(400, res.status_int)
+        self.assertEqual(400, res_dict['badRequest']['code'])
+        self.assertIsNotNone(res_dict['badRequest']['message'])
+
+        consistencygroup.destroy()
+
     def test_update_consistencygroup_invalid_state(self):
         wrong_status = 'wrong_status'
         consistencygroup = self._create_consistencygroup(status=wrong_status,