Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions api/src/main/java/org/apache/cloudstack/usage/UsageTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public class UsageTypes {
public static final int VM_SNAPSHOT_ON_PRIMARY = 27;
public static final int BACKUP = 28;
public static final int BUCKET = 29;
public static final int NETWORK = 30;
public static final int VPC = 31;
public static final int BACKUP_OBJECT = 32;

public static List<UsageTypeResponse> listUsageTypes() {
List<UsageTypeResponse> responseList = new ArrayList<UsageTypeResponse>();
Expand All @@ -72,6 +75,9 @@ public static List<UsageTypeResponse> listUsageTypes() {
responseList.add(new UsageTypeResponse(VM_SNAPSHOT_ON_PRIMARY, "VM Snapshot on primary storage usage"));
responseList.add(new UsageTypeResponse(BACKUP, "Backup storage usage"));
responseList.add(new UsageTypeResponse(BUCKET, "Bucket storage usage"));
responseList.add(new UsageTypeResponse(NETWORK, "Network usage"));
responseList.add(new UsageTypeResponse(VPC, "VPC usage"));
responseList.add(new UsageTypeResponse(BACKUP_OBJECT, "Backup object usage"));
return responseList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

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

public class Account extends GenericPresetVariable{
public class Account extends GenericPresetVariable {
@PresetVariableDefinition(description = "Role of the account. This field will not exist if the account is a project.")

private Role role;

public Role getRole() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.cloudstack.quota.activationrule.presetvariables;

public class BackupOffering extends GenericPresetVariable {
@PresetVariableDefinition(description = "External ID of the backup offering that generated the backup.")
private String externalId;

public String getExternalId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.cloudstack.quota.activationrule.presetvariables;

public class ComputeOffering extends GenericPresetVariable {
@PresetVariableDefinition(description = "A boolean informing if the compute offering is customized or not.")
private boolean customized;

public boolean isCustomized() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@
import org.apache.commons.lang3.builder.ToStringStyle;

public class ComputingResources {
@PresetVariableDefinition(description = "Current VM's memory (in MiB).")
private Integer memory;

@PresetVariableDefinition(description = "Current VM's vCPUs.")
private Integer cpuNumber;

@PresetVariableDefinition(description = "Current VM's CPU speed (in MHz).")
private Integer cpuSpeed;

public Integer getMemory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.cloudstack.quota.activationrule.presetvariables;

public class Domain extends GenericPresetVariable {
@PresetVariableDefinition(description = "Path of the domain owner of the resource.")
private String path;

public String getPath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;

public class GenericPresetVariable {
@PresetVariableDefinition(description = "ID of the resource.")
private String id;

@PresetVariableDefinition(description = "Name of the resource.")
private String name;

protected transient Set<String> fieldNamesToIncludeInToString = new HashSet<>();

public String getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import java.util.List;

public class Host extends GenericPresetVariable {
@PresetVariableDefinition(description = "List of tags of the host where the VM is running (i.e.: [\"a\", \"b\"]).")
private List<String> tags;

@PresetVariableDefinition(description = "Whether the tag is a rule interpreted in JavaScript.")
private Boolean isTagARule;

public List<String> getTags() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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
// 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 java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Describes the preset variable and indicates to which Quota usage types it is loaded.
*/
@Target(FIELD)
@Retention(RUNTIME)
public @interface PresetVariableDefinition {
/**
* An array indicating for which Quota usage types the preset variable is loaded.
* @return an array with the usage types for which the preset variable is loaded.
*/
int[] supportedTypes() default 0;

/**
* A {@link String} describing the preset variable.
* @return the description of the preset variable.
*/
String description() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,22 @@

public class PresetVariables {

@PresetVariableDefinition(description = "Account owner of the resource.")
private Account account;

@PresetVariableDefinition(description = "Domain owner of the resource.")
private Domain domain;

@PresetVariableDefinition(description = "Project owner of the resource. This field will not exist if the resource belongs to an account.")
private GenericPresetVariable project;

@PresetVariableDefinition(description = "Type of the record used. Examples for this are: VirtualMachine, DomainRouter, SourceNat, KVM.")
private String resourceType;

@PresetVariableDefinition(description = "Data related to the resource being processed.")
private Value value;

@PresetVariableDefinition(description = "Zone where the resource is.")
private GenericPresetVariable zone;

public Account getAccount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.cloudstack.acl.RoleType;

public class Role extends GenericPresetVariable {
@PresetVariableDefinition(description = "Role type of the resource's owner.")
private RoleType type;

public RoleType getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
import com.cloud.storage.ScopeType;

public class Storage extends GenericPresetVariable {
@PresetVariableDefinition(description = "List of string representing the tags of the storage where the volume is (i.e.: [\"a\", \"b\"]).")
private List<String> tags;

@PresetVariableDefinition(description = "Whether the tag is a rule interpreted in JavaScript. Applicable only for primary storages.")
private Boolean isTagARule;

@PresetVariableDefinition(description = "Scope of the storage where the volume is. Values can be: ZONE, CLUSTER or HOST. Applicable only for primary storages.")
private ScopeType scope;

public List<String> getTags() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,81 @@
import com.cloud.storage.Snapshot;
import com.cloud.storage.Storage.ProvisioningType;
import com.cloud.vm.snapshot.VMSnapshot;
import org.apache.cloudstack.quota.constant.QuotaTypes;

public class Value extends GenericPresetVariable {

@PresetVariableDefinition(description = "ID of the resource.", supportedTypes = {QuotaTypes.ALLOCATED_VM, QuotaTypes.RUNNING_VM, QuotaTypes.VOLUME, QuotaTypes.TEMPLATE,
QuotaTypes.ISO, QuotaTypes.SNAPSHOT, QuotaTypes.NETWORK, QuotaTypes.NETWORK_OFFERING, QuotaTypes.VM_SNAPSHOT, QuotaTypes.BACKUP_OBJECT})
private String id;

@PresetVariableDefinition(description = "Name of the resource.", supportedTypes = {QuotaTypes.ALLOCATED_VM, QuotaTypes.RUNNING_VM, QuotaTypes.VOLUME, QuotaTypes.TEMPLATE,
QuotaTypes.ISO, QuotaTypes.SNAPSHOT, QuotaTypes.NETWORK, QuotaTypes.NETWORK_OFFERING, QuotaTypes.VM_SNAPSHOT, QuotaTypes.BACKUP_OBJECT})
private String name;

@PresetVariableDefinition(description = "Host where the VM is running.", supportedTypes = {QuotaTypes.RUNNING_VM})
private Host host;

@PresetVariableDefinition(description = "OS of the VM/template.", supportedTypes = {QuotaTypes.RUNNING_VM, QuotaTypes.ALLOCATED_VM, QuotaTypes.TEMPLATE, QuotaTypes.ISO})
private String osName;

@PresetVariableDefinition(description = "A list of resources of the account between the start and end date of the usage record being calculated " +
"(i.e.: [{zoneId: ..., domainId:...}]).")
private List<Resource> accountResources;

@PresetVariableDefinition(supportedTypes = {QuotaTypes.ALLOCATED_VM, QuotaTypes.RUNNING_VM, QuotaTypes.VOLUME, QuotaTypes.TEMPLATE, QuotaTypes.ISO, QuotaTypes.SNAPSHOT,
QuotaTypes.VM_SNAPSHOT}, description = "List of tags of the resource in the format key:value (i.e.: {\"a\":\"b\", \"c\":\"d\"}).")
private Map<String, String> tags;

@PresetVariableDefinition(description = "Tag of the network offering.", supportedTypes = {QuotaTypes.NETWORK_OFFERING})
private String tag;

@PresetVariableDefinition(description = "Size of the resource (in MiB).", supportedTypes = {QuotaTypes.TEMPLATE, QuotaTypes.ISO, QuotaTypes.VOLUME, QuotaTypes.SNAPSHOT,
QuotaTypes.BACKUP, QuotaTypes.BACKUP_OBJECT})
private Long size;

@PresetVariableDefinition(description = "Virtual size of the backup.", supportedTypes = {QuotaTypes.BACKUP, QuotaTypes.BACKUP_OBJECT})
private Long virtualSize;

@PresetVariableDefinition(description = "Provisioning type of the resource. Values can be: thin, sparse or fat.", supportedTypes = {QuotaTypes.VOLUME})
private ProvisioningType provisioningType;

@PresetVariableDefinition(description = "Type of the snapshot. Values can be: MANUAL, RECURRING, HOURLY, DAILY, WEEKLY and MONTHLY.", supportedTypes = {QuotaTypes.SNAPSHOT})
private Snapshot.Type snapshotType;

@PresetVariableDefinition(description = "Type of the VM snapshot. Values can be: Disk or DiskAndMemory.", supportedTypes = {QuotaTypes.VM_SNAPSHOT})
private VMSnapshot.Type vmSnapshotType;

@PresetVariableDefinition(description = "Computing offering of the VM.", supportedTypes = {QuotaTypes.RUNNING_VM, QuotaTypes.ALLOCATED_VM})
private ComputeOffering computeOffering;

@PresetVariableDefinition(description = "Template/ISO with which the VM was created.", supportedTypes = {QuotaTypes.RUNNING_VM, QuotaTypes.ALLOCATED_VM})
private GenericPresetVariable template;

@PresetVariableDefinition(description = "Disk offering of the volume.", supportedTypes = {QuotaTypes.VOLUME})
private GenericPresetVariable diskOffering;

@PresetVariableDefinition(description = "Storage where the volume or snapshot is. While handling with snapshots, this value can be from the primary storage if the global" +
Comment thread
BryanMLima marked this conversation as resolved.
Outdated
"setting 'snapshot.backup.to.secondary' is false, otherwise it will be from secondary storage.", supportedTypes = {QuotaTypes.VOLUME, QuotaTypes.SNAPSHOT})
private Storage storage;

@PresetVariableDefinition(description = "Computing resources consumed by the VM.", supportedTypes = {QuotaTypes.RUNNING_VM})
private ComputingResources computingResources;

@PresetVariableDefinition(description = "Backup offering of the backup.", supportedTypes = {QuotaTypes.BACKUP, QuotaTypes.BACKUP_OBJECT})
private BackupOffering backupOffering;

@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;

@PresetVariableDefinition(description = "The volume format. Values can be: RAW, VHD, VHDX, OVA and QCOW2.", supportedTypes = {QuotaTypes.VOLUME, QuotaTypes.VOLUME_SECONDARY})
private String volumeFormat;

@PresetVariableDefinition(description = "The network state. Values can be: Allocated, Setup, Implementing, Implemented, Shutdown and " +
"Destroy.", supportedTypes = {QuotaTypes.NETWORK})
private String state;

public Host getHost() {
return host;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import java.util.HashMap;
import java.util.Map;

import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.cloudstack.usage.UsageTypes;
import org.apache.cloudstack.usage.UsageUnitTypes;
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
import org.apache.commons.lang3.StringUtils;

public class QuotaTypes extends UsageTypes {
private final Integer quotaType;
Expand Down Expand Up @@ -56,6 +59,9 @@ public class QuotaTypes extends UsageTypes {
quotaTypeList.put(VM_SNAPSHOT_ON_PRIMARY, new QuotaTypes(VM_SNAPSHOT_ON_PRIMARY, "VM_SNAPSHOT_ON_PRIMARY", UsageUnitTypes.GB_MONTH.toString(), "VM Snapshot primary storage usage"));
quotaTypeList.put(BACKUP, new QuotaTypes(BACKUP, "BACKUP", UsageUnitTypes.GB_MONTH.toString(), "Backup storage usage"));
quotaTypeList.put(BUCKET, new QuotaTypes(BUCKET, "BUCKET", UsageUnitTypes.GB_MONTH.toString(), "Object Store bucket usage"));
quotaTypeList.put(NETWORK, new QuotaTypes(NETWORK, "NETWORK", UsageUnitTypes.COMPUTE_MONTH.toString(), "Network usage"));
quotaTypeList.put(VPC, new QuotaTypes(VPC, "VPC", UsageUnitTypes.COMPUTE_MONTH.toString(), "VPC usage"));
quotaTypeList.put(BACKUP_OBJECT, new QuotaTypes(BACKUP_OBJECT, "BACKUP_OBJECT", UsageUnitTypes.GB_MONTH.toString(), "Backup object usage."));
quotaTypeMap = Collections.unmodifiableMap(quotaTypeList);
}

Expand Down Expand Up @@ -98,4 +104,27 @@ static public String getDescription(int quotaType) {
}
return null;
}

static public QuotaTypes getQuotaType(int quotaType) {
return quotaTypeMap.get(quotaType);
}

static public QuotaTypes getQuotaTypeByName(String name) {
if (StringUtils.isBlank(name)) {
throw new CloudRuntimeException("Could not retrieve Quota type by name because the value passed as parameter is null, empty, or blank.");
}

for (QuotaTypes type : quotaTypeMap.values()) {
if (type.getQuotaName().equals(name)) {
return type;
}
}

throw new CloudRuntimeException(String.format("Could not find Quota type with name [%s].", name));
}

@Override
public String toString() {
return ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "quotaType", "quotaName");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@
@RunWith(MockitoJUnitRunner.class)
public class ValueTest {

@Test
public void setIdTestAddFieldIdToCollection() {
Value variable = new Value();
variable.setId(null);
Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("id"));
}

@Test
public void setNameTestAddFieldNameToCollection() {
Value variable = new Value();
variable.setName(null);
Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("name"));
}

@Test
public void setHostTestAddFieldHostToCollection() {
Value variable = new Value();
Expand Down
Loading