feat(kinesis): implement UpdateStreamMode (#488)

* feat(kinesis): implement UpdateStreamMode

UpdateStreamMode previously returned 400 UnsupportedOperation. The
terraform-provider-aws calls UpdateStreamMode unconditionally on every
refresh of an aws_kinesis_stream resource, so the second `terraform
apply` errored even though the stream was functional.

Changes:
- Add UpdateStreamMode handler. Resolves the stream by StreamARN (the
  AWS API does not accept StreamName here), validates StreamMode is
  PROVISIONED or ON_DEMAND, requires the stream to be ACTIVE, and
  treats same-mode requests as a no-op so terraform refresh succeeds.
  Same-mode no-op mirrors the IncreaseStreamRetentionPeriod behaviour
  introduced in #342 for the same reason.
- Honor StreamModeDetails.StreamMode in CreateStream.
- Emit StreamModeDetails on DescribeStream and DescribeStreamSummary
  responses so terraform refresh observes the actual mode and stops
  re-planning the resource.

Closes #440

* chore: retrigger CI (flaky S3 notifications test)
This commit is contained in:
TiM authored and GitHub committed 2026-04-17 00:15:09 -05:00
1 parent 12cf29571d
commit ca2e5d4153
4 files changed
+336 -10

No files matched your search

@@ -61,6 +61,7 @@ public class KinesisJsonHandler {
case "DecreaseStreamRetentionPeriod" -> handleDecreaseStreamRetentionPeriod(request, region);
case "EnableEnhancedMonitoring" -> handleEnableEnhancedMonitoring(request, region);
case "DisableEnhancedMonitoring" -> handleDisableEnhancedMonitoring(request, region);
case "UpdateStreamMode" -> handleUpdateStreamMode(request, region);
default -> Response.status(400)
.entity(new AwsErrorResponse("UnsupportedOperation", "Operation " + action + " is not supported."))
.build();
@@ -75,14 +76,9 @@ public class KinesisJsonHandler {
String streamArn = request.path("StreamARN").asText(null);
if (streamArn != null) {
int streamIdx = streamArn.indexOf(":stream/");
if (streamIdx >= 0) {
String after = streamArn.substring(streamIdx + 8);
int slash = after.indexOf('/');
String name = slash >= 0 ? after.substring(0, slash) : after;
if (!name.isBlank()) {
return name;
}
String name = parseStreamNameFromArn(streamArn);
if (name != null) {
return name;
}
}
@@ -90,13 +86,60 @@ public class KinesisJsonHandler {
"StreamName or valid StreamARN must be provided", 400);
}
private String parseStreamNameFromArn(String streamArn) {
int streamIdx = streamArn.indexOf(":stream/");
if (streamIdx < 0) {
return null;
}
String after = streamArn.substring(streamIdx + 8);
int slash = after.indexOf('/');
String name = slash >= 0 ? after.substring(0, slash) : after;
return name.isBlank() ? null : name;
}
private Response handleCreateStream(JsonNode request, String region) {
String streamName = request.path("StreamName").asText();
int shardCount = request.path("ShardCount").asInt(1);
service.createStream(streamName, shardCount, region);
String streamMode = null;
JsonNode modeDetails = request.path("StreamModeDetails");
if (modeDetails.isObject()) {
String mode = modeDetails.path("StreamMode").asText(null);
if (mode != null && !mode.isBlank()) {
streamMode = mode;
}
}
service.createStream(streamName, shardCount, streamMode, region);
return Response.ok(objectMapper.createObjectNode()).build();
}
private Response handleUpdateStreamMode(JsonNode request, String region) {
// UpdateStreamMode accepts only StreamARN per the AWS API; StreamName is not valid.
String streamArn = request.path("StreamARN").asText(null);
if (streamArn == null || streamArn.isBlank()) {
throw new AwsException("InvalidArgumentException", "StreamARN is required", 400);
}
JsonNode modeDetails = request.path("StreamModeDetails");
if (!modeDetails.isObject()) {
throw new AwsException("InvalidArgumentException", "StreamModeDetails is required", 400);
}
String streamMode = modeDetails.path("StreamMode").asText(null);
if (streamMode == null || streamMode.isBlank()) {
throw new AwsException("InvalidArgumentException", "StreamModeDetails.StreamMode is required", 400);
}
String streamName = extractStreamNameFromArn(streamArn);
service.updateStreamMode(streamName, streamMode, region);
return Response.ok(objectMapper.createObjectNode()).build();
}
private String extractStreamNameFromArn(String streamArn) {
String name = parseStreamNameFromArn(streamArn);
if (name == null) {
throw new AwsException("InvalidArgumentException",
"StreamARN does not contain a valid stream name: " + streamArn, 400);
}
return name;
}
private Response handleDeleteStream(JsonNode request, String region) {
String streamName = resolveStreamName(request);
service.deleteStream(streamName, region);
@@ -128,6 +171,7 @@ public class KinesisJsonHandler {
if (stream.getKeyId() != null) {
desc.put("KeyId", stream.getKeyId());
}
addStreamModeDetailsNode(desc, stream);
addEnhancedMonitoringNode(desc, stream);
@@ -169,6 +213,7 @@ public class KinesisJsonHandler {
if (stream.getKeyId() != null) {
summary.put("KeyId", stream.getKeyId());
}
addStreamModeDetailsNode(summary, stream);
addEnhancedMonitoringNode(summary, stream);
@@ -180,6 +225,10 @@ public class KinesisJsonHandler {
stream.getEnhancedMonitoringMetrics().stream().sorted().forEach(shardLevelMetrics::add);
}
private void addStreamModeDetailsNode(ObjectNode parent, KinesisStream stream) {
parent.putObject("StreamModeDetails").put("StreamMode", stream.getStreamMode());
}
private Response handleRegisterStreamConsumer(JsonNode request, String region) {
String streamArn = request.path("StreamARN").asText();
String consumerName = request.path("ConsumerName").asText();
@@ -25,6 +25,8 @@ public class KinesisService {
"IncomingBytes", "IncomingRecords", "OutgoingBytes", "OutgoingRecords",
"WriteProvisionedThroughputExceeded", "ReadProvisionedThroughputExceeded",
"IteratorAgeMilliseconds", "ALL");
private static final Set<String> VALID_STREAM_MODES = Set.of("PROVISIONED", "ON_DEMAND");
private static final String DEFAULT_STREAM_MODE = "PROVISIONED";
private final StorageBackend<String, KinesisStream> store;
private final StorageBackend<String, KinesisConsumer> consumerStore;
@@ -49,6 +51,16 @@ public class KinesisService {
}
public KinesisStream createStream(String streamName, int shardCount, String region) {
return createStream(streamName, shardCount, null, region);
}
public KinesisStream createStream(String streamName, int shardCount, String streamMode, String region) {
String resolvedMode = streamMode != null ? streamMode : DEFAULT_STREAM_MODE;
if (!VALID_STREAM_MODES.contains(resolvedMode)) {
throw new AwsException("InvalidArgumentException",
"StreamMode must be PROVISIONED or ON_DEMAND, got: " + resolvedMode, 400);
}
String storageKey = regionKey(region, streamName);
if (store.get(storageKey).isPresent()) {
throw new AwsException("ResourceInUseException", "Stream already exists: " + streamName, 400);
@@ -56,6 +68,7 @@ public class KinesisService {
String arn = regionResolver.buildArn("kinesis", region, "stream/" + streamName);
KinesisStream stream = new KinesisStream(streamName, arn);
stream.setStreamMode(resolvedMode);
for (int i = 0; i < shardCount; i++) {
String shardId = String.format("shardId-%012d", i);
@@ -63,10 +76,32 @@ public class KinesisService {
}
store.put(storageKey, stream);
LOG.infov("Created Kinesis stream: {0} in region {1} with {2} shards", streamName, region, shardCount);
LOG.infov("Created Kinesis stream: {0} in region {1} with {2} shards (mode: {3})",
streamName, region, shardCount, resolvedMode);
return stream;
}
public void updateStreamMode(String streamName, String streamMode, String region) {
if (streamMode == null || !VALID_STREAM_MODES.contains(streamMode)) {
throw new AwsException("InvalidArgumentException",
"StreamMode must be PROVISIONED or ON_DEMAND, got: " + streamMode, 400);
}
KinesisStream stream = resolveStream(streamName, region);
if (!"ACTIVE".equals(stream.getStreamStatus())) {
throw new AwsException("ResourceInUseException",
"Stream " + streamName + " is not ACTIVE (current state: " + stream.getStreamStatus() + ")", 400);
}
// Same-mode is a no-op. Mirrors the same-value behaviour in
// increase/decreaseStreamRetentionPeriod (see #342). Avoids breaking
// terraform-provider-aws which calls UpdateStreamMode on every refresh.
if (streamMode.equals(stream.getStreamMode())) {
return;
}
stream.setStreamMode(streamMode);
store.put(regionKey(region, streamName), stream);
LOG.infov("Updated stream mode for {0} to {1}", streamName, streamMode);
}
public List<String> listStreams(String region) {
String prefix = region + "::";
return store.scan(key -> key.startsWith(prefix)).stream()