Skip to content

Commit e44b6d9

Browse files
committed
Add running & stopped VMs as preset variables
1 parent e6c7a71 commit e44b6d9

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import com.cloud.hypervisor.Hypervisor;
3636
import com.cloud.storage.StoragePoolTagVO;
37+
import com.cloud.vm.VirtualMachine;
3738
import org.apache.cloudstack.acl.RoleVO;
3839
import org.apache.cloudstack.acl.dao.RoleDao;
3940
import org.apache.cloudstack.backup.BackupOfferingVO;
@@ -778,6 +779,18 @@ protected void loadPresetVariableValueForNetwork(UsageVO usageRecord, Value valu
778779
value.setId(network.getUuid());
779780
value.setName(network.getName());
780781
value.setState(usageRecord.getState());
782+
value.setResourceCounting(getPresetVariableValueNetworkResourceCounting(networkId));
783+
}
784+
785+
private ResourceCounting getPresetVariableValueNetworkResourceCounting(Long networkId) {
786+
ResourceCounting resourceCounting = new ResourceCounting();
787+
List<VMInstanceVO> vmInstancesVO = vmInstanceDao.listNonRemovedVmsByTypeAndNetwork(networkId, VirtualMachine.Type.User);
788+
int runningVms = (int) vmInstancesVO.stream().filter(vm -> vm.getState().equals(VirtualMachine.State.Running)).count();
789+
int stoppedVms = (int) vmInstancesVO.stream().filter(vm -> vm.getState().equals(VirtualMachine.State.Stopped)).count();
790+
791+
resourceCounting.setRunningVms(runningVms);
792+
resourceCounting.setStoppedVms(stoppedVms);
793+
return resourceCounting;
781794
}
782795

783796
protected void loadPresetVariableValueForVpc(UsageVO usageRecord, Value value) {
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public class Value extends GenericPresetVariable {
8888
@PresetVariableDefinition(description = "Backup offering of the backup.", supportedTypes = {QuotaTypes.BACKUP})
8989
private BackupOffering backupOffering;
9090

91+
@PresetVariableDefinition(description = "The amount of resources of the usage type.")
92+
private ResourceCounting resourceCounting;
93+
9194
@PresetVariableDefinition(description = "The hypervisor where the resource was deployed. Values can be: XenServer, KVM, VMware, Hyperv, BareMetal, Ovm, Ovm3 and LXC.",
9295
supportedTypes = {QuotaTypes.RUNNING_VM, QuotaTypes.ALLOCATED_VM, QuotaTypes.VM_SNAPSHOT, QuotaTypes.SNAPSHOT})
9396
private String hypervisorType;
@@ -279,4 +282,13 @@ public void setState(String state) {
279282
this.state = state;
280283
fieldNamesToIncludeInToString.add("state");
281284
}
285+
286+
public ResourceCounting getResourceCounting() {
287+
return resourceCounting;
288+
}
289+
290+
public void setResourceCounting(ResourceCounting resourceCounting) {
291+
this.resourceCounting = resourceCounting;
292+
fieldNamesToIncludeInToString.add("resourceCounting");
293+
}
282294
}

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
@@ -71,6 +71,7 @@
7171
import org.apache.cloudstack.quota.activationrule.presetvariables.GenericPresetVariable;
7272
import org.apache.cloudstack.quota.activationrule.presetvariables.PresetVariableDefinition;
7373
import org.apache.cloudstack.quota.activationrule.presetvariables.PresetVariables;
74+
import org.apache.cloudstack.quota.activationrule.presetvariables.ResourceCounting;
7475
import org.apache.cloudstack.quota.activationrule.presetvariables.Value;
7576
import org.apache.cloudstack.quota.constant.QuotaConfig;
7677
import org.apache.cloudstack.quota.constant.QuotaTypes;
@@ -148,7 +149,7 @@ public class QuotaResponseBuilderImpl implements QuotaResponseBuilder {
148149
@Inject
149150
private ApiDiscoveryService apiDiscoveryService;
150151

151-
private final Class<?>[] assignableClasses = {GenericPresetVariable.class, ComputingResources.class};
152+
private final Class<?>[] assignableClasses = {GenericPresetVariable.class, ComputingResources.class, ResourceCounting.class};
152153

153154

154155
@Override

0 commit comments

Comments
 (0)