fix(opensearch): keep the domain settings Terraform reads back (#7447)

CreateDomain and UpdateDomainConfig dropped the zone awareness count, the
dedicated master type and count, EBS IOPS and throughput, advanced
options and the address type, and Floci had no off-peak window or
software update options, so a domain created by
terraform-aws-modules/opensearch planned a change after every apply.
Store and report them, with AWS's defaults for a new domain.
This commit is contained in:
Ovidiu (Ovi) authored and GitHub committed 2026-11-03 16:58:16 -04:00
1 parent 31633804de
commit 7f1f22e87c
10 files changed
+486 -8

No files matched your search

@@ -20,6 +20,7 @@ import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.io.IOException;
import java.util.List;
/**
* Legacy Amazon Elasticsearch Service API ({@code 2015-01-01}), still used by
@@ -182,10 +183,12 @@ public class ElasticsearchController {
* way in and out.
*/
private static void swapInstanceTypeSuffix(JsonNode cluster, String from, String to) {
// Only InstanceType: the store keeps no DedicatedMasterType or WarmType to respell.
String type = cluster.path("InstanceType").asText("");
if (cluster instanceof ObjectNode node && type.endsWith(from)) {
node.put("InstanceType", type.substring(0, type.length() - from.length()) + to);
// The store keeps no WarmType to respell.
for (String member : List.of("InstanceType", "DedicatedMasterType")) {
String type = cluster.path(member).asText("");
if (cluster instanceof ObjectNode node && type.endsWith(from)) {
node.put(member, type.substring(0, type.length() - from.length()) + to);
}
}
}
@@ -13,7 +13,9 @@ import io.github.hectorvent.floci.services.opensearch.model.DomainEndpointOption
import io.github.hectorvent.floci.services.opensearch.model.EbsOptions;
import io.github.hectorvent.floci.services.opensearch.model.EncryptionAtRestOptions;
import io.github.hectorvent.floci.services.opensearch.model.NodeToNodeEncryptionOptions;
import io.github.hectorvent.floci.services.opensearch.model.OffPeakWindowOptions;
import io.github.hectorvent.floci.services.opensearch.model.OpenSearchVpcEndpoint;
import io.github.hectorvent.floci.services.opensearch.model.SoftwareUpdateOptions;
import io.github.hectorvent.floci.services.opensearch.model.VpcOptions;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
@@ -26,8 +28,10 @@ import org.jboss.logging.Logger;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;
@Path("/2021-01-01")
@@ -74,7 +78,11 @@ public class OpenSearchController {
parseAdvancedSecurityOptions(req.path("AdvancedSecurityOptions")),
parseEncryptionAtRestOptions(req.path("EncryptionAtRestOptions")),
parseNodeToNodeEncryptionOptions(req.path("NodeToNodeEncryptionOptions")),
parseDomainEndpointOptions(req.path("DomainEndpointOptions")));
parseDomainEndpointOptions(req.path("DomainEndpointOptions")),
parseAdvancedOptions(req.path("AdvancedOptions")),
req.path("IPAddressType").asText(null),
parseOffPeakWindowOptions(req.path("OffPeakWindowOptions")),
parseSoftwareUpdateOptions(req.path("SoftwareUpdateOptions")));
Domain domain = service.createDomain(domainName, engineVersion, clusterConfig,
ebsOptions, tags, accessPolicies, options, region);
@@ -180,7 +188,11 @@ public class OpenSearchController {
parseAdvancedSecurityOptions(req.path("AdvancedSecurityOptions")),
parseEncryptionAtRestOptions(req.path("EncryptionAtRestOptions")),
parseNodeToNodeEncryptionOptions(req.path("NodeToNodeEncryptionOptions")),
parseDomainEndpointOptions(req.path("DomainEndpointOptions")));
parseDomainEndpointOptions(req.path("DomainEndpointOptions")),
parseAdvancedOptions(req.path("AdvancedOptions")),
req.path("IPAddressType").asText(null),
parseOffPeakWindowOptions(req.path("OffPeakWindowOptions")),
parseSoftwareUpdateOptions(req.path("SoftwareUpdateOptions")));
Domain domain = service.updateDomainConfig(domainName, engineVersion, clusterConfig,
ebsOptions, accessPolicies, options, regionResolver.resolveRegion(headers));
@@ -651,6 +663,14 @@ public class OpenSearchController {
if (domain.getDomainEndpointOptions() != null) {
node.set("DomainEndpointOptions", toDomainEndpointOptionsNode(domain.getDomainEndpointOptions()));
}
node.set("AdvancedOptions", toAdvancedOptionsNode(domain));
node.put("IPAddressType", ipAddressTypeOf(domain));
if (domain.getOffPeakWindowOptions() != null) {
node.set("OffPeakWindowOptions", toOffPeakWindowNode(domain.getOffPeakWindowOptions()));
}
if (domain.getSoftwareUpdateOptions() != null) {
node.set("SoftwareUpdateOptions", toSoftwareUpdateNode(domain.getSoftwareUpdateOptions()));
}
return node;
}
@@ -691,6 +711,51 @@ public class OpenSearchController {
section.set("Options", toDomainEndpointOptionsNode(domain.getDomainEndpointOptions()));
section.set("Status", configStatusNode(epochSeconds));
}
ObjectNode advanced = domainConfig.putObject("AdvancedOptions");
advanced.set("Options", toAdvancedOptionsNode(domain));
advanced.set("Status", configStatusNode(epochSeconds));
ObjectNode ipAddressType = domainConfig.putObject("IPAddressType");
ipAddressType.put("Options", ipAddressTypeOf(domain));
ipAddressType.set("Status", configStatusNode(epochSeconds));
if (domain.getOffPeakWindowOptions() != null) {
ObjectNode section = domainConfig.putObject("OffPeakWindowOptions");
section.set("Options", toOffPeakWindowNode(domain.getOffPeakWindowOptions()));
section.set("Status", configStatusNode(epochSeconds));
}
if (domain.getSoftwareUpdateOptions() != null) {
ObjectNode section = domainConfig.putObject("SoftwareUpdateOptions");
section.set("Options", toSoftwareUpdateNode(domain.getSoftwareUpdateOptions()));
section.set("Status", configStatusNode(epochSeconds));
}
}
private ObjectNode toAdvancedOptionsNode(Domain domain) {
ObjectNode node = objectMapper.createObjectNode();
new TreeMap<>(domain.getAdvancedOptions()).forEach(node::put);
return node;
}
/** Records written before the address type was stored are reported as {@code ipv4}. */
private static String ipAddressTypeOf(Domain domain) {
return domain.getIpAddressType() != null
? domain.getIpAddressType() : OpenSearchService.DEFAULT_IP_ADDRESS_TYPE;
}
private ObjectNode toOffPeakWindowNode(OffPeakWindowOptions offPeak) {
ObjectNode node = objectMapper.createObjectNode();
node.put("Enabled", Boolean.TRUE.equals(offPeak.getEnabled()));
if (offPeak.getStartHours() != null) {
ObjectNode start = node.putObject("OffPeakWindow").putObject("WindowStartTime");
start.put("Hours", offPeak.getStartHours());
start.put("Minutes", offPeak.getStartMinutes() != null ? offPeak.getStartMinutes() : 0L);
}
return node;
}
private ObjectNode toSoftwareUpdateNode(SoftwareUpdateOptions software) {
ObjectNode node = objectMapper.createObjectNode();
node.put("AutoSoftwareUpdateEnabled", Boolean.TRUE.equals(software.getAutoSoftwareUpdateEnabled()));
return node;
}
private ObjectNode toVpcOptionsNode(VpcOptions vpc) {
@@ -769,6 +834,15 @@ public class OpenSearchController {
node.put("InstanceCount", cc.getInstanceCount());
node.put("DedicatedMasterEnabled", cc.isDedicatedMasterEnabled());
node.put("ZoneAwarenessEnabled", cc.isZoneAwarenessEnabled());
if (cc.getAvailabilityZoneCount() != null) {
node.putObject("ZoneAwarenessConfig").put("AvailabilityZoneCount", cc.getAvailabilityZoneCount());
}
if (cc.getDedicatedMasterType() != null) {
node.put("DedicatedMasterType", cc.getDedicatedMasterType());
}
if (cc.getDedicatedMasterCount() != null) {
node.put("DedicatedMasterCount", cc.getDedicatedMasterCount());
}
}
return node;
}
@@ -783,6 +857,12 @@ public class OpenSearchController {
node.put("EBSEnabled", ebs.isEbsEnabled());
node.put("VolumeType", ebs.getVolumeType());
node.put("VolumeSize", ebs.getVolumeSize());
if (ebs.getIops() != null) {
node.put("Iops", ebs.getIops());
}
if (ebs.getThroughput() != null) {
node.put("Throughput", ebs.getThroughput());
}
}
return node;
}
@@ -807,6 +887,10 @@ public class OpenSearchController {
? node.get("DedicatedMasterEnabled").asBoolean() : null);
cc.setZoneAwarenessEnabled(node.has("ZoneAwarenessEnabled")
? node.get("ZoneAwarenessEnabled").asBoolean() : null);
JsonNode azCount = node.path("ZoneAwarenessConfig").path("AvailabilityZoneCount");
cc.setAvailabilityZoneCount(azCount.isNumber() ? azCount.asInt() : null);
cc.setDedicatedMasterType(node.has("DedicatedMasterType") ? node.get("DedicatedMasterType").asText() : null);
cc.setDedicatedMasterCount(node.has("DedicatedMasterCount") ? node.get("DedicatedMasterCount").asInt() : null);
return cc;
}
@@ -819,9 +903,40 @@ public class OpenSearchController {
ebs.setEbsEnabled(node.has("EBSEnabled") ? node.get("EBSEnabled").asBoolean() : null);
ebs.setVolumeType(node.has("VolumeType") ? node.get("VolumeType").asText() : null);
ebs.setVolumeSize(node.has("VolumeSize") ? node.get("VolumeSize").asInt() : null);
ebs.setIops(node.has("Iops") ? node.get("Iops").asInt() : null);
ebs.setThroughput(node.has("Throughput") ? node.get("Throughput").asInt() : null);
return ebs;
}
/** {@code AdvancedOptions} as given; null when the request leaves it out. */
private static Map<String, String> parseAdvancedOptions(JsonNode node) {
if (node == null || !node.isObject()) {
return null;
}
Map<String, String> options = new LinkedHashMap<>();
node.properties().forEach(entry -> options.put(entry.getKey(), entry.getValue().asText()));
return options;
}
private static OffPeakWindowOptions parseOffPeakWindowOptions(JsonNode node) {
if (node == null || !node.isObject()) {
return null;
}
JsonNode start = node.path("OffPeakWindow").path("WindowStartTime");
return new OffPeakWindowOptions(
node.has("Enabled") ? node.get("Enabled").asBoolean() : null,
start.path("Hours").isNumber() ? start.get("Hours").asLong() : null,
start.path("Minutes").isNumber() ? start.get("Minutes").asLong() : null);
}
private static SoftwareUpdateOptions parseSoftwareUpdateOptions(JsonNode node) {
if (node == null || !node.isObject()) {
return null;
}
return new SoftwareUpdateOptions(node.has("AutoSoftwareUpdateEnabled")
? node.get("AutoSoftwareUpdateEnabled").asBoolean() : null);
}
private VpcOptions parseVpcOptions(JsonNode node) {
if (node == null || node.isMissingNode() || node.isNull()) {
return null;
@@ -19,6 +19,8 @@ import io.github.hectorvent.floci.services.opensearch.model.DomainEndpointOption
import io.github.hectorvent.floci.services.opensearch.model.EbsOptions;
import io.github.hectorvent.floci.services.opensearch.model.EncryptionAtRestOptions;
import io.github.hectorvent.floci.services.opensearch.model.NodeToNodeEncryptionOptions;
import io.github.hectorvent.floci.services.opensearch.model.OffPeakWindowOptions;
import io.github.hectorvent.floci.services.opensearch.model.SoftwareUpdateOptions;
import io.github.hectorvent.floci.services.opensearch.model.VolumeOwnership;
import io.github.hectorvent.floci.services.opensearch.model.VpcOptions;
import jakarta.annotation.PostConstruct;
@@ -30,6 +32,7 @@ import org.jboss.logging.Logger;
import java.security.SecureRandom;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
@@ -110,11 +113,31 @@ public class OpenSearchService implements ResourceProvider {
AdvancedSecurityOptions advancedSecurityOptions,
EncryptionAtRestOptions encryptionAtRestOptions,
NodeToNodeEncryptionOptions nodeToNodeEncryptionOptions,
DomainEndpointOptions domainEndpointOptions) {
DomainEndpointOptions domainEndpointOptions,
Map<String, String> advancedOptions,
String ipAddressType,
OffPeakWindowOptions offPeakWindowOptions,
SoftwareUpdateOptions softwareUpdateOptions) {
public static final DomainOptions EMPTY = new DomainOptions(null, null, null, null, null);
public DomainOptions(VpcOptions vpcOptions, AdvancedSecurityOptions advancedSecurityOptions,
EncryptionAtRestOptions encryptionAtRestOptions,
NodeToNodeEncryptionOptions nodeToNodeEncryptionOptions,
DomainEndpointOptions domainEndpointOptions) {
this(vpcOptions, advancedSecurityOptions, encryptionAtRestOptions, nodeToNodeEncryptionOptions,
domainEndpointOptions, null, null, null, null);
}
}
/** AWS's address type for a domain whose request names none. */
static final String DEFAULT_IP_ADDRESS_TYPE = "ipv4";
/**
* The start AWS gives an off-peak window the request leaves unset: 10:00 P.M., which AWS
* reads as the Region's local time. Floci keeps no time zone per Region and reports it in UTC.
*/
static final long DEFAULT_OFF_PEAK_START_HOURS = 22;
public Domain createDomain(String domainName, String engineVersion, ClusterConfig clusterConfig,
EbsOptions ebsOptions, Map<String, String> tags, String region) {
return createDomain(domainName, engineVersion, clusterConfig, ebsOptions, tags, null,
@@ -157,6 +180,7 @@ public class OpenSearchService implements ResourceProvider {
domain.setTags(tags);
}
applyDomainOptions(domain, options);
applyCreateDefaults(domain);
String creatingKey = accountId + "/" + storageKey(region, domainName);
if (!creating.add(creatingKey)) {
@@ -284,6 +308,15 @@ public class OpenSearchService implements ResourceProvider {
if (supplied.isZoneAwarenessEnabled() != null) {
existing.setZoneAwarenessEnabled(supplied.isZoneAwarenessEnabled());
}
if (supplied.getAvailabilityZoneCount() != null) {
existing.setAvailabilityZoneCount(supplied.getAvailabilityZoneCount());
}
if (supplied.getDedicatedMasterType() != null) {
existing.setDedicatedMasterType(supplied.getDedicatedMasterType());
}
if (supplied.getDedicatedMasterCount() != null) {
existing.setDedicatedMasterCount(supplied.getDedicatedMasterCount());
}
}
/** Copies only the fields the request supplied (non-null) onto {@code existing}. */
@@ -300,6 +333,12 @@ public class OpenSearchService implements ResourceProvider {
if (supplied.getVolumeSize() != null && supplied.getVolumeSize() > 0) {
existing.setVolumeSize(supplied.getVolumeSize());
}
if (supplied.getIops() != null) {
existing.setIops(supplied.getIops());
}
if (supplied.getThroughput() != null) {
existing.setThroughput(supplied.getThroughput());
}
}
public Domain deleteDomain(String domainName, String region) {
@@ -484,6 +523,55 @@ public class OpenSearchService implements ResourceProvider {
if (options.domainEndpointOptions() != null) {
domain.setDomainEndpointOptions(options.domainEndpointOptions());
}
if (options.advancedOptions() != null) {
domain.setAdvancedOptions(new HashMap<>(options.advancedOptions()));
}
if (options.ipAddressType() != null) {
domain.setIpAddressType(options.ipAddressType());
}
if (options.offPeakWindowOptions() != null) {
OffPeakWindowOptions supplied = options.offPeakWindowOptions();
OffPeakWindowOptions current = domain.getOffPeakWindowOptions() != null
? domain.getOffPeakWindowOptions() : new OffPeakWindowOptions();
if (supplied.getEnabled() != null) {
current.setEnabled(supplied.getEnabled());
}
if (supplied.getStartHours() != null) {
current.setStartHours(supplied.getStartHours());
current.setStartMinutes(supplied.getStartMinutes() != null ? supplied.getStartMinutes() : 0L);
}
domain.setOffPeakWindowOptions(current);
}
if (options.softwareUpdateOptions() != null
&& options.softwareUpdateOptions().getAutoSoftwareUpdateEnabled() != null) {
domain.setSoftwareUpdateOptions(options.softwareUpdateOptions());
}
}
/**
* The settings a new domain reports when its request leaves them out, as AWS fills them: an
* {@code ipv4} address type, an enabled off-peak window starting at 10:00 P.M., and automatic
* software updates off.
*/
private static void applyCreateDefaults(Domain domain) {
if (domain.getIpAddressType() == null) {
domain.setIpAddressType(DEFAULT_IP_ADDRESS_TYPE);
}
OffPeakWindowOptions offPeak = domain.getOffPeakWindowOptions();
if (offPeak == null) {
offPeak = new OffPeakWindowOptions(true, null, null);
domain.setOffPeakWindowOptions(offPeak);
}
if (offPeak.getEnabled() == null) {
offPeak.setEnabled(true);
}
if (offPeak.getStartHours() == null) {
offPeak.setStartHours(DEFAULT_OFF_PEAK_START_HOURS);
offPeak.setStartMinutes(0L);
}
if (domain.getSoftwareUpdateOptions() == null) {
domain.setSoftwareUpdateOptions(new SoftwareUpdateOptions(false));
}
}
private boolean matchesEngineType(String engineVersion, String engineType) {
@@ -20,6 +20,16 @@ public class ClusterConfig {
@JsonProperty("ZoneAwarenessEnabled")
private Boolean zoneAwarenessEnabled = false;
/** {@code ZoneAwarenessConfig.AvailabilityZoneCount}; null when the domain was given none. */
@JsonProperty("AvailabilityZoneCount")
private Integer availabilityZoneCount;
@JsonProperty("DedicatedMasterType")
private String dedicatedMasterType;
@JsonProperty("DedicatedMasterCount")
private Integer dedicatedMasterCount;
public ClusterConfig() {}
public String getInstanceType() {
@@ -46,6 +56,30 @@ public class ClusterConfig {
this.dedicatedMasterEnabled = dedicatedMasterEnabled;
}
public Integer getAvailabilityZoneCount() {
return availabilityZoneCount;
}
public void setAvailabilityZoneCount(Integer availabilityZoneCount) {
this.availabilityZoneCount = availabilityZoneCount;
}
public String getDedicatedMasterType() {
return dedicatedMasterType;
}
public void setDedicatedMasterType(String dedicatedMasterType) {
this.dedicatedMasterType = dedicatedMasterType;
}
public Integer getDedicatedMasterCount() {
return dedicatedMasterCount;
}
public void setDedicatedMasterCount(Integer dedicatedMasterCount) {
this.dedicatedMasterCount = dedicatedMasterCount;
}
public Boolean isZoneAwarenessEnabled() {
return zoneAwarenessEnabled;
}
@@ -64,6 +64,19 @@ public class Domain {
@JsonProperty("DomainEndpointOptions")
private DomainEndpointOptions domainEndpointOptions;
@JsonProperty("AdvancedOptions")
private Map<String, String> advancedOptions = new HashMap<>();
/** Null on records written before this field existed; reported as {@code ipv4}. */
@JsonProperty("IPAddressType")
private String ipAddressType;
@JsonProperty("OffPeakWindowOptions")
private OffPeakWindowOptions offPeakWindowOptions;
@JsonProperty("SoftwareUpdateOptions")
private SoftwareUpdateOptions softwareUpdateOptions;
@JsonProperty("Endpoint")
private String endpoint = "";
@@ -103,6 +116,38 @@ public class Domain {
public Domain() {}
public Map<String, String> getAdvancedOptions() {
return advancedOptions;
}
public void setAdvancedOptions(Map<String, String> advancedOptions) {
this.advancedOptions = advancedOptions != null ? advancedOptions : new HashMap<>();
}
public String getIpAddressType() {
return ipAddressType;
}
public void setIpAddressType(String ipAddressType) {
this.ipAddressType = ipAddressType;
}
public OffPeakWindowOptions getOffPeakWindowOptions() {
return offPeakWindowOptions;
}
public void setOffPeakWindowOptions(OffPeakWindowOptions offPeakWindowOptions) {
this.offPeakWindowOptions = offPeakWindowOptions;
}
public SoftwareUpdateOptions getSoftwareUpdateOptions() {
return softwareUpdateOptions;
}
public void setSoftwareUpdateOptions(SoftwareUpdateOptions softwareUpdateOptions) {
this.softwareUpdateOptions = softwareUpdateOptions;
}
public String getDomainName() {
return domainName;
}
@@ -17,6 +17,12 @@ public class EbsOptions {
@JsonProperty("VolumeSize")
private Integer volumeSize = 10;
@JsonProperty("Iops")
private Integer iops;
@JsonProperty("Throughput")
private Integer throughput;
public EbsOptions() {}
public Boolean isEbsEnabled() {
@@ -35,6 +41,22 @@ public class EbsOptions {
this.volumeType = volumeType;
}
public Integer getIops() {
return iops;
}
public void setIops(Integer iops) {
this.iops = iops;
}
public Integer getThroughput() {
return throughput;
}
public void setThroughput(Integer throughput) {
this.throughput = throughput;
}
public Integer getVolumeSize() {
return volumeSize;
}
@@ -0,0 +1,41 @@
package io.github.hectorvent.floci.services.opensearch.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.quarkus.runtime.annotations.RegisterForReflection;
/**
* A domain's off-peak window: whether it is on, and the UTC time its daily ten-hour window starts.
* The API nests the start as {@code OffPeakWindow.WindowStartTime.Hours/Minutes}; the store keeps
* the two numbers flat.
*/
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public class OffPeakWindowOptions {
@JsonProperty("Enabled")
private Boolean enabled;
@JsonProperty("StartHours")
private Long startHours;
@JsonProperty("StartMinutes")
private Long startMinutes;
public OffPeakWindowOptions() {}
public OffPeakWindowOptions(Boolean enabled, Long startHours, Long startMinutes) {
this.enabled = enabled;
this.startHours = startHours;
this.startMinutes = startMinutes;
}
public Boolean getEnabled() { return enabled; }
public void setEnabled(Boolean enabled) { this.enabled = enabled; }
public Long getStartHours() { return startHours; }
public void setStartHours(Long startHours) { this.startHours = startHours; }
public Long getStartMinutes() { return startMinutes; }
public void setStartMinutes(Long startMinutes) { this.startMinutes = startMinutes; }
}
@@ -0,0 +1,23 @@
package io.github.hectorvent.floci.services.opensearch.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.quarkus.runtime.annotations.RegisterForReflection;
/** Whether the domain takes service software updates on its own. */
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public class SoftwareUpdateOptions {
@JsonProperty("AutoSoftwareUpdateEnabled")
private Boolean autoSoftwareUpdateEnabled;
public SoftwareUpdateOptions() {}
public SoftwareUpdateOptions(Boolean autoSoftwareUpdateEnabled) {
this.autoSoftwareUpdateEnabled = autoSoftwareUpdateEnabled;
}
public Boolean getAutoSoftwareUpdateEnabled() { return autoSoftwareUpdateEnabled; }
public void setAutoSoftwareUpdateEnabled(Boolean enabled) { this.autoSoftwareUpdateEnabled = enabled; }
}