fix(appconfig): support deleting environments (#5337)

* fix(appconfig): support deleting environments

* fix(appconfig): cascade environment deletion

* fix(appconfig): preserve deployment numbering

* fix(appconfig): resolve delete identifiers
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-10 13:45:28 -04:00
1 parent c6068e8cf5
commit 2015d01864
10 files changed
+197 -28

No files matched your search

@@ -0,0 +1,13 @@
package io.github.hectorvent.floci.core.storage;
public final class StorageOperations {
private StorageOperations() {}
public static void deleteByPrefix(StorageBackend<String, ?> store, String prefix) {
store.keys().stream()
.filter(key -> key.startsWith(prefix))
.toList()
.forEach(store::delete);
}
}
@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.core.common.ReservedTags;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.core.storage.StorageOperations;
import io.github.hectorvent.floci.services.apigatewayv2.model.*;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -219,21 +220,17 @@ public class ApiGatewayV2Service {
// Cascade-delete every child resource keyed under region::apiId::*.
// VpcLink is region-scoped (region::vpcLinkId, shared across APIs) and excluded.
String prefix = region + "::" + apiId + "::";
deleteByPrefix(routeStore, prefix);
deleteByPrefix(integrationStore, prefix);
deleteByPrefix(authorizerStore, prefix);
deleteByPrefix(deploymentStore, prefix);
deleteByPrefix(stageStore, prefix);
deleteByPrefix(modelStore, prefix);
deleteByPrefix(routeResponseStore, prefix);
deleteByPrefix(integrationResponseStore, prefix);
StorageOperations.deleteByPrefix(routeStore, prefix);
StorageOperations.deleteByPrefix(integrationStore, prefix);
StorageOperations.deleteByPrefix(authorizerStore, prefix);
StorageOperations.deleteByPrefix(deploymentStore, prefix);
StorageOperations.deleteByPrefix(stageStore, prefix);
StorageOperations.deleteByPrefix(modelStore, prefix);
StorageOperations.deleteByPrefix(routeResponseStore, prefix);
StorageOperations.deleteByPrefix(integrationResponseStore, prefix);
LOG.infov("Deleted HTTP API: {0} in {1}", apiId, region);
}
private static void deleteByPrefix(StorageBackend<String, ?> store, String prefix) {
store.keys().stream().filter(k -> k.startsWith(prefix)).forEach(store::delete);
}
public Api updateApi(String region, String apiId, Map<String, Object> request) {
Api api = getApi(region, apiId);
// Validated before any field is set, so a rejected update leaves the API as it was.
@@ -92,6 +92,13 @@ public class AppConfigController {
return Response.ok(service.getEnvironment(appId, envId)).build();
}
@DELETE
@Path("/applications/{appId}/environments/{envId}")
public Response deleteEnvironment(@PathParam("appId") String appId, @PathParam("envId") String envId) {
service.deleteEnvironment(appId, envId);
return Response.noContent().build();
}
@GET
@Path("/applications/{appId}/environments")
public Response listEnvironments(@PathParam("appId") String appId) {
@@ -104,6 +104,7 @@ public class AppConfigDataService {
public ConfigurationData getLatestConfiguration(String token, String accept) {
ConfigurationSession session = sessionStore.get(token)
.orElseThrow(() -> new AwsException("BadRequestException", "Invalid configuration token", 400));
appConfigService.getEnvironment(session.getApplicationId(), session.getEnvironmentId());
int pollInterval = normalizePollInterval(session.getRequiredMinimumPollIntervalInSeconds());
session.setRequiredMinimumPollIntervalInSeconds(pollInterval);
@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.core.storage.StorageOperations;
import io.github.hectorvent.floci.services.appconfig.model.Application;
import io.github.hectorvent.floci.services.appconfig.model.ConfigurationProfile;
import io.github.hectorvent.floci.services.appconfig.model.Deployment;
@@ -99,6 +100,20 @@ public class AppConfigService {
return env;
}
public synchronized void deleteEnvironment(String applicationIdentifier, String environmentIdentifier) {
Application application = resolveApplication(applicationIdentifier);
Environment environment = resolveEnvironment(application.getId(), environmentIdentifier);
String appId = application.getId();
String envId = environment.getId();
StorageOperations.deleteByPrefix(deploymentStore, appId + "::" + envId + "::");
StorageOperations.deleteByPrefix(activeConfigStore, envId + "::");
String deploymentScope = appId + "::" + envId;
deploymentPageTokens.entrySet().removeIf(entry -> entry.getValue().scope().equals(deploymentScope));
environmentStore.delete(envId);
}
/** The environment of {@code appId} whose ID or name is {@code idOrName}. */
public Environment resolveEnvironment(String appId, String idOrName) {
// An exact ID always wins over another environment's name, and a direct lookup skips the scan.
@@ -163,11 +178,7 @@ public class AppConfigService {
// A hosted configuration version isn't independently addressable outside its profile's
// lifecycle - cascade the delete so a caller can't still fetch versions for a profile
// that's supposedly gone.
String versionPrefix = appId + "::" + profileId + "::";
versionStore.keys().stream()
.filter(k -> k.startsWith(versionPrefix))
.toList()
.forEach(versionStore::delete);
StorageOperations.deleteByPrefix(versionStore, appId + "::" + profileId + "::");
profileStore.delete(profileId);
}
@@ -299,7 +310,7 @@ public class AppConfigService {
// ──────────────────────────── Deployment ────────────────────────────
public Deployment startDeployment(String appId, String envId, Map<String, Object> request) {
public synchronized Deployment startDeployment(String appId, String envId, Map<String, Object> request) {
getEnvironment(appId, envId);
String profileId = (String) request.get("ConfigurationProfileId");
String version = (String) request.get("ConfigurationVersion");
@@ -314,7 +325,11 @@ public class AppConfigService {
deployment.setConfigurationProfileId(profileId);
deployment.setConfigurationVersion(version);
deployment.setDeploymentStrategyId(strategyId);
deployment.setDeploymentNumber(deploymentStore.keys().size() + 1);
int nextDeploymentNumber = deploymentStore.scan(key -> true).stream()
.mapToInt(Deployment::getDeploymentNumber)
.max()
.orElse(0) + 1;
deployment.setDeploymentNumber(nextDeploymentNumber);
deployment.setState("COMPLETE"); // Synchronous immediate deployment
deployment.setDescription((String) request.get("Description"));
@@ -328,6 +343,7 @@ public class AppConfigService {
}
public Deployment getDeployment(String appId, String envId, int deploymentNumber) {
getEnvironment(appId, envId);
return deploymentStore.get(appId + "::" + envId + "::" + deploymentNumber)
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Deployment not found", 404));
}
@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.TagHandler;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.core.storage.StorageOperations;
import io.github.hectorvent.floci.services.fis.model.Experiment;
import io.github.hectorvent.floci.services.fis.model.ExperimentTemplate;
import io.github.hectorvent.floci.services.fis.model.IdempotencyRecord;
@@ -178,7 +179,7 @@ public class FisService implements TagHandler {
public synchronized ObjectNode deleteExperimentTemplate(String region, String id) {
ObjectNode template = requireTemplate(region, id);
stores.templates.delete(templateKey(region, id));
deleteByPrefix(stores.targetAccounts, targetAccountPrefix(region, id));
StorageOperations.deleteByPrefix(stores.targetAccounts, targetAccountPrefix(region, id));
return wrap("experimentTemplate", template);
}
@@ -1508,14 +1509,6 @@ public class FisService implements TagHandler {
putTemplate(region, templateId, template);
}
private <T> void deleteByPrefix(StorageBackend<String, T> store, String prefix) {
for (String key : new HashSet<>(store.keys())) {
if (key.startsWith(prefix)) {
store.delete(key);
}
}
}
private String newId(String region, String type, String prefix) {
String id;
do {