Light, fluffy, and always free - The AWS Local Emulator alternative
fix(elasticbeanstalk): persist applications, versions, and environments across restart (#1729)
Hector Ventura068b76a195e9
diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.javaindex 801cb4ef22..a6f851155d 100644--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java@@ -210,6 +210,7 @@ public interface EmulatorConfig {CodeDeployStorageConfig codedeploy();TranscribeStorageConfig transcribe();TaggingStorageConfig tagging();+ ElasticBeanstalkStorageConfig elasticbeanstalk();}interface SsmStorageConfig {@@ -397,6 +398,13 @@ public interface EmulatorConfig {long flushIntervalMs();}+ interface ElasticBeanstalkStorageConfig {+ Optional<String> mode();++ @WithDefault("5000")+ long flushIntervalMs();+ }+interface CodeDeployStorageConfig {Optional<String> mode();diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.javaindex 3c61bf1f71..fc29b43052 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java@@ -299,7 +299,9 @@ public class ResolvedServiceCatalog {Set.of(), Set.of("autoscaling"), Set.of(), Set.of()),descriptor("elasticbeanstalk", "elasticbeanstalk",config.services().elasticbeanstalk().enabled(), true,- "elasticbeanstalk", config.storage().mode(), 5000L,+ "elasticbeanstalk",+ storageMode(config.storage().services().elasticbeanstalk().mode(), config.storage().mode()),+ config.storage().services().elasticbeanstalk().flushIntervalMs(),AwsNamespaces.ELASTIC_BEANSTALK, ServiceProtocol.QUERY,protocols(ServiceProtocol.QUERY),Set.of(), Set.of("elasticbeanstalk"), Set.of(), Set.of()),diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/ElasticBeanstalkService.java b/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/ElasticBeanstalkService.javaindex 59f5b45da0..26249ceaa6 100644--- a/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/ElasticBeanstalkService.java+++ b/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/ElasticBeanstalkService.java@@ -1,11 +1,16 @@package io.github.hectorvent.floci.services.elasticbeanstalk;+import com.fasterxml.jackson.core.type.TypeReference;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.RegionResolver;+import io.github.hectorvent.floci.core.common.Resettable;+import io.github.hectorvent.floci.core.storage.StorageBackedMap;+import io.github.hectorvent.floci.core.storage.StorageFactory;import io.github.hectorvent.floci.services.elasticbeanstalk.model.BeanstalkApplication;import io.github.hectorvent.floci.services.elasticbeanstalk.model.BeanstalkApplicationVersion;import io.github.hectorvent.floci.services.elasticbeanstalk.model.BeanstalkEnvironment;import io.github.hectorvent.floci.services.elasticbeanstalk.model.ConfigurationOptionSetting;+import jakarta.annotation.PostConstruct;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;@@ -22,7 +27,7 @@ import java.util.Set;import java.util.concurrent.ConcurrentHashMap;@ApplicationScoped-public class ElasticBeanstalkService {+public class ElasticBeanstalkService implements Resettable {private static final SecureRandom RANDOM = new SecureRandom();private static final String LOWER_ALNUM = "abcdefghijklmnopqrstuvwxyz0123456789";@@ -30,16 +35,44 @@ public class ElasticBeanstalkService {"64bit Amazon Linux 2023 v4.3.0 running Docker";private final RegionResolver regionResolver;- private final Map<String, BeanstalkApplication> applications = new ConcurrentHashMap<>();- private final Map<String, BeanstalkApplicationVersion> versions = new ConcurrentHashMap<>();- private final Map<String, BeanstalkEnvironment> environments = new ConcurrentHashMap<>();+ private final StorageFactory storageFactory;+ private Map<String, BeanstalkApplication> applications = new ConcurrentHashMap<>();+ private Map<String, BeanstalkApplicationVersion> versions = new ConcurrentHashMap<>();+ private Map<String, BeanstalkEnvironment> environments = new ConcurrentHashMap<>();@Inject- ElasticBeanstalkService(RegionResolver regionResolver) {+ ElasticBeanstalkService(RegionResolver regionResolver, StorageFactory storageFactory) {this.regionResolver = regionResolver;+ this.storageFactory = storageFactory;}- public BeanstalkApplication createApplication(String region, String name, String description,+ @PostConstruct+ void initializeStorage() {+ if (storageFactory == null) {+ return; // keeps non-CDI unit tests working+ }+ this.applications = new StorageBackedMap<>(storageFactory.create("elasticbeanstalk",+ "elasticbeanstalk-applications.json",+ new TypeReference<Map<String, BeanstalkApplication>>() {}));+ this.versions = new StorageBackedMap<>(storageFactory.create("elasticbeanstalk",+ "elasticbeanstalk-versions.json",+ new TypeReference<Map<String, BeanstalkApplicationVersion>>() {}));+ this.environments = new StorageBackedMap<>(storageFactory.create("elasticbeanstalk",+ "elasticbeanstalk-environments.json",+ new TypeReference<Map<String, BeanstalkEnvironment>>() {}));+ }++ @Override+ public void clear() {+ applications.clear();+ versions.clear();+ environments.clear();+ }++ // Mutators are synchronized: StorageBackedMap has no atomic check-then-put, and+ // persistent backends return fresh copies on get, so every in-place mutation must be+ // re-put and the get-mutate-put sequences must not interleave.+ public synchronized BeanstalkApplication createApplication(String region, String name, String description,Map<String, String> tags) {requireName(name, "ApplicationName");String key = appKey(region, name);@@ -78,16 +111,17 @@ public class ElasticBeanstalkService {.toList();}- public BeanstalkApplication updateApplication(String region, String name, String description) {+ public synchronized BeanstalkApplication updateApplication(String region, String name, String description) {BeanstalkApplication app = requireApplication(region, name);if (description != null) {app.setDescription(description);}app.setDateUpdated(Instant.now());+ applications.put(appKey(region, name), app);return app;}- public void deleteApplication(String region, String name, boolean terminateEnvByForce) {+ public synchronized void deleteApplication(String region, String name, boolean terminateEnvByForce) {requireApplication(region, name);List<BeanstalkEnvironment> activeEnvironments = environments.entrySet().stream().filter(e -> e.getKey().startsWith(region + "::"))@@ -106,7 +140,7 @@ public class ElasticBeanstalkService {versions.entrySet().removeIf(e -> e.getKey().startsWith(versionPrefix(region, name)));}- public BeanstalkApplicationVersion createApplicationVersion(String region, String applicationName,+ public synchronized BeanstalkApplicationVersion createApplicationVersion(String region, String applicationName,String versionLabel, String description,boolean autoCreateApplication,String sourceBundleBucket,@@ -141,6 +175,7 @@ public class ElasticBeanstalkService {versions.put(key, version);app.getVersions().add(versionLabel);app.setDateUpdated(now);+ applications.put(appKey(region, applicationName), app);return version;}@@ -157,7 +192,7 @@ public class ElasticBeanstalkService {.toList();}- public void deleteApplicationVersion(String region, String applicationName, String versionLabel) {+ public synchronized void deleteApplicationVersion(String region, String applicationName, String versionLabel) {requireName(applicationName, "ApplicationName");requireName(versionLabel, "VersionLabel");BeanstalkApplicationVersion removed = versions.remove(versionKey(region, applicationName, versionLabel));@@ -169,10 +204,11 @@ public class ElasticBeanstalkService {if (app != null) {app.getVersions().remove(versionLabel);app.setDateUpdated(Instant.now());+ applications.put(appKey(region, applicationName), app);}}- public BeanstalkEnvironment createEnvironment(String region, String applicationName, String environmentName,+ public synchronized BeanstalkEnvironment createEnvironment(String region, String applicationName, String environmentName,String description, String cnamePrefix, String solutionStackName,String platformArn, String templateName, String versionLabel,List<ConfigurationOptionSetting> optionSettings,@@ -236,7 +272,7 @@ public class ElasticBeanstalkService {.toList();}- public BeanstalkEnvironment updateEnvironment(String region, String environmentName, String environmentId,+ public synchronized BeanstalkEnvironment updateEnvironment(String region, String environmentName, String environmentId,String description, String versionLabel,String solutionStackName, String platformArn,List<ConfigurationOptionSetting> optionSettings) {@@ -268,15 +304,17 @@ public class ElasticBeanstalkService {env.setHealth("Green");env.setHealthStatus("Ok");env.setDateUpdated(Instant.now());+ environments.put(envKey(region, env.getEnvironmentName()), env);return env;}- public BeanstalkEnvironment terminateEnvironment(String region, String environmentName, String environmentId) {+ public synchronized BeanstalkEnvironment terminateEnvironment(String region, String environmentName, String environmentId) {BeanstalkEnvironment env = requireEnvironment(region, environmentName, environmentId);env.setStatus("Terminated");env.setHealth("Grey");env.setHealthStatus("NoData");env.setDateUpdated(Instant.now());+ environments.put(envKey(region, env.getEnvironmentName()), env);return env;}diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/model/BeanstalkApplication.java b/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/model/BeanstalkApplication.javaindex 52c2dfb4d9..dbcdc35a13 100644--- a/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/model/BeanstalkApplication.java+++ b/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/model/BeanstalkApplication.java@@ -1,11 +1,14 @@package io.github.hectorvent.floci.services.elasticbeanstalk.model;+import io.quarkus.runtime.annotations.RegisterForReflection;+import java.time.Instant;import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;+@RegisterForReflectionpublic class BeanstalkApplication {private String applicationName;diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/model/BeanstalkApplicationVersion.java b/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/model/BeanstalkApplicationVersion.javaindex 21c8c7d951..c5ca1cf5e8 100644--- a/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/model/BeanstalkApplicationVersion.java+++ b/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/model/BeanstalkApplicationVersion.java@@ -1,9 +1,12 @@package io.github.hectorvent.floci.services.elasticbeanstalk.model;+import io.quarkus.runtime.annotations.RegisterForReflection;+import java.time.Instant;import java.util.LinkedHashMap;import java.util.Map;+@RegisterForReflectionpublic class BeanstalkApplicationVersion {private String applicationName;diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/model/BeanstalkEnvironment.java b/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/model/BeanstalkEnvironment.javaindex c62895b20f..24092e3b74 100644--- a/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/model/BeanstalkEnvironment.java+++ b/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/model/BeanstalkEnvironment.java@@ -1,11 +1,14 @@package io.github.hectorvent.floci.services.elasticbeanstalk.model;+import io.quarkus.runtime.annotations.RegisterForReflection;+import java.time.Instant;import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;+@RegisterForReflectionpublic class BeanstalkEnvironment {private String applicationName;diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/model/ConfigurationOptionSetting.java b/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/model/ConfigurationOptionSetting.javaindex d471dcc54b..ca6d8a1856 100644--- a/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/model/ConfigurationOptionSetting.java+++ b/src/main/java/io/github/hectorvent/floci/services/elasticbeanstalk/model/ConfigurationOptionSetting.java@@ -1,5 +1,8 @@package io.github.hectorvent.floci.services.elasticbeanstalk.model;+import io.quarkus.runtime.annotations.RegisterForReflection;++@RegisterForReflectionpublic class ConfigurationOptionSetting {private String namespace;diff --git a/src/main/resources/application.yml b/src/main/resources/application.ymlindex e0ba978800..cfaedadee6 100644--- a/src/main/resources/application.yml+++ b/src/main/resources/application.yml@@ -111,6 +111,8 @@ floci:flush-interval-ms: 5000tagging:flush-interval-ms: 5000+ elasticbeanstalk:+ flush-interval-ms: 5000rds:# Override storage mode for RDS (memory = no Docker volumes; hybrid/persistent = named volume per instance)# mode: memorydiff --git a/src/test/java/io/github/hectorvent/floci/services/elasticbeanstalk/ElasticBeanstalkServicePersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticbeanstalk/ElasticBeanstalkServicePersistenceTest.javanew file mode 100644index 0000000000..5c408dc572--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/elasticbeanstalk/ElasticBeanstalkServicePersistenceTest.java@@ -0,0 +1,127 @@+package io.github.hectorvent.floci.services.elasticbeanstalk;++import com.fasterxml.jackson.core.type.TypeReference;+import io.github.hectorvent.floci.core.common.RegionResolver;+import io.github.hectorvent.floci.core.storage.InMemoryStorage;+import io.github.hectorvent.floci.core.storage.StorageBackend;+import io.github.hectorvent.floci.core.storage.StorageFactory;+import io.github.hectorvent.floci.services.elasticbeanstalk.model.BeanstalkApplication;+import io.github.hectorvent.floci.services.elasticbeanstalk.model.BeanstalkApplicationVersion;+import io.github.hectorvent.floci.services.elasticbeanstalk.model.BeanstalkEnvironment;+import org.junit.jupiter.api.Test;++import java.util.HashMap;+import java.util.List;+import java.util.Map;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertTrue;++/**+ * Verifies Elastic Beanstalk applications, versions, and environments survive a restart.+ * Two service instances share the same {@link StorageFactory} backend; the second simulates+ * a process restart reloading from disk.+ */+class ElasticBeanstalkServicePersistenceTest {++ private static final String REGION = "us-east-1";++ @Test+ void applicationsVersionsAndEnvironmentsSurviveRestart() {+ SharedStorageFactory storage = new SharedStorageFactory();++ ElasticBeanstalkService first = serviceWithStorage(storage);+ first.createApplication(REGION, "shop", "storefront", Map.of("team", "web"));+ first.createApplicationVersion(REGION, "shop", "v1", null, false, "bucket", "key", Map.of());+ BeanstalkEnvironment created = first.createEnvironment(REGION, "shop", "shop-prod",+ null, null, null, null, null, "v1", List.of(), Map.of());++ ElasticBeanstalkService reloaded = serviceWithStorage(storage);++ List<BeanstalkApplication> apps = List.copyOf(reloaded.describeApplications(REGION, null));+ assertEquals(1, apps.size());+ assertEquals("shop", apps.getFirst().getApplicationName());+ assertEquals(List.of("v1"), apps.getFirst().getVersions());+ assertEquals(Map.of("team", "web"), apps.getFirst().getTags());++ List<BeanstalkApplicationVersion> versions =+ List.copyOf(reloaded.describeApplicationVersions(REGION, "shop", null));+ assertEquals(1, versions.size());+ assertEquals("v1", versions.getFirst().getVersionLabel());+ assertEquals("bucket", versions.getFirst().getSourceBundleBucket());++ List<BeanstalkEnvironment> envs = List.copyOf(+ reloaded.describeEnvironments(REGION, "shop", null, null, null, false));+ assertEquals(1, envs.size());+ assertEquals("shop-prod", envs.getFirst().getEnvironmentName());+ assertEquals(created.getEnvironmentId(), envs.getFirst().getEnvironmentId());+ assertEquals("Ready", envs.getFirst().getStatus());+ }++ @Test+ void updatesAndTerminationSurviveRestart() {+ SharedStorageFactory storage = new SharedStorageFactory();++ ElasticBeanstalkService first = serviceWithStorage(storage);+ first.createApplication(REGION, "api", null, Map.of());+ first.updateApplication(REGION, "api", "updated description");+ first.createEnvironment(REGION, "api", "api-prod",+ null, null, null, null, null, null, List.of(), Map.of());+ first.terminateEnvironment(REGION, "api-prod", null);++ ElasticBeanstalkService reloaded = serviceWithStorage(storage);++ List<BeanstalkApplication> apps = List.copyOf(reloaded.describeApplications(REGION, List.of("api")));+ assertEquals("updated description", apps.getFirst().getDescription());++ // Terminated environments are excluded by default but visible with includeDeleted.+ assertTrue(reloaded.describeEnvironments(REGION, "api", null, null, null, false).isEmpty());+ List<BeanstalkEnvironment> all = List.copyOf(+ reloaded.describeEnvironments(REGION, "api", null, null, null, true));+ assertEquals(1, all.size());+ assertEquals("Terminated", all.getFirst().getStatus());+ }++ @Test+ void deletionsDoNotReappearAfterRestart() {+ SharedStorageFactory storage = new SharedStorageFactory();++ ElasticBeanstalkService first = serviceWithStorage(storage);+ first.createApplication(REGION, "batch", null, Map.of());+ first.createApplicationVersion(REGION, "batch", "v1", null, false, null, null, Map.of());+ first.deleteApplicationVersion(REGION, "batch", "v1");+ first.createApplication(REGION, "doomed", null, Map.of());+ first.deleteApplication(REGION, "doomed", false);++ ElasticBeanstalkService reloaded = serviceWithStorage(storage);++ assertTrue(reloaded.describeApplicationVersions(REGION, "batch", null).isEmpty());+ List<BeanstalkApplication> apps = List.copyOf(reloaded.describeApplications(REGION, null));+ assertEquals(1, apps.size());+ assertEquals("batch", apps.getFirst().getApplicationName());+ assertTrue(apps.getFirst().getVersions().isEmpty());+ }++ private static ElasticBeanstalkService serviceWithStorage(StorageFactory storage) {+ ElasticBeanstalkService service = new ElasticBeanstalkService(+ new RegionResolver("us-east-1", "000000000000"), storage);+ service.initializeStorage();+ return service;+ }++ private static final class SharedStorageFactory extends StorageFactory {+ private final Map<String, StorageBackend<String, ?>> stores = new HashMap<>();++ private SharedStorageFactory() {+ super(null, null);+ }++ @Override+ @SuppressWarnings("unchecked")+ public <V> StorageBackend<String, V> create(String serviceName,+ String fileName,+ TypeReference<Map<String, V>> typeReference) {+ return (StorageBackend<String, V>) stores.computeIfAbsent(fileName, ignored -> new InMemoryStorage<>());+ }+ }+}diff --git a/src/test/resources/application.yml b/src/test/resources/application.ymlindex a1d33b4b4e..a3810bbfd6 100644--- a/src/test/resources/application.yml+++ b/src/test/resources/application.yml@@ -54,6 +54,8 @@ floci:flush-interval-ms: 60000tagging:flush-interval-ms: 60000+ elasticbeanstalk:+ flush-interval-ms: 60000cloudfront:auth: