You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by da...@apache.org on 2026/02/14 16:50:27 UTC
(cloudstack) 01/01: merge attempt 20.3 -> 22.1
This is an automated email from the ASF dual-hosted git repository.
dahn pushed a commit to branch 22-merge-issues
in repository https://gitbox.apache.org/repos/asf/cloudstack.git
commit bb0920123f26a8e0fa7723394fe8daad0f4064ba
Author: Daan Hoogland <da...@apache.org>
AuthorDate: Fri Feb 13 22:30:51 2026 +0100
merge attempt 20.3 -> 22.1
---
.../cloudstack/backup/RestoreBackupCommand.java | 18 +--
.../java/com/cloud/resource/ResourceManager.java | 2 +
.../java/com/cloud/dc/ClusterDetailsDaoImpl.java | 2 +-
.../com/cloud/dc/dao/DataCenterDetailsDaoImpl.java | 2 +-
.../main/java/com/cloud/host/dao/HostDaoImpl.java | 2 +-
.../storage/datastore/db/ImageStoreDaoImpl.java | 2 +-
.../src/main/java/com/cloud/utils/db/Filter.java | 13 +-
.../java/com/cloud/utils/db/GenericDaoBase.java | 6 +-
.../test/java/com/cloud/utils/db/FilterTest.java | 58 +++++++
.../com/cloud/utils/db/GenericDaoBaseTest.java | 68 ++++++++
.../apache/cloudstack/quota/QuotaManagerImpl.java | 12 +-
.../activationrule/presetvariables/Account.java | 2 -
.../presetvariables/BackupOffering.java | 1 -
.../presetvariables/ComputeOffering.java | 2 -
.../presetvariables/Configuration.java | 1 -
.../DiskOfferingPresetVariables.java | 12 --
.../activationrule/presetvariables/Domain.java | 1 -
.../presetvariables/GenericPresetVariable.java | 18 +--
.../quota/activationrule/presetvariables/Host.java | 2 -
.../presetvariables/PresetVariableHelper.java | 10 +-
.../quota/activationrule/presetvariables/Role.java | 9 +-
.../activationrule/presetvariables/Storage.java | 11 +-
.../activationrule/presetvariables/Tariff.java | 1 -
.../activationrule/presetvariables/Value.java | 34 +---
.../cloudstack/quota/QuotaManagerImplTest.java | 24 +--
.../presetvariables/AccountTest.java | 34 ----
.../presetvariables/BackupOfferingTest.java | 36 -----
.../presetvariables/ComputeOfferingTest.java | 35 -----
.../presetvariables/ComputingResourcesTest.java | 40 -----
.../activationrule/presetvariables/DomainTest.java | 35 -----
.../presetvariables/GenericPresetVariableTest.java | 73 ---------
.../activationrule/presetvariables/HostTest.java | 34 ----
.../presetvariables/PresetVariableHelperTest.java | 127 +++++----------
.../presetvariables/ResourceTest.java | 40 -----
.../activationrule/presetvariables/RoleTest.java | 34 ----
.../presetvariables/StorageTest.java | 41 -----
.../activationrule/presetvariables/ValueTest.java | 175 ---------------------
.../cloudstack/backup/NASBackupProvider.java | 27 +++-
.../LibvirtRestoreBackupCommandWrapper.java | 59 ++++---
.../LibvirtRestoreBackupCommandWrapperTest.java | 17 +-
.../cloudstack/metrics/PrometheusExporterImpl.java | 43 +++++
.../metrics/PrometheusExporterImplTest.java | 108 +++++++++++++
.../com/cloud/resource/ResourceManagerImpl.java | 27 +++-
.../storage/heuristics/HeuristicRuleHelper.java | 20 +--
.../heuristics/presetvariables/Account.java | 2 -
.../storage/heuristics/presetvariables/Domain.java | 1 -
.../GenericHeuristicPresetVariable.java | 17 +-
.../presetvariables/SecondaryStorage.java | 4 -
.../heuristics/presetvariables/Snapshot.java | 10 +-
.../heuristics/presetvariables/Template.java | 24 ++-
.../storage/heuristics/presetvariables/Volume.java | 10 +-
.../cloud/resource/MockResourceManagerImpl.java | 5 +
.../heuristics/HeuristicRuleHelperTest.java | 16 ++
.../heuristics/presetvariables/AccountTest.java | 46 ------
.../heuristics/presetvariables/DomainTest.java | 41 -----
.../GenericHeuristicPresetVariableTest.java | 40 -----
.../presetvariables/SecondaryStorageTest.java | 45 ------
.../heuristics/presetvariables/SnapshotTest.java | 44 ------
.../heuristics/presetvariables/TemplateTest.java | 46 ------
.../heuristics/presetvariables/VolumeTest.java | 44 ------
.../storage/template/UploadManagerImpl.java | 51 +++++-
.../storage/template/UploadManagerImplTest.java | 85 ++++++++++
.../java/com/cloud/usage/UsageManagerImpl.java | 20 ++-
.../utils/jsinterpreter/JsInterpreter.java | 33 ++--
.../utils/jsinterpreter/TagAsRuleHelper.java | 21 ++-
.../utils/jsinterpreter/JsInterpreterTest.java | 18 ---
66 files changed, 692 insertions(+), 1249 deletions(-)
diff --git a/core/src/main/java/org/apache/cloudstack/backup/RestoreBackupCommand.java b/core/src/main/java/org/apache/cloudstack/backup/RestoreBackupCommand.java
index 8e68f4f1e41..f5ad5fbea2c 100644
--- a/core/src/main/java/org/apache/cloudstack/backup/RestoreBackupCommand.java
+++ b/core/src/main/java/org/apache/cloudstack/backup/RestoreBackupCommand.java
@@ -34,9 +34,9 @@ public class RestoreBackupCommand extends Command {
private List<String> backupVolumesUUIDs;
private List<PrimaryDataStoreTO> restoreVolumePools;
private List<String> restoreVolumePaths;
+ private List<String> backupFiles;
private String diskType;
private Boolean vmExists;
- private String restoreVolumeUUID;
private VirtualMachine.State vmState;
private Integer mountTimeout;
@@ -92,6 +92,14 @@ public class RestoreBackupCommand extends Command {
this.restoreVolumePaths = restoreVolumePaths;
}
+ public List<String> getBackupFiles() {
+ return backupFiles;
+ }
+
+ public void setBackupFiles(List<String> backupFiles) {
+ this.backupFiles = backupFiles;
+ }
+
public Boolean isVmExists() {
return vmExists;
}
@@ -116,14 +124,6 @@ public class RestoreBackupCommand extends Command {
this.mountOptions = mountOptions;
}
- public String getRestoreVolumeUUID() {
- return restoreVolumeUUID;
- }
-
- public void setRestoreVolumeUUID(String restoreVolumeUUID) {
- this.restoreVolumeUUID = restoreVolumeUUID;
- }
-
public VirtualMachine.State getVmState() {
return vmState;
}
diff --git a/engine/components-api/src/main/java/com/cloud/resource/ResourceManager.java b/engine/components-api/src/main/java/com/cloud/resource/ResourceManager.java
index f4651237f32..e724f5d081b 100755
--- a/engine/components-api/src/main/java/com/cloud/resource/ResourceManager.java
+++ b/engine/components-api/src/main/java/com/cloud/resource/ResourceManager.java
@@ -167,6 +167,8 @@ public interface ResourceManager extends ResourceService, Configurable {
public HostVO findHostByGuid(String guid);
+ HostVO findHostByGuidPrefix(String guid);
+
public HostVO findHostByName(String name);
HostStats getHostStatistics(Host host);
diff --git a/engine/schema/src/main/java/com/cloud/dc/ClusterDetailsDaoImpl.java b/engine/schema/src/main/java/com/cloud/dc/ClusterDetailsDaoImpl.java
index 4c752ff9b4f..a8888f98ad2 100644
--- a/engine/schema/src/main/java/com/cloud/dc/ClusterDetailsDaoImpl.java
+++ b/engine/schema/src/main/java/com/cloud/dc/ClusterDetailsDaoImpl.java
@@ -171,7 +171,7 @@ public class ClusterDetailsDaoImpl extends ResourceDetailsDaoBase<ClusterDetails
@Override
public String getConfigValue(long id, String key) {
ClusterDetailsVO vo = findDetail(id, key);
- return vo == null ? null : vo.getValue();
+ return vo == null ? null : getActualValue(vo);
}
@Override
diff --git a/engine/schema/src/main/java/com/cloud/dc/dao/DataCenterDetailsDaoImpl.java b/engine/schema/src/main/java/com/cloud/dc/dao/DataCenterDetailsDaoImpl.java
index bb03a96d02e..3772f21ca36 100644
--- a/engine/schema/src/main/java/com/cloud/dc/dao/DataCenterDetailsDaoImpl.java
+++ b/engine/schema/src/main/java/com/cloud/dc/dao/DataCenterDetailsDaoImpl.java
@@ -46,7 +46,7 @@ public class DataCenterDetailsDaoImpl extends ResourceDetailsDaoBase<DataCenterD
@Override
public String getConfigValue(long id, String key) {
ResourceDetail vo = findDetail(id, key);
- return vo == null ? null : vo.getValue();
+ return vo == null ? null : getActualValue(vo);
}
@Override
diff --git a/engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java b/engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java
index 8f218841b07..2d8fcca6cdb 100644
--- a/engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java
+++ b/engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java
@@ -1412,7 +1412,7 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
SearchCriteria<HostVO> sc = TypeStatusStateSearch.create();
sc.setParameters("type", Host.Type.Routing);
sc.setParameters("cluster", clusterId);
- List<HostVO> list = listBy(sc, new Filter(1));
+ List<HostVO> list = listBy(sc, new Filter(1, true));
return list.isEmpty() ? null : list.get(0);
}
diff --git a/engine/schema/src/main/java/org/apache/cloudstack/storage/datastore/db/ImageStoreDaoImpl.java b/engine/schema/src/main/java/org/apache/cloudstack/storage/datastore/db/ImageStoreDaoImpl.java
index 4cb40b5eaf6..5f32a350232 100644
--- a/engine/schema/src/main/java/org/apache/cloudstack/storage/datastore/db/ImageStoreDaoImpl.java
+++ b/engine/schema/src/main/java/org/apache/cloudstack/storage/datastore/db/ImageStoreDaoImpl.java
@@ -202,7 +202,7 @@ public class ImageStoreDaoImpl extends GenericDaoBase<ImageStoreVO, Long> implem
sc.setParameters("dataCenterId", dataCenterId);
sc.setParameters("protocol", protocol);
sc.setParameters("role", DataStoreRole.Image);
- Filter filter = new Filter(1);
+ Filter filter = new Filter(1, true);
List<ImageStoreVO> results = listBy(sc, filter);
return results.size() == 0 ? null : results.get(0);
}
diff --git a/framework/db/src/main/java/com/cloud/utils/db/Filter.java b/framework/db/src/main/java/com/cloud/utils/db/Filter.java
index 90e42952a99..375e508c55f 100644
--- a/framework/db/src/main/java/com/cloud/utils/db/Filter.java
+++ b/framework/db/src/main/java/com/cloud/utils/db/Filter.java
@@ -57,7 +57,18 @@ public class Filter {
}
public Filter(long limit) {
- _orderBy = " ORDER BY RAND()";
+ this(limit, false);
+ }
+
+ /**
+ * Constructor for creating a filter with random ordering
+ * @param limit the maximum number of results to return
+ * @param randomize if true, orders results randomly
+ */
+ public Filter(long limit, boolean randomize) {
+ if (randomize) {
+ _orderBy = " ORDER BY RAND()" ;
+ }
_limit = limit;
}
diff --git a/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java b/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java
index 87d9f11d20b..dcd863465d1 100644
--- a/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java
+++ b/framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java
@@ -347,7 +347,7 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
@Override
@DB()
public T lockOneRandomRow(final SearchCriteria<T> sc, final boolean exclusive) {
- final Filter filter = new Filter(1);
+ final Filter filter = new Filter(1, true);
final List<T> beans = search(sc, filter, exclusive, true);
return beans.isEmpty() ? null : beans.get(0);
}
@@ -927,7 +927,7 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
@DB()
protected T findOneIncludingRemovedBy(final SearchCriteria<T> sc) {
- Filter filter = new Filter(1);
+ Filter filter = new Filter(1, true);
List<T> results = searchIncludingRemoved(sc, filter, null, false);
assert results.size() <= 1 : "Didn't the limiting worked?";
return results.size() == 0 ? null : results.get(0);
@@ -1335,7 +1335,7 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
Filter filter = null;
final long batchSizeFinal = ObjectUtils.defaultIfNull(batchSize, 0L);
if (batchSizeFinal > 0) {
- filter = new Filter(null, batchSizeFinal);
+ filter = new Filter(batchSizeFinal);
}
int expunged = 0;
int currentExpunged = 0;
diff --git a/framework/db/src/test/java/com/cloud/utils/db/FilterTest.java b/framework/db/src/test/java/com/cloud/utils/db/FilterTest.java
index 079611ab69f..9f040e7a5e3 100644
--- a/framework/db/src/test/java/com/cloud/utils/db/FilterTest.java
+++ b/framework/db/src/test/java/com/cloud/utils/db/FilterTest.java
@@ -41,4 +41,62 @@ public class FilterTest {
Assert.assertTrue(filter.getOrderBy().split(",").length == 3);
Assert.assertTrue(filter.getOrderBy().split(",")[2].trim().toLowerCase().equals("test.fld_int asc"));
}
+
+
+ @Test
+ public void testFilterWithLimitOnly() {
+ Filter filter = new Filter(5);
+
+ Assert.assertEquals(Long.valueOf(5), filter.getLimit());
+ Assert.assertNull(filter.getOrderBy());
+ Assert.assertNull(filter.getOffset());
+ }
+
+ @Test
+ public void testFilterWithLimitAndRandomizeFalse() {
+ Filter filter = new Filter(10, false);
+
+ Assert.assertEquals(Long.valueOf(10), filter.getLimit());
+ Assert.assertNull(filter.getOrderBy());
+ Assert.assertNull(filter.getOffset());
+ }
+
+ @Test
+ public void testFilterWithLimitAndRandomizeTrue() {
+ Filter filter = new Filter(3, true);
+
+ Assert.assertNull(filter.getLimit());
+ Assert.assertNotNull(filter.getOrderBy());
+ Assert.assertTrue(filter.getOrderBy().contains("ORDER BY RAND()"));
+ Assert.assertTrue(filter.getOrderBy().contains("LIMIT 3"));
+ Assert.assertEquals(" ORDER BY RAND() LIMIT 3", filter.getOrderBy());
+ }
+
+ @Test
+ public void testFilterRandomizeWithDifferentLimits() {
+ Filter filter1 = new Filter(1, true);
+ Filter filter10 = new Filter(10, true);
+ Filter filter100 = new Filter(100, true);
+
+ Assert.assertEquals(" ORDER BY RAND() LIMIT 1", filter1.getOrderBy());
+ Assert.assertEquals(" ORDER BY RAND() LIMIT 10", filter10.getOrderBy());
+ Assert.assertEquals(" ORDER BY RAND() LIMIT 100", filter100.getOrderBy());
+ }
+
+ @Test
+ public void testFilterConstructorBackwardsCompatibility() {
+ // Test that Filter(long) behaves differently now (no ORDER BY RAND())
+ // compared to Filter(long, true) which preserves old behavior
+ Filter simpleLimitFilter = new Filter(1);
+ Filter randomFilter = new Filter(1, true);
+
+ // Simple limit filter should just set limit
+ Assert.assertEquals(Long.valueOf(1), simpleLimitFilter.getLimit());
+ Assert.assertNull(simpleLimitFilter.getOrderBy());
+
+ // Random filter should set orderBy with RAND()
+ Assert.assertNull(randomFilter.getLimit());
+ Assert.assertNotNull(randomFilter.getOrderBy());
+ Assert.assertTrue(randomFilter.getOrderBy().contains("RAND()"));
+ }
}
diff --git a/framework/db/src/test/java/com/cloud/utils/db/GenericDaoBaseTest.java b/framework/db/src/test/java/com/cloud/utils/db/GenericDaoBaseTest.java
index 308600341c3..ebf514f532f 100644
--- a/framework/db/src/test/java/com/cloud/utils/db/GenericDaoBaseTest.java
+++ b/framework/db/src/test/java/com/cloud/utils/db/GenericDaoBaseTest.java
@@ -20,6 +20,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.List;
import org.junit.Assert;
import org.junit.Before;
@@ -263,4 +264,71 @@ public class GenericDaoBaseTest {
" INNER JOIN tableA tableA2Alias ON tableC.column3=tableA2Alias.column2 " +
" INNER JOIN tableA tableA3Alias ON tableD.column4=tableA3Alias.column3 AND tableD.column5=? ", joinString.toString());
}
+
+
+ @Test
+ public void testLockOneRandomRowUsesRandomFilter() {
+ // Create a mock DAO to test lockOneRandomRow behavior
+ GenericDaoBase<DbTestVO, Long> testDao = Mockito.mock(GenericDaoBase.class);
+
+ // Capture the filter passed to the search method
+ final Filter[] capturedFilter = new Filter[1];
+
+ Mockito.when(testDao.lockOneRandomRow(Mockito.any(SearchCriteria.class), Mockito.anyBoolean()))
+ .thenCallRealMethod();
+
+ Mockito.when(testDao.search(Mockito.any(SearchCriteria.class), Mockito.any(Filter.class),
+ Mockito.anyBoolean(), Mockito.anyBoolean()))
+ .thenAnswer(invocation -> {
+ capturedFilter[0] = invocation.getArgument(1);
+ return new ArrayList<DbTestVO>();
+ });
+
+ SearchCriteria<DbTestVO> sc = Mockito.mock(SearchCriteria.class);
+ testDao.lockOneRandomRow(sc, true);
+
+ // Verify that the filter uses random ordering
+ Assert.assertNotNull(capturedFilter[0]);
+ Assert.assertNotNull(capturedFilter[0].getOrderBy());
+ Assert.assertTrue(capturedFilter[0].getOrderBy().contains("ORDER BY RAND()"));
+ Assert.assertTrue(capturedFilter[0].getOrderBy().contains("LIMIT 1"));
+ }
+
+ @Test
+ public void testLockOneRandomRowReturnsNullOnEmptyResult() {
+ GenericDaoBase<DbTestVO, Long> testDao = Mockito.mock(GenericDaoBase.class);
+
+ Mockito.when(testDao.lockOneRandomRow(Mockito.any(SearchCriteria.class), Mockito.anyBoolean()))
+ .thenCallRealMethod();
+
+ Mockito.when(testDao.search(Mockito.any(SearchCriteria.class), Mockito.any(Filter.class),
+ Mockito.anyBoolean(), Mockito.anyBoolean()))
+ .thenReturn(new ArrayList<DbTestVO>());
+
+ SearchCriteria<DbTestVO> sc = Mockito.mock(SearchCriteria.class);
+ DbTestVO result = testDao.lockOneRandomRow(sc, true);
+
+ Assert.assertNull(result);
+ }
+
+ @Test
+ public void testLockOneRandomRowReturnsFirstElement() {
+ GenericDaoBase<DbTestVO, Long> testDao = Mockito.mock(GenericDaoBase.class);
+ DbTestVO expectedResult = new DbTestVO();
+ List<DbTestVO> resultList = new ArrayList<>();
+ resultList.add(expectedResult);
+
+ Mockito.when(testDao.lockOneRandomRow(Mockito.any(SearchCriteria.class), Mockito.anyBoolean()))
+ .thenCallRealMethod();
+
+ Mockito.when(testDao.search(Mockito.any(SearchCriteria.class), Mockito.any(Filter.class),
+ Mockito.anyBoolean(), Mockito.anyBoolean()))
+ .thenReturn(resultList);
+
+ SearchCriteria<DbTestVO> sc = Mockito.mock(SearchCriteria.class);
+ DbTestVO result = testDao.lockOneRandomRow(sc, true);
+
+ Assert.assertNotNull(result);
+ Assert.assertEquals(expectedResult, result);
+ }
}
diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/QuotaManagerImpl.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/QuotaManagerImpl.java
index 7949259bc82..816144aa2f1 100644
--- a/framework/quota/src/main/java/org/apache/cloudstack/quota/QuotaManagerImpl.java
+++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/QuotaManagerImpl.java
@@ -429,7 +429,7 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
}
injectPresetVariablesIntoJsInterpreter(jsInterpreter, presetVariables);
- jsInterpreter.injectVariable("lastTariffs", lastAppliedTariffsList.toString());
+ jsInterpreter.injectVariable("lastTariffs", lastAppliedTariffsList);
String scriptResult = jsInterpreter.executeScript(activationRule).toString();
@@ -459,12 +459,12 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
protected void injectPresetVariablesIntoJsInterpreter(JsInterpreter jsInterpreter, PresetVariables presetVariables) {
jsInterpreter.discardCurrentVariables();
- jsInterpreter.injectVariable("account", presetVariables.getAccount().toString());
- jsInterpreter.injectVariable("domain", presetVariables.getDomain().toString());
+ jsInterpreter.injectVariable("account", presetVariables.getAccount());
+ jsInterpreter.injectVariable("domain", presetVariables.getDomain());
GenericPresetVariable project = presetVariables.getProject();
if (project != null) {
- jsInterpreter.injectVariable("project", project.toString());
+ jsInterpreter.injectVariable("project", project);
}
@@ -474,8 +474,8 @@ public class QuotaManagerImpl extends ManagerBase implements QuotaManager {
}
jsInterpreter.injectVariable("resourceType", presetVariables.getResourceType());
- jsInterpreter.injectVariable("value", presetVariables.getValue().toString());
- jsInterpreter.injectVariable("zone", presetVariables.getZone().toString());
+ jsInterpreter.injectVariable("value", presetVariables.getValue());
+ jsInterpreter.injectVariable("zone", presetVariables.getZone());
}
/**
diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Account.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Account.java
index 2420d577f10..e34c8d3f31d 100644
--- a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Account.java
+++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Account.java
@@ -36,7 +36,6 @@ public class Account extends GenericPresetVariable {
public void setRole(Role role) {
this.role = role;
- fieldNamesToIncludeInToString.add("role");
}
public String getCreated() {
@@ -45,6 +44,5 @@ public class Account extends GenericPresetVariable {
public void setCreated(Date created) {
this.created = DateUtil.displayDateInTimezone(TimeZone.getTimeZone("GMT"), created);
- fieldNamesToIncludeInToString.add("created");
}
}
diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/BackupOffering.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/BackupOffering.java
index d8457d294ec..e3927d967f7 100644
--- a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/BackupOffering.java
+++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/BackupOffering.java
@@ -29,6 +29,5 @@ public class BackupOffering extends GenericPresetVariable {
public void setExternalId(String externalId) {
this.externalId = externalId;
- fieldNamesToIncludeInToString.add("externalId");
}
}
diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/ComputeOffering.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/ComputeOffering.java
index 09182711ca8..a9ffcb8bb15 100644
--- a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/ComputeOffering.java
+++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/ComputeOffering.java
@@ -32,7 +32,6 @@ public class ComputeOffering extends GenericPresetVariable {
public void setCustomized(boolean customized) {
this.customized = customized;
- fieldNamesToIncludeInToString.add("customized");
}
public boolean offerHa() {
@@ -41,7 +40,6 @@ public class ComputeOffering extends GenericPresetVariable {
public void setOfferHa(boolean offerHa) {
this.offerHa = offerHa;
- fieldNamesToIncludeInToString.add("offerHa");
}
}
diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Configuration.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Configuration.java
index e59f78af8d9..48fee552c5a 100644
--- a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Configuration.java
+++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Configuration.java
@@ -30,6 +30,5 @@ public class Configuration extends GenericPresetVariable{
public void setForceHa(boolean forceHa) {
this.forceHa = forceHa;
- fieldNamesToIncludeInToString.add("forceHa");
}
}
diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/DiskOfferingPresetVariables.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/DiskOfferingPresetVariables.java
index b2f5f69502f..68ba6a331eb 100644
--- a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/DiskOfferingPresetVariables.java
+++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/DiskOfferingPresetVariables.java
@@ -61,7 +61,6 @@ public class DiskOfferingPresetVariables extends GenericPresetVariable {
public void setBytesReadRate(Long bytesReadRate) {
this.bytesReadRate = bytesReadRate;
- fieldNamesToIncludeInToString.add("bytesReadRate");
}
public Long getBytesReadBurst() {
@@ -70,7 +69,6 @@ public class DiskOfferingPresetVariables extends GenericPresetVariable {
public void setBytesReadBurst(Long bytesReadBurst) {
this.bytesReadBurst = bytesReadBurst;
- fieldNamesToIncludeInToString.add("bytesReadBurst");
}
public Long getBytesReadBurstLength() {
@@ -79,7 +77,6 @@ public class DiskOfferingPresetVariables extends GenericPresetVariable {
public void setBytesReadBurstLength(Long bytesReadBurstLength) {
this.bytesReadBurstLength = bytesReadBurstLength;
- fieldNamesToIncludeInToString.add("bytesReadBurstLength");
}
public Long getBytesWriteRate() {
@@ -88,7 +85,6 @@ public class DiskOfferingPresetVariables extends GenericPresetVariable {
public void setBytesWriteRate(Long bytesWriteRate) {
this.bytesWriteRate = bytesWriteRate;
- fieldNamesToIncludeInToString.add("bytesWriteRate");
}
public Long getBytesWriteBurst() {
@@ -97,7 +93,6 @@ public class DiskOfferingPresetVariables extends GenericPresetVariable {
public void setBytesWriteBurst(Long bytesWriteBurst) {
this.bytesWriteBurst = bytesWriteBurst;
- fieldNamesToIncludeInToString.add("bytesWriteBurst");
}
public Long getBytesWriteBurstLength() {
@@ -106,7 +101,6 @@ public class DiskOfferingPresetVariables extends GenericPresetVariable {
public void setBytesWriteBurstLength(Long bytesWriteBurstLength) {
this.bytesWriteBurstLength = bytesWriteBurstLength;
- fieldNamesToIncludeInToString.add("bytesWriteBurstLength");
}
public Long getIopsReadRate() {
@@ -115,7 +109,6 @@ public class DiskOfferingPresetVariables extends GenericPresetVariable {
public void setIopsReadRate(Long iopsReadRate) {
this.iopsReadRate = iopsReadRate;
- fieldNamesToIncludeInToString.add("iopsReadRate");
}
public Long getIopsReadBurst() {
@@ -124,7 +117,6 @@ public class DiskOfferingPresetVariables extends GenericPresetVariable {
public void setIopsReadBurst(Long iopsReadBurst) {
this.iopsReadBurst = iopsReadBurst;
- fieldNamesToIncludeInToString.add("iopsReadBurst");
}
public Long getIopsReadBurstLength() {
@@ -133,7 +125,6 @@ public class DiskOfferingPresetVariables extends GenericPresetVariable {
public void setIopsReadBurstLength(Long iopsReadBurstLength) {
this.iopsReadBurstLength = iopsReadBurstLength;
- fieldNamesToIncludeInToString.add("iopsReadBurstLength");
}
public Long getIopsWriteRate() {
@@ -142,7 +133,6 @@ public class DiskOfferingPresetVariables extends GenericPresetVariable {
public void setIopsWriteRate(Long iopsWriteRate) {
this.iopsWriteRate = iopsWriteRate;
- fieldNamesToIncludeInToString.add("iopsWriteRate");
}
public Long getIopsWriteBurst() {
@@ -151,7 +141,6 @@ public class DiskOfferingPresetVariables extends GenericPresetVariable {
public void setIopsWriteBurst(Long iopsWriteBurst) {
this.iopsWriteBurst = iopsWriteBurst;
- fieldNamesToIncludeInToString.add("iopsWriteBurst");
}
public Long getIopsWriteBurstLength() {
@@ -160,6 +149,5 @@ public class DiskOfferingPresetVariables extends GenericPresetVariable {
public void setIopsWriteBurstLength(Long iopsWriteBurstLength) {
this.iopsWriteBurstLength = iopsWriteBurstLength;
- fieldNamesToIncludeInToString.add("iopsWriteBurstLength");
}
}
diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Domain.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Domain.java
index 6d83da4cd8f..cbdfa3e4bb4 100644
--- a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Domain.java
+++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Domain.java
@@ -27,7 +27,6 @@ public class Domain extends GenericPresetVariable {
public void setPath(String path) {
this.path = path;
- fieldNamesToIncludeInToString.add("path");
}
}
diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/GenericPresetVariable.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/GenericPresetVariable.java
index 7073d2760d7..4db099ca479 100644
--- a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/GenericPresetVariable.java
+++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/GenericPresetVariable.java
@@ -17,10 +17,8 @@
package org.apache.cloudstack.quota.activationrule.presetvariables;
-import java.util.HashSet;
-import java.util.Set;
-
-import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.commons.lang3.builder.ToStringStyle;
public class GenericPresetVariable {
@PresetVariableDefinition(description = "ID of the resource.")
@@ -29,15 +27,12 @@ public class GenericPresetVariable {
@PresetVariableDefinition(description = "Name of the resource.")
private String name;
- protected transient Set<String> fieldNamesToIncludeInToString = new HashSet<>();
-
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
- fieldNamesToIncludeInToString.add("id");
}
public String getName() {
@@ -46,15 +41,10 @@ public class GenericPresetVariable {
public void setName(String name) {
this.name = name;
- fieldNamesToIncludeInToString.add("name");
}
- /***
- * Converts the preset variable into a valid JSON object that will be injected into the JS interpreter.
- * This method should not be overridden or changed.
- */
@Override
- public final String toString() {
- return ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, fieldNamesToIncludeInToString.toArray(new String[0]));
+ public String toString() {
+ return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
}
}
diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Host.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Host.java
index 4a0fd2f5a07..6d54a143834 100644
--- a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Host.java
+++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Host.java
@@ -32,7 +32,6 @@ public class Host extends GenericPresetVariable {
public void setTags(List<String> tags) {
this.tags = tags;
- fieldNamesToIncludeInToString.add("tags");
}
public Boolean getIsTagARule() {
@@ -41,6 +40,5 @@ public class Host extends GenericPresetVariable {
public void setIsTagARule(Boolean isTagARule) {
this.isTagARule = isTagARule;
- fieldNamesToIncludeInToString.add("isTagARule");
}
}
diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelper.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelper.java
index 2a6ad132f63..78ffa51eb12 100644
--- a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelper.java
+++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelper.java
@@ -254,7 +254,7 @@ public class PresetVariableHelper {
Role role = new Role();
role.setId(roleVo.getUuid());
role.setName(roleVo.getName());
- role.setType(roleVo.getRoleType());
+ role.setType(roleVo.getRoleType().toString());
return role;
}
@@ -538,8 +538,8 @@ public class PresetVariableHelper {
value.setDiskOffering(getPresetVariableValueDiskOffering(volumeVo.getDiskOfferingId()));
value.setId(volumeVo.getUuid());
value.setName(volumeVo.getName());
- value.setProvisioningType(volumeVo.getProvisioningType());
value.setVolumeType(volumeVo.getVolumeType());
+ value.setProvisioningType(volumeVo.getProvisioningType());
Long poolId = volumeVo.getPoolId();
if (poolId == null) {
@@ -594,7 +594,7 @@ public class PresetVariableHelper {
storage = new Storage();
storage.setId(storagePoolVo.getUuid());
storage.setName(storagePoolVo.getName());
- storage.setScope(storagePoolVo.getScope());
+ storage.setScope(storagePoolVo.getScope().toString());
List<StoragePoolTagVO> storagePoolTagVOList = storagePoolTagsDao.findStoragePoolTags(storageId);
List<String> storageTags = new ArrayList<>();
boolean isTagARule = false;
@@ -663,7 +663,7 @@ public class PresetVariableHelper {
value.setId(snapshotVo.getUuid());
value.setName(snapshotVo.getName());
value.setSize(ByteScaleUtils.bytesToMebibytes(snapshotVo.getSize()));
- value.setSnapshotType(Snapshot.Type.values()[snapshotVo.getSnapshotType()]);
+ value.setSnapshotType(Snapshot.Type.values()[snapshotVo.getSnapshotType()].toString());
value.setStorage(getPresetVariableValueStorage(getSnapshotDataStoreId(snapshotId, usageRecord.getZoneId()), usageType));
value.setTags(getPresetVariableValueResourceTags(snapshotId, ResourceObjectType.Snapshot));
Hypervisor.HypervisorType hypervisorType = snapshotVo.getHypervisorType();
@@ -732,7 +732,7 @@ public class PresetVariableHelper {
value.setId(vmSnapshotVo.getUuid());
value.setName(vmSnapshotVo.getName());
value.setTags(getPresetVariableValueResourceTags(vmSnapshotId, ResourceObjectType.VMSnapshot));
- value.setVmSnapshotType(vmSnapshotVo.getType());
+ value.setVmSnapshotType(vmSnapshotVo.getType().toString());
VMInstanceVO vmVo = vmInstanceDao.findByIdIncludingRemoved(vmSnapshotVo.getVmId());
if (vmVo != null && vmVo.getHypervisorType() != null) {
diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Role.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Role.java
index 3f953b3a4ff..3c61786cb0a 100644
--- a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Role.java
+++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Role.java
@@ -17,19 +17,16 @@
package org.apache.cloudstack.quota.activationrule.presetvariables;
-import org.apache.cloudstack.acl.RoleType;
-
public class Role extends GenericPresetVariable {
@PresetVariableDefinition(description = "Role type of the resource's owner.")
- private RoleType type;
+ private String type;
- public RoleType getType() {
+ public String getType() {
return type;
}
- public void setType(RoleType type) {
+ public void setType(String type) {
this.type = type;
- fieldNamesToIncludeInToString.add("type");
}
}
diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Storage.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Storage.java
index 9b6cfb31092..8ddae82f383 100644
--- a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Storage.java
+++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Storage.java
@@ -19,8 +19,6 @@ package org.apache.cloudstack.quota.activationrule.presetvariables;
import java.util.List;
-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;
@@ -29,7 +27,7 @@ public class Storage extends GenericPresetVariable {
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;
+ private String scope;
public List<String> getTags() {
return tags;
@@ -37,7 +35,6 @@ public class Storage extends GenericPresetVariable {
public void setTags(List<String> tags) {
this.tags = tags;
- fieldNamesToIncludeInToString.add("tags");
}
public Boolean getIsTagARule() {
@@ -46,16 +43,14 @@ public class Storage extends GenericPresetVariable {
public void setIsTagARule(Boolean isTagARule) {
this.isTagARule = isTagARule;
- fieldNamesToIncludeInToString.add("isTagARule");
}
- public ScopeType getScope() {
+ public String getScope() {
return scope;
}
- public void setScope(ScopeType scope) {
+ public void setScope(String scope) {
this.scope = scope;
- fieldNamesToIncludeInToString.add("scope");
}
}
diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Tariff.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Tariff.java
index 3703820a1a4..9414908b3a2 100644
--- a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Tariff.java
+++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Tariff.java
@@ -28,6 +28,5 @@ public class Tariff extends GenericPresetVariable {
public void setValue(BigDecimal value) {
this.value = value;
- fieldNamesToIncludeInToString.add("value");
}
}
diff --git a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Value.java b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Value.java
index 77e539db0f3..63f92501f1a 100644
--- a/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Value.java
+++ b/framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/Value.java
@@ -20,10 +20,8 @@ package org.apache.cloudstack.quota.activationrule.presetvariables;
import java.util.List;
import java.util.Map;
-import com.cloud.storage.Snapshot;
import com.cloud.storage.Storage.ProvisioningType;
import com.cloud.storage.Volume;
-import com.cloud.vm.snapshot.VMSnapshot;
import org.apache.cloudstack.quota.constant.QuotaTypes;
public class Value extends GenericPresetVariable {
@@ -64,10 +62,10 @@ public class Value extends GenericPresetVariable {
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;
+ private String snapshotType;
@PresetVariableDefinition(description = "Type of the VM snapshot. Values can be: Disk or DiskAndMemory.", supportedTypes = {QuotaTypes.VM_SNAPSHOT})
- private VMSnapshot.Type vmSnapshotType;
+ private String vmSnapshotType;
@PresetVariableDefinition(description = "Computing offering of the VM.", supportedTypes = {QuotaTypes.RUNNING_VM, QuotaTypes.ALLOCATED_VM})
private ComputeOffering computeOffering;
@@ -106,7 +104,6 @@ public class Value extends GenericPresetVariable {
public void setHost(Host host) {
this.host = host;
- fieldNamesToIncludeInToString.add("host");
}
public String getOsName() {
@@ -115,7 +112,6 @@ public class Value extends GenericPresetVariable {
public void setOsName(String osName) {
this.osName = osName;
- fieldNamesToIncludeInToString.add("osName");
}
public List<Resource> getAccountResources() {
@@ -124,7 +120,6 @@ public class Value extends GenericPresetVariable {
public void setAccountResources(List<Resource> accountResources) {
this.accountResources = accountResources;
- fieldNamesToIncludeInToString.add("accountResources");
}
public Map<String, String> getTags() {
@@ -133,7 +128,6 @@ public class Value extends GenericPresetVariable {
public void setTags(Map<String, String> tags) {
this.tags = tags;
- fieldNamesToIncludeInToString.add("tags");
}
public String getTag() {
@@ -142,7 +136,6 @@ public class Value extends GenericPresetVariable {
public void setTag(String tag) {
this.tag = tag;
- fieldNamesToIncludeInToString.add("tag");
}
public Long getSize() {
@@ -151,7 +144,6 @@ public class Value extends GenericPresetVariable {
public void setSize(Long size) {
this.size = size;
- fieldNamesToIncludeInToString.add("size");
}
public ProvisioningType getProvisioningType() {
@@ -160,25 +152,22 @@ public class Value extends GenericPresetVariable {
public void setProvisioningType(ProvisioningType provisioningType) {
this.provisioningType = provisioningType;
- fieldNamesToIncludeInToString.add("provisioningType");
}
- public Snapshot.Type getSnapshotType() {
+ public String getSnapshotType() {
return snapshotType;
}
- public void setSnapshotType(Snapshot.Type snapshotType) {
+ public void setSnapshotType(String snapshotType) {
this.snapshotType = snapshotType;
- fieldNamesToIncludeInToString.add("snapshotType");
}
- public VMSnapshot.Type getVmSnapshotType() {
+ public String getVmSnapshotType() {
return vmSnapshotType;
}
- public void setVmSnapshotType(VMSnapshot.Type vmSnapshotType) {
+ public void setVmSnapshotType(String vmSnapshotType) {
this.vmSnapshotType = vmSnapshotType;
- fieldNamesToIncludeInToString.add("vmSnapshotType");
}
public ComputeOffering getComputeOffering() {
@@ -187,7 +176,6 @@ public class Value extends GenericPresetVariable {
public void setComputeOffering(ComputeOffering computeOffering) {
this.computeOffering = computeOffering;
- fieldNamesToIncludeInToString.add("computeOffering");
}
public GenericPresetVariable getTemplate() {
@@ -196,7 +184,6 @@ public class Value extends GenericPresetVariable {
public void setTemplate(GenericPresetVariable template) {
this.template = template;
- fieldNamesToIncludeInToString.add("template");
}
public DiskOfferingPresetVariables getDiskOffering() {
@@ -205,7 +192,6 @@ public class Value extends GenericPresetVariable {
public void setDiskOffering(DiskOfferingPresetVariables diskOffering) {
this.diskOffering = diskOffering;
- fieldNamesToIncludeInToString.add("diskOffering");
}
public Storage getStorage() {
@@ -214,7 +200,6 @@ public class Value extends GenericPresetVariable {
public void setStorage(Storage storage) {
this.storage = storage;
- fieldNamesToIncludeInToString.add("storage");
}
public ComputingResources getComputingResources() {
@@ -223,7 +208,6 @@ public class Value extends GenericPresetVariable {
public void setComputingResources(ComputingResources computingResources) {
this.computingResources = computingResources;
- fieldNamesToIncludeInToString.add("computingResources");
}
public Long getVirtualSize() {
@@ -232,7 +216,6 @@ public class Value extends GenericPresetVariable {
public void setVirtualSize(Long virtualSize) {
this.virtualSize = virtualSize;
- fieldNamesToIncludeInToString.add("virtualSize");
}
public BackupOffering getBackupOffering() {
@@ -241,12 +224,10 @@ public class Value extends GenericPresetVariable {
public void setBackupOffering(BackupOffering backupOffering) {
this.backupOffering = backupOffering;
- fieldNamesToIncludeInToString.add("backupOffering");
}
public void setHypervisorType(String hypervisorType) {
this.hypervisorType = hypervisorType;
- fieldNamesToIncludeInToString.add("hypervisorType");
}
public String getHypervisorType() {
@@ -255,7 +236,6 @@ public class Value extends GenericPresetVariable {
public void setVolumeFormat(String volumeFormat) {
this.volumeFormat = volumeFormat;
- fieldNamesToIncludeInToString.add("volumeFormat");
}
public String getVolumeFormat() {
@@ -268,7 +248,6 @@ public class Value extends GenericPresetVariable {
public void setVolumeType(Volume.Type volumeType) {
this.volumeType = volumeType;
- fieldNamesToIncludeInToString.add("volumeType");
}
public String getState() {
@@ -277,6 +256,5 @@ public class Value extends GenericPresetVariable {
public void setState(String state) {
this.state = state;
- fieldNamesToIncludeInToString.add("state");
}
}
diff --git a/framework/quota/src/test/java/org/apache/cloudstack/quota/QuotaManagerImplTest.java b/framework/quota/src/test/java/org/apache/cloudstack/quota/QuotaManagerImplTest.java
index 3b2ea54e86d..a33faa054de 100644
--- a/framework/quota/src/test/java/org/apache/cloudstack/quota/QuotaManagerImplTest.java
+++ b/framework/quota/src/test/java/org/apache/cloudstack/quota/QuotaManagerImplTest.java
@@ -267,12 +267,12 @@ public class QuotaManagerImplTest {
quotaManagerImplSpy.injectPresetVariablesIntoJsInterpreter(jsInterpreterMock, presetVariablesMock);
- Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("account"), Mockito.anyString());
- Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("domain"), Mockito.anyString());
- Mockito.verify(jsInterpreterMock, Mockito.never()).injectVariable(Mockito.eq("project"), Mockito.anyString());
- Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("resourceType"), Mockito.anyString());
- Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("value"), Mockito.anyString());
- Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("zone"), Mockito.anyString());
+ Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("account"), Mockito.any());
+ Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("domain"), Mockito.any());
+ Mockito.verify(jsInterpreterMock, Mockito.never()).injectVariable(Mockito.eq("project"), Mockito.any());
+ Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("resourceType"), Mockito.any());
+ Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("value"), Mockito.any());
+ Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("zone"), Mockito.any());
}
@Test
@@ -288,12 +288,12 @@ public class QuotaManagerImplTest {
quotaManagerImplSpy.injectPresetVariablesIntoJsInterpreter(jsInterpreterMock, presetVariablesMock);
- Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("account"), Mockito.anyString());
- Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("domain"), Mockito.anyString());
- Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("project"), Mockito.anyString());
- Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("resourceType"), Mockito.anyString());
- Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("value"), Mockito.anyString());
- Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("zone"), Mockito.anyString());
+ Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("account"), Mockito.any());
+ Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("domain"), Mockito.any());
+ Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("project"), Mockito.any());
+ Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("resourceType"), Mockito.any());
+ Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("value"), Mockito.any());
+ Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("zone"), Mockito.any());
}
@Test
diff --git a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/AccountTest.java b/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/AccountTest.java
deleted file mode 100644
index 1e62235e71c..00000000000
--- a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/AccountTest.java
+++ /dev/null
@@ -1,34 +0,0 @@
-// 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.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class AccountTest {
-
- @Test
- public void setRoleTestAddFieldRoleToCollection() {
- Account variable = new Account();
- variable.setRole(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("role"));
- }
-}
diff --git a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/BackupOfferingTest.java b/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/BackupOfferingTest.java
deleted file mode 100644
index 57c18f936f2..00000000000
--- a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/BackupOfferingTest.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * 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.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class BackupOfferingTest {
- @Test
- public void setExternalIdTestAddFieldExternalIdToCollection() {
- BackupOffering backupOffering = new BackupOffering();
- backupOffering.setExternalId("any-external-id");
- Assert.assertTrue(backupOffering.fieldNamesToIncludeInToString.contains("externalId"));
- }
-
-}
diff --git a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/ComputeOfferingTest.java b/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/ComputeOfferingTest.java
deleted file mode 100644
index 5fbcbe76476..00000000000
--- a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/ComputeOfferingTest.java
+++ /dev/null
@@ -1,35 +0,0 @@
-// 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.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class ComputeOfferingTest {
-
- @Test
- public void setCustomizedTestAddFieldCustomizedToCollection() {
- ComputeOffering variable = new ComputeOffering();
- variable.setCustomized(true);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("customized"));
- }
-
-}
diff --git a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/ComputingResourcesTest.java b/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/ComputingResourcesTest.java
deleted file mode 100644
index f7978f16e04..00000000000
--- a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/ComputingResourcesTest.java
+++ /dev/null
@@ -1,40 +0,0 @@
-// 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.commons.lang3.builder.ToStringBuilder;
-import org.apache.commons.lang3.builder.ToStringStyle;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class ComputingResourcesTest {
-
- @Test
- public void toStringTestReturnAJson() {
- ComputingResources variable = new ComputingResources();
-
- String expected = ToStringBuilder.reflectionToString(variable, ToStringStyle.JSON_STYLE);
- String result = variable.toString();
-
- Assert.assertEquals(expected, result);
- }
-
-}
diff --git a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/DomainTest.java b/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/DomainTest.java
deleted file mode 100644
index f245b4637e6..00000000000
--- a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/DomainTest.java
+++ /dev/null
@@ -1,35 +0,0 @@
-// 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.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class DomainTest {
-
- @Test
- public void setPathTestAddFieldPathToCollection() {
- Domain variable = new Domain();
- variable.setPath("test path");
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("path"));
- }
-
-}
diff --git a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/GenericPresetVariableTest.java b/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/GenericPresetVariableTest.java
deleted file mode 100644
index 4f594ee5d00..00000000000
--- a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/GenericPresetVariableTest.java
+++ /dev/null
@@ -1,73 +0,0 @@
-// 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.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class GenericPresetVariableTest {
-
- @Test
- public void setIdTestAddFieldIdToCollection() {
- GenericPresetVariable variable = new GenericPresetVariable();
- variable.setId("test");
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("id"));
- }
-
- @Test
- public void setNameTestAddFieldNameToCollection() {
- GenericPresetVariable variable = new GenericPresetVariable();
- variable.setName("test");
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("name"));
- }
-
- @Test
- public void toStringTestSetAllFieldsAndReturnAJson() {
- GenericPresetVariable variable = new GenericPresetVariable();
- variable.setId("test id");
- variable.setName("test name");
-
- String expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "id", "name");
- String result = variable.toString();
-
- Assert.assertEquals(expected, result);
- }
-
- @Test
- public void toStringTestSetSomeFieldsAndReturnAJson() {
- GenericPresetVariable variable = new GenericPresetVariable();
- variable.setId("test id");
-
- String expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "id");
- String result = variable.toString();
-
- Assert.assertEquals(expected, result);
-
- variable = new GenericPresetVariable();
- variable.setName("test name");
-
- expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "name");
- result = variable.toString();
-
- Assert.assertEquals(expected, result);
- }
-}
diff --git a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/HostTest.java b/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/HostTest.java
deleted file mode 100644
index 87aae7788e2..00000000000
--- a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/HostTest.java
+++ /dev/null
@@ -1,34 +0,0 @@
-// 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.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class HostTest {
-
- @Test
- public void setTagsTestAddFieldTagsToCollection() {
- Host variable = new Host();
- variable.setTags(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("tags"));
- }
-}
diff --git a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelperTest.java b/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelperTest.java
index c692cb7c1e7..07838f19726 100644
--- a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelperTest.java
+++ b/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelperTest.java
@@ -218,9 +218,9 @@ public class PresetVariableHelperTest {
value.setProvisioningType(ProvisioningType.THIN);
value.setStorage(getStorageForTests());
value.setSize(ByteScaleUtils.GiB);
- value.setSnapshotType(Snapshot.Type.HOURLY);
+ value.setSnapshotType(Snapshot.Type.HOURLY.toString());
value.setTag("tag_test");
- value.setVmSnapshotType(VMSnapshot.Type.Disk);
+ value.setVmSnapshotType(VMSnapshot.Type.Disk.toString());
value.setComputingResources(getComputingResourcesForTests());
value.setVolumeType(Volume.Type.DATADISK);
return value;
@@ -272,7 +272,7 @@ public class PresetVariableHelperTest {
storage.setId("storage_id");
storage.setName("storage_name");
storage.setTags(Arrays.asList("tag1", "tag2"));
- storage.setScope(ScopeType.ZONE);
+ storage.setScope(ScopeType.ZONE.toString());
return storage;
}
@@ -298,9 +298,9 @@ public class PresetVariableHelperTest {
private List<VMInstanceDetailVO> getVmDetailsForTests() {
List<VMInstanceDetailVO> details = new LinkedList<>();
- details.add(new VMInstanceDetailVO(1l, "test_with_value", "277", false));
- details.add(new VMInstanceDetailVO(1l, "test_with_invalid_value", "invalid", false));
- details.add(new VMInstanceDetailVO(1l, "test_with_null", null, false));
+ details.add(new VMInstanceDetailVO(1L, "test_with_value", "277", false));
+ details.add(new VMInstanceDetailVO(1L, "test_with_invalid_value", "invalid", false));
+ details.add(new VMInstanceDetailVO(1L, "test_with_null", null, false));
return details;
}
@@ -309,13 +309,6 @@ public class PresetVariableHelperTest {
Assert.assertEquals(expected.getName(), result.getName());
}
- private void validateFieldNamesToIncludeInToString(List<String> expected, GenericPresetVariable resultObject) {
- List<String> result = new ArrayList<>(resultObject.fieldNamesToIncludeInToString);
- Collections.sort(expected);
- Collections.sort(result);
- Assert.assertEquals(expected, result);
- }
-
private BackupOffering getBackupOfferingForTests() {
BackupOffering backupOffering = new BackupOffering();
backupOffering.setId("backup_offering_id");
@@ -415,7 +408,6 @@ public class PresetVariableHelperTest {
Assert.assertNotNull(result.getProject());
assertPresetVariableIdAndName(account, result.getProject());
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "name"), result.getProject());
}
@Test
@@ -430,10 +422,9 @@ public class PresetVariableHelperTest {
Mockito.doReturn(account.getName()).when(accountVoMock).getName();
Mockito.doReturn(account.getCreated()).when(accountVoMock).getCreated();
- Account result = presetVariableHelperSpy.getPresetVariableAccount(1l);
+ Account result = presetVariableHelperSpy.getPresetVariableAccount(1L);
assertPresetVariableIdAndName(account, result);
- validateFieldNamesToIncludeInToString(Arrays.asList("created", "id", "name"), result);
}
@Test
@@ -463,18 +454,16 @@ public class PresetVariableHelperTest {
Role role = new Role();
role.setId("test_id");
role.setName("test_name");
- role.setType(roleType);
+ role.setType(roleType.toString());
Mockito.doReturn(role.getId()).when(roleVoMock).getUuid();
Mockito.doReturn(role.getName()).when(roleVoMock).getName();
- Mockito.doReturn(role.getType()).when(roleVoMock).getRoleType();
+ Mockito.doReturn(RoleType.fromString(role.getType())).when(roleVoMock).getRoleType();
- Role result = presetVariableHelperSpy.getPresetVariableRole(1l);
+ Role result = presetVariableHelperSpy.getPresetVariableRole(1L);
assertPresetVariableIdAndName(role, result);
Assert.assertEquals(role.getType(), result.getType());
-
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "name", "type"), result);
});
}
@@ -489,12 +478,10 @@ public class PresetVariableHelperTest {
Mockito.doReturn(domain.getName()).when(domainVoMock).getName();
Mockito.doReturn(domain.getPath()).when(domainVoMock).getPath();
- Domain result = presetVariableHelperSpy.getPresetVariableDomain(1l);
+ Domain result = presetVariableHelperSpy.getPresetVariableDomain(1L);
assertPresetVariableIdAndName(domain, result);
Assert.assertEquals(domain.getPath(), result.getPath());
-
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "name", "path"), result);
}
@Test
@@ -507,10 +494,9 @@ public class PresetVariableHelperTest {
Mockito.doReturn(expected.getId()).when(dataCenterVoMock).getUuid();
Mockito.doReturn(expected.getName()).when(dataCenterVoMock).getName();
- GenericPresetVariable result = presetVariableHelperSpy.getPresetVariableZone(1l);
+ GenericPresetVariable result = presetVariableHelperSpy.getPresetVariableZone(1L);
assertPresetVariableIdAndName(expected, result);
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "name"), result);
}
@Test
@@ -531,7 +517,6 @@ public class PresetVariableHelperTest {
Value result = presetVariableHelperSpy.getPresetVariableValue(usageVoMock);
Assert.assertEquals(resources, result.getAccountResources());
- validateFieldNamesToIncludeInToString(Arrays.asList("accountResources"), result);
}
@Test
@@ -541,7 +526,7 @@ public class PresetVariableHelperTest {
Mockito.doReturn(new Date()).when(usageVoMock).getEndDate();
Mockito.doReturn(expected).when(usageDaoMock).listAccountResourcesInThePeriod(Mockito.anyLong(), Mockito.anyInt(), Mockito.any(Date.class), Mockito.any(Date.class));
- List<Resource> result = presetVariableHelperSpy.getPresetVariableAccountResources(usageVoMock, 1l, 0);
+ List<Resource> result = presetVariableHelperSpy.getPresetVariableAccountResources(usageVoMock, 1L, 0);
for (int i = 0; i < expected.size(); i++) {
Assert.assertEquals(expected.get(i).first(), result.get(i).getZoneId());
@@ -590,8 +575,6 @@ public class PresetVariableHelperTest {
Assert.assertEquals(expected.getTags(), result.getTags());
Assert.assertEquals(expected.getTemplate(), result.getTemplate());
Assert.assertEquals(hypervisorType.name(), result.getHypervisorType());
-
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "name", "osName", "tags", "template", "hypervisorType"), result);
});
}
@@ -614,7 +597,6 @@ public class PresetVariableHelperTest {
public void setPresetVariableHostInValueIfUsageTypeIsRunningVmTestQuotaTypeIsRunningVmSetHost() {
Value result = new Value();
Host expectedHost = getHostForTests();
- List<HostTagVO> expectedHostTags = getHostTagsForTests();
Mockito.doReturn(expectedHost).when(presetVariableHelperSpy).getPresetVariableValueHost(Mockito.anyLong());
presetVariableHelperSpy.setPresetVariableHostInValueIfUsageTypeIsRunningVm(result, UsageTypes.RUNNING_VM, vmInstanceVoMock);
@@ -623,7 +605,6 @@ public class PresetVariableHelperTest {
assertPresetVariableIdAndName(expectedHost, result.getHost());
Assert.assertEquals(expectedHost.getTags(), result.getHost().getTags());
- validateFieldNamesToIncludeInToString(Arrays.asList("host"), result);
}
@Test
@@ -638,11 +619,10 @@ public class PresetVariableHelperTest {
Mockito.doReturn(expected.getName()).when(hostVoMock).getName();
Mockito.doReturn(hostTagVOListMock).when(hostTagsDaoMock).getHostTags(Mockito.anyLong());
- Host result = presetVariableHelperSpy.getPresetVariableValueHost(1l);
+ Host result = presetVariableHelperSpy.getPresetVariableValueHost(1L);
assertPresetVariableIdAndName(expected, result);
Assert.assertEquals(expected.getTags(), result.getTags());
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "isTagARule", "name", "tags"), result);
}
@Test
@@ -657,12 +637,11 @@ public class PresetVariableHelperTest {
Mockito.doReturn(expected.getName()).when(hostVoMock).getName();
Mockito.doReturn(hostTagVOListMock).when(hostTagsDaoMock).getHostTags(Mockito.anyLong());
- Host result = presetVariableHelperSpy.getPresetVariableValueHost(1l);
+ Host result = presetVariableHelperSpy.getPresetVariableValueHost(1L);
assertPresetVariableIdAndName(expected, result);
Assert.assertEquals(new ArrayList<>(), result.getTags());
Assert.assertTrue(result.getIsTagARule());
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "isTagARule", "name", "tags"), result);
}
@Test
@@ -674,7 +653,7 @@ public class PresetVariableHelperTest {
String expected = "os_display_name";
Mockito.doReturn(expected).when(guestOsVoMock).getDisplayName();
- String result = presetVariableHelperSpy.getPresetVariableValueOsName(1l);
+ String result = presetVariableHelperSpy.getPresetVariableValueOsName(1L);
Assert.assertEquals(expected, result);
}
@@ -692,7 +671,6 @@ public class PresetVariableHelperTest {
assertPresetVariableIdAndName(expected, result);
Assert.assertEquals(expected.isCustomized(), result.isCustomized());
Assert.assertEquals(expected.offerHa(), result.offerHa());
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "name", "customized", "offerHa"), result);
}
@Test
@@ -706,7 +684,6 @@ public class PresetVariableHelperTest {
assertPresetVariableIdAndName(expected, result);
Assert.assertEquals(expected.isCustomized(), result.isCustomized());
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "name", "customized"), result);
}
@@ -720,10 +697,9 @@ public class PresetVariableHelperTest {
Mockito.doReturn(expected.getId()).when(vmTemplateVoMock).getUuid();
Mockito.doReturn(expected.getName()).when(vmTemplateVoMock).getName();
- GenericPresetVariable result = presetVariableHelperSpy.getPresetVariableValueTemplate(1l);
+ GenericPresetVariable result = presetVariableHelperSpy.getPresetVariableValueTemplate(1L);
assertPresetVariableIdAndName(expected, result);
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "name"), result);
}
@Test
@@ -735,7 +711,7 @@ public class PresetVariableHelperTest {
Mockito.doReturn(listExpected).when(resourceTagDaoMock).listBy(Mockito.anyLong(), Mockito.any(ResourceObjectType.class));
Arrays.asList(ResourceObjectType.values()).forEach(type -> {
- Map<String, String> result = presetVariableHelperSpy.getPresetVariableValueResourceTags(1l, type);
+ Map<String, String> result = presetVariableHelperSpy.getPresetVariableValueResourceTags(1L, type);
for (ResourceTag expected: listExpected) {
Assert.assertEquals(expected.getValue(), result.get(expected.getKey()));
@@ -760,15 +736,15 @@ public class PresetVariableHelperTest {
VolumeVO volumeVoMock = Mockito.mock(VolumeVO.class);
Mockito.doReturn(volumeVoMock).when(volumeDaoMock).findByIdIncludingRemoved(Mockito.anyLong());
- Mockito.doReturn(1l).when(volumeVoMock).getPoolId();
+ Mockito.doReturn(1L).when(volumeVoMock).getPoolId();
mockMethodValidateIfObjectIsNull();
Mockito.doReturn(expected.getId()).when(volumeVoMock).getUuid();
Mockito.doReturn(expected.getName()).when(volumeVoMock).getName();
Mockito.doReturn(expected.getDiskOffering()).when(presetVariableHelperSpy).getPresetVariableValueDiskOffering(Mockito.anyLong());
- Mockito.doReturn(expected.getProvisioningType()).when(volumeVoMock).getProvisioningType();
Mockito.doReturn(expected.getVolumeType()).when(volumeVoMock).getVolumeType();
+ Mockito.doReturn(expected.getProvisioningType()).when(volumeVoMock).getProvisioningType();
Mockito.doReturn(expected.getStorage()).when(presetVariableHelperSpy).getPresetVariableValueStorage(Mockito.anyLong(), Mockito.anyInt());
Mockito.doReturn(expected.getTags()).when(presetVariableHelperSpy).getPresetVariableValueResourceTags(Mockito.anyLong(), Mockito.any(ResourceObjectType.class));
Mockito.doReturn(expected.getSize()).when(volumeVoMock).getSize();
@@ -789,8 +765,6 @@ public class PresetVariableHelperTest {
Assert.assertEquals(expected.getTags(), result.getTags());
Assert.assertEquals(expectedSize, result.getSize());
Assert.assertEquals(imageFormat.name(), result.getVolumeFormat());
-
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "name", "diskOffering", "provisioningType", "volumeType", "storage", "tags", "size", "volumeFormat"), result);
}
Mockito.verify(presetVariableHelperSpy, Mockito.times(ImageFormat.values().length)).getPresetVariableValueResourceTags(Mockito.anyLong(),
@@ -811,8 +785,8 @@ public class PresetVariableHelperTest {
Mockito.doReturn(expected.getId()).when(volumeVoMock).getUuid();
Mockito.doReturn(expected.getName()).when(volumeVoMock).getName();
Mockito.doReturn(expected.getDiskOffering()).when(presetVariableHelperSpy).getPresetVariableValueDiskOffering(Mockito.anyLong());
- Mockito.doReturn(expected.getProvisioningType()).when(volumeVoMock).getProvisioningType();
Mockito.doReturn(expected.getVolumeType()).when(volumeVoMock).getVolumeType();
+ Mockito.doReturn(expected.getProvisioningType()).when(volumeVoMock).getProvisioningType();
Mockito.doReturn(expected.getTags()).when(presetVariableHelperSpy).getPresetVariableValueResourceTags(Mockito.anyLong(), Mockito.any(ResourceObjectType.class));
Mockito.doReturn(expected.getSize()).when(volumeVoMock).getSize();
Mockito.doReturn(imageFormat).when(volumeVoMock).getFormat();
@@ -832,8 +806,6 @@ public class PresetVariableHelperTest {
Assert.assertEquals(expected.getTags(), result.getTags());
Assert.assertEquals(expectedSize, result.getSize());
Assert.assertEquals(imageFormat.name(), result.getVolumeFormat());
-
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "name", "diskOffering", "provisioningType", "volumeType", "tags", "size", "volumeFormat"), result);
}
Mockito.verify(presetVariableHelperSpy, Mockito.times(ImageFormat.values().length)).getPresetVariableValueResourceTags(Mockito.anyLong(),
@@ -850,11 +822,9 @@ public class PresetVariableHelperTest {
Mockito.doReturn(expected.getId()).when(diskOfferingVoMock).getUuid();
Mockito.doReturn(expected.getName()).when(diskOfferingVoMock).getName();
- GenericPresetVariable result = presetVariableHelperSpy.getPresetVariableValueDiskOffering(1l);
+ GenericPresetVariable result = presetVariableHelperSpy.getPresetVariableValueDiskOffering(1L);
assertPresetVariableIdAndName(expected, result);
- validateFieldNamesToIncludeInToString(Arrays.asList("bytesReadBurst", "bytesReadBurstLength", "bytesReadRate", "bytesWriteBurst", "bytesWriteBurstLength", "bytesWriteRate",
- "id", "iopsReadBurst", "iopsReadBurstLength", "iopsReadRate", "iopsWriteBurst", "iopsWriteBurstLength", "iopsWriteRate", "name"), result);
}
@Test
@@ -862,7 +832,7 @@ public class PresetVariableHelperTest {
Storage expected = getStorageForTests();
Mockito.doReturn(expected).when(presetVariableHelperSpy).getSecondaryStorageForSnapshot(Mockito.anyLong(), Mockito.anyInt());
- Storage result = presetVariableHelperSpy.getPresetVariableValueStorage(1l, 2);
+ Storage result = presetVariableHelperSpy.getPresetVariableValueStorage(1L, 2);
Assert.assertEquals(expected, result);
Mockito.verify(primaryStorageDaoMock, Mockito.never()).findByIdIncludingRemoved(Mockito.anyLong());
@@ -880,16 +850,14 @@ public class PresetVariableHelperTest {
Mockito.doReturn(expected.getId()).when(storagePoolVoMock).getUuid();
Mockito.doReturn(expected.getName()).when(storagePoolVoMock).getName();
- Mockito.doReturn(expected.getScope()).when(storagePoolVoMock).getScope();
+ Mockito.doReturn(ScopeType.validateAndGetScopeType(expected.getScope())).when(storagePoolVoMock).getScope();
Mockito.doReturn(storageTagVOListMock).when(storagePoolTagsDaoMock).findStoragePoolTags(Mockito.anyLong());
- Storage result = presetVariableHelperSpy.getPresetVariableValueStorage(1l, 2);
+ Storage result = presetVariableHelperSpy.getPresetVariableValueStorage(1L, 2);
assertPresetVariableIdAndName(expected, result);
Assert.assertEquals(expected.getScope(), result.getScope());
Assert.assertEquals(expected.getTags(), result.getTags());
-
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "isTagARule", "name", "scope", "tags"), result);
}
@Test
@@ -904,24 +872,22 @@ public class PresetVariableHelperTest {
Mockito.doReturn(expected.getId()).when(storagePoolVoMock).getUuid();
Mockito.doReturn(expected.getName()).when(storagePoolVoMock).getName();
- Mockito.doReturn(expected.getScope()).when(storagePoolVoMock).getScope();
+ Mockito.doReturn(ScopeType.validateAndGetScopeType(expected.getScope())).when(storagePoolVoMock).getScope();
Mockito.doReturn(storageTagVOListMock).when(storagePoolTagsDaoMock).findStoragePoolTags(Mockito.anyLong());
- Storage result = presetVariableHelperSpy.getPresetVariableValueStorage(1l, 2);
+ Storage result = presetVariableHelperSpy.getPresetVariableValueStorage(1L, 2);
assertPresetVariableIdAndName(expected, result);
Assert.assertEquals(expected.getScope(), result.getScope());
Assert.assertEquals(new ArrayList<>(), result.getTags());
Assert.assertTrue(result.getIsTagARule());
-
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "isTagARule", "name", "scope", "tags"), result);
}
@Test
public void getSecondaryStorageForSnapshotTestAllTypesAndDoNotBackupSnapshotReturnNull() {
presetVariableHelperSpy.backupSnapshotAfterTakingSnapshot = false;
getQuotaTypesForTests().forEach(type -> {
- Storage result = presetVariableHelperSpy.getSecondaryStorageForSnapshot(1l, type.getKey());
+ Storage result = presetVariableHelperSpy.getSecondaryStorageForSnapshot(1L, type.getKey());
Assert.assertNull(result);
});
}
@@ -930,7 +896,7 @@ public class PresetVariableHelperTest {
public void getSecondaryStorageForSnapshotTestAllTypesExceptSnapshotAndBackupSnapshotReturnNull() {
presetVariableHelperSpy.backupSnapshotAfterTakingSnapshot = true;
getQuotaTypesForTests(UsageTypes.SNAPSHOT).forEach(type -> {
- Storage result = presetVariableHelperSpy.getSecondaryStorageForSnapshot(1l, type.getKey());
+ Storage result = presetVariableHelperSpy.getSecondaryStorageForSnapshot(1L, type.getKey());
Assert.assertNull(result);
});
}
@@ -947,15 +913,14 @@ public class PresetVariableHelperTest {
Mockito.doReturn(expected.getName()).when(imageStoreVoMock).getName();
presetVariableHelperSpy.backupSnapshotAfterTakingSnapshot = true;
- Storage result = presetVariableHelperSpy.getSecondaryStorageForSnapshot(1l, UsageTypes.SNAPSHOT);
+ Storage result = presetVariableHelperSpy.getSecondaryStorageForSnapshot(1L, UsageTypes.SNAPSHOT);
assertPresetVariableIdAndName(expected, result);
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "name"), result);
}
@Test
public void loadPresetVariableValueForTemplateAndIsoTestRecordIsNotAtemplateNorAnIsoDoNothing() {
- getQuotaTypesForTests(templateAndIsoUsageTypes.toArray(new Integer[templateAndIsoUsageTypes.size()])).forEach(type -> {
+ getQuotaTypesForTests(templateAndIsoUsageTypes.toArray(new Integer[0])).forEach(type -> {
Mockito.doReturn(type.getKey()).when(usageVoMock).getUsageType();
presetVariableHelperSpy.loadPresetVariableValueForTemplateAndIso(usageVoMock, null);
});
@@ -991,8 +956,6 @@ public class PresetVariableHelperTest {
Assert.assertEquals(expected.getOsName(), result.getOsName());
Assert.assertEquals(expected.getTags(), result.getTags());
Assert.assertEquals(expectedSize, result.getSize());
-
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "name", "osName", "tags", "size"), result);
});
Mockito.verify(presetVariableHelperSpy).getPresetVariableValueResourceTags(Mockito.anyLong(), Mockito.eq(ResourceObjectType.Template));
@@ -1025,7 +988,7 @@ public class PresetVariableHelperTest {
Mockito.doReturn(expected.getName()).when(snapshotVoMock).getName();
Mockito.doReturn(expected.getSize()).when(snapshotVoMock).getSize();
Mockito.doReturn((short) 3).when(snapshotVoMock).getSnapshotType();
- Mockito.doReturn(1l).when(presetVariableHelperSpy).getSnapshotDataStoreId(Mockito.anyLong(), Mockito.anyLong());
+ Mockito.doReturn(1L).when(presetVariableHelperSpy).getSnapshotDataStoreId(Mockito.anyLong(), Mockito.anyLong());
Mockito.doReturn(expected.getStorage()).when(presetVariableHelperSpy).getPresetVariableValueStorage(Mockito.anyLong(), Mockito.anyInt());
Mockito.doReturn(expected.getTags()).when(presetVariableHelperSpy).getPresetVariableValueResourceTags(Mockito.anyLong(), Mockito.any(ResourceObjectType.class));
Mockito.doReturn(hypervisorType).when(snapshotVoMock).getHypervisorType();
@@ -1043,8 +1006,6 @@ public class PresetVariableHelperTest {
Assert.assertEquals(expected.getTags(), result.getTags());
Assert.assertEquals(expectedSize, result.getSize());
Assert.assertEquals(hypervisorType.name(), result.getHypervisorType());
-
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "name", "snapshotType", "storage", "tags", "size", "hypervisorType"), result);
}
Mockito.verify(presetVariableHelperSpy, Mockito.times(Hypervisor.HypervisorType.values().length)).getPresetVariableValueResourceTags(Mockito.anyLong(),
@@ -1056,12 +1017,12 @@ public class PresetVariableHelperTest {
public void getSnapshotDataStoreIdTestDoNotBackupSnapshotToSecondaryRetrievePrimaryStorage() {
SnapshotDataStoreVO snapshotDataStoreVoMock = Mockito.mock(SnapshotDataStoreVO.class);
- Long expected = 1l;
+ Long expected = 1L;
Mockito.doReturn(snapshotDataStoreVoMock).when(snapshotDataStoreDaoMock).findOneBySnapshotAndDatastoreRole(Mockito.anyLong(), Mockito.any(DataStoreRole.class));
Mockito.doReturn(expected).when(snapshotDataStoreVoMock).getDataStoreId();
presetVariableHelperSpy.backupSnapshotAfterTakingSnapshot = false;
- Long result = presetVariableHelperSpy.getSnapshotDataStoreId(1l, 1l);
+ Long result = presetVariableHelperSpy.getSnapshotDataStoreId(1L, 1L);
Assert.assertEquals(expected, result);
@@ -1078,7 +1039,7 @@ public class PresetVariableHelperTest {
public void getSnapshotDataStoreIdTestBackupSnapshotToSecondaryRetrieveSecondaryStorage() {
SnapshotDataStoreVO snapshotDataStoreVoMock = Mockito.mock(SnapshotDataStoreVO.class);
- Long expected = 2l;
+ Long expected = 2L;
ImageStoreVO imageStore = Mockito.mock(ImageStoreVO.class);
Mockito.when(imageStoreDaoMock.findById(Mockito.anyLong())).thenReturn(imageStore);
Mockito.when(imageStore.getDataCenterId()).thenReturn(1L);
@@ -1086,7 +1047,7 @@ public class PresetVariableHelperTest {
Mockito.doReturn(expected).when(snapshotDataStoreVoMock).getDataStoreId();
presetVariableHelperSpy.backupSnapshotAfterTakingSnapshot = true;
- Long result = presetVariableHelperSpy.getSnapshotDataStoreId(2l, 1L);
+ Long result = presetVariableHelperSpy.getSnapshotDataStoreId(2L, 1L);
Assert.assertEquals(expected, result);
@@ -1129,8 +1090,6 @@ public class PresetVariableHelperTest {
assertPresetVariableIdAndName(expected, result);
Assert.assertEquals(expected.getTag(), result.getTag());
-
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "name", "tag"), result);
}
@Test
@@ -1155,7 +1114,7 @@ public class PresetVariableHelperTest {
Mockito.doReturn(expected.getId()).when(vmSnapshotVoMock).getUuid();
Mockito.doReturn(expected.getName()).when(vmSnapshotVoMock).getName();
Mockito.doReturn(expected.getTags()).when(presetVariableHelperSpy).getPresetVariableValueResourceTags(Mockito.anyLong(), Mockito.any(ResourceObjectType.class));
- Mockito.doReturn(expected.getVmSnapshotType()).when(vmSnapshotVoMock).getType();
+ Mockito.doReturn(VMSnapshot.Type.valueOf(expected.getVmSnapshotType())).when(vmSnapshotVoMock).getType();
Mockito.doReturn(UsageTypes.VM_SNAPSHOT).when(usageVoMock).getUsageType();
@@ -1166,8 +1125,6 @@ public class PresetVariableHelperTest {
Assert.assertEquals(expected.getTags(), result.getTags());
Assert.assertEquals(expected.getVmSnapshotType(), result.getVmSnapshotType());
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "name", "tags", "vmSnapshotType"), result);
-
Mockito.verify(presetVariableHelperSpy).getPresetVariableValueResourceTags(Mockito.anyLong(), Mockito.eq(ResourceObjectType.VMSnapshot));
}
@@ -1200,9 +1157,6 @@ public class PresetVariableHelperTest {
if (typeInt == UsageTypes.RUNNING_VM) {
Assert.assertEquals(expected.getComputingResources(), result.getComputingResources());
- validateFieldNamesToIncludeInToString(Arrays.asList("computeOffering", "computingResources"), result);
- } else {
- validateFieldNamesToIncludeInToString(Arrays.asList("computeOffering"), result);
}
});
}
@@ -1228,7 +1182,7 @@ public class PresetVariableHelperTest {
@Test
public void getDetailByNameTestReturnsValue() {
- int expected = Integer.valueOf(getVmDetailsForTests().get(0).getValue());
+ int expected = Integer.parseInt(getVmDetailsForTests().get(0).getValue());
int result = presetVariableHelperSpy.getDetailByName(getVmDetailsForTests(), "test_with_value", expected);
Assert.assertEquals(expected, result);
}
@@ -1295,8 +1249,6 @@ public class PresetVariableHelperTest {
Assert.assertEquals(expected.getVirtualSize(), result.getVirtualSize());
Assert.assertEquals(expected.getBackupOffering(), result.getBackupOffering());
- validateFieldNamesToIncludeInToString(Arrays.asList("size", "virtualSize", "backupOffering"), result);
-
Mockito.verify(presetVariableHelperSpy).getPresetVariableValueBackupOffering(Mockito.anyLong());
}
@@ -1311,11 +1263,10 @@ public class PresetVariableHelperTest {
Mockito.doReturn(expected.getName()).when(backupOfferingVoMock).getName();
Mockito.doReturn(expected.getExternalId()).when(backupOfferingVoMock).getExternalId();
- BackupOffering result = presetVariableHelperSpy.getPresetVariableValueBackupOffering(1l);
+ BackupOffering result = presetVariableHelperSpy.getPresetVariableValueBackupOffering(1L);
assertPresetVariableIdAndName(expected, result);
Assert.assertEquals(expected.getExternalId(), result.getExternalId());
- validateFieldNamesToIncludeInToString(Arrays.asList("id", "name", "externalId"), result);
}
@Test
diff --git a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/ResourceTest.java b/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/ResourceTest.java
deleted file mode 100644
index cdcfc87cd4e..00000000000
--- a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/ResourceTest.java
+++ /dev/null
@@ -1,40 +0,0 @@
-// 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.commons.lang3.builder.ToStringBuilder;
-import org.apache.commons.lang3.builder.ToStringStyle;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class ResourceTest {
-
- @Test
- public void toStringTestReturnAJson() {
- Resource variable = new Resource();
-
- String expected = ToStringBuilder.reflectionToString(variable, ToStringStyle.JSON_STYLE);
- String result = variable.toString();
-
- Assert.assertEquals(expected, result);
- }
-
-}
diff --git a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/RoleTest.java b/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/RoleTest.java
deleted file mode 100644
index 88265ee4e55..00000000000
--- a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/RoleTest.java
+++ /dev/null
@@ -1,34 +0,0 @@
-// 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.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class RoleTest {
-
- @Test
- public void setTagsTestAddFieldTagsToCollection() {
- Role variable = new Role();
- variable.setType(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("type"));
- }
-}
diff --git a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/StorageTest.java b/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/StorageTest.java
deleted file mode 100644
index f36d5c49581..00000000000
--- a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/StorageTest.java
+++ /dev/null
@@ -1,41 +0,0 @@
-// 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.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class StorageTest {
-
- @Test
- public void setTagsTestAddFieldTagsToCollection() {
- Storage variable = new Storage();
- variable.setTags(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("tags"));
- }
-
- @Test
- public void setScopeTestAddFieldScopeToCollection() {
- Storage variable = new Storage();
- variable.setScope(null);;
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("scope"));
- }
-}
diff --git a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/ValueTest.java b/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/ValueTest.java
deleted file mode 100644
index bad33da8836..00000000000
--- a/framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/ValueTest.java
+++ /dev/null
@@ -1,175 +0,0 @@
-// 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.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@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();
- variable.setHost(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("host"));
- }
-
- @Test
- public void setOsNameTestAddFieldOsNameToCollection() {
- Value variable = new Value();
- variable.setOsName(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("osName"));
- }
-
- @Test
- public void setAccountResourcesTestAddFieldAccountResourcesToCollection() {
- Value variable = new Value();
- variable.setAccountResources(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("accountResources"));
- }
-
- @Test
- public void setTagsTestAddFieldTagsToCollection() {
- Value variable = new Value();
- variable.setTags(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("tags"));
- }
-
- @Test
- public void setTagTestAddFieldTagToCollection() {
- Value variable = new Value();
- variable.setTag(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("tag"));
- }
-
- @Test
- public void setSizeTestAddFieldSizeToCollection() {
- Value variable = new Value();
- variable.setSize(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("size"));
- }
-
- @Test
- public void setProvisioningTypeTestAddFieldProvisioningTypeToCollection() {
- Value variable = new Value();
- variable.setProvisioningType(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("provisioningType"));
- }
-
- @Test
- public void setSnapshotTypeTestAddFieldSnapshotTypeToCollection() {
- Value variable = new Value();
- variable.setSnapshotType(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("snapshotType"));
- }
-
- @Test
- public void setVmSnapshotTypeTestAddFieldVmSnapshotTypeToCollection() {
- Value variable = new Value();
- variable.setVmSnapshotType(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("vmSnapshotType"));
- }
-
- @Test
- public void setComputeOfferingTestAddFieldComputeOfferingToCollection() {
- Value variable = new Value();
- variable.setComputeOffering(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("computeOffering"));
- }
-
- @Test
- public void setTemplateTestAddFieldTemplateToCollection() {
- Value variable = new Value();
- variable.setTemplate(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("template"));
- }
-
- @Test
- public void setDiskOfferingTestAddFieldDiskOfferingToCollection() {
- Value variable = new Value();
- variable.setDiskOffering(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("diskOffering"));
- }
-
- @Test
- public void setStorageTestAddFieldStorageToCollection() {
- Value variable = new Value();
- variable.setStorage(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("storage"));
- }
-
- @Test
- public void setComputingResourcesTestAddFieldComputingResourcesToCollection() {
- Value variable = new Value();
- variable.setComputingResources(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("computingResources"));
- }
-
- @Test
- public void setVirtualSizeTestAddFieldVirtualSizeToCollection() {
- Value variable = new Value();
- variable.setVirtualSize(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("virtualSize"));
- }
-
- @Test
- public void setBackupOfferingTestAddFieldBackupOfferingToCollection() {
- Value variable = new Value();
- variable.setBackupOffering(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("backupOffering"));
- }
-
- @Test
- public void setHypervisorTypeTestAddFieldHypervisorTypeToCollection() {
- Value variable = new Value();
- variable.setHypervisorType(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("hypervisorType"));
- }
-
- @Test
- public void setVolumeFormatTestAddFieldVolumeFormatToCollection() {
- Value variable = new Value();
- variable.setVolumeFormat(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("volumeFormat"));
- }
-
- @Test
- public void setStateTestAddFieldStateToCollection() {
- Value variable = new Value();
- variable.setState(null);
- Assert.assertTrue(variable.fieldNamesToIncludeInToString.contains("state"));
- }
-
-}
diff --git a/plugins/backup/nas/src/main/java/org/apache/cloudstack/backup/NASBackupProvider.java b/plugins/backup/nas/src/main/java/org/apache/cloudstack/backup/NASBackupProvider.java
index 3b2c2692b0d..6648fcc9218 100644
--- a/plugins/backup/nas/src/main/java/org/apache/cloudstack/backup/NASBackupProvider.java
+++ b/plugins/backup/nas/src/main/java/org/apache/cloudstack/backup/NASBackupProvider.java
@@ -69,7 +69,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
-import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
@@ -290,7 +289,8 @@ public class NASBackupProvider extends AdapterBase implements BackupProvider, Co
}
private Pair<Boolean, String> restoreVMBackup(VirtualMachine vm, Backup backup) {
- List<String> backedVolumesUUIDs = backup.getBackedUpVolumes().stream()
+ List<Backup.VolumeInfo> backedVolumes = backup.getBackedUpVolumes();
+ List<String> backedVolumesUUIDs = backedVolumes.stream()
.sorted(Comparator.comparingLong(Backup.VolumeInfo::getDeviceId))
.map(Backup.VolumeInfo::getUuid)
.collect(Collectors.toList());
@@ -313,6 +313,7 @@ public class NASBackupProvider extends AdapterBase implements BackupProvider, Co
Pair<List<PrimaryDataStoreTO>, List<String>> volumePoolsAndPaths = getVolumePoolsAndPaths(restoreVolumes);
restoreCommand.setRestoreVolumePools(volumePoolsAndPaths.first());
restoreCommand.setRestoreVolumePaths(volumePoolsAndPaths.second());
+ restoreCommand.setBackupFiles(getBackupFiles(backedVolumes));
restoreCommand.setVmExists(vm.getRemoved() == null);
restoreCommand.setVmState(vm.getState());
restoreCommand.setMountTimeout(NASBackupRestoreMountTimeout.value());
@@ -328,6 +329,14 @@ public class NASBackupProvider extends AdapterBase implements BackupProvider, Co
return new Pair<>(answer.getResult(), answer.getDetails());
}
+ private List<String> getBackupFiles(List<Backup.VolumeInfo> backedVolumes) {
+ List<String> backupFiles = new ArrayList<>();
+ for (Backup.VolumeInfo backedVolume : backedVolumes) {
+ backupFiles.add(backedVolume.getPath());
+ }
+ return backupFiles;
+ }
+
private Pair<List<PrimaryDataStoreTO>, List<String>> getVolumePoolsAndPaths(List<VolumeVO> volumes) {
List<PrimaryDataStoreTO> volumePools = new ArrayList<>();
List<String> volumePaths = new ArrayList<>();
@@ -366,7 +375,12 @@ public class NASBackupProvider extends AdapterBase implements BackupProvider, Co
final StoragePoolVO pool = primaryDataStoreDao.findByUuid(dataStoreUuid);
final HostVO hostVO = hostDao.findByIp(hostIp);
- LOG.debug("Restoring vm volume {} from backup {} on the NAS Backup Provider", backupVolumeInfo, backup);
+ Backup.VolumeInfo matchingVolume = getBackedUpVolumeInfo(backup.getBackedUpVolumes(), volume.getUuid());
+ if (matchingVolume == null) {
+ throw new CloudRuntimeException(String.format("Unable to find volume %s in the list of backed up volumes for backup %s, cannot proceed with restore", volume.getUuid(), backup));
+ }
+
+ LOG.debug("Restoring vm volume {} from backup {} on the NAS Backup Provider", volume, backup);
BackupRepository backupRepository = getBackupRepository(backup);
VolumeVO restoredVolume = new VolumeVO(Volume.Type.DATADISK, null, backup.getZoneId(),
@@ -404,8 +418,8 @@ public class NASBackupProvider extends AdapterBase implements BackupProvider, Co
restoreCommand.setMountOptions(backupRepository.getMountOptions());
restoreCommand.setVmExists(null);
restoreCommand.setVmState(vmNameAndState.second());
- restoreCommand.setRestoreVolumeUUID(backupVolumeInfo.getUuid());
restoreCommand.setMountTimeout(NASBackupRestoreMountTimeout.value());
+ restoreCommand.setBackupFiles(Collections.singletonList(matchingVolume.getPath()));
BackupAnswer answer;
try {
@@ -435,10 +449,11 @@ public class NASBackupProvider extends AdapterBase implements BackupProvider, Co
return backupRepository;
}
- private Optional<Backup.VolumeInfo> getBackedUpVolumeInfo(List<Backup.VolumeInfo> backedUpVolumes, String volumeUuid) {
+ private Backup.VolumeInfo getBackedUpVolumeInfo(List<Backup.VolumeInfo> backedUpVolumes, String volumeUuid) {
return backedUpVolumes.stream()
.filter(v -> v.getUuid().equals(volumeUuid))
- .findFirst();
+ .findFirst()
+ .orElse(null);
}
@Override
diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java
index fd94013dd50..2a7f113f47a 100644
--- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java
+++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java
@@ -72,10 +72,11 @@ public class LibvirtRestoreBackupCommandWrapper extends CommandWrapper<RestoreBa
List<String> backedVolumeUUIDs = command.getBackupVolumesUUIDs();
List<PrimaryDataStoreTO> restoreVolumePools = command.getRestoreVolumePools();
List<String> restoreVolumePaths = command.getRestoreVolumePaths();
- String restoreVolumeUuid = command.getRestoreVolumeUUID();
Integer mountTimeout = command.getMountTimeout() * 1000;
int timeout = command.getWait();
KVMStoragePoolManager storagePoolMgr = serverResource.getStoragePoolMgr();
+ List<String> volumePaths = command.getRestoreVolumePaths();
+ List<String> backupFiles = command.getBackupFiles();
String newVolumeId = null;
try {
@@ -83,14 +84,15 @@ public class LibvirtRestoreBackupCommandWrapper extends CommandWrapper<RestoreBa
if (Objects.isNull(vmExists)) {
PrimaryDataStoreTO volumePool = restoreVolumePools.get(0);
String volumePath = restoreVolumePaths.get(0);
+ String backupFile = backupFiles.get(0);
int lastIndex = volumePath.lastIndexOf("/");
newVolumeId = volumePath.substring(lastIndex + 1);
- restoreVolume(storagePoolMgr, backupPath, volumePool, volumePath, diskType, restoreVolumeUuid,
+ restoreVolume(storagePoolMgr, backupPath, volumePool, volumePath, diskType, backupFile,
new Pair<>(vmName, command.getVmState()), mountDirectory, timeout);
} else if (Boolean.TRUE.equals(vmExists)) {
- restoreVolumesOfExistingVM(storagePoolMgr, restoreVolumePools, restoreVolumePaths, backedVolumeUUIDs, backupPath, mountDirectory, timeout);
+ restoreVolumesOfExistingVM(storagePoolMgr, restoreVolumePools, restoreVolumePaths, backedVolumeUUIDs, backupPath, backupFiles, mountDirectory, timeout);
} else {
- restoreVolumesOfDestroyedVMs(storagePoolMgr, restoreVolumePools, restoreVolumePaths, vmName, backupPath, mountDirectory, timeout);
+ restoreVolumesOfDestroyedVMs(storagePoolMgr, restoreVolumePools, restoreVolumePaths, backupPath, backupFiles, mountDirectory, timeout);
}
} catch (CloudRuntimeException e) {
String errorMessage = e.getMessage() != null ? e.getMessage() : "";
@@ -109,19 +111,21 @@ public class LibvirtRestoreBackupCommandWrapper extends CommandWrapper<RestoreBa
}
}
- private void restoreVolumesOfExistingVM(KVMStoragePoolManager storagePoolMgr, List<PrimaryDataStoreTO> restoreVolumePools, List<String> restoreVolumePaths, List<String> backedVolumesUUIDs,
- String backupPath, String mountDirectory, int timeout) {
+ private void restoreVolumesOfExistingVM(KVMStoragePoolManager storagePoolMgr, List<PrimaryDataStoreTO> restoreVolumePools,
+ List<String> restoreVolumePaths, List<String> backedVolumesUUIDs,
+ String backupPath, List<String> backupFiles, String mountDirectory, int timeout) {
String diskType = "root";
try {
for (int idx = 0; idx < restoreVolumePaths.size(); idx++) {
PrimaryDataStoreTO restoreVolumePool = restoreVolumePools.get(idx);
String restoreVolumePath = restoreVolumePaths.get(idx);
+ String backupFile = backupFiles.get(idx);
String backupVolumeUuid = backedVolumesUUIDs.get(idx);
- Pair<String, String> bkpPathAndVolUuid = getBackupPath(mountDirectory, null, backupPath, diskType, backupVolumeUuid);
+ String fullPath = getBackupPath(mountDirectory, backupPath, backupFile, diskType);
diskType = "datadisk";
- verifyBackupFile(bkpPathAndVolUuid.first(), bkpPathAndVolUuid.second());
- if (!replaceVolumeWithBackup(storagePoolMgr, restoreVolumePool, restoreVolumePath, bkpPathAndVolUuid.first(), timeout)) {
- throw new CloudRuntimeException(String.format("Unable to restore contents from the backup volume [%s].", bkpPathAndVolUuid.second()));
+ verifyBackupFile(fullPath, backupVolumeUuid);
+ if (!replaceVolumeWithBackup(storagePoolMgr, restoreVolumePool, restoreVolumePath, fullPath, timeout)) {
+ throw new CloudRuntimeException(String.format("Unable to restore contents from the backup volume [%s].", backupVolumeUuid));
}
}
} finally {
@@ -130,17 +134,20 @@ public class LibvirtRestoreBackupCommandWrapper extends CommandWrapper<RestoreBa
}
}
- private void restoreVolumesOfDestroyedVMs(KVMStoragePoolManager storagePoolMgr, List<PrimaryDataStoreTO> volumePools, List<String> volumePaths, String vmName, String backupPath, String mountDirectory, int timeout) {
+ private void restoreVolumesOfDestroyedVMs(KVMStoragePoolManager storagePoolMgr, List<PrimaryDataStoreTO> volumePools,
+ List<String> volumePaths, String backupPath, List<String> backupFiles, String mountDirectory, int timeout) {
String diskType = "root";
try {
for (int i = 0; i < volumePaths.size(); i++) {
PrimaryDataStoreTO volumePool = volumePools.get(i);
String volumePath = volumePaths.get(i);
- Pair<String, String> bkpPathAndVolUuid = getBackupPath(mountDirectory, volumePath, backupPath, diskType, null);
+ String backupFile = backupFiles.get(i);
+ String bkpPath = getBackupPath(mountDirectory, backupPath, backupFile, diskType);
+ String volumeUuid = volumePath.substring(volumePath.lastIndexOf(File.separator) + 1);
diskType = "datadisk";
- verifyBackupFile(bkpPathAndVolUuid.first(), bkpPathAndVolUuid.second());
- if (!replaceVolumeWithBackup(storagePoolMgr, volumePool, volumePath, bkpPathAndVolUuid.first(), timeout)) {
- throw new CloudRuntimeException(String.format("Unable to restore contents from the backup volume [%s].", bkpPathAndVolUuid.second()));
+ verifyBackupFile(bkpPath, volumeUuid);
+ if (!replaceVolumeWithBackup(storagePoolMgr, volumePool, volumePath, bkpPath, timeout)) {
+ throw new CloudRuntimeException(String.format("Unable to restore contents from the backup volume [%s].", volumeUuid));
}
}
} finally {
@@ -149,14 +156,17 @@ public class LibvirtRestoreBackupCommandWrapper extends CommandWrapper<RestoreBa
}
}
- private void restoreVolume(KVMStoragePoolManager storagePoolMgr, String backupPath, PrimaryDataStoreTO volumePool, String volumePath, String diskType, String volumeUUID,
+ private void restoreVolume(KVMStoragePoolManager storagePoolMgr, String backupPath, PrimaryDataStoreTO volumePool,
+ String volumePath, String diskType, String backupFile,
Pair<String, VirtualMachine.State> vmNameAndState, String mountDirectory, int timeout) {
- Pair<String, String> bkpPathAndVolUuid;
+ String bkpPath;
+ String volumeUuid;
try {
- bkpPathAndVolUuid = getBackupPath(mountDirectory, volumePath, backupPath, diskType, volumeUUID);
- verifyBackupFile(bkpPathAndVolUuid.first(), bkpPathAndVolUuid.second());
- if (!replaceVolumeWithBackup(storagePoolMgr, volumePool, volumePath, bkpPathAndVolUuid.first(), timeout, true)) {
- throw new CloudRuntimeException(String.format("Unable to restore contents from the backup volume [%s].", bkpPathAndVolUuid.second()));
+ bkpPath = getBackupPath(mountDirectory, backupPath, backupFile, diskType);
+ volumeUuid = volumePath.substring(volumePath.lastIndexOf(File.separator) + 1);
+ verifyBackupFile(bkpPath, volumeUuid);
+ if (!replaceVolumeWithBackup(storagePoolMgr, volumePool, volumePath, bkpPath, timeout, true)) {
+ throw new CloudRuntimeException(String.format("Unable to restore contents from the backup volume [%s].", volumeUuid));
}
if (VirtualMachine.State.Running.equals(vmNameAndState.second())) {
if (!attachVolumeToVm(storagePoolMgr, vmNameAndState.first(), volumePool, volumePath)) {
@@ -219,12 +229,11 @@ public class LibvirtRestoreBackupCommandWrapper extends CommandWrapper<RestoreBa
}
}
- private Pair<String, String> getBackupPath(String mountDirectory, String volumePath, String backupPath, String diskType, String volumeUuid) {
+ private String getBackupPath(String mountDirectory, String backupPath, String backupFile, String diskType) {
String bkpPath = String.format(FILE_PATH_PLACEHOLDER, mountDirectory, backupPath);
- String volUuid = Objects.isNull(volumeUuid) ? volumePath.substring(volumePath.lastIndexOf(File.separator) + 1) : volumeUuid;
- String backupFileName = String.format("%s.%s.qcow2", diskType.toLowerCase(Locale.ROOT), volUuid);
+ String backupFileName = String.format("%s.%s.qcow2", diskType.toLowerCase(Locale.ROOT), backupFile);
bkpPath = String.format(FILE_PATH_PLACEHOLDER, bkpPath, backupFileName);
- return new Pair<>(bkpPath, volUuid);
+ return bkpPath;
}
private boolean checkBackupFileImage(String backupPath) {
diff --git a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapperTest.java b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapperTest.java
index 7bcd0bf18e6..8dabd9af9cd 100644
--- a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapperTest.java
+++ b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapperTest.java
@@ -35,6 +35,7 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
+import java.util.Collections;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
@@ -69,7 +70,7 @@ public class LibvirtRestoreBackupCommandWrapperTest {
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
- when(command.getRestoreVolumeUUID()).thenReturn("volume-123");
+ when(command.getBackupVolumesUUIDs()).thenReturn(Collections.singletonList("volume-123"));
when(command.getVmState()).thenReturn(VirtualMachine.State.Running);
when(command.getMountTimeout()).thenReturn(30);
@@ -186,7 +187,7 @@ public class LibvirtRestoreBackupCommandWrapperTest {
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
- when(command.getRestoreVolumeUUID()).thenReturn("volume-123");
+ when(command.getBackupVolumesUUIDs()).thenReturn(Collections.singletonList("volume-123"));
when(command.getVmState()).thenReturn(VirtualMachine.State.Running);
when(command.getMountTimeout()).thenReturn(30);
@@ -227,7 +228,7 @@ public class LibvirtRestoreBackupCommandWrapperTest {
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
lenient().when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
- lenient().when(command.getRestoreVolumeUUID()).thenReturn("volume-123");
+ lenient().when(command.getBackupVolumesUUIDs()).thenReturn(Collections.singletonList("volume-123"));
lenient().when(command.getVmState()).thenReturn(VirtualMachine.State.Running);
lenient().when(command.getMountTimeout()).thenReturn(30);
@@ -263,7 +264,7 @@ public class LibvirtRestoreBackupCommandWrapperTest {
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
- when(command.getRestoreVolumeUUID()).thenReturn("volume-123");
+ when(command.getBackupVolumesUUIDs()).thenReturn(Collections.singletonList("volume-123"));
when(command.getVmState()).thenReturn(VirtualMachine.State.Running);
when(command.getMountTimeout()).thenReturn(30);
@@ -309,7 +310,7 @@ public class LibvirtRestoreBackupCommandWrapperTest {
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
- when(command.getRestoreVolumeUUID()).thenReturn("volume-123");
+ when(command.getBackupVolumesUUIDs()).thenReturn(Collections.singletonList("volume-123"));
when(command.getVmState()).thenReturn(VirtualMachine.State.Running);
when(command.getMountTimeout()).thenReturn(30);
@@ -357,7 +358,7 @@ public class LibvirtRestoreBackupCommandWrapperTest {
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
- when(command.getRestoreVolumeUUID()).thenReturn("volume-123");
+ when(command.getBackupVolumesUUIDs()).thenReturn(Collections.singletonList("volume-123"));
when(command.getVmState()).thenReturn(VirtualMachine.State.Running);
when(command.getMountTimeout()).thenReturn(30);
@@ -407,7 +408,7 @@ public class LibvirtRestoreBackupCommandWrapperTest {
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
- when(command.getRestoreVolumeUUID()).thenReturn("volume-123");
+ when(command.getBackupVolumesUUIDs()).thenReturn(Collections.singletonList("volume-123"));
when(command.getVmState()).thenReturn(VirtualMachine.State.Running);
when(command.getMountTimeout()).thenReturn(30);
@@ -461,7 +462,7 @@ public class LibvirtRestoreBackupCommandWrapperTest {
PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class);
when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore));
lenient().when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("/var/lib/libvirt/images/volume-123"));
- lenient().when(command.getRestoreVolumeUUID()).thenReturn("volume-123");
+ lenient().when(command.getBackupVolumesUUIDs()).thenReturn(Collections.singletonList("volume-123"));
lenient().when(command.getVmState()).thenReturn(VirtualMachine.State.Running);
lenient().when(command.getMountTimeout()).thenReturn(30);
diff --git a/plugins/integrations/prometheus/src/main/java/org/apache/cloudstack/metrics/PrometheusExporterImpl.java b/plugins/integrations/prometheus/src/main/java/org/apache/cloudstack/metrics/PrometheusExporterImpl.java
index 8908bfa000f..deb3c788299 100644
--- a/plugins/integrations/prometheus/src/main/java/org/apache/cloudstack/metrics/PrometheusExporterImpl.java
+++ b/plugins/integrations/prometheus/src/main/java/org/apache/cloudstack/metrics/PrometheusExporterImpl.java
@@ -17,6 +17,7 @@
package org.apache.cloudstack.metrics;
import java.math.BigDecimal;
+import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -26,6 +27,7 @@ import java.util.stream.Collectors;
import javax.inject.Inject;
+import org.apache.cloudstack.ca.CAManager;
import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope;
import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
import org.apache.commons.lang3.StringUtils;
@@ -133,6 +135,8 @@ public class PrometheusExporterImpl extends ManagerBase implements PrometheusExp
private ResourceCountDao _resourceCountDao;
@Inject
private HostTagsDao _hostTagsDao;
+ @Inject
+ private CAManager caManager;
public PrometheusExporterImpl() {
super();
@@ -216,6 +220,9 @@ public class PrometheusExporterImpl extends ManagerBase implements PrometheusExp
}
metricsList.add(new ItemHostVM(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), vmDao.listByHostId(host.getId()).size()));
+
+ addSSLCertificateExpirationMetrics(metricsList, zoneName, zoneUuid, host);
+
final CapacityVO coreCapacity = capacityDao.findByHostIdType(host.getId(), Capacity.CAPACITY_TYPE_CPU_CORE);
if (coreCapacity == null && !host.isInMaintenanceStates()){
@@ -253,6 +260,18 @@ public class PrometheusExporterImpl extends ManagerBase implements PrometheusExp
addHostTagsMetrics(metricsList, dcId, zoneName, zoneUuid, totalHosts, upHosts, downHosts, total, up, down);
}
+ private void addSSLCertificateExpirationMetrics(List<Item> metricsList, String zoneName, String zoneUuid, HostVO host) {
+ if (caManager == null || caManager.getActiveCertificatesMap() == null) {
+ return;
+ }
+ X509Certificate cert = caManager.getActiveCertificatesMap().getOrDefault(host.getPrivateIpAddress(), null);
+ if (cert == null) {
+ return;
+ }
+ long certExpiryEpoch = cert.getNotAfter().getTime() / 1000; // Convert to epoch seconds
+ metricsList.add(new ItemHostCertExpiry(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), certExpiryEpoch));
+ }
+
private String markTagMaps(HostVO host, Map<String, Integer> totalHosts, Map<String, Integer> upHosts, Map<String, Integer> downHosts) {
List<HostTagVO> hostTagVOS = _hostTagsDao.getHostTags(host.getId());
List<String> hostTags = new ArrayList<>();
@@ -1060,4 +1079,28 @@ public class PrometheusExporterImpl extends ManagerBase implements PrometheusExp
return String.format("%s{zone=\"%s\",cpu=\"%d\",memory=\"%d\"} %d", name, zoneName, cpu, memory, total);
}
}
+
+ class ItemHostCertExpiry extends Item {
+ String zoneName;
+ String zoneUuid;
+ String hostName;
+ String hostUuid;
+ String hostIp;
+ long expiryTimestamp;
+
+ public ItemHostCertExpiry(final String zoneName, final String zoneUuid, final String hostName, final String hostUuid, final String hostIp, final long expiry) {
+ super("cloudstack_host_cert_expiry_timestamp");
+ this.zoneName = zoneName;
+ this.zoneUuid = zoneUuid;
+ this.hostName = hostName;
+ this.hostUuid = hostUuid;
+ this.hostIp = hostIp;
+ this.expiryTimestamp = expiry;
+ }
+
+ @Override
+ public String toMetricsString() {
+ return String.format("%s{zone=\"%s\",hostname=\"%s\",ip=\"%s\"} %d", name, zoneName, hostName, hostIp, expiryTimestamp);
+ }
+ }
}
diff --git a/plugins/integrations/prometheus/src/test/java/org/apache/cloudstack/metrics/PrometheusExporterImplTest.java b/plugins/integrations/prometheus/src/test/java/org/apache/cloudstack/metrics/PrometheusExporterImplTest.java
new file mode 100644
index 00000000000..40490c46f56
--- /dev/null
+++ b/plugins/integrations/prometheus/src/test/java/org/apache/cloudstack/metrics/PrometheusExporterImplTest.java
@@ -0,0 +1,108 @@
+// 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.metrics;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+public class PrometheusExporterImplTest {
+
+ private static final String TEST_ZONE_NAME = "zone1";
+ private static final String TEST_ZONE_UUID = "zone-uuid-1";
+ private static final String TEST_HOST_NAME = "host1";
+ private static final String TEST_HOST_UUID = "host-uuid-1";
+ private static final String TEST_HOST_IP = "192.168.1.10";
+ private static final long CERT_EXPIRY_TIME = 1735689600000L; // 2025-01-01 00:00:00 UTC
+ private static final long CERT_EXPIRY_EPOCH = CERT_EXPIRY_TIME / 1000;
+
+ @Test
+ public void testItemHostCertExpiryFormat() {
+ PrometheusExporterImpl exporter = new PrometheusExporterImpl();
+ PrometheusExporterImpl.ItemHostCertExpiry item = exporter.new ItemHostCertExpiry(
+ TEST_ZONE_NAME,
+ TEST_ZONE_UUID,
+ TEST_HOST_NAME,
+ TEST_HOST_UUID,
+ TEST_HOST_IP,
+ CERT_EXPIRY_EPOCH
+ );
+
+ String metricsString = item.toMetricsString();
+ String expected = String.format(
+ "cloudstack_host_cert_expiry_timestamp{zone=\"%s\",hostname=\"%s\",ip=\"%s\"} %d",
+ TEST_ZONE_NAME,
+ TEST_HOST_NAME,
+ TEST_HOST_IP,
+ CERT_EXPIRY_EPOCH
+ );
+ assertEquals("Certificate expiry metric format should match expected format", expected, metricsString);
+ }
+
+ @Test
+ public void testItemHostCertExpiryContainsCorrectMetricName() {
+ PrometheusExporterImpl exporter = new PrometheusExporterImpl();
+ PrometheusExporterImpl.ItemHostCertExpiry item = exporter.new ItemHostCertExpiry(
+ TEST_ZONE_NAME,
+ TEST_ZONE_UUID,
+ TEST_HOST_NAME,
+ TEST_HOST_UUID,
+ TEST_HOST_IP,
+ CERT_EXPIRY_EPOCH
+ );
+
+ String metricsString = item.toMetricsString();
+ assertTrue("Metric should contain correct metric name",
+ metricsString.contains("cloudstack_host_cert_expiry_timestamp"));
+ }
+
+ @Test
+ public void testItemHostCertExpiryContainsAllLabels() {
+ PrometheusExporterImpl exporter = new PrometheusExporterImpl();
+ PrometheusExporterImpl.ItemHostCertExpiry item = exporter.new ItemHostCertExpiry(
+ TEST_ZONE_NAME,
+ TEST_ZONE_UUID,
+ TEST_HOST_NAME,
+ TEST_HOST_UUID,
+ TEST_HOST_IP,
+ CERT_EXPIRY_EPOCH
+ );
+
+ String metricsString = item.toMetricsString();
+ assertTrue("Metric should contain zone label", metricsString.contains("zone=\"" + TEST_ZONE_NAME + "\""));
+ assertTrue("Metric should contain hostname label", metricsString.contains("hostname=\"" + TEST_HOST_NAME + "\""));
+ assertTrue("Metric should contain ip label", metricsString.contains("ip=\"" + TEST_HOST_IP + "\""));
+ }
+
+ @Test
+ public void testItemHostCertExpiryContainsTimestampValue() {
+ PrometheusExporterImpl exporter = new PrometheusExporterImpl();
+ PrometheusExporterImpl.ItemHostCertExpiry item = exporter.new ItemHostCertExpiry(
+ TEST_ZONE_NAME,
+ TEST_ZONE_UUID,
+ TEST_HOST_NAME,
+ TEST_HOST_UUID,
+ TEST_HOST_IP,
+ CERT_EXPIRY_EPOCH
+ );
+
+ String metricsString = item.toMetricsString();
+ assertTrue("Metric should contain correct timestamp value",
+ metricsString.endsWith(" " + CERT_EXPIRY_EPOCH));
+ }
+}
diff --git a/server/src/main/java/com/cloud/resource/ResourceManagerImpl.java b/server/src/main/java/com/cloud/resource/ResourceManagerImpl.java
index 6df31c6fa53..0e1b7cdbc80 100755
--- a/server/src/main/java/com/cloud/resource/ResourceManagerImpl.java
+++ b/server/src/main/java/com/cloud/resource/ResourceManagerImpl.java
@@ -3149,15 +3149,26 @@ public class ResourceManagerImpl extends ManagerBase implements ResourceManager,
private HostVO getNewHost(StartupCommand[] startupCommands) {
StartupCommand startupCommand = startupCommands[0];
- HostVO host = findHostByGuid(startupCommand.getGuid());
+ String fullGuid = startupCommand.getGuid();
+ logger.debug(String.format("Trying to find Host by guid %s", fullGuid));
+ HostVO host = findHostByGuid(fullGuid);
if (host != null) {
+ logger.debug(String.format("Found Host by guid %s: %s", fullGuid, host));
return host;
}
- host = findHostByGuid(startupCommand.getGuidWithoutResource());
+ String guidPrefix = startupCommand.getGuidWithoutResource();
+ logger.debug(String.format("Trying to find Host by guid prefix %s", guidPrefix));
+ host = findHostByGuidPrefix(guidPrefix);
- return host; // even when host == null!
+ if (host != null) {
+ logger.debug(String.format("Found Host by guid prefix %s: %s", guidPrefix, host));
+ return host;
+ }
+
+ logger.debug(String.format("Could not find Host by guid %s", fullGuid));
+ return null;
}
protected HostVO createHostVO(final StartupCommand[] cmds, final ServerResource resource, final Map<String, String> details, List<String> hostTags,
@@ -4209,6 +4220,15 @@ public class ResourceManagerImpl extends ManagerBase implements ResourceManager,
public HostVO findHostByGuid(final String guid) {
final QueryBuilder<HostVO> sc = QueryBuilder.create(HostVO.class);
sc.and(sc.entity().getGuid(), Op.EQ, guid);
+ sc.and(sc.entity().getRemoved(), Op.NULL);
+ return sc.find();
+ }
+
+ @Override
+ public HostVO findHostByGuidPrefix(String guid) {
+ final QueryBuilder<HostVO> sc = QueryBuilder.create(HostVO.class);
+ sc.and(sc.entity().getGuid(), Op.LIKE, guid + "%");
+ sc.and(sc.entity().getRemoved(), Op.NULL);
return sc.find();
}
@@ -4216,6 +4236,7 @@ public class ResourceManagerImpl extends ManagerBase implements ResourceManager,
public HostVO findHostByName(final String name) {
final QueryBuilder<HostVO> sc = QueryBuilder.create(HostVO.class);
sc.and(sc.entity().getName(), Op.EQ, name);
+ sc.and(sc.entity().getRemoved(), Op.NULL);
return sc.find();
}
diff --git a/server/src/main/java/org/apache/cloudstack/storage/heuristics/HeuristicRuleHelper.java b/server/src/main/java/org/apache/cloudstack/storage/heuristics/HeuristicRuleHelper.java
index 2e0780e7fe8..beecf90d2b8 100644
--- a/server/src/main/java/org/apache/cloudstack/storage/heuristics/HeuristicRuleHelper.java
+++ b/server/src/main/java/org/apache/cloudstack/storage/heuristics/HeuristicRuleHelper.java
@@ -139,23 +139,23 @@ public class HeuristicRuleHelper {
* @param presetVariables used for injecting in the JS interpreter.
*/
protected void injectPresetVariables(JsInterpreter jsInterpreter, PresetVariables presetVariables) {
- jsInterpreter.injectVariable("secondaryStorages", presetVariables.getSecondaryStorages().toString());
+ jsInterpreter.injectVariable("secondaryStorages", presetVariables.getSecondaryStorages());
if (presetVariables.getTemplate() != null) {
- jsInterpreter.injectVariable("template", presetVariables.getTemplate().toString());
- jsInterpreter.injectVariable("iso", presetVariables.getTemplate().toString());
+ jsInterpreter.injectVariable("template", presetVariables.getTemplate());
+ jsInterpreter.injectVariable("iso", presetVariables.getTemplate());
}
if (presetVariables.getSnapshot() != null) {
- jsInterpreter.injectVariable("snapshot", presetVariables.getSnapshot().toString());
+ jsInterpreter.injectVariable("snapshot", presetVariables.getSnapshot());
}
if (presetVariables.getVolume() != null) {
- jsInterpreter.injectVariable("volume", presetVariables.getVolume().toString());
+ jsInterpreter.injectVariable("volume", presetVariables.getVolume());
}
if (presetVariables.getAccount() != null) {
- jsInterpreter.injectVariable("account", presetVariables.getAccount().toString());
+ jsInterpreter.injectVariable("account", presetVariables.getAccount());
}
}
@@ -185,8 +185,8 @@ public class HeuristicRuleHelper {
Template template = new Template();
template.setName(templateVO.getName());
- template.setFormat(templateVO.getFormat());
- template.setHypervisorType(templateVO.getHypervisorType());
+ template.setFormat(templateVO.getFormat().toString());
+ template.setHypervisorType(templateVO.getHypervisorType().toString());
return template;
}
@@ -195,7 +195,7 @@ public class HeuristicRuleHelper {
Volume volumePresetVariable = new Volume();
volumePresetVariable.setName(volumeVO.getName());
- volumePresetVariable.setFormat(volumeVO.getFormat());
+ volumePresetVariable.setFormat(volumeVO.getFormat().toString());
volumePresetVariable.setSize(volumeVO.getSize());
return volumePresetVariable;
@@ -206,7 +206,7 @@ public class HeuristicRuleHelper {
snapshot.setName(snapshotInfo.getName());
snapshot.setSize(snapshotInfo.getSize());
- snapshot.setHypervisorType(snapshotInfo.getHypervisorType());
+ snapshot.setHypervisorType(snapshotInfo.getHypervisorType().toString());
return snapshot;
}
diff --git a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Account.java b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Account.java
index 67750e8ec5c..f4652d87e7b 100644
--- a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Account.java
+++ b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Account.java
@@ -27,7 +27,6 @@ public class Account extends GenericHeuristicPresetVariable {
public void setId(String id) {
this.id = id;
- fieldNamesToIncludeInToString.add("id");
}
public Domain getDomain() {
@@ -36,6 +35,5 @@ public class Account extends GenericHeuristicPresetVariable {
public void setDomain(Domain domain) {
this.domain = domain;
- fieldNamesToIncludeInToString.add("domain");
}
}
diff --git a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Domain.java b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Domain.java
index 704cbf4373e..0b01604e673 100644
--- a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Domain.java
+++ b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Domain.java
@@ -25,6 +25,5 @@ public class Domain extends GenericHeuristicPresetVariable {
public void setId(String id) {
this.id = id;
- fieldNamesToIncludeInToString.add("id");
}
}
diff --git a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/GenericHeuristicPresetVariable.java b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/GenericHeuristicPresetVariable.java
index b85b7763eee..56c7d48e68a 100644
--- a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/GenericHeuristicPresetVariable.java
+++ b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/GenericHeuristicPresetVariable.java
@@ -16,15 +16,11 @@
// under the License.
package org.apache.cloudstack.storage.heuristics.presetvariables;
-import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
-
-import java.util.HashSet;
-import java.util.Set;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.commons.lang3.builder.ToStringStyle;
public class GenericHeuristicPresetVariable {
- protected transient Set<String> fieldNamesToIncludeInToString = new HashSet<>();
-
private String name;
public String getName() {
@@ -33,15 +29,10 @@ public class GenericHeuristicPresetVariable {
public void setName(String name) {
this.name = name;
- fieldNamesToIncludeInToString.add("name");
}
- /***
- * Converts the preset variable into a valid JSON object that will be injected into the JS interpreter.
- * This method should not be overridden or changed.
- */
@Override
- public final String toString() {
- return ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, fieldNamesToIncludeInToString.toArray(new String[0]));
+ public String toString() {
+ return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
}
}
diff --git a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/SecondaryStorage.java b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/SecondaryStorage.java
index ad7058d8336..e3a8dac43c6 100644
--- a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/SecondaryStorage.java
+++ b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/SecondaryStorage.java
@@ -32,7 +32,6 @@ public class SecondaryStorage extends GenericHeuristicPresetVariable {
public void setId(String id) {
this.id = id;
- fieldNamesToIncludeInToString.add("id");
}
public Long getUsedDiskSize() {
@@ -41,7 +40,6 @@ public class SecondaryStorage extends GenericHeuristicPresetVariable {
public void setUsedDiskSize(Long usedDiskSize) {
this.usedDiskSize = usedDiskSize;
- fieldNamesToIncludeInToString.add("usedDiskSize");
}
public Long getTotalDiskSize() {
@@ -50,7 +48,6 @@ public class SecondaryStorage extends GenericHeuristicPresetVariable {
public void setTotalDiskSize(Long totalDiskSize) {
this.totalDiskSize = totalDiskSize;
- fieldNamesToIncludeInToString.add("totalDiskSize");
}
public String getProtocol() {
@@ -59,6 +56,5 @@ public class SecondaryStorage extends GenericHeuristicPresetVariable {
public void setProtocol(String protocol) {
this.protocol = protocol;
- fieldNamesToIncludeInToString.add("protocol");
}
}
diff --git a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Snapshot.java b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Snapshot.java
index 34acd394dbe..404db3cdebc 100644
--- a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Snapshot.java
+++ b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Snapshot.java
@@ -16,13 +16,11 @@
// under the License.
package org.apache.cloudstack.storage.heuristics.presetvariables;
-import com.cloud.hypervisor.Hypervisor;
-
public class Snapshot extends GenericHeuristicPresetVariable {
private Long size;
- private Hypervisor.HypervisorType hypervisorType;
+ private String hypervisorType;
public Long getSize() {
return size;
@@ -30,15 +28,13 @@ public class Snapshot extends GenericHeuristicPresetVariable {
public void setSize(Long size) {
this.size = size;
- fieldNamesToIncludeInToString.add("size");
}
- public Hypervisor.HypervisorType getHypervisorType() {
+ public String getHypervisorType() {
return hypervisorType;
}
- public void setHypervisorType(Hypervisor.HypervisorType hypervisorType) {
+ public void setHypervisorType(String hypervisorType) {
this.hypervisorType = hypervisorType;
- fieldNamesToIncludeInToString.add("hypervisorType");
}
}
diff --git a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Template.java b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Template.java
index 297c95fad9a..c6df349010f 100644
--- a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Template.java
+++ b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Template.java
@@ -16,41 +16,35 @@
// under the License.
package org.apache.cloudstack.storage.heuristics.presetvariables;
-import com.cloud.hypervisor.Hypervisor;
-import com.cloud.storage.Storage;
-
public class Template extends GenericHeuristicPresetVariable {
- private Hypervisor.HypervisorType hypervisorType;
+ private String hypervisorType;
- private Storage.ImageFormat format;
+ private String format;
- private Storage.TemplateType templateType;
+ private String templateType;
- public Hypervisor.HypervisorType getHypervisorType() {
+ public String getHypervisorType() {
return hypervisorType;
}
- public void setHypervisorType(Hypervisor.HypervisorType hypervisorType) {
+ public void setHypervisorType(String hypervisorType) {
this.hypervisorType = hypervisorType;
- fieldNamesToIncludeInToString.add("hypervisorType");
}
- public Storage.ImageFormat getFormat() {
+ public String getFormat() {
return format;
}
- public void setFormat(Storage.ImageFormat format) {
+ public void setFormat(String format) {
this.format = format;
- fieldNamesToIncludeInToString.add("format");
}
- public Storage.TemplateType getTemplateType() {
+ public String getTemplateType() {
return templateType;
}
- public void setTemplateType(Storage.TemplateType templateType) {
+ public void setTemplateType(String templateType) {
this.templateType = templateType;
- fieldNamesToIncludeInToString.add("templateType");
}
}
diff --git a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Volume.java b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Volume.java
index 4e5e81b117f..8f571a57209 100644
--- a/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Volume.java
+++ b/server/src/main/java/org/apache/cloudstack/storage/heuristics/presetvariables/Volume.java
@@ -16,13 +16,11 @@
// under the License.
package org.apache.cloudstack.storage.heuristics.presetvariables;
-import com.cloud.storage.Storage;
-
public class Volume extends GenericHeuristicPresetVariable {
private Long size;
- private Storage.ImageFormat format;
+ private String format;
public Long getSize() {
return size;
@@ -30,15 +28,13 @@ public class Volume extends GenericHeuristicPresetVariable {
public void setSize(Long size) {
this.size = size;
- fieldNamesToIncludeInToString.add("size");
}
- public Storage.ImageFormat getFormat() {
+ public String getFormat() {
return format;
}
- public void setFormat(Storage.ImageFormat format) {
+ public void setFormat(String format) {
this.format = format;
- fieldNamesToIncludeInToString.add("format");
}
}
diff --git a/server/src/test/java/com/cloud/resource/MockResourceManagerImpl.java b/server/src/test/java/com/cloud/resource/MockResourceManagerImpl.java
index 1408a815322..6373c3277d0 100755
--- a/server/src/test/java/com/cloud/resource/MockResourceManagerImpl.java
+++ b/server/src/test/java/com/cloud/resource/MockResourceManagerImpl.java
@@ -474,6 +474,11 @@ public class MockResourceManagerImpl extends ManagerBase implements ResourceMana
return null;
}
+ @Override
+ public HostVO findHostByGuidPrefix(String guid) {
+ return null;
+ }
+
/* (non-Javadoc)
* @see com.cloud.resource.ResourceManager#findHostByName(java.lang.String)
*/
diff --git a/server/src/test/java/org/apache/cloudstack/storage/heuristics/HeuristicRuleHelperTest.java b/server/src/test/java/org/apache/cloudstack/storage/heuristics/HeuristicRuleHelperTest.java
index 272e79fea49..032e947fdce 100644
--- a/server/src/test/java/org/apache/cloudstack/storage/heuristics/HeuristicRuleHelperTest.java
+++ b/server/src/test/java/org/apache/cloudstack/storage/heuristics/HeuristicRuleHelperTest.java
@@ -16,6 +16,8 @@
// under the License.
package org.apache.cloudstack.storage.heuristics;
+import com.cloud.hypervisor.Hypervisor;
+import com.cloud.storage.Storage;
import com.cloud.storage.VMTemplateVO;
import com.cloud.storage.VolumeVO;
import com.cloud.utils.exception.CloudRuntimeException;
@@ -29,6 +31,7 @@ import org.apache.cloudstack.storage.heuristics.presetvariables.PresetVariables;
import org.apache.cloudstack.utils.jsinterpreter.JsInterpreter;
import org.apache.logging.log4j.Logger;
import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
@@ -68,6 +71,19 @@ public class HeuristicRuleHelperTest {
@InjectMocks
HeuristicRuleHelper heuristicRuleHelperSpy = new HeuristicRuleHelper();
+ @Before
+ public void setUp() {
+ Mockito.doReturn("template-name").when(vmTemplateVOMock).getName();
+ Mockito.doReturn(Storage.ImageFormat.QCOW2).when(vmTemplateVOMock).getFormat();
+ Mockito.doReturn(Hypervisor.HypervisorType.KVM).when(vmTemplateVOMock).getHypervisorType();
+ Mockito.doReturn("snapshot-name").when(snapshotInfoMock).getName();
+ Mockito.doReturn(1024L).when(snapshotInfoMock).getSize();
+ Mockito.doReturn(Hypervisor.HypervisorType.VMware).when(snapshotInfoMock).getHypervisorType();
+ Mockito.doReturn("volume-name").when(volumeVOMock).getName();
+ Mockito.doReturn(Storage.ImageFormat.RAW).when(volumeVOMock).getFormat();
+ Mockito.doReturn(2048L).when(volumeVOMock).getSize();
+ }
+
@Test
public void getImageStoreIfThereIsHeuristicRuleTestZoneDoesNotHaveHeuristicRuleShouldReturnNull() {
Long zoneId = 1L;
diff --git a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/AccountTest.java b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/AccountTest.java
deleted file mode 100644
index f7610438b78..00000000000
--- a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/AccountTest.java
+++ /dev/null
@@ -1,46 +0,0 @@
-// 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.storage.heuristics.presetvariables;
-
-import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class AccountTest {
-
- @Test
- public void toStringTestReturnsValidJson() {
- Account variable = new Account();
- variable.setName("test name");
- variable.setId("test id");
-
- Domain domainVariable = new Domain();
- domainVariable.setId("domain id");
- domainVariable.setName("domain name");
- variable.setDomain(domainVariable);
-
- String expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "name", "id", "domain");
- String result = variable.toString();
-
- Assert.assertEquals(expected, result);
- }
-
-}
diff --git a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/DomainTest.java b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/DomainTest.java
deleted file mode 100644
index a1ec6854ecc..00000000000
--- a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/DomainTest.java
+++ /dev/null
@@ -1,41 +0,0 @@
-// 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.storage.heuristics.presetvariables;
-
-import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class DomainTest {
-
- @Test
- public void toStringTestReturnsValidJson() {
- Domain variable = new Domain();
- variable.setName("test name");
- variable.setId("test id");
-
- String expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "name", "id");
- String result = variable.toString();
-
- Assert.assertEquals(expected, result);
- }
-
-}
diff --git a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/GenericHeuristicPresetVariableTest.java b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/GenericHeuristicPresetVariableTest.java
deleted file mode 100644
index cd295e92caf..00000000000
--- a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/GenericHeuristicPresetVariableTest.java
+++ /dev/null
@@ -1,40 +0,0 @@
-// 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.storage.heuristics.presetvariables;
-
-import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class GenericHeuristicPresetVariableTest {
-
- @Test
- public void toStringTestReturnsValidJson() {
- GenericHeuristicPresetVariable variable = new GenericHeuristicPresetVariable();
- variable.setName("test name");
-
- String expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "name");
- String result = variable.toString();
-
- Assert.assertEquals(expected, result);
- }
-
-}
diff --git a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/SecondaryStorageTest.java b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/SecondaryStorageTest.java
deleted file mode 100644
index a09386789cf..00000000000
--- a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/SecondaryStorageTest.java
+++ /dev/null
@@ -1,45 +0,0 @@
-// 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.storage.heuristics.presetvariables;
-
-import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class SecondaryStorageTest {
-
- @Test
- public void toStringTestReturnsValidJson() {
- SecondaryStorage variable = new SecondaryStorage();
- variable.setName("test name");
- variable.setId("test id");
- variable.setProtocol("test protocol");
- variable.setUsedDiskSize(1L);
- variable.setTotalDiskSize(2L);
-
- String expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "name", "id",
- "protocol", "usedDiskSize", "totalDiskSize");
- String result = variable.toString();
-
- Assert.assertEquals(expected, result);
- }
-
-}
diff --git a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/SnapshotTest.java b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/SnapshotTest.java
deleted file mode 100644
index b8476cd8e46..00000000000
--- a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/SnapshotTest.java
+++ /dev/null
@@ -1,44 +0,0 @@
-// 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.storage.heuristics.presetvariables;
-
-import com.cloud.hypervisor.Hypervisor;
-import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class SnapshotTest {
-
- @Test
- public void toStringTestReturnsValidJson() {
- Snapshot variable = new Snapshot();
- variable.setName("test name");
- variable.setSize(1L);
- variable.setHypervisorType(Hypervisor.HypervisorType.KVM);
-
- String expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "name", "size",
- "hypervisorType");
- String result = variable.toString();
-
- Assert.assertEquals(expected, result);
- }
-
-}
diff --git a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/TemplateTest.java b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/TemplateTest.java
deleted file mode 100644
index 2c1582befb2..00000000000
--- a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/TemplateTest.java
+++ /dev/null
@@ -1,46 +0,0 @@
-// 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.storage.heuristics.presetvariables;
-
-import com.cloud.hypervisor.Hypervisor;
-import com.cloud.storage.Storage;
-import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class TemplateTest {
-
- @Test
- public void toStringTestReturnsValidJson() {
- Template variable = new Template();
- variable.setName("test name");
- variable.setTemplateType(Storage.TemplateType.USER);
- variable.setHypervisorType(Hypervisor.HypervisorType.KVM);
- variable.setFormat(Storage.ImageFormat.QCOW2);
-
- String expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "name", "templateType",
- "hypervisorType", "format");
- String result = variable.toString();
-
- Assert.assertEquals(expected, result);
- }
-
-}
diff --git a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/VolumeTest.java b/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/VolumeTest.java
deleted file mode 100644
index e74ddc93ec5..00000000000
--- a/server/src/test/java/org/apache/cloudstack/storage/heuristics/presetvariables/VolumeTest.java
+++ /dev/null
@@ -1,44 +0,0 @@
-// 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.storage.heuristics.presetvariables;
-
-import com.cloud.storage.Storage;
-import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class VolumeTest {
-
- @Test
- public void toStringTestReturnsValidJson() {
- Volume variable = new Volume();
- variable.setName("test name");
- variable.setFormat(Storage.ImageFormat.QCOW2);
- variable.setSize(1L);
-
- String expected = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(variable, "name", "format",
- "size");
- String result = variable.toString();
-
- Assert.assertEquals(expected, result);
- }
-
-}
diff --git a/services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/template/UploadManagerImpl.java b/services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/template/UploadManagerImpl.java
index ae02d7e8aa7..aacca9926f7 100644
--- a/services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/template/UploadManagerImpl.java
+++ b/services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/template/UploadManagerImpl.java
@@ -16,6 +16,8 @@
// under the License.
package org.apache.cloudstack.storage.template;
+import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
+
import java.io.File;
import java.io.IOException;
import java.net.URI;
@@ -32,11 +34,11 @@ import java.util.concurrent.Executors;
import javax.naming.ConfigurationException;
-import com.cloud.agent.api.Answer;
-
-import com.cloud.agent.api.ConvertSnapshotCommand;
import org.apache.cloudstack.storage.resource.SecondaryStorageResource;
+import org.apache.commons.lang3.StringUtils;
+import com.cloud.agent.api.Answer;
+import com.cloud.agent.api.ConvertSnapshotCommand;
import com.cloud.agent.api.storage.CreateEntityDownloadURLAnswer;
import com.cloud.agent.api.storage.CreateEntityDownloadURLCommand;
import com.cloud.agent.api.storage.DeleteEntityDownloadURLCommand;
@@ -51,15 +53,18 @@ import com.cloud.storage.template.FtpTemplateUploader;
import com.cloud.storage.template.TemplateUploader;
import com.cloud.storage.template.TemplateUploader.Status;
import com.cloud.storage.template.TemplateUploader.UploadCompleteCallback;
+import com.cloud.utils.FileUtil;
import com.cloud.utils.NumbersUtil;
+import com.cloud.utils.UuidUtils;
import com.cloud.utils.component.ManagerBase;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.script.Script;
-import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
-
public class UploadManagerImpl extends ManagerBase implements UploadManager {
+ protected static final String EXTRACT_USERDATA_DIR = "userdata";
+ protected static final String BASE_EXTRACT_PATH = String.format("/var/www/html/%s/", EXTRACT_USERDATA_DIR);
+
public class Completion implements UploadCompleteCallback {
private final String jobId;
@@ -269,7 +274,7 @@ public class UploadManagerImpl extends ManagerBase implements UploadManager {
return new CreateEntityDownloadURLAnswer(errorString, CreateEntityDownloadURLAnswer.RESULT_FAILURE);
}
// Create the directory structure so that its visible under apache server root
- String extractDir = "/var/www/html/userdata/";
+ String extractDir = BASE_EXTRACT_PATH;
extractDir = extractDir + cmd.getFilepathInExtractURL() + File.separator;
Script command = new Script("/bin/su", logger);
command.add("-s");
@@ -333,12 +338,20 @@ public class UploadManagerImpl extends ManagerBase implements UploadManager {
String extractUrl = cmd.getExtractUrl();
String result;
if (extractUrl != null) {
- command.add("unlink /var/www/html/userdata/" + extractUrl.substring(extractUrl.lastIndexOf(File.separator) + 1));
+ URI uri = URI.create(extractUrl);
+ String uriPath = uri.getPath();
+ String marker = String.format("/%s/", EXTRACT_USERDATA_DIR);
+ String linkPath = uriPath.startsWith(marker)
+ ? uriPath.substring(marker.length())
+ : uriPath.substring(uriPath.indexOf(marker) + marker.length());
+ command.add("unlink " + BASE_EXTRACT_PATH + linkPath);
result = command.execute();
if (result != null) {
// FIXME - Ideally should bail out if you can't delete symlink. Not doing it right now.
// This is because the ssvm might already be destroyed and the symlinks do not exist.
logger.warn("Error in deleting symlink :" + result);
+ } else {
+ deleteEntitySymlinkRootDirectoryIfNeeded(cmd, linkPath);
}
}
@@ -371,6 +384,30 @@ public class UploadManagerImpl extends ManagerBase implements UploadManager {
return new Answer(cmd, true, "");
}
+ protected void deleteEntitySymlinkRootDirectoryIfNeeded(DeleteEntityDownloadURLCommand cmd, String linkPath) {
+ if (StringUtils.isEmpty(linkPath)) {
+ return;
+ }
+ String[] parts = linkPath.split("/");
+ if (parts.length == 0) {
+ return;
+ }
+ String rootDir = parts[0];
+ if (StringUtils.isEmpty(rootDir) || !UuidUtils.isUuid(rootDir)) {
+ return;
+ }
+ logger.info("Deleting symlink root directory: {} for {}", rootDir, cmd.getExtractUrl());
+ Path rootDirPath = Path.of(BASE_EXTRACT_PATH, rootDir);
+ String failMsg = "Failed to delete symlink root directory: {} for {}";
+ try {
+ if (!FileUtil.deleteRecursively(rootDirPath)) {
+ logger.warn(failMsg, rootDir, cmd.getExtractUrl());
+ }
+ } catch (IOException e) {
+ logger.warn(failMsg, rootDir, cmd.getExtractUrl(), e);
+ }
+ }
+
private String getInstallPath(String jobId) {
// TODO Auto-generated method stub
return null;
diff --git a/services/secondary-storage/server/src/test/java/org/apache/cloudstack/storage/template/UploadManagerImplTest.java b/services/secondary-storage/server/src/test/java/org/apache/cloudstack/storage/template/UploadManagerImplTest.java
new file mode 100644
index 00000000000..9038098e235
--- /dev/null
+++ b/services/secondary-storage/server/src/test/java/org/apache/cloudstack/storage/template/UploadManagerImplTest.java
@@ -0,0 +1,85 @@
+// 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.storage.template;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+
+import java.nio.file.Path;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import com.cloud.agent.api.storage.DeleteEntityDownloadURLCommand;
+import com.cloud.utils.FileUtil;
+
+@RunWith(MockitoJUnitRunner.class)
+public class UploadManagerImplTest {
+
+ @InjectMocks
+ UploadManagerImpl uploadManager;
+
+ MockedStatic<FileUtil> fileUtilMock;
+
+ @Before
+ public void setup() {
+ fileUtilMock = mockStatic(FileUtil.class, Mockito.CALLS_REAL_METHODS);
+ fileUtilMock.when(() -> FileUtil.deleteRecursively(any(Path.class))).thenReturn(true);
+ }
+
+ @After
+ public void tearDown() {
+ fileUtilMock.close();
+ }
+
+ @Test
+ public void doesNotDeleteWhenLinkPathIsEmpty() {
+ String emptyLinkPath = "";
+ uploadManager.deleteEntitySymlinkRootDirectoryIfNeeded(mock(DeleteEntityDownloadURLCommand.class), emptyLinkPath);
+ fileUtilMock.verify(() -> FileUtil.deleteRecursively(any(Path.class)), never());
+ }
+
+ @Test
+ public void doesNotDeleteWhenRootDirIsNotUuid() {
+ String invalidLinkPath = "invalidRootDir/file";
+ uploadManager.deleteEntitySymlinkRootDirectoryIfNeeded(mock(DeleteEntityDownloadURLCommand.class), invalidLinkPath);
+ fileUtilMock.verify(() -> FileUtil.deleteRecursively(any(Path.class)), never());
+ }
+
+ @Test
+ public void deletesSymlinkRootDirectoryWhenValidUuid() {
+ String validLinkPath = "123e4567-e89b-12d3-a456-426614174000/file";
+ uploadManager.deleteEntitySymlinkRootDirectoryIfNeeded(mock(DeleteEntityDownloadURLCommand.class), validLinkPath);
+ fileUtilMock.verify(() -> FileUtil.deleteRecursively(any(Path.class)), times(1));
+ }
+
+ @Test
+ public void deletesSymlinkRootDirectoryWhenNoFile() {
+ String validLinkPath = "123e4567-e89b-12d3-a456-426614174000";
+ uploadManager.deleteEntitySymlinkRootDirectoryIfNeeded(mock(DeleteEntityDownloadURLCommand.class), validLinkPath);
+ fileUtilMock.verify(() -> FileUtil.deleteRecursively(any(Path.class)), times(1));
+ }
+}
diff --git a/usage/src/main/java/com/cloud/usage/UsageManagerImpl.java b/usage/src/main/java/com/cloud/usage/UsageManagerImpl.java
index 8d278056aa8..eab371ab353 100644
--- a/usage/src/main/java/com/cloud/usage/UsageManagerImpl.java
+++ b/usage/src/main/java/com/cloud/usage/UsageManagerImpl.java
@@ -31,6 +31,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
import com.cloud.network.Network;
import com.cloud.usage.dao.UsageNetworksDao;
@@ -179,6 +180,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
private final List<UsageVmDiskVO> usageVmDisks = new ArrayList<UsageVmDiskVO>();
private final ScheduledExecutorService _executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Usage-Job"));
+ private final AtomicBoolean isParsingJobRunning = new AtomicBoolean(false);
private final ScheduledExecutorService _heartbeatExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Usage-HB"));
private final ScheduledExecutorService _sanityExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Usage-Sanity"));
private Future _scheduledFuture = null;
@@ -354,7 +356,12 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
(new ManagedContextRunnable() {
@Override
protected void runInContext() {
- runInContextInternal();
+ isParsingJobRunning.set(true);
+ try {
+ runInContextInternal();
+ } finally {
+ isParsingJobRunning.set(false);
+ }
}
}).run();
}
@@ -2177,9 +2184,14 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
if ((timeSinceLastSuccessJob > 0) && (timeSinceLastSuccessJob > (aggregationDurationMillis - 100))) {
if (timeToJob > (aggregationDurationMillis / 2)) {
- logger.debug("it's been {} ms since last usage job and {} ms until next job, scheduling an immediate job to catch up (aggregation duration is {} minutes)"
- , timeSinceLastSuccessJob, timeToJob, _aggregationDuration);
- scheduleParse();
+ logger.debug("Heartbeat: it's been {} ms since last finished usage job and {} ms until next job (aggregation duration is {} minutes)",
+ timeSinceLastSuccessJob, timeToJob, _aggregationDuration);
+ if (isParsingJobRunning.get()) {
+ logger.debug("Heartbeat: A parsing job is already running");
+ } else {
+ logger.debug("Heartbeat: Scheduling an immediate job to catch up");
+ scheduleParse();
+ }
}
}
diff --git a/utils/src/main/java/org/apache/cloudstack/utils/jsinterpreter/JsInterpreter.java b/utils/src/main/java/org/apache/cloudstack/utils/jsinterpreter/JsInterpreter.java
index 3126da50bca..6e6ef2bbe59 100644
--- a/utils/src/main/java/org/apache/cloudstack/utils/jsinterpreter/JsInterpreter.java
+++ b/utils/src/main/java/org/apache/cloudstack/utils/jsinterpreter/JsInterpreter.java
@@ -24,7 +24,6 @@ import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
-import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
@@ -44,6 +43,7 @@ import javax.script.SimpleBindings;
import javax.script.SimpleScriptContext;
import org.apache.commons.collections.MapUtils;
+import org.apache.commons.lang3.ObjectUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openjdk.nashorn.api.scripting.ClassFilter;
@@ -67,7 +67,7 @@ public class JsInterpreter implements Closeable {
protected ScriptEngine interpreter;
protected String interpreterName;
- private final String injectingLogMessage = "Injecting variable [%s] with value [%s] into the JS interpreter.";
+ private final String injectingLogMessage = "Injecting variable [{}] with value [{}] into the JS interpreter.";
protected ExecutorService executor;
private TimeUnit defaultTimeUnit = TimeUnit.MILLISECONDS;
private long timeout;
@@ -107,7 +107,7 @@ public class JsInterpreter implements Closeable {
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
this.interpreterName = factory.getEngineName();
- logger.trace(String.format("Initiating JS interpreter: %s.", interpreterName));
+ logger.trace("Initiating JS interpreter: {}.", interpreterName);
setScriptEngineDisablingJavaLanguage(factory);
}
@@ -136,36 +136,25 @@ public class JsInterpreter implements Closeable {
*/
public void injectVariable(String key, Object value) {
if (key == null) return;
- logger.trace(String.format(injectingLogMessage, key, String.valueOf(value)));
+ logger.trace(injectingLogMessage, key, value);
variables.put(key, value);
}
- /**
- * @deprecated Not needed when using Bindings; kept for source compatibility.
- * Prefer {@link #injectVariable(String, Object)}.
- */
- @Deprecated
- public void injectStringVariable(String key, String value) {
- if (value == null) {
- logger.trace(String.format("Not injecting [%s] because its value is null.", key));
- return;
- }
- injectVariable(key, value);
- }
-
/**
* Injects the variables via Bindings and executes the script with a fresh context.
* @param script Code to be executed.
* @return The result of the executed script.
*/
public Object executeScript(String script) {
- Objects.requireNonNull(script, "script");
+ if (script == null) {
+ throw new CloudRuntimeException("Script injected into the JavaScript interpreter must not be null.");
+ }
- logger.debug(String.format("Executing script [%s].", script));
+ logger.debug("Executing script [{}].", script);
Object result = executeScriptInThread(script);
- logger.debug(String.format("The script [%s] had the following result: [%s].", script, result));
+ logger.debug("The script [{}] had the following result: [{}].", script, result);
return result;
}
@@ -193,7 +182,7 @@ public class JsInterpreter implements Closeable {
}
return result;
} catch (ScriptException se) {
- String msg = se.getMessage() == null ? "Script error" : se.getMessage();
+ String msg = ObjectUtils.defaultIfNull(se.getMessage(), "Script error");
throw new ScriptException("Script error: " + msg, se.getFileName(), se.getLineNumber(), se.getColumnNumber());
}
};
@@ -213,7 +202,7 @@ public class JsInterpreter implements Closeable {
logger.error(message, e);
throw new CloudRuntimeException(message, e);
} catch (ExecutionException e) {
- Throwable cause = e.getCause() == null ? e : e.getCause();
+ Throwable cause = ObjectUtils.defaultIfNull(e.getCause(), e);
String message = String.format("Unable to execute script [%s] due to [%s]", script, cause.getMessage());
logger.error(message, cause);
throw new CloudRuntimeException(message, cause);
diff --git a/utils/src/main/java/org/apache/cloudstack/utils/jsinterpreter/TagAsRuleHelper.java b/utils/src/main/java/org/apache/cloudstack/utils/jsinterpreter/TagAsRuleHelper.java
index da3da612a22..b3013567352 100644
--- a/utils/src/main/java/org/apache/cloudstack/utils/jsinterpreter/TagAsRuleHelper.java
+++ b/utils/src/main/java/org/apache/cloudstack/utils/jsinterpreter/TagAsRuleHelper.java
@@ -17,7 +17,11 @@
package org.apache.cloudstack.utils.jsinterpreter;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import com.cloud.utils.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -27,24 +31,25 @@ public class TagAsRuleHelper {
protected static Logger LOGGER = LogManager.getLogger(TagAsRuleHelper.class);
- private static final String PARSE_TAGS = "tags = tags ? tags.split(',') : [];";
-
-
public static boolean interpretTagAsRule(String rule, String tags, long timeout) {
- String script = PARSE_TAGS + rule;
+ List<String> tagsPresetVariable = new ArrayList<>();
+ if (!StringUtils.isEmpty(tags)) {
+ tagsPresetVariable.addAll(Arrays.asList(tags.split(",")));
+ }
+
try (JsInterpreter jsInterpreter = new JsInterpreter(timeout)) {
- jsInterpreter.injectVariable("tags", tags);
- Object scriptReturn = jsInterpreter.executeScript(script);
+ jsInterpreter.injectVariable("tags", tagsPresetVariable);
+ Object scriptReturn = jsInterpreter.executeScript(rule);
if (scriptReturn instanceof Boolean) {
return (Boolean)scriptReturn;
}
} catch (IOException ex) {
- String message = String.format("Error while executing script [%s].", script);
+ String message = String.format("Error while executing script [%s].", rule);
LOGGER.error(message, ex);
throw new CloudRuntimeException(message, ex);
}
- LOGGER.debug(String.format("Result of tag rule [%s] was not a boolean, returning false.", script));
+ LOGGER.debug("Result of tag rule [{}] was not a boolean, returning false.", rule);
return false;
}
diff --git a/utils/src/test/java/org/apache/cloudstack/utils/jsinterpreter/JsInterpreterTest.java b/utils/src/test/java/org/apache/cloudstack/utils/jsinterpreter/JsInterpreterTest.java
index a78ee7b908e..e6ef43f2fc1 100644
--- a/utils/src/test/java/org/apache/cloudstack/utils/jsinterpreter/JsInterpreterTest.java
+++ b/utils/src/test/java/org/apache/cloudstack/utils/jsinterpreter/JsInterpreterTest.java
@@ -159,24 +159,6 @@ public class JsInterpreterTest {
Mockito.any(ClassLoader.class), Mockito.any(ClassFilter.class));
}
- @Test
- public void injectStringVariableTestNullValueDoNothing() {
- jsInterpreterSpy.variables = new LinkedHashMap<>();
-
- jsInterpreterSpy.injectStringVariable("a", null);
-
- Assert.assertTrue(jsInterpreterSpy.variables.isEmpty());
- }
-
- @Test
- public void injectStringVariableTestNotNullValueSurroundWithDoubleQuotes() {
- jsInterpreterSpy.variables = new LinkedHashMap<>();
-
- jsInterpreterSpy.injectStringVariable("a", "b");
-
- Assert.assertEquals(jsInterpreterSpy.variables.get("a"), "b");
- }
-
@Test
public void executeScriptTestValidScriptShouldPassWithMixedVariables() {
try (JsInterpreter jsInterpreter = new JsInterpreter(1000)) {