mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
refactor(cloudformation): move Kinesis and CloudWatch to their own provisioners (#2758)
Migrates AWS::Kinesis::Stream and AWS::CloudWatch::Alarm out of the switch. Two unrelated services in one slice because each is a single service call with property mapping and no shared state; splitting them would cost two review cycles for one mechanical change. Kinesis carries an update path: an unchanged name reconciles stream mode, retention and tags rather than calling createStream again, which the service rejects with ResourceInUseException. It uses ctx.priorPhysicalId() for the same reason the log group slice does, since the body assigns the new id before it returns. sameNameExistingResource, deleteRenamedResource and parseIntProp are copied rather than shared: each still has callers in the monolith. addAlarmActions moves, since all three of its call sites are inside the alarm body. Both service fields leave the monolith; their constructor parameters stay until the endgame. The existing Kinesis and CloudWatch alarm integration tests are QuarkusTests resolved through CDI, so they exercise the new provisioners with no edit, which is the regression evidence for this slice.
This commit is contained in:
1 parent
8765cde6dd
commit
2d724d15aa
6 files changed
+678
-167
No files matched your search
+171
@@ -0,0 +1,171 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.metrics.model.MetricAlarm;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** {@code AWS::CloudWatch::Alarm}. */
|
||||
class CloudWatchCfnProvisionerTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
private CloudWatchMetricsService metrics;
|
||||
private CloudWatchCfnProvisioner provisioner;
|
||||
private ProvisionContext ctx;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
metrics = mock(CloudWatchMetricsService.class);
|
||||
provisioner = new CloudWatchCfnProvisioner(metrics);
|
||||
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
|
||||
when(engine.resolve(any())).thenAnswer(i -> {
|
||||
JsonNode node = i.getArgument(0);
|
||||
return node == null || node.isMissingNode() || node.isNull() ? null : node.asText();
|
||||
});
|
||||
when(engine.resolveNode(any())).thenAnswer(i -> i.getArgument(0));
|
||||
ctx = new ProvisionContext(engine, REGION, "000000000000", "my-stack");
|
||||
}
|
||||
|
||||
private static JsonNode props(String json) {
|
||||
try {
|
||||
return MAPPER.readTree(json);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private MetricAlarm provisionAndCapture(String json) {
|
||||
StackResource r = new StackResource();
|
||||
r.setLogicalId("Alarm");
|
||||
r.setResourceType("AWS::CloudWatch::Alarm");
|
||||
r.setAttributes(new HashMap<>());
|
||||
provisioner.provision(r, props(json), ctx);
|
||||
ArgumentCaptor<MetricAlarm> alarm = ArgumentCaptor.forClass(MetricAlarm.class);
|
||||
verify(metrics).putMetricAlarm(alarm.capture(), eq(REGION));
|
||||
return alarm.getValue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void refIsTheAlarmNameAndTheAlarmReachesTheService() {
|
||||
StackResource r = new StackResource();
|
||||
r.setLogicalId("Alarm");
|
||||
r.setResourceType("AWS::CloudWatch::Alarm");
|
||||
r.setAttributes(new HashMap<>());
|
||||
|
||||
provisioner.provision(r, props("""
|
||||
{"AlarmName": "cpu-high", "MetricName": "CPUUtilization", "Namespace": "AWS/EC2",
|
||||
"ComparisonOperator": "GreaterThanThreshold", "Threshold": "80.5"}
|
||||
"""), ctx);
|
||||
|
||||
assertEquals("cpu-high", r.getPhysicalId(), "Ref is the alarm name");
|
||||
assertTrue(r.getAttributes().containsKey("Arn"), "aws-cloudwatch-alarm.json declares Arn read-only");
|
||||
|
||||
ArgumentCaptor<MetricAlarm> alarm = ArgumentCaptor.forClass(MetricAlarm.class);
|
||||
verify(metrics).putMetricAlarm(alarm.capture(), eq(REGION));
|
||||
assertEquals("CPUUtilization", alarm.getValue().getMetricName());
|
||||
assertEquals("AWS/EC2", alarm.getValue().getNamespace());
|
||||
assertEquals(80.5, alarm.getValue().getThreshold());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnnamedAlarmGetsAGeneratedStackScopedName() {
|
||||
StackResource r = new StackResource();
|
||||
r.setLogicalId("Alarm");
|
||||
r.setResourceType("AWS::CloudWatch::Alarm");
|
||||
r.setAttributes(new HashMap<>());
|
||||
|
||||
provisioner.provision(r, props("{}"), ctx);
|
||||
|
||||
assertTrue(r.getPhysicalId().startsWith("my-stack-Alarm-"), r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void periodAndEvaluationPeriodsFallBackToTheAwsDefaults() {
|
||||
MetricAlarm alarm = provisionAndCapture("""
|
||||
{"AlarmName": "a"}
|
||||
""");
|
||||
|
||||
assertEquals(60, alarm.getPeriod());
|
||||
assertEquals(1, alarm.getEvaluationPeriods());
|
||||
// DatapointsToAlarm defaults to EvaluationPeriods rather than to a fixed number.
|
||||
assertEquals(1, alarm.getDatapointsToAlarm());
|
||||
}
|
||||
|
||||
@Test
|
||||
void datapointsToAlarmDefaultsToEvaluationPeriods() {
|
||||
MetricAlarm alarm = provisionAndCapture("""
|
||||
{"AlarmName": "a", "EvaluationPeriods": "5"}
|
||||
""");
|
||||
|
||||
assertEquals(5, alarm.getEvaluationPeriods());
|
||||
assertEquals(5, alarm.getDatapointsToAlarm());
|
||||
}
|
||||
|
||||
/** ActionsEnabled is true on AWS unless explicitly disabled, so an absent property means true. */
|
||||
@Test
|
||||
void actionsAreEnabledUnlessExplicitlyDisabled() {
|
||||
assertTrue(provisionAndCapture("""
|
||||
{"AlarmName": "a"}
|
||||
""").isActionsEnabled());
|
||||
|
||||
setUp();
|
||||
assertFalse(provisionAndCapture("""
|
||||
{"AlarmName": "a", "ActionsEnabled": "false"}
|
||||
""").isActionsEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
void dimensionsAndAllThreeActionListsAreCarried() {
|
||||
MetricAlarm alarm = provisionAndCapture("""
|
||||
{
|
||||
"AlarmName": "a",
|
||||
"Dimensions": [{"Name": "InstanceId", "Value": "i-123"}],
|
||||
"AlarmActions": ["arn:alarm"],
|
||||
"OKActions": ["arn:ok"],
|
||||
"InsufficientDataActions": ["arn:insufficient"]
|
||||
}
|
||||
""");
|
||||
|
||||
assertEquals(1, alarm.getDimensions().size());
|
||||
assertEquals("InstanceId", alarm.getDimensions().get(0).name());
|
||||
assertEquals("i-123", alarm.getDimensions().get(0).value());
|
||||
assertEquals(List.of("arn:alarm"), alarm.getAlarmActions());
|
||||
assertEquals(List.of("arn:ok"), alarm.getOkActions());
|
||||
assertEquals(List.of("arn:insufficient"), alarm.getInsufficientDataActions());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnparseableThresholdIsIgnoredRatherThanFailingTheResource() {
|
||||
MetricAlarm alarm = provisionAndCapture("""
|
||||
{"AlarmName": "a", "Threshold": "high"}
|
||||
""");
|
||||
|
||||
assertEquals(0.0, alarm.getThreshold());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteReachesTheService() {
|
||||
provisioner.delete("AWS::CloudWatch::Alarm", "cpu-high", REGION);
|
||||
verify(metrics).deleteAlarms(List.of("cpu-high"), REGION);
|
||||
}
|
||||
}
|
||||
+225
@@ -0,0 +1,225 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.kinesis.KinesisService;
|
||||
import io.github.hectorvent.floci.services.kinesis.model.KinesisStream;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** {@code AWS::Kinesis::Stream}, whose provision body doubles as its update path. */
|
||||
class KinesisCfnProvisionerTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
private KinesisService kinesis;
|
||||
private KinesisCfnProvisioner provisioner;
|
||||
private CloudFormationTemplateEngine engine;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
kinesis = mock(KinesisService.class);
|
||||
provisioner = new KinesisCfnProvisioner(kinesis);
|
||||
engine = mock(CloudFormationTemplateEngine.class);
|
||||
when(engine.resolve(any())).thenAnswer(i -> {
|
||||
JsonNode node = i.getArgument(0);
|
||||
return node == null || node.isMissingNode() || node.isNull() ? null : node.asText();
|
||||
});
|
||||
when(engine.resolveNode(any())).thenAnswer(i -> i.getArgument(0));
|
||||
when(kinesis.listTagsForStream(anyString(), anyString())).thenReturn(Map.of());
|
||||
}
|
||||
|
||||
private static JsonNode props(String json) {
|
||||
try {
|
||||
return MAPPER.readTree(json);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static KinesisStream stream(String arn, int retentionHours) {
|
||||
KinesisStream s = new KinesisStream();
|
||||
s.setStreamArn(arn);
|
||||
s.setRetentionPeriodHours(retentionHours);
|
||||
return s;
|
||||
}
|
||||
|
||||
private StackResource provision(String json, String priorPhysicalId) {
|
||||
StackResource r = new StackResource();
|
||||
r.setLogicalId("Stream");
|
||||
r.setResourceType("AWS::Kinesis::Stream");
|
||||
r.setPhysicalId(priorPhysicalId);
|
||||
r.setAttributes(new HashMap<>());
|
||||
provisioner.provision(r, props(json),
|
||||
new ProvisionContext(engine, REGION, "000000000000", "my-stack", priorPhysicalId));
|
||||
return r;
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUsesTheExplicitNameAndExposesTheArn() {
|
||||
when(kinesis.createStream(eq("s"), anyInt(), any(), eq(REGION)))
|
||||
.thenReturn(stream("arn:aws:kinesis:us-east-1:000000000000:stream/s", 24));
|
||||
|
||||
StackResource r = provision("""
|
||||
{"Name": "s", "ShardCount": "3"}
|
||||
""", null);
|
||||
|
||||
verify(kinesis).createStream("s", 3, null, REGION);
|
||||
assertEquals("s", r.getPhysicalId(), "Ref is the stream name");
|
||||
// aws-kinesis-stream.json readOnlyProperties
|
||||
assertEquals(Map.of("Arn", "arn:aws:kinesis:us-east-1:000000000000:stream/s"), r.getAttributes());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shardCountDefaultsToOneWhenAbsentOrUnparseable() {
|
||||
when(kinesis.createStream(anyString(), anyInt(), any(), anyString())).thenReturn(stream("arn", 24));
|
||||
|
||||
provision("""
|
||||
{"Name": "a"}
|
||||
""", null);
|
||||
verify(kinesis).createStream("a", 1, null, REGION);
|
||||
|
||||
provision("""
|
||||
{"Name": "b", "ShardCount": "many"}
|
||||
""", null);
|
||||
verify(kinesis).createStream("b", 1, null, REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void streamModeDetailsAreForwarded() {
|
||||
when(kinesis.createStream(anyString(), anyInt(), any(), anyString())).thenReturn(stream("arn", 24));
|
||||
|
||||
provision("""
|
||||
{"Name": "s", "StreamModeDetails": {"StreamMode": "ON_DEMAND"}}
|
||||
""", null);
|
||||
|
||||
verify(kinesis).createStream("s", 1, "ON_DEMAND", REGION);
|
||||
}
|
||||
|
||||
/**
|
||||
* provision() re-runs on every UpdateStack, so an unchanged name must reconcile rather than
|
||||
* call createStream again, which the service rejects with ResourceInUseException.
|
||||
*/
|
||||
@Test
|
||||
void anUnchangedNameReconcilesInsteadOfRecreating() {
|
||||
when(kinesis.describeStream("s", REGION)).thenReturn(stream("arn", 24));
|
||||
|
||||
provision("""
|
||||
{"Name": "s"}
|
||||
""", "s");
|
||||
|
||||
verify(kinesis, never()).createStream(anyString(), anyInt(), any(), anyString());
|
||||
verify(kinesis).updateStreamMode("s", "PROVISIONED", REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void retentionMovesInTheRightDirectionOnly() {
|
||||
when(kinesis.describeStream("s", REGION)).thenReturn(stream("arn", 24));
|
||||
|
||||
provision("""
|
||||
{"Name": "s", "RetentionPeriodHours": "48"}
|
||||
""", "s");
|
||||
verify(kinesis).increaseStreamRetentionPeriod("s", 48, REGION);
|
||||
|
||||
provision("""
|
||||
{"Name": "s", "RetentionPeriodHours": "12"}
|
||||
""", "s");
|
||||
verify(kinesis).decreaseStreamRetentionPeriod("s", 12, REGION);
|
||||
|
||||
provision("""
|
||||
{"Name": "s", "RetentionPeriodHours": "24"}
|
||||
""", "s");
|
||||
verify(kinesis, never()).increaseStreamRetentionPeriod("s", 24, REGION);
|
||||
verify(kinesis, never()).decreaseStreamRetentionPeriod("s", 24, REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void reconcileRemovesTagsDroppedFromTheTemplate() {
|
||||
when(kinesis.describeStream("s", REGION)).thenReturn(stream("arn", 24));
|
||||
when(kinesis.listTagsForStream("s", REGION)).thenReturn(Map.of("stale", "1", "kept", "2"));
|
||||
|
||||
provision("""
|
||||
{"Name": "s", "Tags": [{"Key": "kept", "Value": "2"}]}
|
||||
""", "s");
|
||||
|
||||
verify(kinesis).removeTagsFromStream("s", List.of("stale"), REGION);
|
||||
verify(kinesis).addTagsToStream("s", Map.of("kept", "2"), REGION);
|
||||
}
|
||||
|
||||
/** A stream deleted out of band since the last update falls back to a fresh create. */
|
||||
@Test
|
||||
void aMissingStreamFallsBackToCreate() {
|
||||
when(kinesis.describeStream("s", REGION))
|
||||
.thenThrow(new AwsException("ResourceNotFoundException", "gone", 400));
|
||||
when(kinesis.createStream(anyString(), anyInt(), any(), anyString())).thenReturn(stream("arn", 24));
|
||||
|
||||
provision("""
|
||||
{"Name": "s"}
|
||||
""", "s");
|
||||
|
||||
verify(kinesis).createStream("s", 1, null, REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRenameCreatesTheNewStreamAndBestEffortDeletesTheOld() {
|
||||
when(kinesis.createStream(eq("new"), anyInt(), any(), anyString())).thenReturn(stream("arn", 24));
|
||||
|
||||
StackResource r = provision("""
|
||||
{"Name": "new"}
|
||||
""", "old");
|
||||
|
||||
assertEquals("new", r.getPhysicalId());
|
||||
verify(kinesis).deleteStream("old", REGION);
|
||||
}
|
||||
|
||||
/** A failure deleting the renamed-away stream must not fail the resource. */
|
||||
@Test
|
||||
void aFailedCleanupOfTheRenamedStreamIsSwallowed() {
|
||||
when(kinesis.createStream(eq("new"), anyInt(), any(), anyString())).thenReturn(stream("arn", 24));
|
||||
org.mockito.Mockito.doThrow(new RuntimeException("boom"))
|
||||
.when(kinesis).deleteStream("old", REGION);
|
||||
|
||||
StackResource r = provision("""
|
||||
{"Name": "new"}
|
||||
""", "old");
|
||||
|
||||
assertEquals("new", r.getPhysicalId(), "the new stream is still the resource's physical id");
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnnamedStreamGetsAGeneratedNameKeptAcrossUpdates() {
|
||||
when(kinesis.createStream(anyString(), anyInt(), any(), anyString())).thenReturn(stream("arn", 24));
|
||||
|
||||
StackResource created = provision("{}", null);
|
||||
assertTrue(created.getPhysicalId().startsWith("my-stack-Stream-"), created.getPhysicalId());
|
||||
|
||||
when(kinesis.describeStream(created.getPhysicalId(), REGION)).thenReturn(stream("arn", 24));
|
||||
StackResource updated = provision("{}", created.getPhysicalId());
|
||||
assertEquals(created.getPhysicalId(), updated.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteReachesTheService() {
|
||||
provisioner.delete("AWS::Kinesis::Stream", "s", REGION);
|
||||
verify(kinesis).deleteStream("s", REGION);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user