Skip to content

Commit 94ac3ea

Browse files
committed
Add validator for config key
1 parent 0bb1616 commit 94ac3ea

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

api/src/main/java/org/apache/cloudstack/backup/BackupManager.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.cloudstack.api.command.user.backup.ListBackupOfferingsCmd;
2727
import org.apache.cloudstack.api.command.user.backup.ListBackupsCmd;
2828
import org.apache.cloudstack.framework.config.ConfigKey;
29+
import org.apache.cloudstack.framework.config.ValidatedConfigKey;
2930
import org.apache.cloudstack.framework.config.Configurable;
3031

3132
import com.cloud.utils.Pair;
@@ -42,10 +43,20 @@ public interface BackupManager extends BackupService, Configurable, PluggableSer
4243
"false",
4344
"Is backup and recovery framework enabled.", false, ConfigKey.Scope.Zone);
4445

45-
ConfigKey<String> BackupProviderPlugin = new ConfigKey<>("Advanced", String.class,
46+
ConfigKey<String> BackupProviderPlugin = new ValidatedConfigKey<>("Advanced", String.class,
4647
"backup.framework.provider.plugin",
4748
"dummy",
48-
"The backup and recovery provider plugin.", true, ConfigKey.Scope.Zone, BackupFrameworkEnabled.key());
49+
"The backup and recovery provider plugin. Valid plugin values: dummy, veeam, networker and nas",
50+
true, ConfigKey.Scope.Zone, BackupFrameworkEnabled.key(),
51+
(value) -> {
52+
if (value != null && ((String)value).contains(",") || ((String)value).contains(" ")) {
53+
throw new IllegalArgumentException("Multiple backup provider plugins are not supported. Please provide a single plugin value.");
54+
}
55+
List<String> validPlugins = List.of("dummy", "veeam", "networker", "nas");
56+
if (!validPlugins.contains(((String) value).toLowerCase())) {
57+
throw new IllegalArgumentException("Invalid backup provider plugin: " + value + ". Valid plugin values are: " + String.join(", ", validPlugins));
58+
}
59+
});
4960

5061
ConfigKey<Long> BackupSyncPollingInterval = new ConfigKey<>("Advanced", Long.class,
5162
"backup.framework.sync.interval",
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.framework.config;
18+
19+
import java.util.function.Consumer;
20+
21+
public class ValidatedConfigKey<T> extends ConfigKey<String> {
22+
private final Consumer<T> validator;
23+
24+
public ValidatedConfigKey(String category, Class<String> type, String name, String defaultValue, String description, boolean dynamic, Scope scope, String parent, Consumer<T> validator) {
25+
super(category, type, name, defaultValue, description, dynamic, scope);
26+
this.validator = validator;
27+
}
28+
29+
public Consumer<T> getValidator() {
30+
return validator;
31+
}
32+
33+
public void validateValue(String value) {
34+
if (validator != null) {
35+
validator.accept((T) value);
36+
}
37+
}
38+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
import org.apache.cloudstack.framework.config.ConfigDepot;
108108
import org.apache.cloudstack.framework.config.ConfigKey;
109109
import org.apache.cloudstack.framework.config.Configurable;
110+
import org.apache.cloudstack.framework.config.ValidatedConfigKey;
110111
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
111112
import org.apache.cloudstack.framework.config.dao.ConfigurationGroupDao;
112113
import org.apache.cloudstack.framework.config.dao.ConfigurationSubGroupDao;
@@ -716,6 +717,12 @@ public String updateConfiguration(final long userId, final String name, final St
716717
throw new InvalidParameterValueException(validationMsg);
717718
}
718719

720+
ConfigKey<?> configKey = _configDepot.get(name);
721+
if (configKey instanceof ValidatedConfigKey) {
722+
ValidatedConfigKey<?> validatedConfigKey = (ValidatedConfigKey<?>) configKey;
723+
validatedConfigKey.validateValue(value);
724+
}
725+
719726
// If scope of the parameter is given then it needs to be updated in the
720727
// corresponding details table,
721728
// if scope is mentioned as global or not mentioned then it is normal

0 commit comments

Comments
 (0)