Skip to content

Commit 6a7b134

Browse files
author
João Jandre
committed
Merge branch 'network-vm-presets-4.20' into '4.20.0.0-scclouds'
Port 4.20 - Quantidade de VMs rodando e paradas como _preset variables_ para tarifas do tipo `Network` See merge request scclouds/scclouds!1038
2 parents 1a15ae0 + 56deec0 commit 6a7b134

4 files changed

Lines changed: 82 additions & 1 deletion

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import com.cloud.hypervisor.Hypervisor;
3434
import com.cloud.storage.StoragePoolTagVO;
35+
import com.cloud.vm.VirtualMachine;
3536
import org.apache.cloudstack.acl.RoleVO;
3637
import org.apache.cloudstack.acl.dao.RoleDao;
3738
import org.apache.cloudstack.backup.BackupOfferingVO;
@@ -717,6 +718,20 @@ protected void loadPresetVariableValueForNetwork(UsageVO usageRecord, Value valu
717718
value.setId(network.getUuid());
718719
value.setName(network.getName());
719720
value.setState(usageRecord.getState());
721+
value.setResourceCounting(getPresetVariableValueNetworkResourceCounting(networkId));
722+
}
723+
724+
private ResourceCounting getPresetVariableValueNetworkResourceCounting(Long networkId) {
725+
ResourceCounting resourceCounting = new ResourceCounting();
726+
List<VMInstanceVO> vmInstancesVO = vmInstanceDao.listNonRemovedVmsByTypeAndNetwork(networkId, VirtualMachine.Type.User);
727+
728+
int runningVms = (int) vmInstancesVO.stream().filter(vm -> vm.getState().equals(VirtualMachine.State.Running)).count();
729+
int stoppedVms = (int) vmInstancesVO.stream().filter(vm -> vm.getState().equals(VirtualMachine.State.Stopped)).count();
730+
731+
resourceCounting.setRunningVms(runningVms);
732+
resourceCounting.setStoppedVms(stoppedVms);
733+
734+
return resourceCounting;
720735
}
721736

722737
protected void loadPresetVariableValueForVpc(UsageVO usageRecord, Value value) {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
30+
@PresetVariableDefinition(description = "The number of stopped user instances.", supportedTypes = {QuotaTypes.NETWORK})
31+
private int stoppedVms;
32+
33+
public int getRunningVms() {
34+
return runningVms;
35+
}
36+
37+
public void setRunningVms(int runningVms) {
38+
this.runningVms = runningVms;
39+
}
40+
41+
public int getStoppedVms() {
42+
return stoppedVms;
43+
}
44+
45+
public void setStoppedVms(int stoppedVms) {
46+
this.stoppedVms = stoppedVms;
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
52+
}
53+
}

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
@@ -87,6 +87,9 @@ public class Value extends GenericPresetVariable {
8787
@PresetVariableDefinition(description = "Backup offering of the backup.", supportedTypes = {QuotaTypes.BACKUP})
8888
private BackupOffering backupOffering;
8989

90+
@PresetVariableDefinition(description = "The amount of resources of the usage type.")
91+
private ResourceCounting resourceCounting;
92+
9093
@PresetVariableDefinition(description = "The hypervisor where the resource was deployed. Values can be: XenServer, KVM, VMware, Hyperv, BareMetal, Ovm, Ovm3 and LXC.",
9194
supportedTypes = {QuotaTypes.RUNNING_VM, QuotaTypes.ALLOCATED_VM, QuotaTypes.VM_SNAPSHOT, QuotaTypes.SNAPSHOT})
9295
private String hypervisorType;
@@ -265,4 +268,13 @@ public void setState(String state) {
265268
this.state = state;
266269
fieldNamesToIncludeInToString.add("state");
267270
}
271+
272+
public ResourceCounting getResourceCounting() {
273+
return resourceCounting;
274+
}
275+
276+
public void setResourceCounting(ResourceCounting resourceCounting) {
277+
this.resourceCounting = resourceCounting;
278+
fieldNamesToIncludeInToString.add("resourceCounting");
279+
}
268280
}

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
@@ -67,6 +67,7 @@
6767
import org.apache.cloudstack.quota.activationrule.presetvariables.GenericPresetVariable;
6868
import org.apache.cloudstack.quota.activationrule.presetvariables.PresetVariableDefinition;
6969
import org.apache.cloudstack.quota.activationrule.presetvariables.PresetVariables;
70+
import org.apache.cloudstack.quota.activationrule.presetvariables.ResourceCounting;
7071
import org.apache.cloudstack.quota.activationrule.presetvariables.Value;
7172
import org.apache.cloudstack.quota.constant.QuotaConfig;
7273
import org.apache.cloudstack.quota.constant.QuotaTypes;
@@ -145,7 +146,7 @@ public class QuotaResponseBuilderImpl implements QuotaResponseBuilder {
145146
@Inject
146147
private ApiDiscoveryService apiDiscoveryService;
147148

148-
private final Class<?>[] assignableClasses = {GenericPresetVariable.class, ComputingResources.class};
149+
private final Class<?>[] assignableClasses = {GenericPresetVariable.class, ComputingResources.class, ResourceCounting.class};
149150

150151

151152
@Override

0 commit comments

Comments
 (0)