Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(cloudformation): move ApiGatewayV2 types into ApiGatewayV2CfnProvisioner (#4021)
Hector Venturadd60fefc4b5f
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.javaindex cb49bad44a..ab8d98107e 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java@@ -22,7 +22,6 @@ import io.github.hectorvent.floci.services.lambda.model.LambdaLayerVersion;import io.github.hectorvent.floci.services.pipes.model.DesiredState;import io.github.hectorvent.floci.services.s3.S3Service;import io.github.hectorvent.floci.services.ssm.model.Parameter;-import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;import io.github.hectorvent.floci.services.apigatewayv2.model.*;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.AwsException;@@ -65,11 +64,6 @@ public class CloudFormationResourceProvisioner {private static final int LAMBDA_DEFAULT_MEMORY_MB = 128;private static final int LAMBDA_DEFAULT_EPHEMERAL_STORAGE_MB = 512;private static final String LAMBDA_DEFAULT_TRACING_MODE = "PassThrough";- private static final String APIGATEWAY_V2_BODY_ROUTE_IDS_ATTR = "__FlociApiGatewayV2BodyRouteIds";- private static final String APIGATEWAY_V2_BODY_INTEGRATION_IDS_ATTR =- "__FlociApiGatewayV2BodyIntegrationIds";- private static final String APIGATEWAY_V2_BODY_AUTHORIZER_IDS_ATTR =- "__FlociApiGatewayV2BodyAuthorizerIds";/*** Types whose delete needs the whole {@link StackResource} — a create-time attribute (the@@ -84,7 +78,6 @@ public class CloudFormationResourceProvisioner {* {@code CfnDeletePrecedenceTest} fails while both claim it.*/static final Set<String> DELETE_NEEDS_STACK_RESOURCE = Set.of(- "AWS::ApiGatewayV2::Authorizer","AWS::CloudFormation::CustomResource");/**@@ -95,12 +88,6 @@ public class CloudFormationResourceProvisioner {* {@code CfnResourceInventoryTest}.*/static final Set<String> LEGACY_SWITCH_TYPES = Set.of(- "AWS::ApiGatewayV2::Api",- "AWS::ApiGatewayV2::Authorizer",- "AWS::ApiGatewayV2::Deployment",- "AWS::ApiGatewayV2::Integration",- "AWS::ApiGatewayV2::Route",- "AWS::ApiGatewayV2::Stage","AWS::CloudFormation::CustomResource","AWS::Lambda::Function","AWS::Lambda::LayerVersion");@@ -118,7 +105,6 @@ public class CloudFormationResourceProvisioner {private final S3Service s3Service;private final LambdaService lambdaService;private final IamService iamService;- private final ApiGatewayV2Service apiGatewayV2Service;private final LambdaLayerService lambdaLayerService;private final ObjectMapper objectMapper;private final CustomResourceResponseStore customResourceResponseStore;@@ -134,7 +120,6 @@ public class CloudFormationResourceProvisioner {public CloudFormationResourceProvisioner(S3Service s3Service,LambdaService lambdaService,IamService iamService,- ApiGatewayV2Service apiGatewayV2Service,LambdaLayerService lambdaLayerService,ObjectMapper objectMapper,CustomResourceResponseStore customResourceResponseStore,@@ -146,7 +131,6 @@ public class CloudFormationResourceProvisioner {this.s3Service = s3Service;this.lambdaService = lambdaService;this.iamService = iamService;- this.apiGatewayV2Service = apiGatewayV2Service;this.lambdaLayerService = lambdaLayerService;this.objectMapper = objectMapper;this.customResourceResponseStore = customResourceResponseStore;@@ -207,12 +191,6 @@ public class CloudFormationResourceProvisioner {case "AWS::Lambda::Function" -> provisionLambda(resource, properties, engine, region, accountId, stackName);case "AWS::Lambda::LayerVersion" ->provisionLambdaLayerVersion(resource, properties, engine, region, stackName);- case "AWS::ApiGatewayV2::Api" -> provisionApiGatewayV2Api(resource, properties, engine, region, accountId, stackName);- case "AWS::ApiGatewayV2::Authorizer" -> provisionApiGatewayV2Authorizer(resource, properties, engine, region);- case "AWS::ApiGatewayV2::Route" -> provisionApiGatewayV2Route(resource, properties, engine, region);- case "AWS::ApiGatewayV2::Integration" -> provisionApiGatewayV2Integration(resource, properties, engine, region);- case "AWS::ApiGatewayV2::Stage" -> provisionApiGatewayV2Stage(resource, properties, engine, region);- case "AWS::ApiGatewayV2::Deployment" -> provisionApiGatewayV2Deployment(resource, properties, engine, region);case "AWS::CloudFormation::CustomResource" ->provisionCustomResource(resource, properties, engine, region, accountId, stackName);default -> {@@ -357,21 +335,6 @@ public class CloudFormationResourceProvisioner {deleteCustomResource(resource, region);return;}- // Authorizer deletion needs the api id (a stored attribute, not the physical id, which is- // the authorizer id) — same shape as the Nodegroup case above. Without this, the generic- // type/physicalId delete path has no case for this type at all and silently no-ops,- // leaving the authorizer behind in AWS after the stack reports deleted.- if ("AWS::ApiGatewayV2::Authorizer".equals(resourceType)) {- String apiId = resource.getAttributes().get("ApiId");- if (apiId != null && !apiId.isBlank()) {- try {- apiGatewayV2Service.deleteAuthorizer(region, apiId, resource.getPhysicalId());- } catch (Exception e) {- LOG.debugv("Error deleting authorizer {0}: {1}", resource.getPhysicalId(), e.getMessage());- }- }- return;- }throw new IllegalStateException("DELETE_NEEDS_STACK_RESOURCE lists " + resourceType+ " but no branch here deletes it — deleting it by physical id alone would "+ "silently no-op and leave the resource live.");@@ -394,7 +357,6 @@ public class CloudFormationResourceProvisioner {switch (resourceType) {case "AWS::Lambda::Function" -> deleteLambdaFunctionSafe(physicalId, region);// No bus context on the type/physicalId path (e.g. CREATE-rollback); targets the default bus.- case "AWS::ApiGatewayV2::Api" -> apiGatewayV2Service.deleteApi(region, physicalId);case "AWS::Lambda::LayerVersion" -> deleteLambdaLayerVersion(physicalId, region);// Warn for the same reason the create path does: the delete reports success over a// type nothing here removes, and at debug that is invisible at the default log level.@@ -1167,478 +1129,6 @@ public class CloudFormationResourceProvisioner {// ── ApiGateway (V1) ──────────────────────────────────────────────────────- // ── ApiGatewayV2 (HTTP/WebSocket) ────────────────────────────────────────-- private void provisionApiGatewayV2Api(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,- String region, String accountId, String stackName) {- String name = resolveOptional(props, "Name", engine);- if (name == null || name.isBlank()) {- name = generatePhysicalName(stackName, r.getLogicalId(), 255, false);- }- Map<String, Object> req = new HashMap<>();- req.put("name", name);- req.put("protocolType", resolveOrDefault(props, "ProtocolType", engine, "HTTP"));- req.put("routeSelectionExpression", resolveOptional(props, "RouteSelectionExpression", engine));- req.put("description", resolveOptional(props, "Description", engine));- req.put("apiKeySelectionExpression", resolveOptional(props, "ApiKeySelectionExpression", engine));-- Map<String, String> tags = parseApiGatewayV2Tags(props != null ? props.get("Tags") : null, engine);- if (!tags.isEmpty()) {- req.put("tags", tags);- }-- Map<String, Object> cors = parseApiGatewayV2Cors(props != null ? props.get("CorsConfiguration") : null, engine);- if (cors != null) {- req.put("corsConfiguration", cors);- }-- Api api;- if (r.getPhysicalId() == null) {- api = apiGatewayV2Service.createApi(region, req);- } else {- api = apiGatewayV2Service.updateApi(region, r.getPhysicalId(), req);- }- r.setPhysicalId(api.getApiId());- r.getAttributes().put("ApiEndpoint", api.getApiEndpoint());- reconcileApiGatewayV2BodyRoutes(r, region, api.getApiId(), props, engine);- }-- /**- * Reconciles the routes, integrations, and authorizers materialized from an ApiGatewayV2 OpenAPI body.- * Only IDs stored on this CloudFormation resource are removed, so separately declared V2- * resources remain outside this generated-resource lifecycle.- */- private void reconcileApiGatewayV2BodyRoutes(StackResource r, String region, String apiId, JsonNode props,- CloudFormationTemplateEngine engine) {- JsonNode body = OpenApiDocuments.resolve(props, engine, s3Service, objectMapper);- ApiGatewayV2BodyResourceState previous = null;- try {- previous = snapshotApiGatewayV2BodyResources(r, region, apiId);- // API Gateway requires route keys to be unique. Remove only the tracked body-generated- // resources before creating their replacements; rollback restores this snapshot.- deleteApiGatewayV2BodyResources(r, region, apiId);- } catch (RuntimeException e) {- rollbackApiGatewayV2BodyReplacement(r, region, apiId,- new ApiGatewayV2BodyResources(List.of(), List.of(), List.of()), previous, e);- throw e;- }-- if (body == null) {- return;- }-- ApiGatewayV2BodyResources replacement;- try {- replacement = materializeApiGatewayV2BodyRoutes(region, apiId, body);- } catch (ApiGatewayV2BodyMaterializationException e) {- rollbackApiGatewayV2BodyReplacement(r, region, apiId, e.resources(), previous, e);- throw e;- } catch (RuntimeException e) {- // materializeApiGatewayV2BodyRoutes already removed its partial replacement.- rollbackApiGatewayV2BodyReplacement(r, region, apiId,- new ApiGatewayV2BodyResources(List.of(), List.of(), List.of()), previous, e);- throw e;- }- storeApiGatewayV2BodyResourceIds(r, APIGATEWAY_V2_BODY_ROUTE_IDS_ATTR, replacement.routeIds());- storeApiGatewayV2BodyResourceIds(r, APIGATEWAY_V2_BODY_INTEGRATION_IDS_ATTR,- replacement.integrationIds());- storeApiGatewayV2BodyResourceIds(r, APIGATEWAY_V2_BODY_AUTHORIZER_IDS_ATTR,- replacement.authorizerIds());- }-- /**- * CloudFormation's ApiGatewayV2 {@code Body} is an OpenAPI document. Materialize each- * declared HTTP operation as the route that API Gateway V2 serves, including its OpenAPI- * security requirement and a route target when it declares an integration extension.- */- private ApiGatewayV2BodyResources materializeApiGatewayV2BodyRoutes(String region, String apiId,- JsonNode body) {- List<String> routeIds = new ArrayList<>();- List<String> integrationIds = new ArrayList<>();- List<String> authorizerIds = new ArrayList<>();- try {- Map<String, OpenApiAuthorizerBinding> authorizers = materializeApiGatewayV2BodyAuthorizers(- region, apiId, body, authorizerIds);- JsonNode paths = body.path("paths");- if (!paths.isObject()) {- return new ApiGatewayV2BodyResources(routeIds, integrationIds, authorizerIds);- }-- Iterator<Map.Entry<String, JsonNode>> pathEntries = paths.fields();- while (pathEntries.hasNext()) {- Map.Entry<String, JsonNode> pathEntry = pathEntries.next();- if (!pathEntry.getValue().isObject()) {- continue;- }- Iterator<Map.Entry<String, JsonNode>> operations = pathEntry.getValue().fields();- while (operations.hasNext()) {- Map.Entry<String, JsonNode> operation = operations.next();- String method = operation.getKey();- if (!isHttpApiOperation(method) || !operation.getValue().isObject()) {- continue;- }-- Map<String, Object> routeRequest = new HashMap<>();- routeRequest.put("routeKey", openApiRouteKey(method, pathEntry.getKey()));- applyOpenApiRouteSecurity(body, operation.getValue(), pathEntry.getKey(), method,- authorizers, routeRequest);- JsonNode integration = operation.getValue().path("x-amazon-apigateway-integration");- if (integration.isObject()) {- String integrationType = textOrNull(integration, "type");- if (integrationType != null && !integrationType.isBlank()) {- Map<String, Object> integrationRequest = new HashMap<>();- integrationRequest.put("integrationType", integrationType.toUpperCase(Locale.ROOT));- putOpenApiIntegrationValue(integrationRequest, "integrationUri", integration, "uri");- putOpenApiIntegrationValue(integrationRequest, "integrationMethod", integration,- "httpMethod");- putOpenApiIntegrationValue(integrationRequest, "payloadFormatVersion", integration,- "payloadFormatVersion");- Integration createdIntegration = apiGatewayV2Service.createIntegration(region, apiId,- integrationRequest);- integrationIds.add(createdIntegration.getIntegrationId());- routeRequest.put("target", "integrations/" + createdIntegration.getIntegrationId());- }- }- Route createdRoute = apiGatewayV2Service.createRoute(region, apiId, routeRequest);- routeIds.add(createdRoute.getRouteId());- }- }- return new ApiGatewayV2BodyResources(routeIds, integrationIds, authorizerIds);- } catch (RuntimeException e) {- ApiGatewayV2BodyResources partial = new ApiGatewayV2BodyResources(- routeIds, integrationIds, authorizerIds);- List<RuntimeException> cleanupFailures = cleanupApiGatewayV2BodyResources(region, apiId, partial);- if (!cleanupFailures.isEmpty()) {- cleanupFailures.forEach(e::addSuppressed);- throw new ApiGatewayV2BodyMaterializationException(e, partial);- }- throw e;- }- }-- private Map<String, OpenApiAuthorizerBinding> materializeApiGatewayV2BodyAuthorizers(- String region, String apiId, JsonNode body, List<String> authorizerIds) {- Map<String, OpenApiAuthorizerBinding> bindings = new LinkedHashMap<>();- JsonNode schemes = body.path("components").path("securitySchemes");- if (!schemes.isObject()) {- return bindings;- }-- Iterator<Map.Entry<String, JsonNode>> entries = schemes.fields();- while (entries.hasNext()) {- Map.Entry<String, JsonNode> entry = entries.next();- String schemeName = entry.getKey();- JsonNode scheme = entry.getValue();- if (!scheme.isObject()) {- continue;- }-- JsonNode definition = scheme.path("x-amazon-apigateway-authorizer");- if (!definition.isObject()) {- continue;- }-- String type = textOrNull(definition, "type");- String authorizerType;- String routeAuthorizationType;- if ("jwt".equalsIgnoreCase(type)) {- authorizerType = "JWT";- routeAuthorizationType = "JWT";- } else if ("request".equalsIgnoreCase(type)) {- authorizerType = "REQUEST";- routeAuthorizationType = "CUSTOM";- } else {- throw invalidOpenApiV2Security("Authorizer " + schemeName- + " must declare type jwt or request");- }-- Map<String, Object> request = new HashMap<>();- request.put("name", schemeName);- request.put("authorizerType", authorizerType);- putOpenApiAuthorizerIdentitySource(request, definition);- putOpenApiAuthorizerValue(request, "authorizerUri", definition, "authorizerUri");- putOpenApiAuthorizerValue(request, "authorizerPayloadFormatVersion", definition,- "authorizerPayloadFormatVersion");- putOpenApiAuthorizerValue(request, "authorizerResultTtlInSeconds", definition,- "authorizerResultTtlInSeconds");- putOpenApiAuthorizerValue(request, "enableSimpleResponses", definition,- "enableSimpleResponses");-- if ("JWT".equals(authorizerType)) {- JsonNode jwt = definition.path("jwtConfiguration");- if (!jwt.isObject()) {- throw invalidOpenApiV2Security("JWT authorizer " + schemeName- + " must declare jwtConfiguration");- }- Map<String, Object> jwtConfiguration = new HashMap<>();- jwtConfiguration.put("issuer", textOrNull(jwt, "issuer"));- jwtConfiguration.put("audience", openApiStringList(jwt.get("audience"),- "jwtConfiguration.audience for authorizer " + schemeName));- request.put("jwtConfiguration", jwtConfiguration);- }-- Authorizer created = apiGatewayV2Service.createAuthorizer(region, apiId, request);- authorizerIds.add(created.getAuthorizerId());- bindings.put(schemeName,- new OpenApiAuthorizerBinding(routeAuthorizationType, created.getAuthorizerId()));- }- return bindings;- }-- private void applyOpenApiRouteSecurity(JsonNode body, JsonNode operation, String path, String method,- Map<String, OpenApiAuthorizerBinding> authorizers,- Map<String, Object> routeRequest) {- JsonNode security = operation.has("security") ? operation.get("security") : body.get("security");- if (security == null || security.isNull() || security.isMissingNode()) {- return;- }- if (!security.isArray()) {- throw invalidOpenApiV2Security("security must be an array");- }- if (security.isEmpty()) {- routeRequest.put("authorizationType", "NONE");- return; // An operation-level empty array explicitly overrides inherited security.- }-- // Each object is one alternative in the outer OR-list, but names inside one object are- // an AND requirement. A V2 route can attach only one authorizer, so accepting a multi-name- // object would silently weaken its authentication contract. AWS classifies multiple- // security requirements as an HTTP API import error:- // https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-open-api.html- // Validate every alternative before selecting a representable one.- for (JsonNode requirement : security) {- if (!requirement.isObject()) {- throw invalidOpenApiV2Security("security requirements must be objects");- }- if (requirement.isEmpty()) {- routeRequest.put("authorizationType", "NONE");- return; // An empty requirement allows anonymous access by OpenAPI definition.- }- if (requirement.size() > 1) {- throw invalidOpenApiV2Security(- "HTTP API routes do not support AND security requirements with multiple schemes");- }- }- if (security.size() > 1) {- throw invalidOpenApiV2Security(- "HTTP API routes do not support OR security requirements with multiple alternatives");- }-- String unsupportedScheme = null;- for (JsonNode requirement : security) {- Iterator<Map.Entry<String, JsonNode>> schemes = requirement.fields();- while (schemes.hasNext()) {- Map.Entry<String, JsonNode> scheme = schemes.next();- OpenApiAuthorizerBinding binding = authorizers.get(scheme.getKey());- if (binding == null) {- unsupportedScheme = scheme.getKey();- continue;- }- routeRequest.put("authorizationType", binding.authorizationType());- if (binding.authorizerId() != null) {- routeRequest.put("authorizerId", binding.authorizerId());- }- if ("JWT".equals(binding.authorizationType())) {- List<String> scopes = openApiStringList(scheme.getValue(),- "security scopes for scheme " + scheme.getKey());- if (!scopes.isEmpty()) {- routeRequest.put("authorizationScopes", scopes);- }- }- return;- }- }- throw invalidOpenApiV2Security(- "Protected operation " + openApiRouteKey(method, path)- + " references unsupported security scheme '" + unsupportedScheme + "'");- }-- private static void putOpenApiAuthorizerIdentitySource(Map<String, Object> request, JsonNode definition) {- JsonNode identitySource = definition.get("identitySource");- if (identitySource == null || identitySource.isNull()) {- return;- }- if (identitySource.isTextual()) {- request.put("identitySource", identitySource.asText());- return;- }- request.put("identitySource", openApiStringList(identitySource, "authorizer identitySource"));- }-- private static void putOpenApiAuthorizerValue(Map<String, Object> request, String requestKey,- JsonNode definition, String definitionKey) {- JsonNode value = definition.get(definitionKey);- if (value == null || value.isNull()) {- return;- }- if (value.isTextual()) {- request.put(requestKey, value.asText());- } else if (value.isBoolean()) {- request.put(requestKey, value.booleanValue());- } else if (value.isIntegralNumber()) {- request.put(requestKey, value.intValue());- } else {- throw invalidOpenApiV2Security(definitionKey + " has an invalid value");- }- }-- private static List<String> openApiStringList(JsonNode value, String fieldName) {- if (value == null || value.isNull()) {- return List.of();- }- if (!value.isArray()) {- throw invalidOpenApiV2Security(fieldName + " must be an array of strings");- }- List<String> values = new ArrayList<>();- for (JsonNode element : value) {- if (!element.isTextual()) {- throw invalidOpenApiV2Security(fieldName + " must be an array of strings");- }- values.add(element.asText());- }- return values;- }-- private static AwsException invalidOpenApiV2Security(String message) {- return new AwsException("ValidationException", message, 400);- }-- private void deleteApiGatewayV2BodyResources(StackResource r, String region, String apiId) {- deleteApiGatewayV2BodyResources(region, apiId, new ApiGatewayV2BodyResources(- apiGatewayV2BodyResourceIds(r, APIGATEWAY_V2_BODY_ROUTE_IDS_ATTR),- apiGatewayV2BodyResourceIds(r, APIGATEWAY_V2_BODY_INTEGRATION_IDS_ATTR),- apiGatewayV2BodyResourceIds(r, APIGATEWAY_V2_BODY_AUTHORIZER_IDS_ATTR)));- r.getAttributes().remove(APIGATEWAY_V2_BODY_ROUTE_IDS_ATTR);- r.getAttributes().remove(APIGATEWAY_V2_BODY_INTEGRATION_IDS_ATTR);- r.getAttributes().remove(APIGATEWAY_V2_BODY_AUTHORIZER_IDS_ATTR);- }-- private void deleteApiGatewayV2BodyResources(String region, String apiId,- ApiGatewayV2BodyResources resources) {- for (String routeId : resources.routeIds()) {- deleteApiGatewayV2BodyRouteIfPresent(region, apiId, routeId);- }- for (String integrationId : resources.integrationIds()) {- deleteApiGatewayV2BodyIntegrationIfPresent(region, apiId, integrationId);- }- for (String authorizerId : resources.authorizerIds()) {- deleteApiGatewayV2BodyAuthorizerIfPresent(region, apiId, authorizerId);- }- }-- private ApiGatewayV2BodyResourceState snapshotApiGatewayV2BodyResources(StackResource r, String region,- String apiId) {- List<Route> routes = new ArrayList<>();- for (String routeId : apiGatewayV2BodyResourceIds(r, APIGATEWAY_V2_BODY_ROUTE_IDS_ATTR)) {- try {- routes.add(apiGatewayV2Service.getRoute(region, apiId, routeId));- } catch (AwsException e) {- if (e.getHttpStatus() != 404) {- throw e;- }- }- }-- List<Integration> integrations = new ArrayList<>();- for (String integrationId : apiGatewayV2BodyResourceIds(r, APIGATEWAY_V2_BODY_INTEGRATION_IDS_ATTR)) {- try {- integrations.add(apiGatewayV2Service.getIntegration(region, apiId, integrationId));- } catch (AwsException e) {- if (e.getHttpStatus() != 404) {- throw e;- }- }- }-- List<Authorizer> authorizers = new ArrayList<>();- for (String authorizerId : apiGatewayV2BodyResourceIds(r, APIGATEWAY_V2_BODY_AUTHORIZER_IDS_ATTR)) {- try {- authorizers.add(apiGatewayV2Service.getAuthorizer(region, apiId, authorizerId));- } catch (AwsException e) {- if (e.getHttpStatus() != 404) {- throw e;- }- }- }- return new ApiGatewayV2BodyResourceState(routes, integrations, authorizers);- }-- private void rollbackApiGatewayV2BodyReplacement(StackResource r, String region, String apiId,- ApiGatewayV2BodyResources replacement,- ApiGatewayV2BodyResourceState previous,- RuntimeException failure) {- List<RuntimeException> cleanupFailures = cleanupApiGatewayV2BodyResources(region, apiId, replacement);-- if (previous != null) {- storeApiGatewayV2BodyResourceIds(r, APIGATEWAY_V2_BODY_ROUTE_IDS_ATTR,- previous.routes().stream().map(Route::getRouteId).toList());- storeApiGatewayV2BodyResourceIds(r, APIGATEWAY_V2_BODY_INTEGRATION_IDS_ATTR,- previous.integrations().stream().map(Integration::getIntegrationId).toList());- storeApiGatewayV2BodyResourceIds(r, APIGATEWAY_V2_BODY_AUTHORIZER_IDS_ATTR,- previous.authorizers().stream().map(Authorizer::getAuthorizerId).toList());- }- if (!cleanupFailures.isEmpty()) {- cleanupFailures.forEach(failure::addSuppressed);- retainApiGatewayV2BodyResourceIds(r, replacement);- }- if (previous == null) {- return;- }- try {- // Routes refer to integrations and authorizers, so restore both before their routes.- for (Authorizer authorizer : previous.authorizers()) {- apiGatewayV2Service.restoreAuthorizer(region, apiId, authorizer);- }- for (Integration integration : previous.integrations()) {- apiGatewayV2Service.restoreIntegration(region, apiId, integration);- }- for (Route route : previous.routes()) {- apiGatewayV2Service.restoreRoute(region, apiId, route, replacement.routeIds());- }- } catch (RuntimeException restoreFailure) {- failure.addSuppressed(restoreFailure);- String reason = restoreFailure.getMessage() != null- ? restoreFailure.getMessage()- : restoreFailure.getClass().getSimpleName();- r.getAttributes().put(UPDATE_ROLLBACK_FAILURE_ATTR, reason);- }- }-- private List<RuntimeException> cleanupApiGatewayV2BodyResources(String region, String apiId,- ApiGatewayV2BodyResources resources) {- List<RuntimeException> failures = new ArrayList<>();- for (String routeId : resources.routeIds()) {- try {- deleteApiGatewayV2BodyRouteIfPresent(region, apiId, routeId);- } catch (RuntimeException e) {- failures.add(e);- }- }- for (String integrationId : resources.integrationIds()) {- try {- deleteApiGatewayV2BodyIntegrationIfPresent(region, apiId, integrationId);- } catch (RuntimeException e) {- failures.add(e);- }- }- for (String authorizerId : resources.authorizerIds()) {- try {- deleteApiGatewayV2BodyAuthorizerIfPresent(region, apiId, authorizerId);- } catch (RuntimeException e) {- failures.add(e);- }- }- return failures;- }-- private void retainApiGatewayV2BodyResourceIds(StackResource r, ApiGatewayV2BodyResources resources) {- retainApiGatewayV2BodyResourceIds(r, APIGATEWAY_V2_BODY_ROUTE_IDS_ATTR, resources.routeIds());- retainApiGatewayV2BodyResourceIds(r, APIGATEWAY_V2_BODY_INTEGRATION_IDS_ATTR,- resources.integrationIds());- retainApiGatewayV2BodyResourceIds(r, APIGATEWAY_V2_BODY_AUTHORIZER_IDS_ATTR,- resources.authorizerIds());- }-/*** Carries ownership discovered by a failed update onto the last known-good resource metadata* that CloudFormation restores. Only additive cleanup tracking belongs here; normal attempted@@ -1648,290 +1138,13 @@ public class CloudFormationResourceProvisioner {// Any provisioner using ReplacementCleanup: an entity the failed attempt created and could// not remove is owed to the next cleanup, which runs on the restored resource.ReplacementCleanup.mergeDisplaced(previous, attempted);- if (!"AWS::ApiGatewayV2::Api".equals(previous.getResourceType())- || !Objects.equals(previous.getResourceType(), attempted.getResourceType())) {+ if (!Objects.equals(previous.getResourceType(), attempted.getResourceType())) {return;}- retainApiGatewayV2BodyResourceIds(previous, APIGATEWAY_V2_BODY_ROUTE_IDS_ATTR,- apiGatewayV2BodyResourceIds(attempted, APIGATEWAY_V2_BODY_ROUTE_IDS_ATTR));- retainApiGatewayV2BodyResourceIds(previous, APIGATEWAY_V2_BODY_INTEGRATION_IDS_ATTR,- apiGatewayV2BodyResourceIds(attempted, APIGATEWAY_V2_BODY_INTEGRATION_IDS_ATTR));- retainApiGatewayV2BodyResourceIds(previous, APIGATEWAY_V2_BODY_AUTHORIZER_IDS_ATTR,- apiGatewayV2BodyResourceIds(attempted, APIGATEWAY_V2_BODY_AUTHORIZER_IDS_ATTR));- }-- private static void retainApiGatewayV2BodyResourceIds(StackResource r, String attributeName,- List<String> resourceIds) {- LinkedHashSet<String> retained = new LinkedHashSet<>(apiGatewayV2BodyResourceIds(r, attributeName));- retained.addAll(resourceIds);- storeApiGatewayV2BodyResourceIds(r, attributeName, new ArrayList<>(retained));- }-- private static List<String> apiGatewayV2BodyResourceIds(StackResource r, String attributeName) {- String ids = r.getAttributes().get(attributeName);- return ids == null || ids.isBlank() ? List.of() : Arrays.asList(ids.split(","));- }-- private static void storeApiGatewayV2BodyResourceIds(StackResource r, String attributeName,- List<String> resourceIds) {- if (resourceIds.isEmpty()) {- r.getAttributes().remove(attributeName);- } else {- r.getAttributes().put(attributeName, String.join(",", resourceIds));- }- }-- private void deleteApiGatewayV2BodyRouteIfPresent(String region, String apiId, String routeId) {- try {- apiGatewayV2Service.deleteRoute(region, apiId, routeId);- } catch (AwsException e) {- if (e.getHttpStatus() != 404) {- throw e;- }- }- }-- private void deleteApiGatewayV2BodyIntegrationIfPresent(String region, String apiId, String integrationId) {- try {- apiGatewayV2Service.deleteIntegration(region, apiId, integrationId);- } catch (AwsException e) {- if (e.getHttpStatus() != 404) {- throw e;- }- }- }-- private void deleteApiGatewayV2BodyAuthorizerIfPresent(String region, String apiId, String authorizerId) {- try {- apiGatewayV2Service.deleteAuthorizer(region, apiId, authorizerId);- } catch (AwsException e) {- if (e.getHttpStatus() != 404) {- throw e;- }- }- }-- private record ApiGatewayV2BodyResources(List<String> routeIds, List<String> integrationIds,- List<String> authorizerIds) {}-- private record ApiGatewayV2BodyResourceState(List<Route> routes, List<Integration> integrations,- List<Authorizer> authorizers) {}-- private record OpenApiAuthorizerBinding(String authorizationType, String authorizerId) {}-- private static final class ApiGatewayV2BodyMaterializationException extends RuntimeException {- private final ApiGatewayV2BodyResources resources;-- private ApiGatewayV2BodyMaterializationException(RuntimeException cause,- ApiGatewayV2BodyResources resources) {- super(cause.getMessage(), cause);- this.resources = resources;- }-- private ApiGatewayV2BodyResources resources() {- return resources;- }- }-- private static boolean isHttpApiOperation(String method) {- return switch (method.toLowerCase(Locale.ROOT)) {- case "get", "put", "post", "delete", "options", "head", "patch", "trace",- "x-amazon-apigateway-any-method" -> true;- default -> false;- };- }-- private static String openApiRouteKey(String method, String path) {- String routeMethod = "x-amazon-apigateway-any-method".equals(method) ? "ANY"- : method.toUpperCase(Locale.ROOT);- return routeMethod + " " + path;- }-- private static void putOpenApiIntegrationValue(Map<String, Object> request, String requestKey,- JsonNode integration, String openApiKey) {- String value = textOrNull(integration, openApiKey);- if (value != null) {- request.put(requestKey, value);- }- }-- private Map<String, String> parseApiGatewayV2Tags(JsonNode tagsNode, CloudFormationTemplateEngine engine) {- Map<String, String> out = new HashMap<>();- if (tagsNode == null || tagsNode.isNull()) {- return out;- }- JsonNode resolved = engine.resolveNode(tagsNode);- if (!resolved.isObject()) {- return out;- }- resolved.properties().forEach(e -> out.put(e.getKey(), e.getValue().asText("")));- return out;- }-- private Map<String, Object> parseApiGatewayV2Cors(JsonNode corsNode, CloudFormationTemplateEngine engine) {- if (corsNode == null || corsNode.isNull()) {- return null;- }- JsonNode resolved = engine.resolveNode(corsNode);- if (!resolved.isObject()) {- return null;- }- Map<String, Object> out = new HashMap<>();- resolved.properties().forEach(e -> {- String key = e.getKey();- String camel = key.isEmpty() || !Character.isUpperCase(key.charAt(0))- ? key- : Character.toLowerCase(key.charAt(0)) + key.substring(1);- JsonNode v = e.getValue();- if (v.isArray()) {- List<String> list = new ArrayList<>();- v.forEach(item -> list.add(item.asText()));- out.put(camel, list);- } else if (v.isBoolean()) {- out.put(camel, v.booleanValue());- } else if (v.isNumber()) {- out.put(camel, v.numberValue());- } else if (!v.isNull()) {- out.put(camel, v.asText());- }- });- return out;- }-- /**- * Resolves {@code IdentitySource} accepting either the documented array form or a single- * scalar string — {@code ApiGatewayV2Service.createAuthorizer}/{@code updateAuthorizer}- * already accept both ({@code identitySourceRaw instanceof String}), so the CFN provisioner- * should not be stricter than the service it calls.- */- private List<String> resolveIdentitySource(JsonNode props, String source, CloudFormationTemplateEngine engine) {- if (props == null || !props.has(source) || props.get(source).isNull()) {- return List.of();- }- JsonNode resolved = engine.resolveNode(props.get(source));- if (resolved == null) {- return List.of();- }- if (resolved.isTextual()) {- return List.of(resolved.asText());- }- if (!resolved.isArray()) {- return List.of();- }- List<String> values = new ArrayList<>();- resolved.forEach(v -> values.add(v.asText()));- return values;- }-- private void provisionApiGatewayV2Authorizer(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,- String region) {- String apiId = resolveOptional(props, "ApiId", engine);- Map<String, Object> req = new HashMap<>();- req.put("name", resolveOptional(props, "Name", engine));- req.put("authorizerType", resolveOptional(props, "AuthorizerType", engine));- req.put("identitySource", resolveIdentitySource(props, "IdentitySource", engine));- req.put("authorizerUri", resolveOptional(props, "AuthorizerUri", engine));- req.put("authorizerPayloadFormatVersion", resolveOptional(props, "AuthorizerPayloadFormatVersion", engine));-- String ttl = resolveOptional(props, "AuthorizerResultTtlInSeconds", engine);- if (ttl != null) {- req.put("authorizerResultTtlInSeconds", Integer.parseInt(ttl));- }- String simpleResponses = resolveOptional(props, "EnableSimpleResponses", engine);- if (simpleResponses != null) {- req.put("enableSimpleResponses", simpleResponses);- }-- JsonNode jwtConfigNode = props != null ? props.get("JwtConfiguration") : null;- if (jwtConfigNode != null && !jwtConfigNode.isNull()) {- Map<String, Object> jwtConfig = new HashMap<>();- jwtConfig.put("audience", resolveStringListOrEmpty(jwtConfigNode, "Audience", engine));- jwtConfig.put("issuer", resolveOptional(jwtConfigNode, "Issuer", engine));- req.put("jwtConfiguration", jwtConfig);- }-- Authorizer authorizer;- if (r.getPhysicalId() == null) {- authorizer = apiGatewayV2Service.createAuthorizer(region, apiId, req);- } else {- authorizer = apiGatewayV2Service.updateAuthorizer(region, apiId, r.getPhysicalId(), req);- }- r.setPhysicalId(authorizer.getAuthorizerId());- r.getAttributes().put("AuthorizerId", authorizer.getAuthorizerId());- // ApiId is needed by delete(StackResource, region) to scope deleteAuthorizer — the- // type/physicalId-only delete overload has no apiId to call it with.- r.getAttributes().put("ApiId", apiId);- }-- private void provisionApiGatewayV2Route(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,- String region) {- String apiId = resolveOptional(props, "ApiId", engine);- Map<String, Object> req = new HashMap<>();- req.put("routeKey", resolveOptional(props, "RouteKey", engine));- req.put("authorizationType", resolveOrDefault(props, "AuthorizationType", engine, "NONE"));- req.put("authorizerId", resolveOptional(props, "AuthorizerId", engine));- // 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.- req.put("authorizationScopes", resolveStringListOrEmpty(props, "AuthorizationScopes", engine));- req.put("target", resolveOptional(props, "Target", engine));-- Route route;- if (r.getPhysicalId() == null) {- route = apiGatewayV2Service.createRoute(region, apiId, req);- } else {- route = apiGatewayV2Service.updateRoute(region, apiId, r.getPhysicalId(), req);- }- r.setPhysicalId(route.getRouteId());- }-- private void provisionApiGatewayV2Integration(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,- String region) {- String apiId = resolveOptional(props, "ApiId", engine);- Map<String, Object> req = new HashMap<>();- req.put("integrationType", resolveOptional(props, "IntegrationType", engine));- req.put("integrationUri", resolveOptional(props, "IntegrationUri", engine));- req.put("payloadFormatVersion", resolveOrDefault(props, "PayloadFormatVersion", engine, "2.0"));-- Integration integration;- if (r.getPhysicalId() == null) {- integration = apiGatewayV2Service.createIntegration(region, apiId, req);- } else {- integration = apiGatewayV2Service.updateIntegration(region, apiId, r.getPhysicalId(), req);- }- r.setPhysicalId(integration.getIntegrationId());- }-- private void provisionApiGatewayV2Stage(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,- String region) {- String apiId = resolveOptional(props, "ApiId", engine);- String stageName = resolveOptional(props, "StageName", engine);-- Map<String, Object> req = new HashMap<>();- req.put("stageName", stageName);- req.put("autoDeploy", resolveOrDefault(props, "AutoDeploy", engine, "false"));- putResolvedMapIfPresent(req, props, "StageVariables", "stageVariables", engine);-- if (r.getPhysicalId() == null) {- apiGatewayV2Service.createStage(region, apiId, req);- r.setPhysicalId(stageName);- } else {- apiGatewayV2Service.updateStage(region, apiId, r.getPhysicalId(), req);- }- }-- private void provisionApiGatewayV2Deployment(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,- String region) {- // Deployments are immutable point-in-time snapshots; on redeploy keep the existing one- // rather than minting a duplicate (idempotent re-deploy).- if (r.getPhysicalId() != null) {- return;- }- String apiId = resolveOptional(props, "ApiId", engine);- Map<String, Object> req = new HashMap<>();- req.put("description", resolveOptional(props, "Description", engine));-- Deployment deployment = apiGatewayV2Service.createDeployment(region, apiId, req);- r.setPhysicalId(deployment.getDeploymentId());+ // Extracted provisioners that track their own generated sub-resources (ApiGatewayV2's Api+ // body routes/integrations/authorizers) carry that tracking forward themselves.+ resourceRegistry.forType(previous.getResourceType())+ .ifPresent(owner -> owner.mergeFailedUpdateResourceTracking(previous, attempted));}// ── Lambda LayerVersion ──────────────────────────────────────────────────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.javanew file mode 100644index 0000000000..5b52d75ddd--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ApiGatewayV2CfnProvisioner.java@@ -0,0 +1,900 @@+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.AwsArnUtils;+import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;+import io.github.hectorvent.floci.services.apigatewayv2.model.Api;+import io.github.hectorvent.floci.services.apigatewayv2.model.Authorizer;+import io.github.hectorvent.floci.services.apigatewayv2.model.Deployment;+import io.github.hectorvent.floci.services.apigatewayv2.model.Integration;+import io.github.hectorvent.floci.services.apigatewayv2.model.Route;+import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;+import io.github.hectorvent.floci.services.cloudformation.model.StackResource;+import io.github.hectorvent.floci.services.s3.S3Service;+import jakarta.enterprise.context.ApplicationScoped;+import jakarta.inject.Inject;+import org.jboss.logging.Logger;++import java.util.ArrayList;+import java.util.Arrays;+import java.util.HashMap;+import java.util.Iterator;+import java.util.LinkedHashMap;+import java.util.LinkedHashSet;+import java.util.List;+import java.util.Locale;+import java.util.Map;+import java.util.Set;++/**+ * CloudFormation provisioning for the six {@code AWS::ApiGatewayV2::*} types (HTTP and WebSocket+ * APIs). They are one coupled family over {@link ApiGatewayV2Service}: an authorizer, route,+ * integration, stage and deployment all belong to an api, and the api's OpenAPI {@code Body}+ * materializes routes, integrations and authorizers of its own. {@code Ref} returns the api id for+ * an Api and the id component for each child type. Deleting the Api cascades to its children, so the+ * child types own no separate delete, except the Authorizer whose delete needs the api id (kept as a+ * create-time attribute).+ */+@ApplicationScoped+public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner {++ private static final Logger LOG = Logger.getLogger(ApiGatewayV2CfnProvisioner.class);++ private static final String API = "AWS::ApiGatewayV2::Api";+ private static final String AUTHORIZER = "AWS::ApiGatewayV2::Authorizer";+ private static final String ROUTE = "AWS::ApiGatewayV2::Route";+ private static final String INTEGRATION = "AWS::ApiGatewayV2::Integration";+ private static final String STAGE = "AWS::ApiGatewayV2::Stage";+ private static final String DEPLOYMENT = "AWS::ApiGatewayV2::Deployment";++ private static final String BODY_ROUTE_IDS_ATTR = "__FlociApiGatewayV2BodyRouteIds";+ private static final String BODY_INTEGRATION_IDS_ATTR = "__FlociApiGatewayV2BodyIntegrationIds";+ private static final String BODY_AUTHORIZER_IDS_ATTR = "__FlociApiGatewayV2BodyAuthorizerIds";++ private final ApiGatewayV2Service apiGatewayV2Service;+ private final S3Service s3Service;+ private final ObjectMapper objectMapper;++ @Inject+ public ApiGatewayV2CfnProvisioner(ApiGatewayV2Service apiGatewayV2Service, S3Service s3Service,+ ObjectMapper objectMapper) {+ this.apiGatewayV2Service = apiGatewayV2Service;+ this.s3Service = s3Service;+ this.objectMapper = objectMapper;+ }++ @Override+ public Set<String> resourceTypes() {+ return Set.of(API, AUTHORIZER, ROUTE, INTEGRATION, STAGE, DEPLOYMENT);+ }++ @Override+ public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {+ switch (r.getResourceType()) {+ case API -> provisionApi(r, props, ctx);+ case AUTHORIZER -> provisionAuthorizer(r, props, ctx);+ case ROUTE -> provisionRoute(r, props, ctx);+ case INTEGRATION -> provisionIntegration(r, props, ctx);+ case STAGE -> provisionStage(r, props, ctx);+ case DEPLOYMENT -> provisionDeployment(r, props, ctx);+ default -> throw new IllegalStateException("Unhandled type: " + r.getResourceType());+ }+ }++ @Override+ public void delete(StackResource resource, String region) {+ switch (resource.getResourceType()) {+ case API -> apiGatewayV2Service.deleteApi(region, resource.getPhysicalId());+ case AUTHORIZER -> {+ // Deleting an authorizer needs the api id (a create-time attribute) as well as the+ // authorizer id (the physical id), which the id-only delete path cannot supply.+ String apiId = resource.getAttributes().get("ApiId");+ if (apiId != null && !apiId.isBlank()) {+ try {+ apiGatewayV2Service.deleteAuthorizer(region, apiId, resource.getPhysicalId());+ } catch (RuntimeException e) {+ LOG.debugv("Error deleting authorizer {0}: {1}",+ resource.getPhysicalId(), e.getMessage());+ }+ }+ }+ default -> {+ // Route, Integration, Stage and Deployment are removed when their Api is deleted.+ }+ }+ }++ @Override+ public void mergeFailedUpdateResourceTracking(StackResource previous, StackResource attempted) {+ // Only the Api carries body-generated sub-resource ids; keep any the failed attempt created+ // so the restored resource still owns them for the next cleanup or stack delete.+ if (!API.equals(previous.getResourceType())) {+ return;+ }+ retainBodyResourceIds(previous, BODY_ROUTE_IDS_ATTR, bodyResourceIds(attempted, BODY_ROUTE_IDS_ATTR));+ retainBodyResourceIds(previous, BODY_INTEGRATION_IDS_ATTR,+ bodyResourceIds(attempted, BODY_INTEGRATION_IDS_ATTR));+ retainBodyResourceIds(previous, BODY_AUTHORIZER_IDS_ATTR,+ bodyResourceIds(attempted, BODY_AUTHORIZER_IDS_ATTR));+ }++ // ── Api ──────────────────────────────────────────────────────────────────++ private void provisionApi(StackResource r, JsonNode props, ProvisionContext ctx) {+ CloudFormationTemplateEngine engine = ctx.engine();+ String region = ctx.region();+ String name = ctx.resolveOptional(props, "Name");+ if (name == null || name.isBlank()) {+ name = ctx.generatePhysicalName(r.getLogicalId(), 255, false);+ }+ Map<String, Object> req = new HashMap<>();+ req.put("name", name);+ req.put("protocolType", resolveOrDefault(ctx, props, "ProtocolType", "HTTP"));+ req.put("routeSelectionExpression", ctx.resolveOptional(props, "RouteSelectionExpression"));+ req.put("description", ctx.resolveOptional(props, "Description"));+ req.put("apiKeySelectionExpression", ctx.resolveOptional(props, "ApiKeySelectionExpression"));++ Map<String, String> tags = parseTags(props != null ? props.get("Tags") : null, engine);+ if (!tags.isEmpty()) {+ req.put("tags", tags);+ }+ Map<String, Object> cors = parseCors(props != null ? props.get("CorsConfiguration") : null, engine);+ if (cors != null) {+ req.put("corsConfiguration", cors);+ }++ Api api;+ if (r.getPhysicalId() == null) {+ api = apiGatewayV2Service.createApi(region, req);+ } else {+ api = apiGatewayV2Service.updateApi(region, r.getPhysicalId(), req);+ }+ r.setPhysicalId(api.getApiId());+ r.getAttributes().put("ApiId", api.getApiId());+ r.getAttributes().put("ApiEndpoint", api.getApiEndpoint());+ // The ARN prefix a Lambda permission's SourceArn is built from; AWS returns+ // arn:<partition>:execute-api:<region>:<account>:<apiId> for this attribute.+ r.getAttributes().put("ExecuteApiArn",+ AwsArnUtils.Arn.of("execute-api", region, ctx.accountId(), api.getApiId()).toString());+ reconcileBodyRoutes(r, region, api.getApiId(), props, engine);+ }++ /**+ * Reconciles the routes, integrations and authorizers materialized from an ApiGatewayV2 OpenAPI+ * body. Only ids stored on this resource are removed, so separately declared V2 resources stay+ * outside this generated-resource lifecycle.+ */+ private void reconcileBodyRoutes(StackResource r, String region, String apiId, JsonNode props,+ CloudFormationTemplateEngine engine) {+ JsonNode body = OpenApiDocuments.resolve(props, engine, s3Service, objectMapper);+ BodyResourceState previous = null;+ try {+ previous = snapshotBodyResources(r, region, apiId);+ // API Gateway requires route keys to be unique. Remove only the tracked body-generated+ // resources before creating their replacements; rollback restores this snapshot.+ deleteBodyResources(r, region, apiId);+ } catch (RuntimeException e) {+ rollbackBodyReplacement(r, region, apiId, new BodyResources(List.of(), List.of(), List.of()),+ previous, e);+ throw e;+ }++ if (body == null) {+ return;+ }++ BodyResources replacement;+ try {+ replacement = materializeBodyRoutes(region, apiId, body);+ } catch (BodyMaterializationException e) {+ rollbackBodyReplacement(r, region, apiId, e.resources(), previous, e);+ throw e;+ } catch (RuntimeException e) {+ // materializeBodyRoutes already removed its partial replacement.+ rollbackBodyReplacement(r, region, apiId, new BodyResources(List.of(), List.of(), List.of()),+ previous, e);+ throw e;+ }+ storeBodyResourceIds(r, BODY_ROUTE_IDS_ATTR, replacement.routeIds());+ storeBodyResourceIds(r, BODY_INTEGRATION_IDS_ATTR, replacement.integrationIds());+ storeBodyResourceIds(r, BODY_AUTHORIZER_IDS_ATTR, replacement.authorizerIds());+ }++ /**+ * CloudFormation's ApiGatewayV2 {@code Body} is an OpenAPI document. Materialize each declared+ * HTTP operation as the route API Gateway V2 serves, including its OpenAPI security requirement+ * and a route target when it declares an integration extension.+ */+ private BodyResources materializeBodyRoutes(String region, String apiId, JsonNode body) {+ List<String> routeIds = new ArrayList<>();+ List<String> integrationIds = new ArrayList<>();+ List<String> authorizerIds = new ArrayList<>();+ try {+ Map<String, OpenApiAuthorizerBinding> authorizers = materializeBodyAuthorizers(+ region, apiId, body, authorizerIds);+ JsonNode paths = body.path("paths");+ if (!paths.isObject()) {+ return new BodyResources(routeIds, integrationIds, authorizerIds);+ }++ Iterator<Map.Entry<String, JsonNode>> pathEntries = paths.fields();+ while (pathEntries.hasNext()) {+ Map.Entry<String, JsonNode> pathEntry = pathEntries.next();+ if (!pathEntry.getValue().isObject()) {+ continue;+ }+ Iterator<Map.Entry<String, JsonNode>> operations = pathEntry.getValue().fields();+ while (operations.hasNext()) {+ Map.Entry<String, JsonNode> operation = operations.next();+ String method = operation.getKey();+ if (!isHttpApiOperation(method) || !operation.getValue().isObject()) {+ continue;+ }++ Map<String, Object> routeRequest = new HashMap<>();+ routeRequest.put("routeKey", openApiRouteKey(method, pathEntry.getKey()));+ applyOpenApiRouteSecurity(body, operation.getValue(), pathEntry.getKey(), method,+ authorizers, routeRequest);+ JsonNode integration = operation.getValue().path("x-amazon-apigateway-integration");+ if (integration.isObject()) {+ String integrationType = textOrNull(integration, "type");+ if (integrationType != null && !integrationType.isBlank()) {+ Map<String, Object> integrationRequest = new HashMap<>();+ integrationRequest.put("integrationType", integrationType.toUpperCase(Locale.ROOT));+ putOpenApiIntegrationValue(integrationRequest, "integrationUri", integration, "uri");+ putOpenApiIntegrationValue(integrationRequest, "integrationMethod", integration,+ "httpMethod");+ putOpenApiIntegrationValue(integrationRequest, "payloadFormatVersion", integration,+ "payloadFormatVersion");+ Integration createdIntegration = apiGatewayV2Service.createIntegration(region, apiId,+ integrationRequest);+ integrationIds.add(createdIntegration.getIntegrationId());+ routeRequest.put("target", "integrations/" + createdIntegration.getIntegrationId());+ }+ }+ Route createdRoute = apiGatewayV2Service.createRoute(region, apiId, routeRequest);+ routeIds.add(createdRoute.getRouteId());+ }+ }+ return new BodyResources(routeIds, integrationIds, authorizerIds);+ } catch (RuntimeException e) {+ BodyResources partial = new BodyResources(routeIds, integrationIds, authorizerIds);+ List<RuntimeException> cleanupFailures = cleanupBodyResources(region, apiId, partial);+ if (!cleanupFailures.isEmpty()) {+ cleanupFailures.forEach(e::addSuppressed);+ throw new BodyMaterializationException(e, partial);+ }+ throw e;+ }+ }++ private Map<String, OpenApiAuthorizerBinding> materializeBodyAuthorizers(String region, String apiId,+ JsonNode body, List<String> authorizerIds) {+ Map<String, OpenApiAuthorizerBinding> bindings = new LinkedHashMap<>();+ JsonNode schemes = body.path("components").path("securitySchemes");+ if (!schemes.isObject()) {+ return bindings;+ }++ Iterator<Map.Entry<String, JsonNode>> entries = schemes.fields();+ while (entries.hasNext()) {+ Map.Entry<String, JsonNode> entry = entries.next();+ String schemeName = entry.getKey();+ JsonNode scheme = entry.getValue();+ if (!scheme.isObject()) {+ continue;+ }+ JsonNode definition = scheme.path("x-amazon-apigateway-authorizer");+ if (!definition.isObject()) {+ continue;+ }++ String type = textOrNull(definition, "type");+ String authorizerType;+ String routeAuthorizationType;+ if ("jwt".equalsIgnoreCase(type)) {+ authorizerType = "JWT";+ routeAuthorizationType = "JWT";+ } else if ("request".equalsIgnoreCase(type)) {+ authorizerType = "REQUEST";+ routeAuthorizationType = "CUSTOM";+ } else {+ throw invalidOpenApiSecurity("Authorizer " + schemeName + " must declare type jwt or request");+ }++ Map<String, Object> request = new HashMap<>();+ request.put("name", schemeName);+ request.put("authorizerType", authorizerType);+ putOpenApiAuthorizerIdentitySource(request, definition);+ putOpenApiAuthorizerValue(request, "authorizerUri", definition, "authorizerUri");+ putOpenApiAuthorizerValue(request, "authorizerPayloadFormatVersion", definition,+ "authorizerPayloadFormatVersion");+ putOpenApiAuthorizerValue(request, "authorizerResultTtlInSeconds", definition,+ "authorizerResultTtlInSeconds");+ putOpenApiAuthorizerValue(request, "enableSimpleResponses", definition, "enableSimpleResponses");++ if ("JWT".equals(authorizerType)) {+ JsonNode jwt = definition.path("jwtConfiguration");+ if (!jwt.isObject()) {+ throw invalidOpenApiSecurity("JWT authorizer " + schemeName+ + " must declare jwtConfiguration");+ }+ Map<String, Object> jwtConfiguration = new HashMap<>();+ jwtConfiguration.put("issuer", textOrNull(jwt, "issuer"));+ jwtConfiguration.put("audience", openApiStringList(jwt.get("audience"),+ "jwtConfiguration.audience for authorizer " + schemeName));+ request.put("jwtConfiguration", jwtConfiguration);+ }++ Authorizer created = apiGatewayV2Service.createAuthorizer(region, apiId, request);+ authorizerIds.add(created.getAuthorizerId());+ bindings.put(schemeName, new OpenApiAuthorizerBinding(routeAuthorizationType, created.getAuthorizerId()));+ }+ return bindings;+ }++ private void applyOpenApiRouteSecurity(JsonNode body, JsonNode operation, String path, String method,+ Map<String, OpenApiAuthorizerBinding> authorizers,+ Map<String, Object> routeRequest) {+ JsonNode security = operation.has("security") ? operation.get("security") : body.get("security");+ if (security == null || security.isNull() || security.isMissingNode()) {+ return;+ }+ if (!security.isArray()) {+ throw invalidOpenApiSecurity("security must be an array");+ }+ if (security.isEmpty()) {+ routeRequest.put("authorizationType", "NONE");+ return; // An operation-level empty array explicitly overrides inherited security.+ }++ // Each object is one alternative in the outer OR-list, but names inside one object are an+ // AND requirement. A V2 route can attach only one authorizer, so accepting a multi-name+ // object would silently weaken its authentication contract; AWS classifies multiple+ // security requirements as an HTTP API import error. Validate every alternative before+ // selecting a representable one.+ for (JsonNode requirement : security) {+ if (!requirement.isObject()) {+ throw invalidOpenApiSecurity("security requirements must be objects");+ }+ if (requirement.isEmpty()) {+ routeRequest.put("authorizationType", "NONE");+ return; // An empty requirement allows anonymous access by OpenAPI definition.+ }+ if (requirement.size() > 1) {+ throw invalidOpenApiSecurity(+ "HTTP API routes do not support AND security requirements with multiple schemes");+ }+ }+ if (security.size() > 1) {+ throw invalidOpenApiSecurity(+ "HTTP API routes do not support OR security requirements with multiple alternatives");+ }++ String unsupportedScheme = null;+ for (JsonNode requirement : security) {+ Iterator<Map.Entry<String, JsonNode>> schemes = requirement.fields();+ while (schemes.hasNext()) {+ Map.Entry<String, JsonNode> scheme = schemes.next();+ OpenApiAuthorizerBinding binding = authorizers.get(scheme.getKey());+ if (binding == null) {+ unsupportedScheme = scheme.getKey();+ continue;+ }+ routeRequest.put("authorizationType", binding.authorizationType());+ if (binding.authorizerId() != null) {+ routeRequest.put("authorizerId", binding.authorizerId());+ }+ if ("JWT".equals(binding.authorizationType())) {+ List<String> scopes = openApiStringList(scheme.getValue(),+ "security scopes for scheme " + scheme.getKey());+ if (!scopes.isEmpty()) {+ routeRequest.put("authorizationScopes", scopes);+ }+ }+ return;+ }+ }+ throw invalidOpenApiSecurity("Protected operation " + openApiRouteKey(method, path)+ + " references unsupported security scheme '" + unsupportedScheme + "'");+ }++ private static void putOpenApiAuthorizerIdentitySource(Map<String, Object> request, JsonNode definition) {+ JsonNode identitySource = definition.get("identitySource");+ if (identitySource == null || identitySource.isNull()) {+ return;+ }+ if (identitySource.isTextual()) {+ request.put("identitySource", identitySource.asText());+ return;+ }+ request.put("identitySource", openApiStringList(identitySource, "authorizer identitySource"));+ }++ private static void putOpenApiAuthorizerValue(Map<String, Object> request, String requestKey,+ JsonNode definition, String definitionKey) {+ JsonNode value = definition.get(definitionKey);+ if (value == null || value.isNull()) {+ return;+ }+ if (value.isTextual()) {+ request.put(requestKey, value.asText());+ } else if (value.isBoolean()) {+ request.put(requestKey, value.booleanValue());+ } else if (value.isIntegralNumber()) {+ request.put(requestKey, value.intValue());+ } else {+ throw invalidOpenApiSecurity(definitionKey + " has an invalid value");+ }+ }++ private static List<String> openApiStringList(JsonNode value, String fieldName) {+ if (value == null || value.isNull()) {+ return List.of();+ }+ if (!value.isArray()) {+ throw invalidOpenApiSecurity(fieldName + " must be an array of strings");+ }+ List<String> values = new ArrayList<>();+ for (JsonNode element : value) {+ if (!element.isTextual()) {+ throw invalidOpenApiSecurity(fieldName + " must be an array of strings");+ }+ values.add(element.asText());+ }+ return values;+ }++ private static AwsException invalidOpenApiSecurity(String message) {+ return new AwsException("ValidationException", message, 400);+ }++ private void deleteBodyResources(StackResource r, String region, String apiId) {+ deleteBodyResources(region, apiId, new BodyResources(+ bodyResourceIds(r, BODY_ROUTE_IDS_ATTR),+ bodyResourceIds(r, BODY_INTEGRATION_IDS_ATTR),+ bodyResourceIds(r, BODY_AUTHORIZER_IDS_ATTR)));+ r.getAttributes().remove(BODY_ROUTE_IDS_ATTR);+ r.getAttributes().remove(BODY_INTEGRATION_IDS_ATTR);+ r.getAttributes().remove(BODY_AUTHORIZER_IDS_ATTR);+ }++ private void deleteBodyResources(String region, String apiId, BodyResources resources) {+ for (String routeId : resources.routeIds()) {+ deleteRouteIfPresent(region, apiId, routeId);+ }+ for (String integrationId : resources.integrationIds()) {+ deleteIntegrationIfPresent(region, apiId, integrationId);+ }+ for (String authorizerId : resources.authorizerIds()) {+ deleteAuthorizerIfPresent(region, apiId, authorizerId);+ }+ }++ private BodyResourceState snapshotBodyResources(StackResource r, String region, String apiId) {+ List<Route> routes = new ArrayList<>();+ for (String routeId : bodyResourceIds(r, BODY_ROUTE_IDS_ATTR)) {+ try {+ routes.add(apiGatewayV2Service.getRoute(region, apiId, routeId));+ } catch (AwsException e) {+ if (e.getHttpStatus() != 404) {+ throw e;+ }+ }+ }+ List<Integration> integrations = new ArrayList<>();+ for (String integrationId : bodyResourceIds(r, BODY_INTEGRATION_IDS_ATTR)) {+ try {+ integrations.add(apiGatewayV2Service.getIntegration(region, apiId, integrationId));+ } catch (AwsException e) {+ if (e.getHttpStatus() != 404) {+ throw e;+ }+ }+ }+ List<Authorizer> authorizers = new ArrayList<>();+ for (String authorizerId : bodyResourceIds(r, BODY_AUTHORIZER_IDS_ATTR)) {+ try {+ authorizers.add(apiGatewayV2Service.getAuthorizer(region, apiId, authorizerId));+ } catch (AwsException e) {+ if (e.getHttpStatus() != 404) {+ throw e;+ }+ }+ }+ return new BodyResourceState(routes, integrations, authorizers);+ }++ private void rollbackBodyReplacement(StackResource r, String region, String apiId,+ BodyResources replacement, BodyResourceState previous,+ RuntimeException failure) {+ List<RuntimeException> cleanupFailures = cleanupBodyResources(region, apiId, replacement);++ if (previous != null) {+ storeBodyResourceIds(r, BODY_ROUTE_IDS_ATTR,+ previous.routes().stream().map(Route::getRouteId).toList());+ storeBodyResourceIds(r, BODY_INTEGRATION_IDS_ATTR,+ previous.integrations().stream().map(Integration::getIntegrationId).toList());+ storeBodyResourceIds(r, BODY_AUTHORIZER_IDS_ATTR,+ previous.authorizers().stream().map(Authorizer::getAuthorizerId).toList());+ }+ if (!cleanupFailures.isEmpty()) {+ cleanupFailures.forEach(failure::addSuppressed);+ retainBodyResourceIds(r, replacement);+ }+ if (previous == null) {+ return;+ }+ try {+ // Routes refer to integrations and authorizers, so restore both before their routes.+ for (Authorizer authorizer : previous.authorizers()) {+ apiGatewayV2Service.restoreAuthorizer(region, apiId, authorizer);+ }+ for (Integration integration : previous.integrations()) {+ apiGatewayV2Service.restoreIntegration(region, apiId, integration);+ }+ for (Route route : previous.routes()) {+ apiGatewayV2Service.restoreRoute(region, apiId, route, replacement.routeIds());+ }+ } catch (RuntimeException restoreFailure) {+ failure.addSuppressed(restoreFailure);+ String reason = restoreFailure.getMessage() != null+ ? restoreFailure.getMessage()+ : restoreFailure.getClass().getSimpleName();+ r.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR, reason);+ }+ }++ private List<RuntimeException> cleanupBodyResources(String region, String apiId, BodyResources resources) {+ List<RuntimeException> failures = new ArrayList<>();+ for (String routeId : resources.routeIds()) {+ try {+ deleteRouteIfPresent(region, apiId, routeId);+ } catch (RuntimeException e) {+ failures.add(e);+ }+ }+ for (String integrationId : resources.integrationIds()) {+ try {+ deleteIntegrationIfPresent(region, apiId, integrationId);+ } catch (RuntimeException e) {+ failures.add(e);+ }+ }+ for (String authorizerId : resources.authorizerIds()) {+ try {+ deleteAuthorizerIfPresent(region, apiId, authorizerId);+ } catch (RuntimeException e) {+ failures.add(e);+ }+ }+ return failures;+ }++ private void retainBodyResourceIds(StackResource r, BodyResources resources) {+ retainBodyResourceIds(r, BODY_ROUTE_IDS_ATTR, resources.routeIds());+ retainBodyResourceIds(r, BODY_INTEGRATION_IDS_ATTR, resources.integrationIds());+ retainBodyResourceIds(r, BODY_AUTHORIZER_IDS_ATTR, resources.authorizerIds());+ }++ private static void retainBodyResourceIds(StackResource r, String attributeName, List<String> resourceIds) {+ LinkedHashSet<String> retained = new LinkedHashSet<>(bodyResourceIds(r, attributeName));+ retained.addAll(resourceIds);+ storeBodyResourceIds(r, attributeName, new ArrayList<>(retained));+ }++ private static List<String> bodyResourceIds(StackResource r, String attributeName) {+ String ids = r.getAttributes().get(attributeName);+ return ids == null || ids.isBlank() ? List.of() : Arrays.asList(ids.split(","));+ }++ private static void storeBodyResourceIds(StackResource r, String attributeName, List<String> resourceIds) {+ if (resourceIds.isEmpty()) {+ r.getAttributes().remove(attributeName);+ } else {+ r.getAttributes().put(attributeName, String.join(",", resourceIds));+ }+ }++ private void deleteRouteIfPresent(String region, String apiId, String routeId) {+ try {+ apiGatewayV2Service.deleteRoute(region, apiId, routeId);+ } catch (AwsException e) {+ if (e.getHttpStatus() != 404) {+ throw e;+ }+ }+ }++ private void deleteIntegrationIfPresent(String region, String apiId, String integrationId) {+ try {+ apiGatewayV2Service.deleteIntegration(region, apiId, integrationId);+ } catch (AwsException e) {+ if (e.getHttpStatus() != 404) {+ throw e;+ }+ }+ }++ private void deleteAuthorizerIfPresent(String region, String apiId, String authorizerId) {+ try {+ apiGatewayV2Service.deleteAuthorizer(region, apiId, authorizerId);+ } catch (AwsException e) {+ if (e.getHttpStatus() != 404) {+ throw e;+ }+ }+ }++ private record BodyResources(List<String> routeIds, List<String> integrationIds,+ List<String> authorizerIds) {}++ private record BodyResourceState(List<Route> routes, List<Integration> integrations,+ List<Authorizer> authorizers) {}++ private record OpenApiAuthorizerBinding(String authorizationType, String authorizerId) {}++ private static final class BodyMaterializationException extends RuntimeException {+ private final transient BodyResources resources;++ private BodyMaterializationException(RuntimeException cause, BodyResources resources) {+ super(cause.getMessage(), cause);+ this.resources = resources;+ }++ private BodyResources resources() {+ return resources;+ }+ }++ private static boolean isHttpApiOperation(String method) {+ return switch (method.toLowerCase(Locale.ROOT)) {+ case "get", "put", "post", "delete", "options", "head", "patch", "trace",+ "x-amazon-apigateway-any-method" -> true;+ default -> false;+ };+ }++ private static String openApiRouteKey(String method, String path) {+ String routeMethod = "x-amazon-apigateway-any-method".equals(method) ? "ANY"+ : method.toUpperCase(Locale.ROOT);+ return routeMethod + " " + path;+ }++ private static void putOpenApiIntegrationValue(Map<String, Object> request, String requestKey,+ JsonNode integration, String openApiKey) {+ String value = textOrNull(integration, openApiKey);+ if (value != null) {+ request.put(requestKey, value);+ }+ }++ private Map<String, String> parseTags(JsonNode tagsNode, CloudFormationTemplateEngine engine) {+ Map<String, String> out = new HashMap<>();+ if (tagsNode == null || tagsNode.isNull()) {+ return out;+ }+ JsonNode resolved = engine.resolveNode(tagsNode);+ if (!resolved.isObject()) {+ return out;+ }+ resolved.properties().forEach(e -> out.put(e.getKey(), e.getValue().asText("")));+ return out;+ }++ private Map<String, Object> parseCors(JsonNode corsNode, CloudFormationTemplateEngine engine) {+ if (corsNode == null || corsNode.isNull()) {+ return null;+ }+ JsonNode resolved = engine.resolveNode(corsNode);+ if (!resolved.isObject()) {+ return null;+ }+ Map<String, Object> out = new HashMap<>();+ resolved.properties().forEach(e -> {+ String key = e.getKey();+ String camel = key.isEmpty() || !Character.isUpperCase(key.charAt(0))+ ? key+ : Character.toLowerCase(key.charAt(0)) + key.substring(1);+ JsonNode v = e.getValue();+ if (v.isArray()) {+ List<String> list = new ArrayList<>();+ v.forEach(item -> list.add(item.asText()));+ out.put(camel, list);+ } else if (v.isBoolean()) {+ out.put(camel, v.booleanValue());+ } else if (v.isNumber()) {+ out.put(camel, v.numberValue());+ } else if (!v.isNull()) {+ out.put(camel, v.asText());+ }+ });+ return out;+ }++ // ── Authorizer ─────────────────────────────────────────────────────────────++ private List<String> resolveIdentitySource(JsonNode props, String source, CloudFormationTemplateEngine engine) {+ if (props == null || !props.has(source) || props.get(source).isNull()) {+ return List.of();+ }+ JsonNode resolved = engine.resolveNode(props.get(source));+ if (resolved == null) {+ return List.of();+ }+ if (resolved.isTextual()) {+ return List.of(resolved.asText());+ }+ if (!resolved.isArray()) {+ return List.of();+ }+ List<String> values = new ArrayList<>();+ resolved.forEach(v -> values.add(v.asText()));+ return values;+ }++ private void provisionAuthorizer(StackResource r, JsonNode props, ProvisionContext ctx) {+ CloudFormationTemplateEngine engine = ctx.engine();+ String region = ctx.region();+ String apiId = ctx.resolveOptional(props, "ApiId");+ Map<String, Object> req = new HashMap<>();+ req.put("name", ctx.resolveOptional(props, "Name"));+ req.put("authorizerType", ctx.resolveOptional(props, "AuthorizerType"));+ req.put("identitySource", resolveIdentitySource(props, "IdentitySource", engine));+ req.put("authorizerUri", ctx.resolveOptional(props, "AuthorizerUri"));+ req.put("authorizerPayloadFormatVersion", ctx.resolveOptional(props, "AuthorizerPayloadFormatVersion"));++ String ttl = ctx.resolveOptional(props, "AuthorizerResultTtlInSeconds");+ if (ttl != null) {+ req.put("authorizerResultTtlInSeconds", Integer.parseInt(ttl));+ }+ String simpleResponses = ctx.resolveOptional(props, "EnableSimpleResponses");+ if (simpleResponses != null) {+ req.put("enableSimpleResponses", simpleResponses);+ }++ JsonNode jwtConfigNode = props != null ? props.get("JwtConfiguration") : null;+ if (jwtConfigNode != null && !jwtConfigNode.isNull()) {+ Map<String, Object> jwtConfig = new HashMap<>();+ jwtConfig.put("audience", resolveStringListOrEmpty(jwtConfigNode, "Audience", engine));+ jwtConfig.put("issuer", ctx.resolveOptional(jwtConfigNode, "Issuer"));+ req.put("jwtConfiguration", jwtConfig);+ }++ Authorizer authorizer;+ if (r.getPhysicalId() == null) {+ authorizer = apiGatewayV2Service.createAuthorizer(region, apiId, req);+ } else {+ authorizer = apiGatewayV2Service.updateAuthorizer(region, apiId, r.getPhysicalId(), req);+ }+ r.setPhysicalId(authorizer.getAuthorizerId());+ r.getAttributes().put("AuthorizerId", authorizer.getAuthorizerId());+ // ApiId is needed by delete() to scope deleteAuthorizer; the id-only delete path has none.+ r.getAttributes().put("ApiId", apiId);+ }++ // ── Route ────────────────────────────────────────────────────────────────++ private void provisionRoute(StackResource r, JsonNode props, ProvisionContext ctx) {+ String region = ctx.region();+ 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("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.+ req.put("authorizationScopes", resolveStringListOrEmpty(props, "AuthorizationScopes", ctx.engine()));+ req.put("target", ctx.resolveOptional(props, "Target"));++ Route route;+ if (r.getPhysicalId() == null) {+ route = apiGatewayV2Service.createRoute(region, apiId, req);+ } else {+ route = apiGatewayV2Service.updateRoute(region, apiId, r.getPhysicalId(), req);+ }+ r.setPhysicalId(route.getRouteId());+ r.getAttributes().put("RouteId", route.getRouteId());+ }++ // ── Integration ──────────────────────────────────────────────────────────++ private void provisionIntegration(StackResource r, JsonNode props, ProvisionContext ctx) {+ String region = ctx.region();+ String apiId = ctx.resolveOptional(props, "ApiId");+ 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"));++ Integration integration;+ if (r.getPhysicalId() == null) {+ integration = apiGatewayV2Service.createIntegration(region, apiId, req);+ } else {+ integration = apiGatewayV2Service.updateIntegration(region, apiId, r.getPhysicalId(), req);+ }+ r.setPhysicalId(integration.getIntegrationId());+ r.getAttributes().put("IntegrationId", integration.getIntegrationId());+ }++ // ── Stage ────────────────────────────────────────────────────────────────++ private void provisionStage(StackResource r, JsonNode props, ProvisionContext ctx) {+ CloudFormationTemplateEngine engine = ctx.engine();+ String region = ctx.region();+ String apiId = ctx.resolveOptional(props, "ApiId");+ String stageName = ctx.resolveOptional(props, "StageName");++ Map<String, Object> req = new HashMap<>();+ req.put("stageName", stageName);+ req.put("autoDeploy", resolveOrDefault(ctx, props, "AutoDeploy", "false"));+ Map<String, String> stageVariables = parseStageVariables(props, engine);+ if (stageVariables != null) {+ req.put("stageVariables", stageVariables);+ }++ if (r.getPhysicalId() == null) {+ apiGatewayV2Service.createStage(region, apiId, req);+ r.setPhysicalId(stageName);+ } else {+ apiGatewayV2Service.updateStage(region, apiId, r.getPhysicalId(), req);+ }+ }++ // ── Deployment ───────────────────────────────────────────────────────────++ private void provisionDeployment(StackResource r, JsonNode props, ProvisionContext ctx) {+ // Deployments are immutable point-in-time snapshots; on redeploy keep the existing one+ // rather than minting a duplicate (idempotent re-deploy).+ if (r.getPhysicalId() != null) {+ return;+ }+ String apiId = ctx.resolveOptional(props, "ApiId");+ Map<String, Object> req = new HashMap<>();+ req.put("description", ctx.resolveOptional(props, "Description"));++ Deployment deployment = apiGatewayV2Service.createDeployment(ctx.region(), apiId, req);+ r.setPhysicalId(deployment.getDeploymentId());+ r.getAttributes().put("DeploymentId", deployment.getDeploymentId());+ }++ // ── 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;+ }++ private List<String> resolveStringListOrEmpty(JsonNode props, String source,+ CloudFormationTemplateEngine engine) {+ if (props == null || !props.has(source) || props.get(source).isNull()) {+ return List.of();+ }+ JsonNode resolved = engine.resolveNode(props.get(source));+ if (resolved == null || !resolved.isArray()) {+ return List.of();+ }+ List<String> values = new ArrayList<>();+ resolved.forEach(v -> values.add(v.asText()));+ return values;+ }++ private Map<String, String> parseStageVariables(JsonNode props, CloudFormationTemplateEngine engine) {+ if (props == null || !props.has("StageVariables") || props.get("StageVariables").isNull()) {+ return null;+ }+ JsonNode resolved = engine.resolveNode(props.get("StageVariables"));+ if (resolved == null || !resolved.isObject()) {+ return null;+ }+ Map<String, String> out = new LinkedHashMap<>();+ resolved.fields().forEachRemaining(e -> out.put(e.getKey(), e.getValue().asText()));+ return out;+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceProvisioner.javaindex a1a739d009..5a438609f8 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceProvisioner.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnResourceProvisioner.java@@ -101,4 +101,14 @@ public interface CfnResourceProvisioner {default void clearUpdate(StackResource resource) {// no-op by default: a type with no replacement cleanup records nothing to clear}++ /**+ * Carries additive ownership tracking a failed update discovered onto the last known-good+ * resource metadata CloudFormation restores, so entities the failed attempt created but could+ * not remove are not orphaned. Only additive tracking belongs here; committed resource state+ * must not be overwritten. The default has none to carry.+ */+ default void mergeFailedUpdateResourceTracking(StackResource previous, StackResource attempted) {+ // no-op by default: only a provisioner that tracks generated sub-resources needs this+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayV2CfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayV2CfnProvisionerTest.javaindex e4f427a107..ea026eb69a 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayV2CfnProvisionerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayV2CfnProvisionerTest.java@@ -8,10 +8,13 @@ import io.github.hectorvent.floci.services.apigatewayv2.model.Api;import io.github.hectorvent.floci.services.apigatewayv2.model.Authorizer;import io.github.hectorvent.floci.services.apigatewayv2.model.Route;import io.github.hectorvent.floci.services.cloudformation.model.StackResource;+import io.github.hectorvent.floci.services.cloudformation.provisioners.ApiGatewayV2CfnProvisioner;import io.github.hectorvent.floci.services.cloudformation.provisioners.CloudFormationResourceRegistry;+import io.github.hectorvent.floci.services.s3.S3Service;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;+import java.util.List;import java.util.Map;import java.util.concurrent.atomic.AtomicInteger;import java.util.function.Function;@@ -42,7 +45,7 @@ class ApiGatewayV2CfnProvisionerTest {provisioner = CfnProvisionerFixture.builder().apiGatewayV2(apiGatewayV2Service).objectMapper(mapper)- .registry(new CloudFormationResourceRegistry(java.util.List.of()))+ .provisioners(new ApiGatewayV2CfnProvisioner(apiGatewayV2Service, mock(S3Service.class), mapper)).build();Api api = new Api();@@ -52,6 +55,17 @@ class ApiGatewayV2CfnProvisionerTest {when(apiGatewayV2Service.updateApi(eq(REGION), eq(API_ID), anyMap())).thenReturn(api);}+ @Test+ void publishesApiIdEndpointAndExecuteApiArn() throws Exception {+ StackResource api = provision(propertiesWithoutBody(), null, Map.of());++ assertEquals("CREATE_COMPLETE", api.getStatus());+ assertEquals(API_ID, api.getAttributes().get("ApiId"));+ assertEquals("https://" + API_ID + ".execute-api.localhost", api.getAttributes().get("ApiEndpoint"));+ assertEquals("arn:aws:execute-api:us-east-1:000000000000:api-123",+ api.getAttributes().get("ExecuteApiArn"));+ }+@Testvoid restoresExistingRoutesAndCleansPartialReplacementWhenRouteCreationFails() throws Exception {Route oldRoute = route("old-route", "GET /before");@@ -80,7 +94,7 @@ class ApiGatewayV2CfnProvisionerTest {assertEquals("old-route", replacement.getAttributes().get("__FlociApiGatewayV2BodyRouteIds"));verify(apiGatewayV2Service).deleteRoute(REGION, API_ID, "old-route");verify(apiGatewayV2Service).deleteRoute(REGION, API_ID, "partial-route");- verify(apiGatewayV2Service).restoreRoute(REGION, API_ID, oldRoute, java.util.List.of());+ verify(apiGatewayV2Service).restoreRoute(REGION, API_ID, oldRoute, List.of());}@Test@@ -140,7 +154,7 @@ class ApiGatewayV2CfnProvisionerTest {assertEquals("old-route,replacement-route",replacement.getAttributes().get("__FlociApiGatewayV2BodyRouteIds"));verify(apiGatewayV2Service).restoreRoute(REGION, API_ID, oldRoute,- java.util.List.of("replacement-route"));+ List.of("replacement-route"));}@Test@@ -236,7 +250,7 @@ class ApiGatewayV2CfnProvisionerTest {"GET /protected".equals(request.get("routeKey"))&& "JWT".equals(request.get("authorizationType"))&& "body-authorizer".equals(request.get("authorizerId"))- && java.util.List.of("orders/read").equals(request.get("authorizationScopes"))));+ && List.of("orders/read").equals(request.get("authorizationScopes"))));verify(apiGatewayV2Service).createRoute(eq(REGION), eq(API_ID), argThat(request ->"GET /public".equals(request.get("routeKey"))&& "NONE".equals(request.get("authorizationType"))@@ -403,7 +417,7 @@ class ApiGatewayV2CfnProvisionerTest {verify(apiGatewayV2Service).deleteAuthorizer(REGION, API_ID, "old-authorizer");verify(apiGatewayV2Service).deleteAuthorizer(REGION, API_ID, "replacement-authorizer");verify(apiGatewayV2Service).restoreAuthorizer(REGION, API_ID, oldAuthorizer);- verify(apiGatewayV2Service).restoreRoute(REGION, API_ID, oldRoute, java.util.List.of());+ verify(apiGatewayV2Service).restoreRoute(REGION, API_ID, oldRoute, List.of());}@Test@@ -460,8 +474,8 @@ class ApiGatewayV2CfnProvisionerTest {assertEquals("old-one,old-two", replacement.getAttributes().get("__FlociApiGatewayV2BodyRouteIds"));verify(apiGatewayV2Service, never()).createRoute(eq(REGION), eq(API_ID),argThat(request -> "GET /after".equals(request.get("routeKey"))));- verify(apiGatewayV2Service).restoreRoute(REGION, API_ID, oldOne, java.util.List.of());- verify(apiGatewayV2Service).restoreRoute(REGION, API_ID, oldTwo, java.util.List.of());+ verify(apiGatewayV2Service).restoreRoute(REGION, API_ID, oldOne, List.of());+ verify(apiGatewayV2Service).restoreRoute(REGION, API_ID, oldTwo, List.of());}@Test@@ -496,8 +510,8 @@ class ApiGatewayV2CfnProvisionerTest {assertEquals("CREATE_COMPLETE", original.getStatus());assertEquals("CREATE_FAILED", removal.getStatus());assertEquals("old-one,old-two", removal.getAttributes().get("__FlociApiGatewayV2BodyRouteIds"));- verify(apiGatewayV2Service).restoreRoute(REGION, API_ID, oldOne, java.util.List.of());- verify(apiGatewayV2Service).restoreRoute(REGION, API_ID, oldTwo, java.util.List.of());+ verify(apiGatewayV2Service).restoreRoute(REGION, API_ID, oldOne, List.of());+ verify(apiGatewayV2Service).restoreRoute(REGION, API_ID, oldTwo, List.of());}private StackResource provision(JsonNode properties, String existingPhysicalId,diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.javaindex 21c1311390..a42ad2ebe9 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CfnProvisionerFixture.java@@ -34,6 +34,7 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.ApiGatewaimport io.github.hectorvent.floci.services.cloudformation.provisioners.ApiGatewayDomainCfnProvisioner;import io.github.hectorvent.floci.services.cloudformation.provisioners.ApiGatewayGatewayResponseCfnProvisioner;import io.github.hectorvent.floci.services.cloudformation.provisioners.ApiGatewayRestApiCfnProvisioner;+import io.github.hectorvent.floci.services.cloudformation.provisioners.ApiGatewayV2CfnProvisioner;import io.github.hectorvent.floci.services.cloudformation.provisioners.AutoScalingGroupCfnProvisioner;import io.github.hectorvent.floci.services.cloudformation.provisioners.AutoScalingLifecycleHookCfnProvisioner;import io.github.hectorvent.floci.services.cloudformation.provisioners.AutoScalingScalingPolicyCfnProvisioner;@@ -320,6 +321,9 @@ final class CfnProvisionerFixture {discovered.add(new ApiGatewayGatewayResponseCfnProvisioner(apiGatewayService));discovered.add(new ApiGatewayRestApiCfnProvisioner(apiGatewayService, s3Service, objectMapper));}+ if (apiGatewayV2Service != null) {+ discovered.add(new ApiGatewayV2CfnProvisioner(apiGatewayV2Service, s3Service, objectMapper));+ }if (autoScalingService != null) {discovered.add(new AutoScalingGroupCfnProvisioner(autoScalingService));discovered.add(new AutoScalingLifecycleHookCfnProvisioner(autoScalingService));@@ -700,7 +704,6 @@ final class CfnProvisionerFixture {s3Service,lambdaService,iamService,- apiGatewayV2Service,lambdaLayerService,objectMapper,customResourceResponseStore,diff --git a/src/test/resources/cloudformation/supported-resource-types.tsv b/src/test/resources/cloudformation/supported-resource-types.tsvindex d5b42d7e2d..c0f72d373b 100644--- a/src/test/resources/cloudformation/supported-resource-types.tsv+++ b/src/test/resources/cloudformation/supported-resource-types.tsv@@ -11,12 +11,12 @@ AWS::ApiGateway::RestApi ApiGatewayRestApiCfnProvisionerAWS::ApiGateway::Stage ApiGatewayRestApiCfnProvisionerAWS::ApiGateway::UsagePlan ApiGatewayUsagePlanCfnProvisionerAWS::ApiGateway::UsagePlanKey ApiGatewayUsagePlanCfnProvisioner-AWS::ApiGatewayV2::Api LEGACY_SWITCH-AWS::ApiGatewayV2::Authorizer LEGACY_SWITCH-AWS::ApiGatewayV2::Deployment LEGACY_SWITCH-AWS::ApiGatewayV2::Integration LEGACY_SWITCH-AWS::ApiGatewayV2::Route LEGACY_SWITCH-AWS::ApiGatewayV2::Stage LEGACY_SWITCH+AWS::ApiGatewayV2::Api ApiGatewayV2CfnProvisioner+AWS::ApiGatewayV2::Authorizer ApiGatewayV2CfnProvisioner+AWS::ApiGatewayV2::Deployment ApiGatewayV2CfnProvisioner+AWS::ApiGatewayV2::Integration ApiGatewayV2CfnProvisioner+AWS::ApiGatewayV2::Route ApiGatewayV2CfnProvisioner+AWS::ApiGatewayV2::Stage ApiGatewayV2CfnProvisionerAWS::AppSync::ApiKey AppSyncCfnProvisionerAWS::AppSync::DataSource AppSyncCfnProvisionerAWS::AppSync::FunctionConfiguration AppSyncCfnProvisioner