From 2015d01864b7bb6cd09070c4ffa73920c0fcc421 Mon Sep 17 00:00:00 2001 From: Aniket Shukla Date: Sat, 10 Oct 2026 23:15:28 +0530 Subject: [PATCH] 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 --- docs/services/appconfig.md | 1 + .../floci/core/storage/StorageOperations.java | 13 +++ .../apigatewayv2/ApiGatewayV2Service.java | 21 ++--- .../appconfig/AppConfigController.java | 7 ++ .../appconfig/AppConfigDataService.java | 1 + .../services/appconfig/AppConfigService.java | 30 ++++-- .../floci/services/fis/FisService.java | 11 +-- .../core/storage/StorageOperationsTest.java | 21 +++++ .../appconfig/AppConfigIntegrationTest.java | 28 ++++++ .../appconfig/AppConfigServiceTest.java | 92 +++++++++++++++++++ 10 files changed, 197 insertions(+), 28 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/core/storage/StorageOperations.java create mode 100644 src/test/java/io/github/hectorvent/floci/core/storage/StorageOperationsTest.java diff --git a/docs/services/appconfig.md b/docs/services/appconfig.md index 2b5edcb82f..56f9896220 100644 --- a/docs/services/appconfig.md +++ b/docs/services/appconfig.md @@ -15,6 +15,7 @@ The management plane allows you to create and manage applications, environments, - `CreateEnvironment` - `GetEnvironment` - `ListEnvironments` +- `DeleteEnvironment` - `CreateConfigurationProfile` - `GetConfigurationProfile` - `ListConfigurationProfiles` diff --git a/src/main/java/io/github/hectorvent/floci/core/storage/StorageOperations.java b/src/main/java/io/github/hectorvent/floci/core/storage/StorageOperations.java new file mode 100644 index 0000000000..e489a7755a --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/storage/StorageOperations.java @@ -0,0 +1,13 @@ +package io.github.hectorvent.floci.core.storage; + +public final class StorageOperations { + + private StorageOperations() {} + + public static void deleteByPrefix(StorageBackend store, String prefix) { + store.keys().stream() + .filter(key -> key.startsWith(prefix)) + .toList() + .forEach(store::delete); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java index f3d0082d46..b4294bc503 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java @@ -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 store, String prefix) { - store.keys().stream().filter(k -> k.startsWith(prefix)).forEach(store::delete); - } - public Api updateApi(String region, String apiId, Map request) { Api api = getApi(region, apiId); // Validated before any field is set, so a rejected update leaves the API as it was. diff --git a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigController.java b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigController.java index 49d7a9b427..fdde743632 100644 --- a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigController.java +++ b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigController.java @@ -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) { diff --git a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataService.java b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataService.java index a9c2b37600..03ea784e8c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataService.java +++ b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigDataService.java @@ -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); diff --git a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigService.java b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigService.java index 3e4f94df14..5cb25d31ef 100644 --- a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigService.java +++ b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigService.java @@ -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 request) { + public synchronized Deployment startDeployment(String appId, String envId, Map 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)); } diff --git a/src/main/java/io/github/hectorvent/floci/services/fis/FisService.java b/src/main/java/io/github/hectorvent/floci/services/fis/FisService.java index 4365d0f641..88d842ca1e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/fis/FisService.java +++ b/src/main/java/io/github/hectorvent/floci/services/fis/FisService.java @@ -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 void deleteByPrefix(StorageBackend 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 { diff --git a/src/test/java/io/github/hectorvent/floci/core/storage/StorageOperationsTest.java b/src/test/java/io/github/hectorvent/floci/core/storage/StorageOperationsTest.java new file mode 100644 index 0000000000..6213e1c362 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/core/storage/StorageOperationsTest.java @@ -0,0 +1,21 @@ +package io.github.hectorvent.floci.core.storage; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class StorageOperationsTest { + + @Test + void deleteByPrefixDeletesOnlyMatchingKeys() { + InMemoryStorage storage = new InMemoryStorage<>(); + storage.put("app::env::1", "first"); + storage.put("app::env::2", "second"); + storage.put("app::other::1", "other"); + + StorageOperations.deleteByPrefix(storage, "app::env::"); + + assertEquals(1, storage.keys().size()); + assertEquals("other", storage.get("app::other::1").orElseThrow()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigIntegrationTest.java index c44e918752..2f783b4fbd 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigIntegrationTest.java @@ -1084,4 +1084,32 @@ class AppConfigIntegrationTest { .extract().asByteArray(); assertArrayEquals(stored, body); } + + @Test @Order(51) + void deleteEnvironmentRemovesIt() { + String token = given() + .contentType(ContentType.JSON) + .body("{\"ApplicationIdentifier\":\"" + appId + "\",\"EnvironmentIdentifier\":\"" + + envId + "\",\"ConfigurationProfileIdentifier\":\"" + profileId + "\"}") + .when().post("/configurationsessions") + .then().statusCode(201) + .extract().path("InitialConfigurationToken"); + + given() + .when().delete("/applications/test-app/environments/test-env") + .then().statusCode(204); + + given() + .when().get("/applications/" + appId + "/environments/" + envId) + .then().statusCode(404); + + given() + .when().get("/applications/" + appId + "/environments/" + envId + "/deployments/1") + .then().statusCode(404); + + given() + .queryParam("configuration_token", token) + .when().get("/configuration") + .then().statusCode(404); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceTest.java index c51e217d30..9e3d6e4ec2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceTest.java @@ -2,9 +2,11 @@ package io.github.hectorvent.floci.services.appconfig; import com.fasterxml.jackson.core.type.TypeReference; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; 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; import io.github.hectorvent.floci.services.appconfig.model.DeploymentSummary; import io.github.hectorvent.floci.services.appconfig.model.Environment; @@ -15,6 +17,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -23,30 +32,40 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; class AppConfigServiceTest { private AccountAwareStorageBackend applicationStore; private AccountAwareStorageBackend environmentStore; private AccountAwareStorageBackend deploymentStore; + private AccountAwareStorageBackend activeConfigStore; private AppConfigService service; @BeforeEach void setUp() { applicationStore = mock(AccountAwareStorageBackend.class); environmentStore = mock(AccountAwareStorageBackend.class); + AccountAwareStorageBackend profileStore = mock(AccountAwareStorageBackend.class); deploymentStore = mock(AccountAwareStorageBackend.class); + activeConfigStore = mock(AccountAwareStorageBackend.class); Map deployments = new HashMap<>(); doAnswer(invocation -> { deployments.put(invocation.getArgument(0, String.class), invocation.getArgument(1, Deployment.class)); return null; }).when(deploymentStore).put(anyString(), any(Deployment.class)); when(deploymentStore.scan(any())).thenAnswer(invocation -> List.copyOf(deployments.values())); + when(deploymentStore.keys()).thenAnswer(invocation -> Set.copyOf(deployments.keySet())); + doAnswer(invocation -> deployments.remove(invocation.getArgument(0, String.class))) + .when(deploymentStore).delete(anyString()); + when(activeConfigStore.keys()).thenReturn(Set.of()); StorageFactory storageFactory = mock(StorageFactory.class); doAnswer(invocation -> switch (invocation.getArgument(1, String.class)) { case "appconfig-applications.json" -> applicationStore; case "appconfig-environments.json" -> environmentStore; + case "appconfig-profiles.json" -> profileStore; case "appconfig-deployments.json" -> deploymentStore; + case "appconfig-active-configs.json" -> activeConfigStore; default -> mock(AccountAwareStorageBackend.class); }).when(storageFactory).create(anyString(), anyString(), any(TypeReference.class)); @@ -59,6 +78,10 @@ class AppConfigServiceTest { environment.setApplicationId("app"); environmentStore.put("env", environment); when(environmentStore.get("env")).thenReturn(Optional.of(environment)); + ConfigurationProfile profile = new ConfigurationProfile(); + profile.setId("profile"); + profile.setApplicationId("app"); + when(profileStore.get("profile")).thenReturn(Optional.of(profile)); service = new AppConfigService(storageFactory, mock(EmulatorConfig.class)); } @@ -111,6 +134,75 @@ class AppConfigServiceTest { assertThrows(RuntimeException.class, () -> service.listDeployments("app", "env", 1, "unknown")); } + @Test + void deleteEnvironmentRemovesRuntimeState() { + deploymentStore.put("app::env::1", deployment("app", "env", 1)); + when(activeConfigStore.keys()).thenReturn(Set.of("env::profile")); + + service.deleteEnvironment("app", "env"); + + verify(deploymentStore).delete("app::env::1"); + verify(activeConfigStore).delete("env::profile"); + verify(environmentStore).delete("env"); + } + + @Test + void startDeploymentUsesHighestNumberAfterAnotherEnvironmentIsDeleted() { + Environment otherEnvironment = new Environment(); + otherEnvironment.setId("other-env"); + otherEnvironment.setApplicationId("app"); + when(environmentStore.get("other-env")).thenReturn(Optional.of(otherEnvironment)); + deploymentStore.put("app::env::1", deployment("app", "env", 1)); + deploymentStore.put("app::other-env::2", deployment("app", "other-env", 2)); + deploymentStore.put("app::env::3", deployment("app", "env", 3)); + + service.deleteEnvironment("app", "other-env"); + Deployment deployment = service.startDeployment("app", "env", Map.of( + "ConfigurationProfileId", "profile", + "ConfigurationVersion", "version", + "DeploymentStrategyId", "AppConfig.AllAtOnce")); + + assertEquals(4, deployment.getDeploymentNumber()); + assertEquals(Set.of("app::env::1", "app::env::3", "app::env::4"), deploymentStore.keys()); + } + + @Test + void concurrentEnvironmentDeletesDoNotBothSucceed() throws Exception { + AtomicReference stored = new AtomicReference<>(environmentStore.get("env").orElseThrow()); + CountDownLatch reads = new CountDownLatch(2); + when(environmentStore.get("env")).thenAnswer(invocation -> { + Environment environment = stored.get(); + reads.countDown(); + reads.await(200, TimeUnit.MILLISECONDS); + return Optional.ofNullable(environment); + }); + doAnswer(invocation -> { + stored.set(null); + return null; + }).when(environmentStore).delete("env"); + + ExecutorService executor = Executors.newFixedThreadPool(2); + try { + Future first = executor.submit(this::deleteEnvironment); + Future second = executor.submit(this::deleteEnvironment); + long successes = List.of(first.get(), second.get()).stream() + .filter(Boolean::booleanValue) + .count(); + assertEquals(1, successes); + } finally { + executor.shutdownNow(); + } + } + + private boolean deleteEnvironment() { + try { + service.deleteEnvironment("app", "env"); + return true; + } catch (AwsException e) { + return false; + } + } + private static Deployment deployment(String applicationId, String environmentId, int number) { Deployment deployment = new Deployment(); deployment.setApplicationId(applicationId);