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

Filter by extension

Filter by extension

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

import com.cloud.user.Account;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.cloudstack.quota.activationrule.presetvariables.Configuration;
import org.apache.cloudstack.quota.activationrule.presetvariables.GenericPresetVariable;
import org.apache.cloudstack.quota.activationrule.presetvariables.PresetVariableHelper;
import org.apache.cloudstack.quota.activationrule.presetvariables.PresetVariables;
Expand Down Expand Up @@ -467,6 +468,11 @@

}

Configuration configuration = presetVariables.getConfiguration();
if (configuration != null) {
jsInterpreter.injectVariable("configuration", configuration.toString());

Check warning on line 473 in framework/quota/src/main/java/org/apache/cloudstack/quota/QuotaManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L473 was not covered by tests
}

jsInterpreter.injectStringVariable("resourceType", presetVariables.getResourceType());
jsInterpreter.injectVariable("value", presetVariables.getValue().toString());
jsInterpreter.injectVariable("zone", presetVariables.getZone().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@

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

import org.apache.cloudstack.quota.constant.QuotaTypes;

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

@PresetVariableDefinition(description = "A boolean informing if the compute offering offers HA or not.", supportedTypes = {QuotaTypes.RUNNING_VM})
private boolean offerHa;

public boolean isCustomized() {
return customized;
}
Expand All @@ -30,4 +35,13 @@ public void setCustomized(boolean customized) {
fieldNamesToIncludeInToString.add("customized");
}

public boolean offerHa() {
return offerHa;
}

public void setOfferHa(boolean offerHa) {
this.offerHa = offerHa;
fieldNamesToIncludeInToString.add("offerHa");
}

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

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

import org.apache.cloudstack.quota.constant.QuotaTypes;

public class Configuration extends GenericPresetVariable{

@PresetVariableDefinition(description = "A boolean informing if the cluster configuration force.ha is enabled or not.", supportedTypes = {QuotaTypes.RUNNING_VM})
private boolean forceHa;

public boolean getForceHa() {
return forceHa;
}

public void setForceHa(boolean forceHa) {
this.forceHa = forceHa;
fieldNamesToIncludeInToString.add("forceHa");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.Map;
import java.util.stream.Collectors;

import com.cloud.dc.ClusterDetailsDao;
import com.cloud.dc.ClusterDetailsVO;
import com.cloud.host.HostTagVO;
import com.cloud.network.dao.NetworkVO;
import com.cloud.network.vpc.VpcVO;
Expand All @@ -37,6 +39,7 @@
import org.apache.cloudstack.backup.BackupOfferingVO;
import org.apache.cloudstack.backup.dao.BackupOfferingDao;
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.cloudstack.quota.constant.QuotaTypes;
import org.apache.cloudstack.quota.dao.NetworkDao;
import org.apache.cloudstack.quota.dao.VmTemplateDao;
Expand All @@ -51,6 +54,7 @@
import org.apache.cloudstack.utils.bytescale.ByteScaleUtils;
import org.apache.cloudstack.utils.jsinterpreter.JsInterpreter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -181,6 +185,11 @@
@Inject
VpcDao vpcDao;

@Inject
ConfigurationDao configDao;

@Inject
ClusterDetailsDao clusterDetailsDao;

protected boolean backupSnapshotAfterTakingSnapshot = SnapshotInfo.BackupSnapshotAfterTakingSnapshot.value();

Expand All @@ -194,6 +203,7 @@

presetVariables.setAccount(getPresetVariableAccount(usageRecord.getAccountId()));
setPresetVariableProject(presetVariables);
setPresetVariableConfiguration(presetVariables, usageRecord);

presetVariables.setDomain(getPresetVariableDomain(usageRecord.getDomainId()));
presetVariables.setResourceType(usageRecord.getType());
Expand Down Expand Up @@ -271,6 +281,39 @@
return zone;
}

protected void setPresetVariableConfiguration(PresetVariables presetVariables, UsageVO usageRecord) {
if (usageRecord.getUsageType() != UsageTypes.RUNNING_VM) {
return;
}

Configuration configuration = new Configuration();
setForceHaInConfiguration(configuration, usageRecord);

presetVariables.setConfiguration(configuration);
}

protected void setForceHaInConfiguration(Configuration configuration, UsageVO usageRecord) {
Long vmId = usageRecord.getUsageId();
VMInstanceVO vmVo = vmInstanceDao.findByIdIncludingRemoved(vmId);
validateIfObjectIsNull(vmVo, vmId, "VM");

Long hostId = ObjectUtils.defaultIfNull(vmVo.getHostId(), vmVo.getLastHostId());

HostVO hostVo = hostDao.findByIdIncludingRemoved(hostId);
validateIfObjectIsNull(hostVo, hostId, "host");
ClusterDetailsVO forceHa = clusterDetailsDao.findDetail(hostVo.getClusterId(), "force.ha");

String forceHaValue;

if (forceHa != null) {
forceHaValue = forceHa.getValue();
} else {
forceHaValue = configDao.getValue("force.ha");

Check warning on line 311 in framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelper.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L311 was not covered by tests
}

configuration.setForceHa((Boolean.parseBoolean(forceHaValue)));
}

protected Value getPresetVariableValue(UsageVO usageRecord) {
Long accountId = usageRecord.getAccountId();
int usageType = usageRecord.getUsageType();
Expand Down Expand Up @@ -389,12 +432,16 @@
return guestOsVo.getDisplayName();
}

protected ComputeOffering getPresetVariableValueComputeOffering(ServiceOfferingVO serviceOfferingVo) {
protected ComputeOffering getPresetVariableValueComputeOffering(ServiceOfferingVO serviceOfferingVo, int usageType) {
ComputeOffering computeOffering = new ComputeOffering();
computeOffering.setId(serviceOfferingVo.getUuid());
computeOffering.setName(serviceOfferingVo.getName());
computeOffering.setCustomized(serviceOfferingVo.isDynamic());

if (usageType == UsageTypes.RUNNING_VM) {
computeOffering.setOfferHa(serviceOfferingVo.isOfferHA());
}

return computeOffering;
}

Expand All @@ -403,7 +450,7 @@
long computeOfferingId = vmVo.getServiceOfferingId();
ServiceOfferingVO serviceOfferingVo = serviceOfferingDao.findByIdIncludingRemoved(computeOfferingId);
validateIfObjectIsNull(serviceOfferingVo, computeOfferingId, "compute offering");
value.setComputeOffering(getPresetVariableValueComputeOffering(serviceOfferingVo));
value.setComputeOffering(getPresetVariableValueComputeOffering(serviceOfferingVo, usageType));

if (usageType == UsageTypes.RUNNING_VM) {
value.setComputingResources(getPresetVariableValueComputingResource(vmVo, serviceOfferingVo));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public class PresetVariables {
@PresetVariableDefinition(description = "Zone where the resource is.")
private GenericPresetVariable zone;

@PresetVariableDefinition(description = "Configurations of the resource.")
private Configuration configuration;

@PresetVariableDefinition(description = "A list containing the tariffs ordered by the field 'position'.")
private List<Tariff> lastTariffs;

Expand Down Expand Up @@ -90,6 +93,14 @@ public void setZone(GenericPresetVariable zone) {
this.zone = zone;
}

public Configuration getConfiguration() {
return configuration;
}

public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
}

public List<Tariff> getLastTariffs() {
return lastTariffs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ public void injectPresetVariablesIntoJsInterpreterTestProjectIsNullDoNotInjectPr
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, Mockito.never()).injectVariable(Mockito.eq("configuration"), Mockito.anyString());
Mockito.verify(jsInterpreterMock).injectStringVariable(Mockito.eq("resourceType"), Mockito.anyString());
Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("value"), Mockito.anyString());
Mockito.verify(jsInterpreterMock).injectVariable(Mockito.eq("zone"), Mockito.anyString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.Map;
import java.util.Set;

import com.cloud.dc.ClusterDetailsDao;
import com.cloud.dc.ClusterDetailsVO;
import com.cloud.host.HostTagVO;
import com.cloud.hypervisor.Hypervisor;
import com.cloud.storage.StoragePoolTagVO;
Expand Down Expand Up @@ -122,6 +124,9 @@ public class PresetVariableHelperTest {
@Mock
HostTagsDao hostTagsDaoMock;

@Mock
ClusterDetailsDao clusterDetailsDaoMock;

@Mock
ImageStoreDao imageStoreDaoMock;

Expand Down Expand Up @@ -232,6 +237,7 @@ private ComputeOffering getComputeOfferingForTests() {
computeOffering.setId("compute_offering_id");
computeOffering.setName("compute_offering_name");
computeOffering.setCustomized(false);
computeOffering.setOfferHa(false);
return computeOffering;
}

Expand All @@ -243,6 +249,14 @@ private Host getHostForTests() {
return host;
}

private Configuration getConfigurationForTests() {
Configuration configuration = new Configuration();
configuration.setId("config_id");
configuration.setName("config_name");
configuration.setForceHa(false);
return configuration;
}

private List<HostTagVO> getHostTagsForTests() {
return Arrays.asList(new HostTagVO(1, "tag1", false), new HostTagVO(1, "tag2", false));
}
Expand Down Expand Up @@ -329,6 +343,7 @@ public void getPresetVariablesTestSetFieldsAndReturnObject() {

Mockito.doReturn(expected.getAccount()).when(presetVariableHelperSpy).getPresetVariableAccount(Mockito.anyLong());
Mockito.doNothing().when(presetVariableHelperSpy).setPresetVariableProject(Mockito.any());
Mockito.doNothing().when(presetVariableHelperSpy).setPresetVariableConfiguration(Mockito.any(), Mockito.any());
Mockito.doReturn(expected.getDomain()).when(presetVariableHelperSpy).getPresetVariableDomain(Mockito.anyLong());
Mockito.doReturn(expected.getValue()).when(presetVariableHelperSpy).getPresetVariableValue(Mockito.any(UsageVO.class));
Mockito.doReturn(expected.getZone()).when(presetVariableHelperSpy).getPresetVariableZone(Mockito.anyLong());
Expand All @@ -352,6 +367,35 @@ public void setPresetVariableProjectTestAccountWithRoleDoNotSetAsProject() {
Assert.assertNull(result.getProject());
}

@Test
public void setPresetVariableConfigurationTestQuotaTypeDifferentFromRunningVmDoNothing() {
getQuotaTypesForTests(UsageTypes.RUNNING_VM).forEach(type -> {
PresetVariables result = new PresetVariables();
Mockito.doReturn(type.getKey()).when(usageVoMock).getUsageType();
presetVariableHelperSpy.setPresetVariableConfiguration(result, usageVoMock);

Assert.assertNull(result.getConfiguration());
});
}

@Test
public void setPresetVariableConfigurationTestQuotaTypeIsRunningVmSetConfiguration() {
PresetVariables result = new PresetVariables();
Configuration expectedConfig = getConfigurationForTests();
HostVO hostVoMock = Mockito.mock(HostVO.class);
ClusterDetailsVO clusterDetailsVoMock = Mockito.mock(ClusterDetailsVO.class);

Mockito.doReturn(vmInstanceVoMock).when(vmInstanceDaoMock).findByIdIncludingRemoved(Mockito.anyLong());
Mockito.doReturn(hostVoMock).when(hostDaoMock).findByIdIncludingRemoved(Mockito.anyLong());
Mockito.doReturn(1L).when(vmInstanceVoMock).getHostId();
Mockito.doReturn(1).when(usageVoMock).getUsageType();
Mockito.doReturn(clusterDetailsVoMock).when(clusterDetailsDaoMock).findDetail(Mockito.anyLong(), Mockito.anyString());
presetVariableHelperSpy.setPresetVariableConfiguration(result, usageVoMock);

Assert.assertNotNull(result.getConfiguration());
Assert.assertEquals(expectedConfig.getForceHa(), result.getConfiguration().getForceHa());
}

@Test
public void setPresetVariableProjectTestAccountWithoutRoleSetAsProject() {
PresetVariables result = new PresetVariables();
Expand Down Expand Up @@ -626,19 +670,36 @@ public void getPresetVariableValueOsNameTestReturnDisplayName() {
}

@Test
public void getPresetVariableValueComputeOfferingTestSetFieldsAndReturnObject() {
public void getPresetVariableValueComputeOfferingForTestSetFieldsAndReturnObjectForRunningVm() {
ComputeOffering expected = getComputeOfferingForTests();
Mockito.doReturn(expected.getId()).when(serviceOfferingVoMock).getUuid();
Mockito.doReturn(expected.getName()).when(serviceOfferingVoMock).getName();
Mockito.doReturn(expected.isCustomized()).when(serviceOfferingVoMock).isDynamic();
Mockito.doReturn(expected.offerHa()).when(serviceOfferingVoMock).isOfferHA();

ComputeOffering result = presetVariableHelperSpy.getPresetVariableValueComputeOffering(serviceOfferingVoMock);
ComputeOffering result = presetVariableHelperSpy.getPresetVariableValueComputeOffering(serviceOfferingVoMock, UsageTypes.RUNNING_VM);

assertPresetVariableIdAndName(expected, result);
Assert.assertEquals(expected.isCustomized(), result.isCustomized());
Assert.assertEquals(expected.offerHa(), result.offerHa());
validateFieldNamesToIncludeInToString(Arrays.asList("id", "name", "customized", "offerHa"), result);
}

@Test
public void getPresetVariableValueComputeOfferingForTestSetFieldsAndReturnObjectForAllocatedVm() {
ComputeOffering expected = getComputeOfferingForTests();
Mockito.doReturn(expected.getId()).when(serviceOfferingVoMock).getUuid();
Mockito.doReturn(expected.getName()).when(serviceOfferingVoMock).getName();
Mockito.doReturn(expected.isCustomized()).when(serviceOfferingVoMock).isDynamic();

ComputeOffering result = presetVariableHelperSpy.getPresetVariableValueComputeOffering(serviceOfferingVoMock, UsageTypes.ALLOCATED_VM);

assertPresetVariableIdAndName(expected, result);
Assert.assertEquals(expected.isCustomized(), result.isCustomized());
validateFieldNamesToIncludeInToString(Arrays.asList("id", "name", "customized"), result);
}


@Test
public void getPresetVariableValueTemplateTestSetValuesAndReturnObject() {
VMTemplateVO vmTemplateVoMock = Mockito.mock(VMTemplateVO.class);
Expand Down Expand Up @@ -1112,7 +1173,7 @@ public void setPresetVariableValueServiceOfferingAndComputingResourcesTestSetCom
Mockito.doReturn(serviceOfferingVoMock).when(serviceOfferingDaoMock).findByIdIncludingRemoved(Mockito.anyLong());
mockMethodValidateIfObjectIsNull();

Mockito.doReturn(expected.getComputeOffering()).when(presetVariableHelperSpy).getPresetVariableValueComputeOffering(Mockito.any());
Mockito.doReturn(expected.getComputeOffering()).when(presetVariableHelperSpy).getPresetVariableValueComputeOffering(Mockito.any(), Mockito.anyInt());
Mockito.doReturn(expected.getComputingResources()).when(presetVariableHelperSpy).getPresetVariableValueComputingResource(Mockito.any(), Mockito.any());

QuotaTypes.listQuotaTypes().forEach((typeInt, value) -> {
Expand Down
Loading