feat(cloudformation): provision AWS::ApiGatewayV2::VpcLink (#5008)

* feat(apigatewayv2): implement UpdateVpcLink and VPC link tagging

* feat(cloudformation): provision AWS::ApiGatewayV2::VpcLink

* fix(cloudformation): keep VPC link and integration connection state on updates

* fix(cloudformation): restore the integration connection when its own update fails
This commit is contained in:
Aymen JAMAAOUI authored and GitHub committed 2026-10-04 09:09:18 -04:00
1 parent db40361a3c
commit 2c544fdb9f
12 files changed
+927 -14

No files matched your search

@@ -1559,6 +1559,22 @@ public class ApiGatewayController {
.type(MediaType.APPLICATION_JSON).build();
}
@PATCH
@Path("/v2/vpclinks/{vpcLinkId}")
public Response updateVpcLink(@Context HttpHeaders headers,
@PathParam("vpcLinkId") String vpcLinkId,
String body) {
String region = regionResolver.resolveRegion(headers);
try {
@SuppressWarnings("unchecked")
Map<String, Object> request = objectMapper.readValue(body, Map.class);
VpcLink link = v2Service.updateVpcLink(region, vpcLinkId, request);
return Response.ok(toV2VpcLinkNode(link).toString()).type(MediaType.APPLICATION_JSON).build();
} catch (IOException e) {
throw new AwsException("BadRequestException", e.getMessage(), 400);
}
}
@DELETE
@Path("/v2/vpclinks/{vpcLinkId}")
public Response deleteVpcLink(@Context HttpHeaders headers, @PathParam("vpcLinkId") String vpcLinkId) {
@@ -15,6 +15,7 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -782,6 +783,19 @@ public class ApiGatewayV2Service {
return integration;
}
/**
* Sets the connection type and id exactly as given, null included. UpdateIntegration keeps a
* field the request leaves out, so it cannot remove a connection that CloudFormation dropped.
*/
public Integration replaceIntegrationConnection(String region, String apiId, String integrationId,
String connectionType, String connectionId) {
Integration integration = getIntegration(region, apiId, integrationId);
integration.setConnectionType(connectionType);
integration.setConnectionId(connectionId);
integrationStore.put(integrationKey(region, apiId, integrationId), integration);
return integration;
}
// ──────────────────────────── Stage CRUD ────────────────────────────
public Stage createStage(String region, String apiId, Map<String, Object> request) {
@@ -1143,6 +1157,16 @@ public class ApiGatewayV2Service {
return vpcLinkStore.scan(k -> k.startsWith(prefix));
}
/** UpdateVpcLink: only the name can change, as in the API model. */
public VpcLink updateVpcLink(String region, String vpcLinkId, Map<String, Object> request) {
VpcLink link = getVpcLink(region, vpcLinkId);
if (request.get("name") instanceof String name) {
link.setName(name);
}
vpcLinkStore.put(vpcLinkKey(region, vpcLinkId), link);
return link;
}
public void deleteVpcLink(String region, String vpcLinkId) {
getVpcLink(region, vpcLinkId);
vpcLinkStore.delete(vpcLinkKey(region, vpcLinkId));
@@ -1150,18 +1174,23 @@ public class ApiGatewayV2Service {
// ──────────────────────────── Standalone Tagging ────────────────────────────
/** A taggable v2 resource: an API, or a stage within one. */
private record TaggedResource(String region, String apiId, String stageName) {
/** A taggable v2 resource: an API, a stage within one, or a VPC link. */
private record TaggedResource(String region, String apiId, String stageName, String vpcLinkId) {
boolean isStage() {
return stageName != null;
}
boolean isVpcLink() {
return vpcLinkId != null;
}
}
/**
* Parses an API Gateway v2 resource ARN.
*
* <p>Accepted forms: {@code arn:aws:apigateway:{region}::/apis/{apiId}} and
* {@code arn:aws:apigateway:{region}::/apis/{apiId}/stages/{stageName}}. Matching on the
* <p>Accepted forms: {@code arn:aws:apigateway:{region}::/apis/{apiId}},
* {@code arn:aws:apigateway:{region}::/apis/{apiId}/stages/{stageName}} and
* {@code arn:aws:apigateway:{region}::/vpclinks/{vpcLinkId}}. Matching on the
* trailing segment alone would read a stage ARN's stage name as the API id, which surfaces as
* a misleading "Invalid API id specified" on TagResource.
*/
@@ -1181,10 +1210,13 @@ public class ApiGatewayV2Service {
String[] segments = arn.resource().replaceFirst("^/", "").split("/");
if (segments.length >= 4 && "apis".equals(segments[0]) && "stages".equals(segments[2])
&& !segments[1].isEmpty() && !segments[3].isEmpty()) {
return new TaggedResource(region, segments[1], segments[3]);
return new TaggedResource(region, segments[1], segments[3], null);
}
if (segments.length >= 2 && "apis".equals(segments[0]) && !segments[1].isEmpty()) {
return new TaggedResource(region, segments[1], null);
return new TaggedResource(region, segments[1], null, null);
}
if (segments.length == 2 && "vpclinks".equals(segments[0]) && !segments[1].isEmpty()) {
return new TaggedResource(region, null, null, segments[1]);
}
throw new AwsException("BadRequestException",
"Cannot extract apiId from ResourceArn: " + resourceArn, 400);
@@ -1192,6 +1224,18 @@ public class ApiGatewayV2Service {
public void tagResource(String resourceArn, Map<String, String> tags) {
TaggedResource target = parseArn(resourceArn);
if (target.isVpcLink()) {
ReservedTags.rejectApiGatewayReservedTagsOnUpdate(tags);
VpcLink link = getVpcLink(target.region(), target.vpcLinkId());
if (tags != null && !tags.isEmpty()) {
if (link.getTags() == null) {
link.setTags(new HashMap<>());
}
link.getTags().putAll(tags);
}
vpcLinkStore.put(vpcLinkKey(target.region(), target.vpcLinkId()), link);
return;
}
if (target.isStage()) {
ReservedTags.rejectApiGatewayReservedTagsOnUpdate(tags);
Stage stage = getStage(target.region(), target.apiId(), target.stageName());
@@ -1217,6 +1261,14 @@ public class ApiGatewayV2Service {
public void untagResource(String resourceArn, List<String> tagKeys) {
TaggedResource target = parseArn(resourceArn);
if (target.isVpcLink()) {
VpcLink link = getVpcLink(target.region(), target.vpcLinkId());
if (tagKeys != null && link.getTags() != null) {
tagKeys.forEach(k -> link.getTags().remove(k));
}
vpcLinkStore.put(vpcLinkKey(target.region(), target.vpcLinkId()), link);
return;
}
if (target.isStage()) {
Stage stage = getStage(target.region(), target.apiId(), target.stageName());
if (tagKeys != null && stage.getTags() != null) {
@@ -1234,9 +1286,14 @@ public class ApiGatewayV2Service {
public Map<String, String> getTags(String resourceArn) {
TaggedResource target = parseArn(resourceArn);
Map<String, String> tags = target.isStage()
? getStage(target.region(), target.apiId(), target.stageName()).getTags()
: getApi(target.region(), target.apiId()).getTags();
Map<String, String> tags;
if (target.isVpcLink()) {
tags = getVpcLink(target.region(), target.vpcLinkId()).getTags();
} else if (target.isStage()) {
tags = getStage(target.region(), target.apiId(), target.stageName()).getTags();
} else {
tags = getApi(target.region(), target.apiId()).getTags();
}
return (tags != null) ? new java.util.HashMap<>(tags) : java.util.Collections.emptyMap();
}
@@ -1,7 +1,9 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
@@ -10,6 +12,7 @@ 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.apigatewayv2.model.VpcLink;
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;
@@ -48,6 +51,7 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner {
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 VPC_LINK = "AWS::ApiGatewayV2::VpcLink";
private static final String BODY_ROUTE_IDS_ATTR = "__FlociApiGatewayV2BodyRouteIds";
private static final String BODY_INTEGRATION_IDS_ATTR = "__FlociApiGatewayV2BodyIntegrationIds";
@@ -67,7 +71,7 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner {
@Override
public Set<String> resourceTypes() {
return Set.of(API, AUTHORIZER, ROUTE, INTEGRATION, STAGE, DEPLOYMENT);
return Set.of(API, AUTHORIZER, ROUTE, INTEGRATION, STAGE, DEPLOYMENT, VPC_LINK);
}
@Override
@@ -79,6 +83,7 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner {
case INTEGRATION -> provisionIntegration(r, props, ctx);
case STAGE -> provisionStage(r, props, ctx);
case DEPLOYMENT -> provisionDeployment(r, props, ctx);
case VPC_LINK -> provisionVpcLink(r, props, ctx);
default -> throw new IllegalStateException("Unhandled type: " + r.getResourceType());
}
}
@@ -100,12 +105,57 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner {
}
}
}
case VPC_LINK -> delete(VPC_LINK, resource.getPhysicalId(), region);
default -> {
// Route, Integration, Stage and Deployment are removed when their Api is deleted.
}
}
}
@Override
public void delete(String resourceType, String physicalId, String region) {
if (VPC_LINK.equals(resourceType) && physicalId != null && !physicalId.isBlank()) {
CfnDeletes.safeDelete("API Gateway V2 VPC link", physicalId,
() -> apiGatewayV2Service.deleteVpcLink(region, physicalId), "NotFoundException");
}
}
@Override
public boolean hasReplacementUpdate(StackResource resource) {
return ReplacementCleanup.hasReplacement(resource);
}
@Override
public String updateCleanupPhysicalId(StackResource resource) {
return ReplacementCleanup.cleanupPhysicalId(resource);
}
@Override
public UpdateCleanupResult completeUpdate(StackResource resource) {
return ReplacementCleanup.complete(resource, this::delete);
}
@Override
public void clearUpdate(StackResource resource) {
resource.getAttributes().remove(CfnRollback.VPC_LINK_UPDATE_SNAPSHOT_ATTR);
resource.getAttributes().remove(CfnRollback.INTEGRATION_CONNECTION_SNAPSHOT_ATTR);
ReplacementCleanup.clear(resource);
}
@Override
public boolean rollbackUpdate(StackResource resource) {
if (ReplacementCleanup.rollback(resource, this::delete)) {
return true;
}
if (VPC_LINK.equals(resource.getResourceType())) {
return rollbackVpcLinkUpdate(resource);
}
if (INTEGRATION.equals(resource.getResourceType())) {
return rollbackIntegrationConnection(resource);
}
return false;
}
@Override
public void mergeFailedUpdateResourceTracking(StackResource previous, StackResource attempted) {
// Only the Api carries body-generated sub-resource ids; keep any the failed attempt created
@@ -813,17 +863,95 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner {
req.put("integrationType", ctx.resolveOptional(props, "IntegrationType"));
req.put("integrationUri", ctx.resolveOptional(props, "IntegrationUri"));
req.put("payloadFormatVersion", ctx.resolveOrDefault(props, "PayloadFormatVersion", "2.0"));
String connectionType = ctx.resolveOptional(props, "ConnectionType");
String connectionId = ctx.resolveOptional(props, "ConnectionId");
req.put("connectionType", connectionType);
req.put("connectionId", connectionId);
// A snapshot describes the update in flight; one an earlier update left behind is stale.
r.getAttributes().remove(CfnRollback.INTEGRATION_CONNECTION_SNAPSHOT_ATTR);
Integration integration;
if (r.getPhysicalId() == null) {
integration = apiGatewayV2Service.createIntegration(region, apiId, req);
} else {
integration = apiGatewayV2Service.updateIntegration(region, apiId, r.getPhysicalId(), req);
Integration prior = apiGatewayV2Service.getIntegration(region, apiId, r.getPhysicalId());
r.getAttributes().put(CfnRollback.INTEGRATION_CONNECTION_SNAPSHOT_ATTR,
integrationConnectionSnapshot(region, apiId, prior, req));
// The connection changes first, in one call. If the update after it fails, the connection
// is the only change to undo.
try {
// A connection removed from the template is removed here too. INTERNET is the AWS default.
apiGatewayV2Service.replaceIntegrationConnection(region, apiId, r.getPhysicalId(),
connectionType != null ? connectionType : "INTERNET", connectionId);
integration = apiGatewayV2Service.updateIntegration(region, apiId, r.getPhysicalId(), req);
} catch (RuntimeException failure) {
// The stack skips rollbackUpdate for a resource whose own update failed, so undo it here.
try {
rollbackIntegrationConnection(r);
} catch (RuntimeException restoreFailure) {
r.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR,
"Could not roll back the connection of integration " + r.getPhysicalId() + ": "
+ restoreFailure.getMessage());
if (restoreFailure != failure) {
failure.addSuppressed(restoreFailure);
}
}
throw failure;
}
}
r.setPhysicalId(integration.getIntegrationId());
r.getAttributes().put("IntegrationId", integration.getIntegrationId());
}
/**
* The connection the integration had before this update, and whether the update also changes
* another field. UpdateIntegration keeps a field the request leaves out, so only a value that
* is set and differs counts as a change.
*/
private String integrationConnectionSnapshot(String region, String apiId, Integration prior,
Map<String, Object> req) {
boolean otherFieldChanged = changes(req.get("integrationType"), prior.getIntegrationType())
|| changes(req.get("integrationUri"), prior.getIntegrationUri())
|| changes(req.get("payloadFormatVersion"), prior.getPayloadFormatVersion());
ObjectNode snapshot = objectMapper.createObjectNode();
snapshot.put("region", region);
snapshot.put("apiId", apiId);
snapshot.put("connectionType", prior.getConnectionType());
snapshot.put("connectionId", prior.getConnectionId());
snapshot.put("otherFieldChanged", otherFieldChanged);
return snapshot.toString();
}
private static boolean changes(Object requested, String current) {
return requested != null && !requested.equals(current);
}
/**
* Puts the connection back from the snapshot taken before the update. Answers true only when the
* update changed nothing else: this provisioner keeps no copy of the other fields, so the stack
* must still report that their rollback is not implemented.
*/
private boolean rollbackIntegrationConnection(StackResource resource) {
String raw = resource.getAttributes().get(CfnRollback.INTEGRATION_CONNECTION_SNAPSHOT_ATTR);
if (raw == null) {
return false;
}
JsonNode snapshot;
try {
snapshot = objectMapper.readTree(raw);
} catch (JsonProcessingException e) {
LOG.errorv("Could not parse integration connection snapshot for {0}: {1}",
resource.getLogicalId(), e.getMessage());
return false;
}
apiGatewayV2Service.replaceIntegrationConnection(snapshot.path("region").asText(),
snapshot.path("apiId").asText(), resource.getPhysicalId(),
textOrNull(snapshot, "connectionType"), textOrNull(snapshot, "connectionId"));
// Spent only once the restore succeeded, so a restore that throws can be tried again.
resource.getAttributes().remove(CfnRollback.INTEGRATION_CONNECTION_SNAPSHOT_ATTR);
return !snapshot.path("otherFieldChanged").asBoolean();
}
// ── Stage ────────────────────────────────────────────────────────────────
private void provisionStage(StackResource r, JsonNode props, ProvisionContext ctx) {
@@ -865,6 +993,147 @@ public class ApiGatewayV2CfnProvisioner implements CfnResourceProvisioner {
r.getAttributes().put("DeploymentId", deployment.getDeploymentId());
}
// ── VpcLink ──────────────────────────────────────────────────────────────
/**
* Creates the VPC link, or updates it in place when its subnets and security groups did not
* change: on AWS only {@code Name} and {@code Tags} update without replacement. Any other change
* creates a new link, and {@link ReplacementCleanup} deletes the old one after the update.
*/
private void provisionVpcLink(StackResource r, JsonNode props, ProvisionContext ctx) {
String region = ctx.region();
String name = ctx.resolveOptional(props, "Name");
if (name == null || name.isBlank()) {
throw new AwsException("ValidationError", VPC_LINK + " requires Name", 400);
}
List<String> subnetIds = ctx.resolveStringList(props, "SubnetIds");
if (subnetIds.isEmpty()) {
throw new AwsException("ValidationError", VPC_LINK + " requires SubnetIds", 400);
}
List<String> securityGroupIds = ctx.resolveStringList(props, "SecurityGroupIds");
Map<String, String> tags = ctx.resolveTags(props, "Tags");
// A snapshot describes the update in flight; one an earlier update left behind is stale.
r.getAttributes().remove(CfnRollback.VPC_LINK_UPDATE_SNAPSHOT_ATTR);
Map<String, String> attributesBefore = new HashMap<>(r.getAttributes());
VpcLink prior = ctx.isUpdate() ? findVpcLink(region, ctx.priorPhysicalId()) : null;
String vpcLinkId;
if (prior != null && subnetIds.equals(listOrEmpty(prior.getSubnetIds()))
&& securityGroupIds.equals(listOrEmpty(prior.getSecurityGroupIds()))) {
vpcLinkId = prior.getVpcLinkId();
r.getAttributes().put(CfnRollback.VPC_LINK_UPDATE_SNAPSHOT_ATTR, vpcLinkSnapshot(prior, region));
r.setPhysicalId(vpcLinkId);
// Tags go first: the service refuses a reserved tag key, and refusing it before the
// rename leaves the name as the stack last committed it.
try {
reconcileVpcLinkTags(region, vpcLinkId, tags);
apiGatewayV2Service.updateVpcLink(region, vpcLinkId, Map.of("name", name));
} catch (RuntimeException failure) {
// The stack skips rollbackUpdate for a resource whose own update failed, so undo it here.
try {
rollbackVpcLinkUpdate(r);
} catch (RuntimeException restoreFailure) {
r.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR,
"Could not roll back the update of VPC link " + vpcLinkId + ": "
+ restoreFailure.getMessage());
if (restoreFailure != failure) {
failure.addSuppressed(restoreFailure);
}
}
throw failure;
}
} else {
Map<String, Object> req = new HashMap<>();
req.put("name", name);
req.put("subnetIds", subnetIds);
req.put("securityGroupIds", securityGroupIds);
if (!tags.isEmpty()) {
req.put("tags", tags);
}
vpcLinkId = apiGatewayV2Service.createVpcLink(region, req).getVpcLinkId();
}
r.setPhysicalId(vpcLinkId);
r.getAttributes().put("VpcLinkId", vpcLinkId);
ReplacementCleanup.record(r, ctx, attributesBefore);
}
private VpcLink findVpcLink(String region, String vpcLinkId) {
try {
return apiGatewayV2Service.getVpcLink(region, vpcLinkId);
} catch (AwsException e) {
if (!"NotFoundException".equals(e.getErrorCode())) {
throw e;
}
// The prior link is gone, so the update creates a new one.
return null;
}
}
private void reconcileVpcLinkTags(String region, String vpcLinkId, Map<String, String> desired) {
String arn = AwsArnUtils.Arn.of("apigateway", region, "", "/vpclinks/" + vpcLinkId).toString();
Map<String, String> current = apiGatewayV2Service.getTags(arn);
// Only new or changed tags are sent: TagResource refuses a reserved key, even one the link
// already carries from its creation.
Map<String, String> changed = new LinkedHashMap<>();
desired.forEach((key, value) -> {
if (!value.equals(current.get(key))) {
changed.put(key, value);
}
});
if (!changed.isEmpty()) {
apiGatewayV2Service.tagResource(arn, changed);
}
List<String> stale = ProvisionContext.staleTagKeys(current, desired);
if (!stale.isEmpty()) {
apiGatewayV2Service.untagResource(arn, stale);
}
}
private String vpcLinkSnapshot(VpcLink link, String region) {
ObjectNode snapshot = objectMapper.createObjectNode();
snapshot.put("region", region);
snapshot.put("name", link.getName());
ObjectNode tags = snapshot.putObject("tags");
if (link.getTags() != null) {
link.getTags().forEach(tags::put);
}
return snapshot.toString();
}
/**
* Puts the name and tags back from the snapshot taken before an in-place update. With no
* snapshot, this update did not change the link.
*/
private boolean rollbackVpcLinkUpdate(StackResource resource) {
String raw = resource.getAttributes().get(CfnRollback.VPC_LINK_UPDATE_SNAPSHOT_ATTR);
if (raw == null) {
return true;
}
JsonNode snapshot;
try {
snapshot = objectMapper.readTree(raw);
} catch (JsonProcessingException e) {
LOG.errorv("Could not parse VPC link update snapshot for {0}: {1}",
resource.getLogicalId(), e.getMessage());
return false;
}
String region = snapshot.path("region").asText();
Map<String, String> tags = new LinkedHashMap<>();
snapshot.path("tags").properties().forEach(e -> tags.put(e.getKey(), e.getValue().asText()));
if (snapshot.path("name").isTextual()) {
apiGatewayV2Service.updateVpcLink(region, resource.getPhysicalId(),
Map.of("name", snapshot.path("name").asText()));
}
reconcileVpcLinkTags(region, resource.getPhysicalId(), tags);
// Spent only once the restore succeeded, so a restore that throws can be tried again.
resource.getAttributes().remove(CfnRollback.VPC_LINK_UPDATE_SNAPSHOT_ATTR);
return true;
}
private static List<String> listOrEmpty(List<String> values) {
return values == null ? List.of() : values;
}
// ── Local copies of shared utilities (still used by staying monolith types) ──
private static String textOrNull(JsonNode node, String field) {
@@ -95,6 +95,21 @@ public final class CfnRollback {
*/
public static final String API_KEY_UPDATE_SNAPSHOT_ATTR = "__FlociApiKeyUpdateSnapshot";
/**
* Holds the name and tags an API Gateway V2 VPC link carried before an in-place update changed
* them, so a failed stack update can put them back. Written by {@code ApiGatewayV2CfnProvisioner}
* before its update call and spent by its {@code rollbackUpdate}.
*/
public static final String VPC_LINK_UPDATE_SNAPSHOT_ATTR = "__FlociVpcLinkUpdateSnapshot";
/**
* Holds the connection type and id an API Gateway V2 integration carried before an update, and
* whether that update also changed another field, so a failed stack update can put the
* connection back. Written by {@code ApiGatewayV2CfnProvisioner} before its update call and
* spent by its {@code rollbackUpdate}.
*/
public static final String INTEGRATION_CONNECTION_SNAPSHOT_ATTR = "__FlociIntegrationConnectionSnapshot";
/**
* Holds the complete prior metric filter, identity, name mode and per-address mutation outcomes
* and ownership states.