Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import com.cloud.hypervisor.Hypervisor;
import com.cloud.storage.StoragePoolTagVO;
import com.cloud.vm.VirtualMachine;
import org.apache.cloudstack.acl.RoleVO;
import org.apache.cloudstack.acl.dao.RoleDao;
import org.apache.cloudstack.backup.BackupOfferingVO;
Expand Down Expand Up @@ -778,6 +779,18 @@ protected void loadPresetVariableValueForNetwork(UsageVO usageRecord, Value valu
value.setId(network.getUuid());
value.setName(network.getName());
value.setState(usageRecord.getState());
value.setResourceCounting(getPresetVariableValueNetworkResourceCounting(networkId));
}

private ResourceCounting getPresetVariableValueNetworkResourceCounting(Long networkId) {
ResourceCounting resourceCounting = new ResourceCounting();
List<VMInstanceVO> vmInstancesVO = vmInstanceDao.listNonRemovedVmsByTypeAndNetwork(networkId, VirtualMachine.Type.User);
int runningVms = (int) vmInstancesVO.stream().filter(vm -> vm.getState().equals(VirtualMachine.State.Running)).count();
int stoppedVms = (int) vmInstancesVO.stream().filter(vm -> vm.getState().equals(VirtualMachine.State.Stopped)).count();

resourceCounting.setRunningVms(runningVms);
resourceCounting.setStoppedVms(stoppedVms);
return resourceCounting;
}

protected void loadPresetVariableValueForVpc(UsageVO usageRecord, Value value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.cloudstack.quota.activationrule.presetvariables;


import org.apache.cloudstack.quota.constant.QuotaTypes;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class ResourceCounting {

@PresetVariableDefinition(description = "The number of running user instances.", supportedTypes = {QuotaTypes.NETWORK})
private int runningVms;
@PresetVariableDefinition(description = "The number of stopped user instances.", supportedTypes = {QuotaTypes.NETWORK})
private int stoppedVms;

public int getRunningVms() {
return runningVms;
}

public void setRunningVms(int runningVms) {
this.runningVms = runningVms;
}

public int getStoppedVms() {
return stoppedVms;
}

public void setStoppedVms(int stoppedVms) {
this.stoppedVms = stoppedVms;
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public class Value extends GenericPresetVariable {
@PresetVariableDefinition(description = "Backup offering of the backup.", supportedTypes = {QuotaTypes.BACKUP})
private BackupOffering backupOffering;

@PresetVariableDefinition(description = "The amount of resources of the usage type.")
private ResourceCounting resourceCounting;

@PresetVariableDefinition(description = "The hypervisor where the resource was deployed. Values can be: XenServer, KVM, VMware, Hyperv, BareMetal, Ovm, Ovm3 and LXC.",
supportedTypes = {QuotaTypes.RUNNING_VM, QuotaTypes.ALLOCATED_VM, QuotaTypes.VM_SNAPSHOT, QuotaTypes.SNAPSHOT})
private String hypervisorType;
Expand Down Expand Up @@ -279,4 +282,13 @@ public void setState(String state) {
this.state = state;
fieldNamesToIncludeInToString.add("state");
}

public ResourceCounting getResourceCounting() {
return resourceCounting;
}

public void setResourceCounting(ResourceCounting resourceCounting) {
this.resourceCounting = resourceCounting;
fieldNamesToIncludeInToString.add("resourceCounting");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.apache.cloudstack.quota.activationrule.presetvariables.GenericPresetVariable;
import org.apache.cloudstack.quota.activationrule.presetvariables.PresetVariableDefinition;
import org.apache.cloudstack.quota.activationrule.presetvariables.PresetVariables;
import org.apache.cloudstack.quota.activationrule.presetvariables.ResourceCounting;
import org.apache.cloudstack.quota.activationrule.presetvariables.Value;
import org.apache.cloudstack.quota.constant.QuotaConfig;
import org.apache.cloudstack.quota.constant.QuotaTypes;
Expand Down Expand Up @@ -148,7 +149,7 @@ public class QuotaResponseBuilderImpl implements QuotaResponseBuilder {
@Inject
private ApiDiscoveryService apiDiscoveryService;

private final Class<?>[] assignableClasses = {GenericPresetVariable.class, ComputingResources.class};
private final Class<?>[] assignableClasses = {GenericPresetVariable.class, ComputingResources.class, ResourceCounting.class};


@Override
Expand Down
Loading