Skip to content

Commit 28bc995

Browse files
marcaureleRohit Yadav
authored andcommitted
CLOUDSTACK-9631: API: affinitygroupids or affinitygroupnames must be given (#1798)
Return an exception if both parameter are missing. This fixes an NPE in AffinityGroupServiceImpl.updateVMAffinityGroups() when the list was null. Signed-off-by: Marc-Aurèle Brothier <m@brothier.org>
1 parent 471b686 commit 28bc995

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

api/src/org/apache/cloudstack/api/command/user/affinitygroup/UpdateVMAffinityGroupCmd.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@ public Long getId() {
9292
}
9393

9494
public List<Long> getAffinityGroupIdList() {
95-
if (affinityGroupNameList != null && affinityGroupIdList != null) {
96-
throw new InvalidParameterValueException("affinitygroupids parameter is mutually exclusive with affinitygroupnames parameter");
97-
}
98-
9995
// transform group names to ids here
10096
if (affinityGroupNameList != null) {
10197
List<Long> affinityGroupIds = new ArrayList<Long>();
@@ -138,6 +134,14 @@ public long getEntityOwnerId() {
138134

139135
@Override
140136
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException {
137+
if (affinityGroupNameList != null && affinityGroupIdList != null) {
138+
throw new InvalidParameterValueException("affinitygroupids parameter is mutually exclusive with affinitygroupnames parameter");
139+
}
140+
141+
if (affinityGroupNameList == null && affinityGroupIdList == null) {
142+
throw new InvalidParameterValueException("affinitygroupids parameter or affinitygroupnames parameter must be given");
143+
}
144+
141145
CallContext.current().setEventDetails("VM ID: " + getId());
142146
UserVm result = _affinityGroupService.updateVMAffinityGroups(getId(), getAffinityGroupIdList());
143147
ArrayList<VMDetails> dc = new ArrayList<VMDetails>();

test/integration/component/test_affinity_groups.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,6 @@ def test_03_update_aff_grp_for_vm_with_no_aff_grp(self):
10401040
for aff_grp in aff_grps:
10411041
aff_grp.delete(self.api_client)
10421042

1043-
@unittest.skip("Skip - Failing - work in progress")
10441043
@attr(tags=["simulator", "basic", "advanced", "multihost", "NotRun"])
10451044
def test_04_update_aff_grp_remove_all(self):
10461045
"""
@@ -1087,6 +1086,32 @@ def test_05_update_aff_grp_on_running_vm(self):
10871086
for aff_grp in aff_grps:
10881087
aff_grp.delete(self.api_client)
10891088

1089+
@attr(tags=["simulator", "basic", "advanced", "multihost", "NotRun"])
1090+
def test_06_update_aff_grp_invalid_args(self):
1091+
"""
1092+
Update the list of Affinity Groups with either both args or none
1093+
"""
1094+
1095+
self.create_aff_grp(aff_grp=self.services["host_anti_affinity"])
1096+
self.create_aff_grp(aff_grp=self.services["host_anti_affinity"])
1097+
vm1, hostid1 = self.create_vm_in_aff_grps([], account_name=self.account.name, domain_id=self.domain.id)
1098+
1099+
aff_grps = [self.aff_grp[0], self.aff_grp[1]]
1100+
vm1.stop(self.api_client)
1101+
1102+
with self.assertRaises(Exception):
1103+
vm1.update_affinity_group(self.api_client)
1104+
1105+
with self.assertRaises(Exception):
1106+
vm1.update_affinity_group(self.api_client, affinitygroupids=[self.aff_grp[0].id], affinitygroupnames=[self.aff_grp[1].name])
1107+
1108+
vm1.update_affinity_group(self.api_client, affinitygroupids=[])
1109+
1110+
vm1.delete(self.api_client)
1111+
# Can cleanup affinity groups since none are set on the VM
1112+
for aff_grp in aff_grps:
1113+
aff_grp.delete(self.api_client)
1114+
10901115
class TestDeployVMAffinityGroups(cloudstackTestCase):
10911116

10921117
@classmethod

0 commit comments

Comments
 (0)