mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(redshift): add ResetClusterParameterGroup (#6230)
* feat(redshift): add ResetClusterParameterGroup * docs(redshift): update supported operation count * fix(redshift): validate resets and preserve legacy parameter groups * fix(redshift): accept empty Query parameter lists
This commit is contained in:
1 parent
f08ce36298
commit
d2f2625005
11 files changed
+823
-21
No files matched your search
+53
-13
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.redshift;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.AwsNamespaces;
|
||||
import io.github.hectorvent.floci.core.common.IamEnforcementFilter;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
@@ -266,6 +267,13 @@ public class RedshiftQueryHandler {
|
||||
throw new AwsException("InvalidParameterValue", "ParameterGroupName is required", 400);
|
||||
}
|
||||
List<Parameter> parameters = service.describeClusterParameters(parameterGroupName);
|
||||
String source = params.getFirst("Source");
|
||||
if (source != null) {
|
||||
if (!"user".equals(source) && !"engine-default".equals(source)) {
|
||||
throw new AwsException("InvalidParameterValue", "Source must be user or engine-default.", 400);
|
||||
}
|
||||
parameters = parameters.stream().filter(parameter -> source.equals(parameter.getSource())).toList();
|
||||
}
|
||||
|
||||
XmlBuilder xmlBuilder = new XmlBuilder()
|
||||
.start("DescribeClusterParametersResponse")
|
||||
@@ -303,6 +311,28 @@ public class RedshiftQueryHandler {
|
||||
.build();
|
||||
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
|
||||
}
|
||||
case "ResetClusterParameterGroup" -> {
|
||||
List<String> parameterNames = parseParameters(params).stream().map(Parameter::getParameterName).toList();
|
||||
String rawResetAll = params.getFirst("ResetAllParameters");
|
||||
if (rawResetAll != null && !"true".equalsIgnoreCase(rawResetAll) && !"false".equalsIgnoreCase(rawResetAll)) {
|
||||
throw new AwsException("InvalidParameterValue", "ResetAllParameters must be a boolean.", 400);
|
||||
}
|
||||
boolean resetAll = rawResetAll == null ? parameterNames.isEmpty() : Boolean.parseBoolean(rawResetAll);
|
||||
String parameterGroupName = params.getFirst("ParameterGroupName");
|
||||
service.resetClusterParameterGroup(parameterGroupName, resetAll, parameterNames);
|
||||
String xml = new XmlBuilder()
|
||||
.start("ResetClusterParameterGroupResponse", AwsNamespaces.REDSHIFT)
|
||||
.start("ResetClusterParameterGroupResult")
|
||||
.elem("ParameterGroupName", parameterGroupName)
|
||||
.elem("ParameterGroupStatus", "Your parameter group has been updated.")
|
||||
.end("ResetClusterParameterGroupResult")
|
||||
.start("ResponseMetadata")
|
||||
.elem("RequestId", "test-req-id")
|
||||
.end("ResponseMetadata")
|
||||
.end("ResetClusterParameterGroupResponse")
|
||||
.build();
|
||||
return Response.ok(xml).type(MediaType.APPLICATION_XML).build();
|
||||
}
|
||||
case "DeleteClusterParameterGroup" -> {
|
||||
String parameterGroupName = params.getFirst("ParameterGroupName");
|
||||
service.deleteClusterParameterGroup(parameterGroupName);
|
||||
@@ -1203,6 +1233,7 @@ public class RedshiftQueryHandler {
|
||||
if (param.getDataType() != null) {
|
||||
builder.elem("DataType", param.getDataType());
|
||||
}
|
||||
builder.elem("Source", param.getSource());
|
||||
return builder.end("Parameter").build();
|
||||
}
|
||||
|
||||
@@ -1359,21 +1390,30 @@ public class RedshiftQueryHandler {
|
||||
}
|
||||
|
||||
private static List<Parameter> parseParameters(MultivaluedMap<String, String> params) {
|
||||
List<Parameter> parsed = new ArrayList<>();
|
||||
readParameters(params, "Parameters.member", parsed);
|
||||
readParameters(params, "Parameters.Parameter", parsed);
|
||||
return parsed;
|
||||
}
|
||||
|
||||
private static void readParameters(MultivaluedMap<String, String> params, String prefix, List<Parameter> parsed) {
|
||||
for (int i = 1; ; i++) {
|
||||
String name = params.getFirst(prefix + "." + i + ".ParameterName");
|
||||
if (name == null) {
|
||||
break;
|
||||
Map<String, Parameter> parsed = new LinkedHashMap<>();
|
||||
for (String key : params.keySet().stream().sorted().toList()) {
|
||||
if ("Parameters".equals(key)) {
|
||||
if (!"".equals(params.getFirst(key))) {
|
||||
throw new AwsException("InvalidParameterValue", "Parameters must be an indexed list or empty.", 400);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
String value = params.getFirst(prefix + "." + i + ".ParameterValue");
|
||||
parsed.add(new Parameter(name, value));
|
||||
if (!key.startsWith("Parameters.")) {
|
||||
continue;
|
||||
}
|
||||
if (!key.matches("Parameters\\.(?:member|Parameter)\\.[1-9]\\d*\\.[A-Za-z]+")) {
|
||||
throw new AwsException("InvalidParameterValue", "Invalid Parameters member: " + key, 400);
|
||||
}
|
||||
String prefix = key.substring(0, key.lastIndexOf('.'));
|
||||
String name = params.getFirst(prefix + ".ParameterName");
|
||||
if (name == null || name.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "Each parameter must have a ParameterName.", 400);
|
||||
}
|
||||
parsed.putIfAbsent(prefix, new Parameter(name, params.getFirst(prefix + ".ParameterValue")));
|
||||
}
|
||||
return parsed.entrySet().stream()
|
||||
.sorted(Comparator.comparingInt(entry -> numericSuffix(entry.getKey())))
|
||||
.map(Map.Entry::getValue).toList();
|
||||
}
|
||||
|
||||
private static Map<String, String> parseTags(MultivaluedMap<String, String> params) {
|
||||
|
||||
@@ -62,6 +62,7 @@ public class RedshiftService {
|
||||
|
||||
// The group every cluster starts on until one is named.
|
||||
static final String DEFAULT_PARAMETER_GROUP_NAME = "default.redshift-1.0";
|
||||
private static final int MAX_RESET_PARAMETERS = 20;
|
||||
|
||||
private final AccountAwareStorageBackend<Cluster> clusters;
|
||||
private final AccountAwareStorageBackend<Snapshot> snapshots;
|
||||
@@ -1104,6 +1105,9 @@ public class RedshiftService {
|
||||
// ── Parameter Group Operations ───────────────────────────────────────────
|
||||
|
||||
public ClusterParameterGroup createClusterParameterGroup(String parameterGroupName, String parameterGroupFamily, String description) {
|
||||
if (DEFAULT_PARAMETER_GROUP_NAME.equals(parameterGroupName)) {
|
||||
throw new AwsException("InvalidParameterValue", "The default parameter group name is reserved.", 400);
|
||||
}
|
||||
if (parameterGroups.get(parameterGroupName).isPresent()) {
|
||||
throw new AwsException("ClusterParameterGroupAlreadyExists", "Cluster parameter group " + parameterGroupName + " already exists", 400);
|
||||
}
|
||||
@@ -1144,18 +1148,22 @@ public class RedshiftService {
|
||||
|
||||
public List<Parameter> describeClusterParameters(String parameterGroupName) {
|
||||
ClusterParameterGroup group = parameterGroups.get(parameterGroupName)
|
||||
.or(() -> DEFAULT_PARAMETER_GROUP_NAME.equals(parameterGroupName)
|
||||
? Optional.of(defaultParameterGroup()) : Optional.empty())
|
||||
.orElseThrow(() -> new AwsException("ClusterParameterGroupNotFound",
|
||||
"Cluster parameter group " + parameterGroupName + " not found", 404));
|
||||
return group.getParameters();
|
||||
return group.getParameters().stream().map(RedshiftService::copyParameterWithSource).toList();
|
||||
}
|
||||
|
||||
public synchronized ClusterParameterGroup modifyClusterParameterGroup(
|
||||
String parameterGroupName, List<Parameter> updates) {
|
||||
requireMutableParameterGroup(parameterGroupName);
|
||||
ClusterParameterGroup group = parameterGroups.get(parameterGroupName)
|
||||
.orElseThrow(() -> new AwsException("ClusterParameterGroupNotFound",
|
||||
"Cluster parameter group " + parameterGroupName + " not found", 404));
|
||||
|
||||
List<Parameter> current = new ArrayList<>(group.getParameters());
|
||||
List<Parameter> current = new ArrayList<>(group.getParameters().stream()
|
||||
.map(RedshiftService::copyParameterWithSource).toList());
|
||||
for (Parameter update : updates) {
|
||||
boolean matched = false;
|
||||
for (int i = 0; i < current.size(); i++) {
|
||||
@@ -1163,6 +1171,7 @@ public class RedshiftService {
|
||||
if (existing.getParameterName().equals(update.getParameterName())) {
|
||||
// Preserve metadata (description, dataType) from existing parameter if not provided in update
|
||||
existing.setParameterValue(update.getParameterValue());
|
||||
existing.setSource("user");
|
||||
if (update.getDescription() != null) {
|
||||
existing.setDescription(update.getDescription());
|
||||
}
|
||||
@@ -1174,7 +1183,9 @@ public class RedshiftService {
|
||||
}
|
||||
}
|
||||
if (!matched) {
|
||||
current.add(update);
|
||||
Parameter added = copyParameterWithSource(update);
|
||||
added.setSource("user");
|
||||
current.add(added);
|
||||
}
|
||||
}
|
||||
group.setParameters(current);
|
||||
@@ -1183,6 +1194,73 @@ public class RedshiftService {
|
||||
return group;
|
||||
}
|
||||
|
||||
public synchronized void resetClusterParameterGroup(String parameterGroupName, boolean resetAllParameters,
|
||||
List<String> parameterNames) {
|
||||
requireMutableParameterGroup(parameterGroupName);
|
||||
if (resetAllParameters && !parameterNames.isEmpty()) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"Parameters cannot be specified when ResetAllParameters is true.", 400);
|
||||
}
|
||||
if (!resetAllParameters && parameterNames.isEmpty()) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"At least one parameter is required when ResetAllParameters is false.", 400);
|
||||
}
|
||||
if (parameterNames.size() > MAX_RESET_PARAMETERS) {
|
||||
throw new AwsException("InvalidParameterValue", "A maximum of 20 parameters can be reset.", 400);
|
||||
}
|
||||
ClusterParameterGroup group = parameterGroups.get(parameterGroupName)
|
||||
.orElseThrow(() -> new AwsException("ClusterParameterGroupNotFound",
|
||||
"Cluster parameter group " + parameterGroupName + " not found", 404));
|
||||
Set<String> names = new LinkedHashSet<>();
|
||||
for (String name : parameterNames) {
|
||||
if (name == null || name.isBlank() || group.getParameters().stream()
|
||||
.noneMatch(parameter -> name.equals(parameter.getParameterName()))) {
|
||||
throw new AwsException("InvalidParameterValue", "Unknown parameter: " + name, 400);
|
||||
}
|
||||
names.add(name);
|
||||
}
|
||||
List<Parameter> defaults = ClusterParameterGroup.defaultParameters();
|
||||
List<Parameter> reset = defaults;
|
||||
if (!resetAllParameters) {
|
||||
reset = new ArrayList<>();
|
||||
for (Parameter parameter : group.getParameters()) {
|
||||
if (names.contains(parameter.getParameterName())) {
|
||||
defaults.stream().filter(candidate -> candidate.getParameterName().equals(parameter.getParameterName()))
|
||||
.findFirst().ifPresent(reset::add);
|
||||
} else {
|
||||
reset.add(copyParameterWithSource(parameter));
|
||||
}
|
||||
}
|
||||
}
|
||||
group.setParameters(reset);
|
||||
parameterGroups.put(parameterGroupName, group);
|
||||
parameterGroups.flush();
|
||||
}
|
||||
|
||||
private void requireMutableParameterGroup(String name) {
|
||||
if (name == null || name.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "ParameterGroupName is required", 400);
|
||||
}
|
||||
if (DEFAULT_PARAMETER_GROUP_NAME.equals(name) && parameterGroups.get(name).isEmpty()) {
|
||||
throw new AwsException("InvalidParameterValue", "The default parameter group cannot be modified.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static Parameter copyParameterWithSource(Parameter parameter) {
|
||||
Parameter copy = new Parameter(parameter.getParameterName(), parameter.getParameterValue(),
|
||||
parameter.getDescription(), parameter.getDataType());
|
||||
String source = parameter.getSource();
|
||||
if (source == null) {
|
||||
// Older persisted groups do not record whether a value was explicitly set to its default.
|
||||
boolean engineDefault = ClusterParameterGroup.defaultParameters().stream().anyMatch(candidate ->
|
||||
candidate.getParameterName().equals(parameter.getParameterName())
|
||||
&& candidate.getParameterValue().equals(parameter.getParameterValue()));
|
||||
source = engineDefault ? "engine-default" : "user";
|
||||
}
|
||||
copy.setSource(source);
|
||||
return copy;
|
||||
}
|
||||
|
||||
public ClusterParameterGroup deleteClusterParameterGroup(String parameterGroupName) {
|
||||
Optional<ClusterParameterGroup> groupOpt = parameterGroups.get(parameterGroupName);
|
||||
if (groupOpt.isEmpty()) {
|
||||
|
||||
+10
-4
@@ -50,10 +50,16 @@ public class ClusterParameterGroup {
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> tags) { this.tags = tags; }
|
||||
|
||||
private List<Parameter> parameters = new ArrayList<>(List.of(
|
||||
new Parameter("max_cursor_result_set_size", "0", "Maximum cursor result set size", "integer"),
|
||||
new Parameter("wlm_json_configuration", "{}", "WLM configuration", "string")
|
||||
));
|
||||
private List<Parameter> parameters = defaultParameters();
|
||||
|
||||
public static List<Parameter> defaultParameters() {
|
||||
List<Parameter> defaults = new ArrayList<>(List.of(
|
||||
new Parameter("max_cursor_result_set_size", "0", "Maximum cursor result set size", "integer"),
|
||||
new Parameter("wlm_json_configuration", "{}", "WLM configuration", "string")
|
||||
));
|
||||
defaults.forEach(parameter -> parameter.setSource("engine-default"));
|
||||
return defaults;
|
||||
}
|
||||
|
||||
public List<Parameter> getParameters() { return parameters; }
|
||||
public void setParameters(List<Parameter> parameters) { this.parameters = parameters; }
|
||||
|
||||
@@ -8,6 +8,7 @@ public class Parameter {
|
||||
private String parameterValue;
|
||||
private String description;
|
||||
private String dataType;
|
||||
private String source;
|
||||
|
||||
public Parameter() {}
|
||||
|
||||
@@ -54,4 +55,12 @@ public class Parameter {
|
||||
public void setDataType(String dataType) {
|
||||
this.dataType = dataType;
|
||||
}
|
||||
|
||||
public String getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
public void setSource(String source) {
|
||||
this.source = source;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user