Skip to content

armgit / floci

Public Imported from GitHub
0

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

refactor(cloudformation): hoist resolveOrDefault onto ProvisionContext (#4049)

Hector Venturaf9d1b8101c27
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisioner.java
index e1d974b4ac..685b9d94f6 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayRestApiCfnProvisioner.java
@@ -169,12 +169,12 @@ public class ApiGatewayRestApiCfnProvisioner implements CfnResourceProvisioner {
String httpMethod = ctx.resolveOptional(props, "HttpMethod");
Map<String, Object> req = new HashMap<>();
- req.put("authorizationType", resolveOrDefault(ctx, props, "AuthorizationType", "NONE"));
+ req.put("authorizationType", ctx.resolveOrDefault(props, "AuthorizationType", "NONE"));
String authorizerId = ctx.resolveOptional(props, "AuthorizerId");
if (authorizerId != null) {
req.put("authorizerId", authorizerId);
}
- req.put("apiKeyRequired", Boolean.parseBoolean(resolveOrDefault(ctx, props, "ApiKeyRequired", "false")));
+ req.put("apiKeyRequired", Boolean.parseBoolean(ctx.resolveOrDefault(props, "ApiKeyRequired", "false")));
apiGatewayService.putMethod(region, apiId, resourceId, httpMethod, req);
r.setPhysicalId(apiId + "-" + resourceId + "-" + httpMethod);
@@ -230,8 +230,4 @@ public class ApiGatewayRestApiCfnProvisioner implements CfnResourceProvisioner {
}
/** Resolves an optional property, falling back to {@code defaultValue} when absent or blank. */
- private static String resolveOrDefault(ProvisionContext ctx, JsonNode props, String name, String defaultValue) {
- String value = ctx.resolveOptional(props, name);
- return value != null && !value.isBlank() ? value : defaultValue;
- }
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayV2CfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayV2CfnProvisioner.java
index 5b52d75ddd..a89e377bcf 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayV2CfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayV2CfnProvisioner.java
@@ -131,7 +131,7 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner {
}
Map<String, Object> req = new HashMap<>();
req.put("name", name);
- req.put("protocolType", resolveOrDefault(ctx, props, "ProtocolType", "HTTP"));
+ req.put("protocolType", ctx.resolveOrDefault(props, "ProtocolType", "HTTP"));
req.put("routeSelectionExpression", ctx.resolveOptional(props, "RouteSelectionExpression"));
req.put("description", ctx.resolveOptional(props, "Description"));
req.put("apiKeySelectionExpression", ctx.resolveOptional(props, "ApiKeySelectionExpression"));
@@ -782,7 +782,7 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner {
String apiId = ctx.resolveOptional(props, "ApiId");
Map<String, Object> req = new HashMap<>();
req.put("routeKey", ctx.resolveOptional(props, "RouteKey"));
- req.put("authorizationType", resolveOrDefault(ctx, props, "AuthorizationType", "NONE"));
+ req.put("authorizationType", ctx.resolveOrDefault(props, "AuthorizationType", "NONE"));
req.put("authorizerId", ctx.resolveOptional(props, "AuthorizerId"));
// Always present (empty when the property is absent) so an UpdateStack that removes
// AuthorizationScopes from the template clears the route's scopes instead of keeping them.
@@ -807,7 +807,7 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner {
Map<String, Object> req = new HashMap<>();
req.put("integrationType", ctx.resolveOptional(props, "IntegrationType"));
req.put("integrationUri", ctx.resolveOptional(props, "IntegrationUri"));
- req.put("payloadFormatVersion", resolveOrDefault(ctx, props, "PayloadFormatVersion", "2.0"));
+ req.put("payloadFormatVersion", ctx.resolveOrDefault(props, "PayloadFormatVersion", "2.0"));
Integration integration;
if (r.getPhysicalId() == null) {
@@ -829,7 +829,7 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner {
Map<String, Object> req = new HashMap<>();
req.put("stageName", stageName);
- req.put("autoDeploy", resolveOrDefault(ctx, props, "AutoDeploy", "false"));
+ req.put("autoDeploy", ctx.resolveOrDefault(props, "AutoDeploy", "false"));
Map<String, String> stageVariables = parseStageVariables(props, engine);
if (stageVariables != null) {
req.put("stageVariables", stageVariables);
@@ -862,11 +862,6 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner {
// ── Local copies of shared utilities (still used by staying monolith types) ──
- private static String resolveOrDefault(ProvisionContext ctx, JsonNode props, String name, String defaultValue) {
- String value = ctx.resolveOptional(props, name);
- return value != null && !value.isBlank() ? value : defaultValue;
- }
-
private static String textOrNull(JsonNode node, String field) {
return node != null && node.hasNonNull(field) ? node.path(field).asText() : null;
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CognitoCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CognitoCfnProvisioner.java
index 1786b6614a..af30e7e887 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CognitoCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CognitoCfnProvisioner.java
@@ -206,9 +206,9 @@ public class CognitoCfnProvisioner implements CfnResourceProvisioner {
String clientName = requestedClientName == null || requestedClientName.isBlank()
? ctx.generatePhysicalName(r.getLogicalId(), 128, false)
: requestedClientName;
- boolean generateSecret = Boolean.parseBoolean(resolveOrDefault(props, "GenerateSecret", ctx, "false"));
+ boolean generateSecret = Boolean.parseBoolean(ctx.resolveOrDefault(props, "GenerateSecret", "false"));
boolean allowedOAuthFlowsUserPoolClient =
- Boolean.parseBoolean(resolveOrDefault(props, "AllowedOAuthFlowsUserPoolClient", ctx, "false"));
+ Boolean.parseBoolean(ctx.resolveOrDefault(props, "AllowedOAuthFlowsUserPoolClient", "false"));
List<String> allowedOAuthFlows = ctx.resolveStringList(props, "AllowedOAuthFlows");
List<String> allowedOAuthScopes = ctx.resolveStringList(props, "AllowedOAuthScopes");
@@ -412,11 +412,6 @@ public class CognitoCfnProvisioner implements CfnResourceProvisioner {
}
}
- private static String resolveOrDefault(JsonNode props, String name, ProvisionContext ctx, String defaultValue) {
- String value = ctx.resolveOptional(props, name);
- return (value != null && !value.isBlank()) ? value : defaultValue;
- }
-
private static Integer parseInteger(JsonNode props, String name, ProvisionContext ctx) {
String value = ctx.resolveOptional(props, name);
if (value == null || value.isBlank()) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElbV2CfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElbV2CfnProvisioner.java
index d7b1cde1b8..86e09394c4 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElbV2CfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElbV2CfnProvisioner.java
@@ -206,7 +206,7 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
*/
private void provisionListener(StackResource r, JsonNode props, ProvisionContext ctx) {
String lbArn = ctx.resolveOptional(props, "LoadBalancerArn");
- String protocol = resolveOrDefault(props, "Protocol", ctx, "HTTP");
+ String protocol = ctx.resolveOrDefault(props, "Protocol", "HTTP");
int port = parseInt(ctx.resolveOptional(props, "Port"), "Port", 80);
String sslPolicy = ctx.resolveOptional(props, "SslPolicy");
List<String> certificates = parseCertificates(props, ctx);
@@ -463,11 +463,6 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
return result;
}
- private static String resolveOrDefault(JsonNode props, String name, ProvisionContext ctx, String fallback) {
- String value = ctx.resolveOptional(props, name);
- return value != null && !value.isBlank() ? value : fallback;
- }
-
/** An absent value takes the default; anything present has to be an integer. */
private static int parseInt(String value, String property, int fallback) {
if (value == null || value.isBlank()) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/EventsCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/EventsCfnProvisioner.java
index 558079a3e6..f5ef89eeaa 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/EventsCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/EventsCfnProvisioner.java
@@ -636,7 +636,7 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
private void provisionEventBusPolicy(StackResource r, JsonNode props, ProvisionContext ctx) {
- String busName = resolveOrDefault(props, "EventBusName", ctx, "default");
+ String busName = ctx.resolveOrDefault(props, "EventBusName", "default");
String statementId = ctx.resolveOptional(props, "StatementId");
if (statementId == null || statementId.isBlank()) {
throw new AwsException("ValidationException", "EventBusPolicy StatementId is required.", 400);
@@ -728,10 +728,6 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
() -> eventBridgeService.removePermission(busName, statementId, false, region),
"ResourceNotFoundException");
}
- private static String resolveOrDefault(JsonNode props, String name, ProvisionContext ctx, String defaultValue) {
- String value = ctx.resolveOptional(props, name);
- return (value != null && !value.isBlank()) ? value : defaultValue;
- }
/** Copied from the monolith: the shared original serves six other callers and stays there. */
private Map<String, Object> jsonObjectToMap(JsonNode node) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaCfnProvisioner.java
index 2914ddaf54..2d829732a3 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaCfnProvisioner.java
@@ -134,7 +134,7 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
String stackName = ctx.stackName();
String explicitName = ctx.resolveOptional(props, "FunctionName");
boolean hasExplicitName = explicitName != null && !explicitName.isBlank();
- String packageType = resolveOrDefault(ctx, props, "PackageType", "Zip");
+ String packageType = ctx.resolveOrDefault(props, "PackageType", "Zip");
String previousNameMode = r.getAttributes().get(LAMBDA_NAME_MODE_ATTR);
if (previousNameMode == null && r.getPhysicalId() != null) {
// Functions persisted before LAMBDA_NAME_MODE_ATTR existed have no recorded mode, but an
@@ -182,7 +182,7 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
createRequest.put("FunctionName", functionName);
createRequest.put("PackageType", packageType);
- String role = resolveOrDefault(ctx, props, "Role",
+ String role = ctx.resolveOrDefault(props, "Role",
AwsArnUtils.Arn.of("iam", "", ctx.accountId(), "role/default").toString());
createRequest.put("Role", role);
configRequest.put("Role", role);
@@ -190,8 +190,8 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
String runtime;
String handler;
if ("Zip".equals(packageType)) {
- runtime = resolveOrDefault(ctx, props, "Runtime", "nodejs18.x");
- handler = resolveOrDefault(ctx, props, "Handler", "index.handler");
+ runtime = ctx.resolveOrDefault(props, "Runtime", "nodejs18.x");
+ handler = ctx.resolveOrDefault(props, "Handler", "index.handler");
createRequest.put("Runtime", runtime);
createRequest.put("Handler", handler);
configRequest.put("Runtime", runtime);
@@ -787,11 +787,6 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
}
}
- private String resolveOrDefault(ProvisionContext ctx, JsonNode props, String name, String defaultValue) {
- String value = ctx.resolveOptional(props, name);
- return (value != null && !value.isBlank()) ? value : defaultValue;
- }
-
/**
* True when {@code physicalId} has the deterministic shape {@code generatePhysicalName} produces:
* the expected {stackName}-{logicalId} prefix (truncated the same way) followed by exactly 12
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContext.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContext.java
index 1a2e845311..c9e4ca9e10 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContext.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContext.java
@@ -97,6 +97,16 @@ public record ProvisionContext(CloudFormationTemplateEngine engine, String regio
return engine.resolve(props.get(name));
}
+ /**
+ * Resolves an optional property through the engine, falling back to {@code defaultValue} when it
+ * is absent or resolves to blank. Shared by the per-service provisioners so none carries its own
+ * copy.
+ */
+ public String resolveOrDefault(JsonNode props, String name, String defaultValue) {
+ String value = resolveOptional(props, name);
+ return (value != null && !value.isBlank()) ? value : defaultValue;
+ }
+
/**
* Resolves a list property to its non-blank elements, or an empty list when absent.
*
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/StepFunctionsCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/StepFunctionsCfnProvisioner.java
index f2ed02771e..b1e662959b 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/StepFunctionsCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/StepFunctionsCfnProvisioner.java
@@ -274,7 +274,7 @@ public class StepFunctionsCfnProvisioner implements CfnResourceProvisioner {
if (roleArn == null || roleArn.isBlank()) {
throw new AwsException("ValidationError", "RoleArn is required for a state machine", 400);
}
- String type = resolveOrDefault(props, "StateMachineType", ctx, "STANDARD");
+ String type = ctx.resolveOrDefault(props, "StateMachineType", "STANDARD");
Map<String, String> tags = parseCfnTags(props != null ? props.get("Tags") : null, engine);
String definition = resolveStateMachineDefinition(props, ctx);
JsonNode loggingConfiguration = resolveStateMachineLoggingConfiguration(props, engine);
@@ -713,11 +713,6 @@ public class StepFunctionsCfnProvisioner implements CfnResourceProvisioner {
+ resource.getLogicalId(), e);
}
}
- private String resolveOrDefault(JsonNode props, String name,
- ProvisionContext ctx, String defaultValue) {
- String value = ctx.resolveOptional(props, name);
- return (value != null && !value.isBlank()) ? value : defaultValue;
- }
private Map<String, String> parseCfnTags(JsonNode tagsNode, CloudFormationTemplateEngine engine) {
tagsNode = engine.resolveNode(tagsNode);
Map<String, String> out = new HashMap<>();
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContextTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContextTest.java
index a9fd815b8e..7217aca43c 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContextTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContextTest.java
@@ -69,6 +69,17 @@ class ProvisionContextTest {
assertNull(ctx.priorPhysicalId());
}
+ @Test
+ void resolveOrDefaultReturnsTheValueOtherwiseTheDefault() {
+ ProvisionContext ctx = context(null);
+
+ assertEquals("HTTP", ctx.resolveOrDefault(props("{\"ProtocolType\":\"HTTP\"}"), "ProtocolType", "WEBSOCKET"));
+ assertEquals("WEBSOCKET", ctx.resolveOrDefault(props("{}"), "ProtocolType", "WEBSOCKET"), "absent falls back");
+ assertEquals("WEBSOCKET", ctx.resolveOrDefault(props("{\"ProtocolType\":\"\"}"), "ProtocolType", "WEBSOCKET"),
+ "blank falls back");
+ assertEquals("WEBSOCKET", ctx.resolveOrDefault(null, "ProtocolType", "WEBSOCKET"), "null props falls back");
+ }
+
@Test
void stablePhysicalNamePrefersTheTemplatesName() {
assertEquals("chosen",