mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
refactor(cloudformation): hoist resolveOrDefault onto ProvisionContext (#4049)
Seven per-service provisioners (Lambda, StepFunctions, ElbV2, Events, ApiGatewayRestApi, ApiGatewayV2, Cognito) each carried an identical private resolveOrDefault helper, differing only in argument order. Move it to ProvisionContext as an instance method next to resolveOptional and rewrite the 15 call sites to ctx.resolveOrDefault(props, name, default), removing the copies. Behavior is unchanged: the bodies were byte-identical (resolveOptional plus a non-blank fallback). The monolith's own resolveOrDefault(props, name, engine, default) is left alone: it takes a raw engine rather than a ProvisionContext, is already dead (no callers), and belongs to the later dispatcher-retirement cleanup. Refs #3885
This commit is contained in:
1 parent
3976f2bd05
commit
f9d1b8101c
9 files changed
+36
-48
No files matched your search
+2
-6
@@ -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;
|
||||
}
|
||||
}
|
||||
+4
-9
@@ -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;
|
||||
}
|
||||
|
||||
+2
-7
@@ -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()) {
|
||||
|
||||
+1
-6
@@ -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()) {
|
||||
|
||||
+1
-5
@@ -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) {
|
||||
|
||||
+4
-9
@@ -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
|
||||
|
||||
+10
@@ -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.
|
||||
*
|
||||
|
||||
+1
-6
@@ -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<>();
|
||||
|
||||
Reference in new issue
Block a user