From b9f7825ea6fc3f6ea213627631e7d64b655074bf Mon Sep 17 00:00:00 2001 From: Xing Yang Date: Tue, 11 Aug 2015 23:09:30 -0400 Subject: [PATCH] Prevent volume already in CG to be added to another 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 | 13 ++++++++++ .../api/contrib/test_consistencygroups.py | 25 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/cinder/consistencygroup/api.py b/cinder/consistencygroup/api.py index 52cc19162..62b3b402e 100644 --- a/cinder/consistencygroup/api.py +++ b/cinder/consistencygroup/api.py @@ -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: diff --git a/cinder/tests/unit/api/contrib/test_consistencygroups.py b/cinder/tests/unit/api/contrib/test_consistencygroups.py index f37164585..07e3e4a3c 100644 --- a/cinder/tests/unit/api/contrib/test_consistencygroups.py +++ b/cinder/tests/unit/api/contrib/test_consistencygroups.py @@ -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, -- 2.45.2