Skip to content

Commit 1cf1786

Browse files
[Quota] Add API to list preset variables (#8372)
* Add API for listing Quota preset variables * Add new line at EOF * Address review * Remove usage types * Remove usage types from quotatypes * Remove unused imports * Add space for preset variable definition description Co-authored-by: Bernardo De Marco Gonçalves <bernardomg2004@gmail.com> --------- Co-authored-by: Bernardo De Marco Gonçalves <bernardomg2004@gmail.com>
1 parent cb48202 commit 1cf1786

File tree

20 files changed

+444
-2
lines changed

20 files changed

+444
-2
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

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

20-
public class Account extends GenericPresetVariable{
20+
public class Account extends GenericPresetVariable {
21+
@PresetVariableDefinition(description = "Role of the account. This field will not exist if the account is a project.")
22+
2123
private Role role;
2224

2325
public Role getRole() {

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

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.cloudstack.quota.activationrule.presetvariables;
2121

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

2526
public String getExternalId() {

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

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.cloudstack.quota.activationrule.presetvariables;
1919

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

2324
public boolean isCustomized() {

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

+5
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@
2121
import org.apache.commons.lang3.builder.ToStringStyle;
2222

2323
public class ComputingResources {
24+
@PresetVariableDefinition(description = "Current VM's memory (in MiB).")
2425
private Integer memory;
26+
27+
@PresetVariableDefinition(description = "Current VM's vCPUs.")
2528
private Integer cpuNumber;
29+
30+
@PresetVariableDefinition(description = "Current VM's CPU speed (in MHz).")
2631
private Integer cpuSpeed;
2732

2833
public Integer getMemory() {

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

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.cloudstack.quota.activationrule.presetvariables;
1919

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

2324
public String getPath() {

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

+4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@
2323
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
2424

2525
public class GenericPresetVariable {
26+
@PresetVariableDefinition(description = "ID of the resource.")
2627
private String id;
28+
29+
@PresetVariableDefinition(description = "Name of the resource.")
2730
private String name;
31+
2832
protected transient Set<String> fieldNamesToIncludeInToString = new HashSet<>();
2933

3034
public String getId() {

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

+2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import java.util.List;
2121

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

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

2729
public List<String> getTags() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
// 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+
package org.apache.cloudstack.quota.activationrule.presetvariables;
18+
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.Target;
21+
22+
import static java.lang.annotation.ElementType.FIELD;
23+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
24+
25+
/**
26+
* Describes the preset variable and indicates to which Quota usage types it is loaded.
27+
*/
28+
@Target(FIELD)
29+
@Retention(RUNTIME)
30+
public @interface PresetVariableDefinition {
31+
/**
32+
* An array indicating for which Quota usage types the preset variable is loaded.
33+
* @return an array with the usage types for which the preset variable is loaded.
34+
*/
35+
int[] supportedTypes() default 0;
36+
37+
/**
38+
* A {@link String} describing the preset variable.
39+
* @return the description of the preset variable.
40+
*/
41+
String description() default "";
42+
}

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

+11
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,22 @@
1919

2020
public class PresetVariables {
2121

22+
@PresetVariableDefinition(description = "Account owner of the resource.")
2223
private Account account;
24+
25+
@PresetVariableDefinition(description = "Domain owner of the resource.")
2326
private Domain domain;
27+
28+
@PresetVariableDefinition(description = "Project owner of the resource. This field will not exist if the resource belongs to an account.")
2429
private GenericPresetVariable project;
30+
31+
@PresetVariableDefinition(description = "Type of the record used. Examples for this are: VirtualMachine, DomainRouter, SourceNat, KVM.")
2532
private String resourceType;
33+
34+
@PresetVariableDefinition(description = "Data related to the resource being processed.")
2635
private Value value;
36+
37+
@PresetVariableDefinition(description = "Zone where the resource is.")
2738
private GenericPresetVariable zone;
2839

2940
public Account getAccount() {

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

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.cloudstack.acl.RoleType;
2121

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

2526
public RoleType getType() {

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

+4
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222
import com.cloud.storage.ScopeType;
2323

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

28+
@PresetVariableDefinition(description = "Whether the tag is a rule interpreted in JavaScript. Applicable only for primary storages.")
2729
private Boolean isTagARule;
30+
31+
@PresetVariableDefinition(description = "Scope of the storage where the volume is. Values can be: ZONE, CLUSTER or HOST. Applicable only for primary storages.")
2832
private ScopeType scope;
2933

3034
public List<String> getTags() {

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

+50
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,75 @@
2323
import com.cloud.storage.Snapshot;
2424
import com.cloud.storage.Storage.ProvisioningType;
2525
import com.cloud.vm.snapshot.VMSnapshot;
26+
import org.apache.cloudstack.quota.constant.QuotaTypes;
2627

2728
public class Value extends GenericPresetVariable {
29+
30+
@PresetVariableDefinition(description = "ID of the resource.", supportedTypes = {QuotaTypes.ALLOCATED_VM, QuotaTypes.RUNNING_VM, QuotaTypes.VOLUME, QuotaTypes.TEMPLATE,
31+
QuotaTypes.ISO, QuotaTypes.SNAPSHOT, QuotaTypes.NETWORK_OFFERING, QuotaTypes.VM_SNAPSHOT})
32+
private String id;
33+
34+
@PresetVariableDefinition(description = "Name of the resource.", supportedTypes = {QuotaTypes.ALLOCATED_VM, QuotaTypes.RUNNING_VM, QuotaTypes.VOLUME, QuotaTypes.TEMPLATE,
35+
QuotaTypes.ISO, QuotaTypes.SNAPSHOT, QuotaTypes.NETWORK_OFFERING, QuotaTypes.VM_SNAPSHOT})
36+
private String name;
37+
38+
@PresetVariableDefinition(description = "Host where the VM is running.", supportedTypes = {QuotaTypes.RUNNING_VM})
2839
private Host host;
40+
41+
@PresetVariableDefinition(description = "OS of the VM/template.", supportedTypes = {QuotaTypes.RUNNING_VM, QuotaTypes.ALLOCATED_VM, QuotaTypes.TEMPLATE, QuotaTypes.ISO})
2942
private String osName;
43+
44+
@PresetVariableDefinition(description = "A list of resources of the account between the start and end date of the usage record being calculated " +
45+
"(i.e.: [{zoneId: ..., domainId:...}]).")
3046
private List<Resource> accountResources;
47+
48+
@PresetVariableDefinition(supportedTypes = {QuotaTypes.ALLOCATED_VM, QuotaTypes.RUNNING_VM, QuotaTypes.VOLUME, QuotaTypes.TEMPLATE, QuotaTypes.ISO, QuotaTypes.SNAPSHOT,
49+
QuotaTypes.VM_SNAPSHOT}, description = "List of tags of the resource in the format key:value (i.e.: {\"a\":\"b\", \"c\":\"d\"}).")
3150
private Map<String, String> tags;
51+
52+
@PresetVariableDefinition(description = "Tag of the network offering.", supportedTypes = {QuotaTypes.NETWORK_OFFERING})
3253
private String tag;
54+
55+
@PresetVariableDefinition(description = "Size of the resource (in MiB).", supportedTypes = {QuotaTypes.TEMPLATE, QuotaTypes.ISO, QuotaTypes.VOLUME, QuotaTypes.SNAPSHOT,
56+
QuotaTypes.BACKUP})
3357
private Long size;
58+
59+
@PresetVariableDefinition(description = "Virtual size of the backup.", supportedTypes = {QuotaTypes.BACKUP})
3460
private Long virtualSize;
61+
62+
@PresetVariableDefinition(description = "Provisioning type of the resource. Values can be: thin, sparse or fat.", supportedTypes = {QuotaTypes.VOLUME})
3563
private ProvisioningType provisioningType;
64+
65+
@PresetVariableDefinition(description = "Type of the snapshot. Values can be: MANUAL, RECURRING, HOURLY, DAILY, WEEKLY and MONTHLY.", supportedTypes = {QuotaTypes.SNAPSHOT})
3666
private Snapshot.Type snapshotType;
67+
68+
@PresetVariableDefinition(description = "Type of the VM snapshot. Values can be: Disk or DiskAndMemory.", supportedTypes = {QuotaTypes.VM_SNAPSHOT})
3769
private VMSnapshot.Type vmSnapshotType;
70+
71+
@PresetVariableDefinition(description = "Computing offering of the VM.", supportedTypes = {QuotaTypes.RUNNING_VM, QuotaTypes.ALLOCATED_VM})
3872
private ComputeOffering computeOffering;
73+
74+
@PresetVariableDefinition(description = "Template/ISO with which the VM was created.", supportedTypes = {QuotaTypes.RUNNING_VM, QuotaTypes.ALLOCATED_VM})
3975
private GenericPresetVariable template;
76+
77+
@PresetVariableDefinition(description = "Disk offering of the volume.", supportedTypes = {QuotaTypes.VOLUME})
4078
private GenericPresetVariable diskOffering;
79+
80+
@PresetVariableDefinition(description = "Storage where the volume or snapshot is. While handling with snapshots, this value can be from the primary storage if the global " +
81+
"setting 'snapshot.backup.to.secondary' is false, otherwise it will be from secondary storage.", supportedTypes = {QuotaTypes.VOLUME, QuotaTypes.SNAPSHOT})
4182
private Storage storage;
83+
84+
@PresetVariableDefinition(description = "Computing resources consumed by the VM.", supportedTypes = {QuotaTypes.RUNNING_VM})
4285
private ComputingResources computingResources;
86+
87+
@PresetVariableDefinition(description = "Backup offering of the backup.", supportedTypes = {QuotaTypes.BACKUP})
4388
private BackupOffering backupOffering;
89+
90+
@PresetVariableDefinition(description = "The hypervisor where the resource was deployed. Values can be: XenServer, KVM, VMware, Hyperv, BareMetal, Ovm, Ovm3 and LXC.",
91+
supportedTypes = {QuotaTypes.RUNNING_VM, QuotaTypes.ALLOCATED_VM, QuotaTypes.VM_SNAPSHOT, QuotaTypes.SNAPSHOT})
4492
private String hypervisorType;
93+
94+
@PresetVariableDefinition(description = "The volume format. Values can be: RAW, VHD, VHDX, OVA and QCOW2.", supportedTypes = {QuotaTypes.VOLUME, QuotaTypes.VOLUME_SECONDARY})
4595
private String volumeFormat;
4696
private String state;
4797

framework/quota/src/main/java/org/apache/cloudstack/quota/constant/QuotaTypes.java

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.apache.cloudstack.usage.UsageTypes;
2424
import org.apache.cloudstack.usage.UsageUnitTypes;
25+
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
2526

2627
public class QuotaTypes extends UsageTypes {
2728
private final Integer quotaType;
@@ -100,4 +101,13 @@ static public String getDescription(int quotaType) {
100101
}
101102
return null;
102103
}
104+
105+
static public QuotaTypes getQuotaType(int quotaType) {
106+
return quotaTypeMap.get(quotaType);
107+
}
108+
109+
@Override
110+
public String toString() {
111+
return ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "quotaType", "quotaName");
112+
}
103113
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@
2525
@RunWith(MockitoJUnitRunner.class)
2626
public class ValueTest {
2727

28+
@Test
29+
public void setIdTestAddFieldIdToCollection() {
30+
Value variable = new Value();
31+
variable.setId(null);
32+
Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("id"));
33+
}
34+
35+
@Test
36+
public void setNameTestAddFieldNameToCollection() {
37+
Value variable = new Value();
38+
variable.setName(null);
39+
Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("name"));
40+
}
41+
2842
@Test
2943
public void setHostTestAddFieldHostToCollection() {
3044
Value variable = new Value();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
package org.apache.cloudstack.api.command;
18+
19+
import com.cloud.exception.InvalidParameterValueException;
20+
import com.cloud.user.Account;
21+
import org.apache.cloudstack.api.APICommand;
22+
import org.apache.cloudstack.api.ApiConstants;
23+
import org.apache.cloudstack.api.BaseCmd;
24+
import org.apache.cloudstack.api.Parameter;
25+
import org.apache.cloudstack.api.response.ListResponse;
26+
import org.apache.cloudstack.api.response.QuotaPresetVariablesItemResponse;
27+
import org.apache.cloudstack.api.response.QuotaResponseBuilder;
28+
import org.apache.cloudstack.quota.constant.QuotaTypes;
29+
30+
import javax.inject.Inject;
31+
import java.util.List;
32+
33+
@APICommand(name = "quotaPresetVariablesList", responseObject = QuotaPresetVariablesItemResponse.class, description = "List the preset variables available for using in the " +
34+
"Quota tariff activation rules given the usage type.", since = "4.20", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
35+
public class QuotaPresetVariablesListCmd extends BaseCmd {
36+
37+
@Inject
38+
QuotaResponseBuilder quotaResponseBuilder;
39+
40+
@Parameter(name = ApiConstants.USAGE_TYPE, type = CommandType.INTEGER, required = true, description = "The usage type for which the preset variables will be retrieved.")
41+
private Integer quotaType;
42+
43+
@Override
44+
public void execute() {
45+
List<QuotaPresetVariablesItemResponse> responses = quotaResponseBuilder.listQuotaPresetVariables(this);
46+
ListResponse<QuotaPresetVariablesItemResponse> listResponse = new ListResponse<>();
47+
listResponse.setResponses(responses);
48+
listResponse.setResponseName(getCommandName());
49+
setResponseObject(listResponse);
50+
}
51+
52+
public QuotaTypes getQuotaType() {
53+
QuotaTypes quotaTypes = QuotaTypes.getQuotaType(quotaType);
54+
55+
if (quotaTypes == null) {
56+
throw new InvalidParameterValueException(String.format("Usage type not found for value [%s].", quotaType));
57+
}
58+
59+
return quotaTypes;
60+
}
61+
62+
@Override
63+
public long getEntityOwnerId() {
64+
return Account.ACCOUNT_ID_SYSTEM;
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
package org.apache.cloudstack.api.response;
18+
19+
import com.cloud.serializer.Param;
20+
import com.google.gson.annotations.SerializedName;
21+
import org.apache.cloudstack.api.BaseResponse;
22+
23+
public class QuotaPresetVariablesItemResponse extends BaseResponse {
24+
@SerializedName("variable")
25+
@Param(description = "variable")
26+
private String variable;
27+
28+
@SerializedName("description")
29+
@Param(description = "description")
30+
private String description;
31+
32+
public QuotaPresetVariablesItemResponse() {
33+
super("variables");
34+
}
35+
36+
public void setVariable(String variable) {
37+
this.variable = variable;
38+
}
39+
40+
public String getDescription() {
41+
return description;
42+
}
43+
44+
public void setDescription(String description) {
45+
this.description = description;
46+
}
47+
}

0 commit comments

Comments
 (0)