Skip to content

Commit c07f1fd

Browse files
hsato03winterhazel
andauthored
Number of running and stopped VMs as preset variables for Network type Quota tariffs (#11689)
Co-authored-by: Fabricio Duarte <fabricio.duarte.jr@gmail.com>
1 parent c45596c commit c07f1fd

5 files changed

Lines changed: 112 additions & 5 deletions

File tree

framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelper.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import javax.inject.Inject;
3636

3737
import com.cloud.storage.StoragePoolTagVO;
38+
import com.cloud.vm.VirtualMachine;
3839
import org.apache.cloudstack.acl.RoleVO;
3940
import org.apache.cloudstack.acl.dao.RoleDao;
4041
import org.apache.cloudstack.backup.BackupOfferingVO;
@@ -783,10 +784,21 @@ protected void loadPresetVariableValueForNetwork(UsageVO usageRecord, Value valu
783784
value.setId(network.getUuid());
784785
value.setName(network.getName());
785786
value.setState(usageRecord.getState());
786-
787+
value.setResourceCounting(getPresetVariableValueNetworkResourceCounting(networkId));
787788
value.setNetworkOffering(getPresetVariableValueNetworkOffering(network.getNetworkOfferingId()));
788789
}
789790

791+
protected ResourceCounting getPresetVariableValueNetworkResourceCounting(Long networkId) {
792+
ResourceCounting resourceCounting = new ResourceCounting();
793+
List<VMInstanceVO> vmInstancesVO = vmInstanceDao.listNonRemovedVmsByTypeAndNetwork(networkId, VirtualMachine.Type.User);
794+
int runningVms = (int) vmInstancesVO.stream().filter(vm -> vm.getState().equals(VirtualMachine.State.Running)).count();
795+
int stoppedVms = (int) vmInstancesVO.stream().filter(vm -> vm.getState().equals(VirtualMachine.State.Stopped)).count();
796+
797+
resourceCounting.setRunningVms(runningVms);
798+
resourceCounting.setStoppedVms(stoppedVms);
799+
return resourceCounting;
800+
}
801+
790802
protected GenericPresetVariable getPresetVariableValueNetworkOffering(Long networkOfferingId) {
791803
NetworkOfferingVO networkOfferingVo = networkOfferingDao.findByIdIncludingRemoved(networkOfferingId);
792804
validateIfObjectIsNull(networkOfferingVo, networkOfferingId, "network offering");
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.cloudstack.quota.activationrule.presetvariables;
19+
20+
21+
import org.apache.cloudstack.quota.constant.QuotaTypes;
22+
import org.apache.commons.lang3.builder.ToStringBuilder;
23+
import org.apache.commons.lang3.builder.ToStringStyle;
24+
25+
public class ResourceCounting {
26+
27+
@PresetVariableDefinition(description = "The number of running user instances.", supportedTypes = {QuotaTypes.NETWORK})
28+
private int runningVms;
29+
@PresetVariableDefinition(description = "The number of stopped user instances.", supportedTypes = {QuotaTypes.NETWORK})
30+
private int stoppedVms;
31+
32+
public int getRunningVms() {
33+
return runningVms;
34+
}
35+
36+
public void setRunningVms(int runningVms) {
37+
this.runningVms = runningVms;
38+
}
39+
40+
public int getStoppedVms() {
41+
return stoppedVms;
42+
}
43+
44+
public void setStoppedVms(int stoppedVms) {
45+
this.stoppedVms = stoppedVms;
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
51+
}
52+
}

framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Value.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ public class Value extends GenericPresetVariable {
8484
@PresetVariableDefinition(description = "Backup offering of the backup.", supportedTypes = {QuotaTypes.BACKUP})
8585
private BackupOffering backupOffering;
8686

87+
@PresetVariableDefinition(description = "The amount of resources of the usage type.")
88+
private ResourceCounting resourceCounting;
89+
8790
@PresetVariableDefinition(description = "The hypervisor where the resource was deployed. Values can be: XenServer, KVM, VMware, Hyperv, BareMetal, Ovm, Ovm3 and LXC.",
8891
supportedTypes = {QuotaTypes.RUNNING_VM, QuotaTypes.ALLOCATED_VM, QuotaTypes.VM_SNAPSHOT, QuotaTypes.SNAPSHOT})
8992
private String hypervisorType;
@@ -262,6 +265,14 @@ public void setState(String state) {
262265
this.state = state;
263266
}
264267

268+
public ResourceCounting getResourceCounting() {
269+
return resourceCounting;
270+
}
271+
272+
public void setResourceCounting(ResourceCounting resourceCounting) {
273+
this.resourceCounting = resourceCounting;
274+
}
275+
265276
public GenericPresetVariable getNetworkOffering() {
266277
return networkOffering;
267278
}

framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelperTest.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
import com.cloud.dc.ClusterDetailsVO;
3232
import com.cloud.host.HostTagVO;
3333
import com.cloud.hypervisor.Hypervisor;
34+
import com.cloud.network.Network;
3435
import com.cloud.network.dao.NetworkVO;
3536
import com.cloud.network.vpc.VpcOfferingVO;
3637
import com.cloud.network.vpc.VpcVO;
3738
import com.cloud.network.vpc.dao.VpcOfferingDao;
3839
import com.cloud.storage.StoragePoolTagVO;
40+
import com.cloud.vm.VirtualMachine;
3941
import org.apache.cloudstack.acl.RoleType;
4042
import org.apache.cloudstack.acl.RoleVO;
4143
import org.apache.cloudstack.acl.dao.RoleDao;
@@ -238,6 +240,8 @@ private Value getValueForTests() {
238240
value.setVmSnapshotType(VMSnapshot.Type.Disk.toString());
239241
value.setComputingResources(getComputingResourcesForTests());
240242
value.setVolumeType(Volume.Type.DATADISK.toString());
243+
value.setState(Network.State.Implemented.toString());
244+
value.setResourceCounting(getResourceCountingForTests());
241245
value.setNetworkOffering(getNetworkOfferingForTests());
242246
value.setVpcOffering(getVpcOfferingForTests());
243247
return value;
@@ -276,6 +280,13 @@ private Configuration getConfigurationForTests() {
276280
return configuration;
277281
}
278282

283+
private ResourceCounting getResourceCountingForTests() {
284+
ResourceCounting resourceCounting = new ResourceCounting();
285+
resourceCounting.setRunningVms(1);
286+
resourceCounting.setStoppedVms(1);
287+
return resourceCounting;
288+
}
289+
279290
private List<HostTagVO> getHostTagsForTests() {
280291
return Arrays.asList(new HostTagVO(1, "tag1", false), new HostTagVO(1, "tag2", false));
281292
}
@@ -1343,16 +1354,36 @@ public void loadPresetVariableValueForNetworkTestRecordIsNetworkSetFields() {
13431354
Mockito.doReturn(expected.getId()).when(networkVoMock).getUuid();
13441355
Mockito.doReturn(expected.getName()).when(networkVoMock).getName();
13451356
Mockito.doReturn(expected.getState()).when(usageVoMock).getState();
1357+
Mockito.doReturn(expected.getResourceCounting()).when(presetVariableHelperSpy).getPresetVariableValueNetworkResourceCounting(Mockito.anyLong());
13461358
Mockito.doReturn(expected.getNetworkOffering()).when(presetVariableHelperSpy).getPresetVariableValueNetworkOffering(Mockito.anyLong());
1347-
13481359
Mockito.doReturn(UsageTypes.NETWORK).when(usageVoMock).getUsageType();
13491360

13501361
Value result = new Value();
13511362
presetVariableHelperSpy.loadPresetVariableValueForNetwork(usageVoMock, result);
13521363

13531364
assertPresetVariableIdAndName(expected, result);
13541365
Assert.assertEquals(expected.getState(), result.getState());
1355-
Assert.assertEquals(expected.getNetworkOffering(), result.getNetworkOffering());
1366+
Assert.assertEquals(expected.getResourceCounting(), result.getResourceCounting());
1367+
}
1368+
1369+
@Test
1370+
public void getPresetVariableValueNetworkResourceCountingTestSetValueAndReturnObject() {
1371+
VMInstanceVO vmInstanceVoMock1 = Mockito.spy(VMInstanceVO.class);
1372+
vmInstanceVoMock1.setState(VirtualMachine.State.Stopped);
1373+
1374+
VMInstanceVO vmInstanceVoMock2 = Mockito.spy(VMInstanceVO.class);
1375+
vmInstanceVoMock2.setState(VirtualMachine.State.Running);
1376+
1377+
Mockito.doReturn(List.of(vmInstanceVoMock1, vmInstanceVoMock2)).when(vmInstanceDaoMock).listNonRemovedVmsByTypeAndNetwork(Mockito.anyLong(), Mockito.any());
1378+
1379+
mockMethodValidateIfObjectIsNull();
1380+
1381+
ResourceCounting expected = getResourceCountingForTests();
1382+
1383+
ResourceCounting result = presetVariableHelperSpy.getPresetVariableValueNetworkResourceCounting(1L);
1384+
1385+
Assert.assertEquals(expected.getRunningVms(), result.getRunningVms());
1386+
Assert.assertEquals(expected.getStoppedVms(), result.getStoppedVms());
13561387
}
13571388

13581389
@Test
@@ -1362,7 +1393,7 @@ public void loadPresetVariableValueForVpcTestRecordIsNotAVpcDoNothing() {
13621393
presetVariableHelperSpy.loadPresetVariableValueForVpc(usageVoMock, null);
13631394
});
13641395

1365-
Mockito.verifyNoInteractions(networkDaoMock);
1396+
Mockito.verifyNoInteractions(vpcDaoMock);
13661397
}
13671398

13681399
@Test

plugins/database/quota/src/main/java/org/apache/cloudstack/api/response/QuotaResponseBuilderImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
import org.apache.cloudstack.quota.activationrule.presetvariables.GenericPresetVariable;
9999
import org.apache.cloudstack.quota.activationrule.presetvariables.PresetVariableDefinition;
100100
import org.apache.cloudstack.quota.activationrule.presetvariables.PresetVariables;
101+
import org.apache.cloudstack.quota.activationrule.presetvariables.ResourceCounting;
101102
import org.apache.cloudstack.quota.activationrule.presetvariables.Value;
102103
import org.apache.cloudstack.quota.constant.QuotaConfig;
103104
import org.apache.cloudstack.quota.constant.QuotaTypes;
@@ -185,7 +186,7 @@ public class QuotaResponseBuilderImpl implements QuotaResponseBuilder {
185186
private VolumeDao volumeDao;
186187

187188

188-
private final Class<?>[] assignableClasses = {GenericPresetVariable.class, ComputingResources.class};
189+
private final Class<?>[] assignableClasses = {GenericPresetVariable.class, ComputingResources.class, ResourceCounting.class};
189190

190191
private Set<Account.Type> accountTypesThatCanListAllQuotaSummaries = Sets.newHashSet(Account.Type.ADMIN, Account.Type.DOMAIN_ADMIN);
191192

0 commit comments

Comments
 (0)