Skip to content

Commit dfab15b

Browse files
sureshanapartiDaanHoogland
authored andcommitted
Configuration validation improvements, and some code refactoring
1 parent a208db5 commit dfab15b

File tree

8 files changed

+55
-43
lines changed

8 files changed

+55
-43
lines changed

engine/api/src/main/java/org/apache/cloudstack/engine/orchestration/service/StorageOrchestrationService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,17 @@
2020
import java.util.List;
2121

2222
import org.apache.cloudstack.api.response.MigrationResponse;
23+
import org.apache.cloudstack.framework.config.ConfigKey;
2324
import org.apache.cloudstack.storage.ImageStoreService.MigrationPolicy;
2425

2526
public interface StorageOrchestrationService {
27+
ConfigKey<Double> ImageStoreImbalanceThreshold = new ConfigKey<>("Advanced", Double.class,
28+
"image.store.imbalance.threshold",
29+
"0.3",
30+
"The storage imbalance threshold that is compared with the standard deviation percentage for a storage utilization metric. " +
31+
"The value is a percentage in decimal format.",
32+
true, ConfigKey.Scope.Global);
33+
2634
MigrationResponse migrateData(Long srcDataStoreId, List<Long> destDatastores, MigrationPolicy migrationPolicy);
2735

2836
MigrationResponse migrateResources(Long srcImgStoreId, Long destImgStoreId, List<Long> templateIdList, List<Long> snapshotIdList);

engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/StorageOrchestrator.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,6 @@ public class StorageOrchestrator extends ManagerBase implements StorageOrchestra
9999
@Inject
100100
DataMigrationUtility migrationHelper;
101101

102-
ConfigKey<Double> ImageStoreImbalanceThreshold = new ConfigKey<>("Advanced", Double.class,
103-
"image.store.imbalance.threshold",
104-
"0.3",
105-
"The storage imbalance threshold that is compared with the standard deviation percentage for a storage utilization metric. " +
106-
"The value is a percentage in decimal format.",
107-
true, ConfigKey.Scope.Global);
108-
109102
Integer numConcurrentCopyTasksPerSSVM = 2;
110103

111104
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface QuotaConfig {
2424
public static final ConfigKey<Boolean> QuotaPluginEnabled = new ConfigKey<Boolean>("Advanced", Boolean.class, "quota.enable.service", "false",
2525
"Indicates whether Quota plugin is enabled or not.", true);
2626

27-
public static final ConfigKey<String> QuotaEnableEnforcement = new ConfigKey<String>("Advanced", String.class, "quota.enable.enforcement", "false",
27+
public static final ConfigKey<Boolean> QuotaEnableEnforcement = new ConfigKey<Boolean>("Advanced", Boolean.class, "quota.enable.enforcement", "false",
2828
"Enable the usage quota enforcement, i.e. on true when exceeding quota the respective account will be locked.", true);
2929

3030
public static final ConfigKey<String> QuotaCurrencySymbol = new ConfigKey<String>("Advanced", String.class, "quota.currency.symbol", "$",
@@ -57,7 +57,7 @@ public interface QuotaConfig {
5757
public static final ConfigKey<String> QuotaSmtpEnabledSecurityProtocols = new ConfigKey<String>("Advanced", String.class, "quota.usage.smtp.enabledSecurityProtocols", "",
5858
"White-space separated security protocols; ex: \"TLSv1 TLSv1.1\". Supported protocols: SSLv2Hello, SSLv3, TLSv1, TLSv1.1 and TLSv1.2.", true);
5959

60-
public static final ConfigKey<String> QuotaSmtpUseStartTLS = new ConfigKey<String>("Advanced", String.class, "quota.usage.smtp.useStartTLS", "false",
60+
public static final ConfigKey<Boolean> QuotaSmtpUseStartTLS = new ConfigKey<Boolean>("Advanced", Boolean.class, "quota.usage.smtp.useStartTLS", "false",
6161
"If set to true and if we enable security via quota.usage.smtp.useAuth, this will enable StartTLS to secure the connection.", true);
6262

6363
public static final ConfigKey<Long> QuotaActivationRuleTimeout = new ConfigKey<>("Advanced", Long.class, "quota.activationrule.timeout", "2000", "The maximum runtime,"

plugins/database/quota/src/main/java/org/apache/cloudstack/api/response/QuotaResponseBuilderImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ public QuotaCreditsResponse addQuotaCredits(Long accountId, Long domainId, Doubl
504504
if (account == null) {
505505
throw new InvalidParameterValueException("Account does not exist with account id " + accountId);
506506
}
507-
final boolean lockAccountEnforcement = "true".equalsIgnoreCase(QuotaConfig.QuotaEnableEnforcement.value());
507+
final boolean lockAccountEnforcement = QuotaConfig.QuotaEnableEnforcement.value();
508508
final BigDecimal currentAccountBalance = _quotaBalanceDao.lastQuotaBalance(accountId, domainId, startOfNextDay(new Date(despositedOn.getTime())));
509509
if (s_logger.isDebugEnabled()) {
510510
s_logger.debug("AddQuotaCredits: Depositing " + amount + " on adjusted date " + despositedOn + ", current balance " + currentAccountBalance);

server/src/main/java/com/cloud/configuration/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ public enum Config {
941941
ElasticLoadBalancerEnabled(
942942
"Advanced",
943943
ManagementServer.class,
944-
String.class,
944+
Boolean.class,
945945
"network.loadbalancer.basiczone.elb.enabled",
946946
"false",
947947
"Whether the load balancing service is enabled for basic zones",

server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
import org.apache.cloudstack.config.ApiServiceConfiguration;
101101
import org.apache.cloudstack.config.Configuration;
102102
import org.apache.cloudstack.context.CallContext;
103+
import org.apache.cloudstack.engine.orchestration.service.StorageOrchestrationService;
103104
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
104105
import org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService;
105106
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
@@ -594,6 +595,8 @@ private void weightBasedParametersForValidation() {
594595
weightBasedParametersForValidation.add(CapacityManager.SecondaryStorageCapacityThreshold.key());
595596
weightBasedParametersForValidation.add(ClusterDrsService.ClusterDrsImbalanceThreshold.key());
596597
weightBasedParametersForValidation.add(ClusterDrsService.ClusterDrsImbalanceSkipThreshold.key());
598+
weightBasedParametersForValidation.add(StorageOrchestrationService.ImageStoreImbalanceThreshold.key());
599+
weightBasedParametersForValidation.add(AlertManager.Ipv6SubnetCapacityThreshold.key());
597600
}
598601

599602
private void overProvisioningFactorsForValidation() {
@@ -1205,7 +1208,7 @@ protected String validateConfigurationValue(final String name, String value, fin
12051208
return "Invalid scope id provided for the parameter " + name;
12061209
}
12071210
}
1208-
Class<?> type = null;
1211+
Class<?> type;
12091212
final Config configuration = Config.getConfig(name);
12101213
if (configuration == null) {
12111214
s_logger.warn("Did not find configuration " + name + " in Config.java. Perhaps moved to ConfigDepot");
@@ -1218,25 +1221,10 @@ protected String validateConfigurationValue(final String name, String value, fin
12181221
} else {
12191222
type = configuration.getType();
12201223
}
1224+
12211225
//no need to validate further if a
12221226
//config can have null value.
1223-
String errMsg = null;
1224-
try {
1225-
if (type.equals(Integer.class)) {
1226-
errMsg = "There was error in trying to parse value: " + value + ". Please enter a valid integer value for parameter " + name;
1227-
Integer.parseInt(value);
1228-
} else if (type.equals(Float.class)) {
1229-
errMsg = "There was error in trying to parse value: " + value + ". Please enter a valid float value for parameter " + name;
1230-
Float.parseFloat(value);
1231-
} else if (type.equals(Long.class)) {
1232-
errMsg = "There was error in trying to parse value: " + value + ". Please enter a valid long value for parameter " + name;
1233-
Long.parseLong(value);
1234-
}
1235-
} catch (final Exception e) {
1236-
// catching generic exception as some throws NullPointerException and some throws NumberFormatExcpeion
1237-
s_logger.error(errMsg);
1238-
return errMsg;
1239-
}
1227+
String errMsg = validateConfigValueAndType(name, value, type);
12401228

12411229
if (value == null) {
12421230
if (type.equals(Boolean.class)) {
@@ -1328,6 +1316,18 @@ protected String validateConfigurationValue(final String name, String value, fin
13281316
}
13291317
}
13301318

1319+
if (type.equals(Double.class)) {
1320+
try {
1321+
final Double val = Double.parseDouble(value);
1322+
if (weightBasedParametersForValidation.contains(name) && (val < 0f || val > 1f)) {
1323+
throw new InvalidParameterValueException("Please enter a value between 0 and 1 for the configuration parameter: " + name);
1324+
}
1325+
} catch (final NumberFormatException e) {
1326+
s_logger.error("There was an error trying to parse the double value for configuration parameter: " + name);
1327+
throw new InvalidParameterValueException("There was an error trying to parse the double value for configuration parameter: " + name);
1328+
}
1329+
}
1330+
13311331
if (type.equals(String.class)) {
13321332
if (name.equalsIgnoreCase(SecStorageAllowedInternalDownloadSites.key()) && StringUtils.isNotEmpty(value)) {
13331333
final String[] cidrs = value.split(",");
@@ -1386,6 +1386,30 @@ protected void validateConfigurationAllowedOnlyForDefaultAdmin(String configName
13861386
}
13871387
}
13881388

1389+
private String validateConfigValueAndType(String name, String value, Class<?> type) {
1390+
String errMsg = null;
1391+
try {
1392+
if (type.equals(Integer.class)) {
1393+
errMsg = "There was error in trying to parse value: " + value + ". Please enter a valid integer value for parameter " + name;
1394+
Integer.parseInt(value);
1395+
} else if (type.equals(Float.class)) {
1396+
errMsg = "There was error in trying to parse value: " + value + ". Please enter a valid float value for parameter " + name;
1397+
Float.parseFloat(value);
1398+
} else if (type.equals(Long.class)) {
1399+
errMsg = "There was error in trying to parse value: " + value + ". Please enter a valid long value for parameter " + name;
1400+
Long.parseLong(value);
1401+
} else if (type.equals(Double.class)) {
1402+
errMsg = "There was error in trying to parse value: " + value + ". Please enter a valid double value for parameter " + name;
1403+
Double.parseDouble(value);
1404+
}
1405+
} catch (final Exception e) {
1406+
// catching generic exception as some throws NullPointerException and some throws NumberFormatExcpeion
1407+
s_logger.error(errMsg);
1408+
return errMsg;
1409+
}
1410+
return errMsg;
1411+
}
1412+
13891413
/**
13901414
* A valid value should be an integer between min and max (the values from the range).
13911415
*/

server/src/main/java/com/cloud/network/firewall/FirewallManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public class FirewallManagerImpl extends ManagerBase implements FirewallService,
155155
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
156156
_name = name;
157157
String elbEnabledString = _configDao.getValue(Config.ElasticLoadBalancerEnabled.key());
158-
_elbEnabled = Boolean.parseBoolean(elbEnabledString);
158+
_elbEnabled = elbEnabledString == null ? false : Boolean.parseBoolean(elbEnabledString);
159159
if (_ipAddrMgr.RulesContinueOnError.value() != null) {
160160
rulesContinueOnErrFlag = _ipAddrMgr.RulesContinueOnError.value();
161161
}

server/src/main/java/com/cloud/storage/ImageStoreServiceImpl.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.apache.cloudstack.context.CallContext;
3333
import org.apache.cloudstack.engine.orchestration.service.StorageOrchestrationService;
3434
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreProvider;
35-
import org.apache.cloudstack.framework.config.ConfigKey;
3635
import org.apache.cloudstack.framework.jobs.AsyncJobManager;
3736
import org.apache.cloudstack.storage.ImageStoreService;
3837
import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
@@ -59,18 +58,6 @@ public class ImageStoreServiceImpl extends ManagerBase implements ImageStoreServ
5958
@Inject
6059
public UUIDManager uuidMgr;
6160

62-
ConfigKey<Double> ImageStoreImbalanceThreshold = new ConfigKey<>("Advanced", Double.class,
63-
"image.store.imbalance.threshold",
64-
"0.3",
65-
"The storage imbalance threshold that is compared with the standard deviation percentage for a storage utilization metric. " +
66-
"The value is a percentage in decimal format.",
67-
true, ConfigKey.Scope.Global);
68-
69-
70-
public Integer numConcurrentCopyTasksPerSSVM = null;
71-
72-
73-
7461
@Override
7562
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
7663
return true;

0 commit comments

Comments
 (0)