Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cloudformation): keep a generated DynamoDB table name across updates (#3721)

Hector Ventura2d444801fd84
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java
index f555540726..4724ecbb83 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java
@@ -99,10 +99,11 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner {
private void provisionTable(StackResource r, JsonNode props, ProvisionContext ctx) {
CloudFormationTemplateEngine engine = ctx.engine();
String region = ctx.region();
- String tableName = ctx.resolveOptional(props, "TableName");
- if (tableName == null || tableName.isBlank()) {
- tableName = ctx.generatePhysicalName(r.getLogicalId(), TABLE_NAME_MAX_LENGTH, false);
- }
+ // TableName is create-only, so an update without one must keep the name generated on
+ // create rather than mint another: a fresh name here created a second table on every
+ // UpdateStack and re-pointed Ref at it.
+ String tableName = ctx.stablePhysicalName(ctx.resolveOptional(props, "TableName"),
+ r.getLogicalId(), TABLE_NAME_MAX_LENGTH, false);
List<KeySchemaElement> keySchema = new ArrayList<>();
List<AttributeDefinition> attrDefs = new ArrayList<>();
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisionerTest.java
index 5726d7cc19..570051fedc 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisionerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisionerTest.java
@@ -54,6 +54,13 @@ class DynamoDbCfnProvisionerTest {
return new ProvisionContext(engine, "us-east-1", "000000000000", "my-stack");
}
+ /** The update path: the same engine plus the physical id the previous provision assigned. */
+ private ProvisionContext updateCtx(String priorPhysicalId) {
+ ProvisionContext create = ctx();
+ return new ProvisionContext(create.engine(), create.region(), create.accountId(),
+ create.stackName(), priorPhysicalId);
+ }
+
private StackResource resource(String type, String logicalId) {
StackResource r = new StackResource();
r.setLogicalId(logicalId);
@@ -228,6 +235,29 @@ class DynamoDbCfnProvisionerTest {
verifyNoInteractions(dynamoDb);
}
+ /**
+ * A table without an explicit TableName keeps the name generated on create: regenerating one
+ * on every UpdateStack created a second table and re-pointed Ref at it.
+ */
+ @Test
+ void updateWithoutExplicitNameReusesTheGeneratedName() throws Exception {
+ String priorName = "my-stack-Orders-0123456789ab";
+ TableDefinition existing = table(false);
+ existing.setTableName(priorName);
+ when(dynamoDb.createTable(eq(priorName), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))
+ .thenThrow(new AwsException("ResourceInUseException", "Table already exists", 400));
+ when(dynamoDb.describeTable(priorName, "us-east-1")).thenReturn(existing);
+ StackResource r = resource("AWS::DynamoDB::Table", "Orders");
+ r.setPhysicalId(priorName);
+
+ provisioner.provision(r, props("{}"), updateCtx(priorName));
+
+ assertEquals(priorName, r.getPhysicalId());
+ verify(dynamoDb).createTable(eq(priorName), anyList(), anyList(), any(), any(), anyList(), anyList(),
+ eq("us-east-1"));
+ verify(dynamoDb).describeTable(priorName, "us-east-1");
+ }
+
@Test
void tagsAreAppliedAndStaleOnesRemoved() throws Exception {
when(dynamoDb.createTable(anyString(), anyList(), anyList(), any(), any(), anyList(), anyList(), anyString()))