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
This commit is contained in:
Xing Yang 2015-08-11 23:09:30 -04:00
parent ec9caa8420
commit b9f7825ea6
2 changed files with 38 additions and 0 deletions

View 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:

View 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,