Skip to content

Commit a0ffad2

Browse files
committed
Fix injection of preset variables into the JS interpreter
1 parent 8db065a commit a0ffad2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+105
-1135
lines changed

framework/quota/src/main/java/org/apache/cloudstack/quota/QuotaManagerImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ protected BigDecimal getQuotaTariffValueToBeApplied(QuotaTariffVO quotaTariff, J
428428
}
429429

430430
injectPresetVariablesIntoJsInterpreter(jsInterpreter, presetVariables);
431-
jsInterpreter.injectVariable("lastTariffs", lastAppliedTariffsList.toString());
431+
jsInterpreter.injectVariable("lastTariffs", lastAppliedTariffsList);
432432

433433
String scriptResult = jsInterpreter.executeScript(activationRule).toString();
434434

@@ -458,18 +458,18 @@ protected BigDecimal getQuotaTariffValueToBeApplied(QuotaTariffVO quotaTariff, J
458458
protected void injectPresetVariablesIntoJsInterpreter(JsInterpreter jsInterpreter, PresetVariables presetVariables) {
459459
jsInterpreter.discardCurrentVariables();
460460

461-
jsInterpreter.injectVariable("account", presetVariables.getAccount().toString());
462-
jsInterpreter.injectVariable("domain", presetVariables.getDomain().toString());
461+
jsInterpreter.injectVariable("account", presetVariables.getAccount());
462+
jsInterpreter.injectVariable("domain", presetVariables.getDomain());
463463

464464
GenericPresetVariable project = presetVariables.getProject();
465465
if (project != null) {
466-
jsInterpreter.injectVariable("project", project.toString());
466+
jsInterpreter.injectVariable("project", project);
467467

468468
}
469469

470470
jsInterpreter.injectVariable("resourceType", presetVariables.getResourceType());
471-
jsInterpreter.injectVariable("value", presetVariables.getValue().toString());
472-
jsInterpreter.injectVariable("zone", presetVariables.getZone().toString());
471+
jsInterpreter.injectVariable("value", presetVariables.getValue());
472+
jsInterpreter.injectVariable("zone", presetVariables.getZone());
473473
}
474474

475475
/**

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public Role getRole() {
2828

2929
public void setRole(Role role) {
3030
this.role = role;
31-
fieldNamesToIncludeInToString.add("role");
3231
}
3332

3433
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@ public String getExternalId() {
2929

3030
public void setExternalId(String externalId) {
3131
this.externalId = externalId;
32-
fieldNamesToIncludeInToString.add("externalId");
3332
}
3433
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public boolean isCustomized() {
2727

2828
public void setCustomized(boolean customized) {
2929
this.customized = customized;
30-
fieldNamesToIncludeInToString.add("customized");
3130
}
3231

3332
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public String getPath() {
2727

2828
public void setPath(String path) {
2929
this.path = path;
30-
fieldNamesToIncludeInToString.add("path");
3130
}
3231

3332
}

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717

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

20-
import java.util.HashSet;
21-
import java.util.Set;
22-
23-
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
20+
import org.apache.commons.lang3.builder.ToStringBuilder;
21+
import org.apache.commons.lang3.builder.ToStringStyle;
2422

2523
public class GenericPresetVariable {
2624
@PresetVariableDefinition(description = "ID of the resource.")
@@ -29,15 +27,12 @@ public class GenericPresetVariable {
2927
@PresetVariableDefinition(description = "Name of the resource.")
3028
private String name;
3129

32-
protected transient Set<String> fieldNamesToIncludeInToString = new HashSet<>();
33-
3430
public String getId() {
3531
return id;
3632
}
3733

3834
public void setId(String id) {
3935
this.id = id;
40-
fieldNamesToIncludeInToString.add("id");
4136
}
4237

4338
public String getName() {
@@ -46,15 +41,10 @@ public String getName() {
4641

4742
public void setName(String name) {
4843
this.name = name;
49-
fieldNamesToIncludeInToString.add("name");
5044
}
5145

52-
/***
53-
* Converts the preset variable into a valid JSON object that will be injected into the JS interpreter.
54-
* This method should not be overridden or changed.
55-
*/
5646
@Override
57-
public final String toString() {
58-
return ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, fieldNamesToIncludeInToString.toArray(new String[0]));
47+
public String toString() {
48+
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
5949
}
6050
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public List<String> getTags() {
3232

3333
public void setTags(List<String> tags) {
3434
this.tags = tags;
35-
fieldNamesToIncludeInToString.add("tags");
3635
}
3736

3837
public Boolean getIsTagARule() {
@@ -41,6 +40,5 @@ public Boolean getIsTagARule() {
4140

4241
public void setIsTagARule(Boolean isTagARule) {
4342
this.isTagARule = isTagARule;
44-
fieldNamesToIncludeInToString.add("isTagARule");
4543
}
4644
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ protected Role getPresetVariableRole(Long roleId) {
243243
Role role = new Role();
244244
role.setId(roleVo.getUuid());
245245
role.setName(roleVo.getName());
246-
role.setType(roleVo.getRoleType());
246+
role.setType(roleVo.getRoleType().toString());
247247

248248
return role;
249249
}
@@ -490,7 +490,7 @@ protected void loadPresetVariableValueForVolume(UsageVO usageRecord, Value value
490490
value.setDiskOffering(getPresetVariableValueDiskOffering(volumeVo.getDiskOfferingId()));
491491
value.setId(volumeVo.getUuid());
492492
value.setName(volumeVo.getName());
493-
value.setProvisioningType(volumeVo.getProvisioningType());
493+
value.setProvisioningType(volumeVo.getProvisioningType().toString());
494494

495495
Long poolId = volumeVo.getPoolId();
496496
if (poolId == null) {
@@ -533,7 +533,7 @@ protected Storage getPresetVariableValueStorage(Long storageId, int usageType) {
533533
storage = new Storage();
534534
storage.setId(storagePoolVo.getUuid());
535535
storage.setName(storagePoolVo.getName());
536-
storage.setScope(storagePoolVo.getScope());
536+
storage.setScope(storagePoolVo.getScope().toString());
537537
List<StoragePoolTagVO> storagePoolTagVOList = storagePoolTagsDao.findStoragePoolTags(storageId);
538538
List<String> storageTags = new ArrayList<>();
539539
boolean isTagARule = false;
@@ -602,7 +602,7 @@ protected void loadPresetVariableValueForSnapshot(UsageVO usageRecord, Value val
602602
value.setId(snapshotVo.getUuid());
603603
value.setName(snapshotVo.getName());
604604
value.setSize(ByteScaleUtils.bytesToMebibytes(snapshotVo.getSize()));
605-
value.setSnapshotType(Snapshot.Type.values()[snapshotVo.getSnapshotType()]);
605+
value.setSnapshotType(Snapshot.Type.values()[snapshotVo.getSnapshotType()].toString());
606606
value.setStorage(getPresetVariableValueStorage(getSnapshotDataStoreId(snapshotId, usageRecord.getZoneId()), usageType));
607607
value.setTags(getPresetVariableValueResourceTags(snapshotId, ResourceObjectType.Snapshot));
608608
Hypervisor.HypervisorType hypervisorType = snapshotVo.getHypervisorType();
@@ -671,7 +671,7 @@ protected void loadPresetVariableValueForVmSnapshot(UsageVO usageRecord, Value v
671671
value.setId(vmSnapshotVo.getUuid());
672672
value.setName(vmSnapshotVo.getName());
673673
value.setTags(getPresetVariableValueResourceTags(vmSnapshotId, ResourceObjectType.VMSnapshot));
674-
value.setVmSnapshotType(vmSnapshotVo.getType());
674+
value.setVmSnapshotType(vmSnapshotVo.getType().toString());
675675

676676
VMInstanceVO vmVo = vmInstanceDao.findByIdIncludingRemoved(vmSnapshotVo.getVmId());
677677
if (vmVo != null && vmVo.getHypervisorType() != null) {

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,16 @@
1717

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

20-
import org.apache.cloudstack.acl.RoleType;
21-
2220
public class Role extends GenericPresetVariable {
2321
@PresetVariableDefinition(description = "Role type of the resource's owner.")
24-
private RoleType type;
22+
private String type;
2523

26-
public RoleType getType() {
24+
public String getType() {
2725
return type;
2826
}
2927

30-
public void setType(RoleType type) {
28+
public void setType(String type) {
3129
this.type = type;
32-
fieldNamesToIncludeInToString.add("type");
3330
}
3431

3532
}

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
import java.util.List;
2121

22-
import com.cloud.storage.ScopeType;
23-
2422
public class Storage extends GenericPresetVariable {
2523
@PresetVariableDefinition(description = "List of string representing the tags of the storage where the volume is (i.e.: [\"a\", \"b\"]).")
2624
private List<String> tags;
@@ -29,15 +27,14 @@ public class Storage extends GenericPresetVariable {
2927
private Boolean isTagARule;
3028

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

3432
public List<String> getTags() {
3533
return tags;
3634
}
3735

3836
public void setTags(List<String> tags) {
3937
this.tags = tags;
40-
fieldNamesToIncludeInToString.add("tags");
4138
}
4239

4340
public Boolean getIsTagARule() {
@@ -46,16 +43,14 @@ public Boolean getIsTagARule() {
4643

4744
public void setIsTagARule(Boolean isTagARule) {
4845
this.isTagARule = isTagARule;
49-
fieldNamesToIncludeInToString.add("isTagARule");
5046
}
5147

52-
public ScopeType getScope() {
48+
public String getScope() {
5349
return scope;
5450
}
5551

56-
public void setScope(ScopeType scope) {
52+
public void setScope(String scope) {
5753
this.scope = scope;
58-
fieldNamesToIncludeInToString.add("scope");
5954
}
6055

6156
}

0 commit comments

Comments
 (0)