refactor(cloudformation): move ApiGateway v1 core types into ApiGatewayRestApiCfnProvisioner (#3885)

Move the six coupled REST API Gateway types (RestApi, Resource, Method, Deployment, Stage, Authorizer) out of the CloudFormationResourceProvisioner monolith into a new ApiGatewayRestApiCfnProvisioner. The shared OpenAPI Body and BodyS3Location resolver, used by both AWS::ApiGateway::RestApi and AWS::ApiGatewayV2::Api, is extracted into OpenApiDocuments so the monolith's V2 arm reuses it rather than keeping a copy. With the v1 arms gone, the monolith no longer needs its apiGatewayService dependency, so that constructor parameter is dropped.

The move preserves Ref, but it is not a pure move for Fn::GetAtt: it also resolves four keys the monolith never populated. Once a type is owned by a registry provisioner, CfnSchemaCoverageTest requires every readOnly attribute to be published or recorded as a gap, so the new provisioner sets RestApiId, ResourceId, AuthorizerId and DeploymentId, which previously resolved to the literal "Logical.Attr". RootResourceId was already set. The inventory tsv, fixture wiring and a focused unit test cover the six types.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-18 23:39:14 -04:00
1 parent 21beccceb1
commit 25fc8e452f
6 files changed
+491 -230

No files matched your search

@@ -33,6 +33,7 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.ApiGatewa
import io.github.hectorvent.floci.services.cloudformation.provisioners.ApiGatewayUsagePlanCfnProvisioner;
import 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.AutoScalingGroupCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.AutoScalingLifecycleHookCfnProvisioner;
import io.github.hectorvent.floci.services.cloudformation.provisioners.AutoScalingScalingPolicyCfnProvisioner;
@@ -303,6 +304,7 @@ final class CfnProvisionerFixture {
discovered.add(new ApiGatewayUsagePlanCfnProvisioner(apiGatewayService));
discovered.add(new ApiGatewayDomainCfnProvisioner(apiGatewayService));
discovered.add(new ApiGatewayGatewayResponseCfnProvisioner(apiGatewayService));
discovered.add(new ApiGatewayRestApiCfnProvisioner(apiGatewayService, s3Service, objectMapper));
}
if (autoScalingService != null) {
discovered.add(new AutoScalingGroupCfnProvisioner(autoScalingService));
@@ -684,7 +686,6 @@ final class CfnProvisionerFixture {
s3Service,
lambdaService,
iamService,
apiGatewayService,
apiGatewayV2Service,
lambdaLayerService,
objectMapper,
@@ -0,0 +1,159 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.services.apigateway.ApiGatewayService;
import io.github.hectorvent.floci.services.apigateway.model.ApiGatewayResource;
import io.github.hectorvent.floci.services.apigateway.model.Authorizer;
import io.github.hectorvent.floci.services.apigateway.model.Deployment;
import io.github.hectorvent.floci.services.apigateway.model.RestApi;
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 org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* The REST API Gateway core types in isolation: the physical id and the exact Fn::GetAtt keys each
* publishes, the inline-stage shortcut on a Deployment, and that only the RestApi deletes.
*/
class ApiGatewayRestApiCfnProvisionerTest {
private final ApiGatewayService api = mock(ApiGatewayService.class);
private final S3Service s3 = mock(S3Service.class);
private final ApiGatewayRestApiCfnProvisioner provisioner =
new ApiGatewayRestApiCfnProvisioner(api, s3, new ObjectMapper());
private final ObjectMapper mapper = new ObjectMapper();
@Test
void restApiPublishesIdAndRootResourceId() throws Exception {
RestApi created = new RestApi();
created.setId("api-1");
when(api.createRestApi(eq("us-east-1"), anyMap())).thenReturn(created);
ApiGatewayResource root = new ApiGatewayResource();
root.setId("root-1");
when(api.getResources("us-east-1", "api-1")).thenReturn(List.of(root));
StackResource r = resource("AWS::ApiGateway::RestApi", "Api");
provisioner.provision(r, props("{\"Name\": \"shop\"}"), ctx());
assertEquals("api-1", r.getPhysicalId());
assertEquals(Set.of("RestApiId", "RootResourceId"), r.getAttributes().keySet());
assertEquals("api-1", r.getAttributes().get("RestApiId"));
assertEquals("root-1", r.getAttributes().get("RootResourceId"));
verify(api, never()).putRestApi(anyString(), anyString(), anyString(), anyString());
}
@Test
void resourcePublishesResourceId() throws Exception {
ApiGatewayResource res = new ApiGatewayResource();
res.setId("res-1");
when(api.createResource(eq("us-east-1"), eq("api-1"), eq("root-1"), anyMap())).thenReturn(res);
StackResource r = resource("AWS::ApiGateway::Resource", "Res");
provisioner.provision(r, props("""
{"RestApiId": "api-1", "ParentId": "root-1", "PathPart": "orders"}
"""), ctx());
assertEquals("res-1", r.getPhysicalId());
assertEquals("res-1", r.getAttributes().get("ResourceId"));
}
@Test
void authorizerPublishesAuthorizerId() throws Exception {
Authorizer authorizer = new Authorizer();
authorizer.setId("auth-1");
when(api.createAuthorizer(eq("us-east-1"), eq("api-1"), anyMap())).thenReturn(authorizer);
StackResource r = resource("AWS::ApiGateway::Authorizer", "Auth");
provisioner.provision(r, props("""
{"RestApiId": "api-1", "Name": "jwt", "Type": "TOKEN"}
"""), ctx());
assertEquals("auth-1", r.getPhysicalId());
assertEquals("auth-1", r.getAttributes().get("AuthorizerId"));
}
@Test
void methodUsesTheCompositePhysicalIdAndProvisionsItsIntegration() throws Exception {
StackResource r = resource("AWS::ApiGateway::Method", "Get");
provisioner.provision(r, props("""
{"RestApiId": "api-1", "ResourceId": "res-1", "HttpMethod": "GET",
"Integration": {"Type": "AWS_PROXY", "IntegrationHttpMethod": "POST", "Uri": "arn:..."}}
"""), ctx());
assertEquals("api-1-res-1-GET", r.getPhysicalId());
verify(api).putMethod(eq("us-east-1"), eq("api-1"), eq("res-1"), eq("GET"), anyMap());
verify(api).putIntegration(eq("us-east-1"), eq("api-1"), eq("res-1"), eq("GET"), anyMap());
}
@Test
void deploymentPublishesDeploymentIdAndCreatesTheInlineStage() throws Exception {
when(api.createDeployment(eq("us-east-1"), eq("api-1"), anyMap()))
.thenReturn(new Deployment("dep-1", null, 0L));
StackResource r = resource("AWS::ApiGateway::Deployment", "Dep");
provisioner.provision(r, props("""
{"RestApiId": "api-1", "StageName": "prod"}
"""), ctx());
assertEquals("dep-1", r.getPhysicalId());
assertEquals("dep-1", r.getAttributes().get("DeploymentId"));
verify(api).createStage(eq("us-east-1"), eq("api-1"), anyMap());
}
@Test
void stageUsesTheStageNameAsPhysicalId() throws Exception {
StackResource r = resource("AWS::ApiGateway::Stage", "Stage");
provisioner.provision(r, props("""
{"RestApiId": "api-1", "StageName": "prod", "DeploymentId": "dep-1"}
"""), ctx());
assertEquals("prod", r.getPhysicalId());
verify(api).createStage(eq("us-east-1"), eq("api-1"), anyMap());
}
@Test
void deleteRemovesOnlyTheRestApi() {
provisioner.delete("AWS::ApiGateway::RestApi", "api-1", "us-east-1");
verify(api).deleteRestApi("us-east-1", "api-1");
provisioner.delete("AWS::ApiGateway::Method", "api-1-res-1-GET", "us-east-1");
verify(api, never()).deleteRestApi("us-east-1", "api-1-res-1-GET");
}
private ProvisionContext ctx() {
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
when(engine.resolve(any())).thenAnswer(inv -> {
JsonNode node = inv.getArgument(0);
return node == null ? null : node.asText();
});
when(engine.resolveNode(any())).thenAnswer(inv -> inv.getArgument(0));
return new ProvisionContext(engine, "us-east-1", "000000000000", "my-stack");
}
private JsonNode props(String json) throws Exception {
return mapper.readTree(json);
}
private static StackResource resource(String type, String logicalId) {
StackResource r = new StackResource();
r.setLogicalId(logicalId);
r.setResourceType(type);
r.setAttributes(new HashMap<>());
return r;
}
}