Light, fluffy, and always free - The AWS Local Emulator alternative
fix(appconfig): accept names in management APIs (#5417)
Aniket Shukla4ff263271c18
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.javaindex 5cb25d31ef..62e9cf77c3 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@@ -61,11 +61,11 @@ public class AppConfigService {return app;}- public Application getApplication(String id) {- return applicationStore.get(id).orElseThrow(() -> new AwsException("ResourceNotFoundException", "Application not found", 404));+ public Application getApplication(String idOrName) {+ return resolveApplication(idOrName);}- /** The application whose ID or name is {@code idOrName}; the AppConfigData APIs accept either. */+ /** The application whose ID or name is {@code idOrName}. */public Application resolveApplication(String idOrName) {return applicationStore.get(idOrName).or(() -> listApplications().stream().filter(a -> idOrName.equals(a.getName())).findFirst())@@ -76,14 +76,16 @@ public class AppConfigService {return applicationStore.scan(k -> true);}- public void deleteApplication(String id) {- applicationStore.delete(id);+ public void deleteApplication(String idOrName) {+ applicationStore.get(idOrName)+ .or(() -> listApplications().stream().filter(a -> idOrName.equals(a.getName())).findFirst())+ .ifPresent(application -> applicationStore.delete(application.getId()));}// ──────────────────────────── Environment ────────────────────────────- public Environment createEnvironment(String appId, Map<String, Object> request) {- getApplication(appId);+ public Environment createEnvironment(String applicationIdentifier, Map<String, Object> request) {+ String appId = resolveApplication(applicationIdentifier).getId();Environment env = new Environment();env.setId(shortId(7));env.setApplicationId(appId);@@ -94,10 +96,9 @@ public class AppConfigService {return env;}- public Environment getEnvironment(String appId, String envId) {- Environment env = environmentStore.get(envId).orElseThrow(() -> new AwsException("ResourceNotFoundException", "Environment not found", 404));- if (!env.getApplicationId().equals(appId)) throw new AwsException("ResourceNotFoundException", "Environment not found in this application", 404);- return env;+ public Environment getEnvironment(String applicationIdentifier, String environmentIdentifier) {+ String appId = resolveApplication(applicationIdentifier).getId();+ return resolveEnvironment(appId, environmentIdentifier);}public synchronized void deleteEnvironment(String applicationIdentifier, String environmentIdentifier) {@@ -123,7 +124,8 @@ public class AppConfigService {.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Environment not found", 404));}- public List<Environment> listEnvironments(String appId) {+ public List<Environment> listEnvironments(String applicationIdentifier) {+ String appId = resolveApplication(applicationIdentifier).getId();return environmentStore.scan(k -> true).stream().filter(e -> e.getApplicationId().equals(appId)).toList();@@ -131,8 +133,8 @@ public class AppConfigService {// ──────────────────────────── Configuration Profile ────────────────────────────- public ConfigurationProfile createConfigurationProfile(String appId, Map<String, Object> request) {- getApplication(appId);+ public ConfigurationProfile createConfigurationProfile(String applicationIdentifier, Map<String, Object> request) {+ String appId = resolveApplication(applicationIdentifier).getId();ConfigurationProfile profile = new ConfigurationProfile();profile.setId(shortId(7));profile.setApplicationId(appId);@@ -144,10 +146,9 @@ public class AppConfigService {return profile;}- public ConfigurationProfile getConfigurationProfile(String appId, String profileId) {- ConfigurationProfile profile = profileStore.get(profileId).orElseThrow(() -> new AwsException("ResourceNotFoundException", "Configuration profile not found", 404));- if (!profile.getApplicationId().equals(appId)) throw new AwsException("ResourceNotFoundException", "Profile not found in this application", 404);- return profile;+ public ConfigurationProfile getConfigurationProfile(String applicationIdentifier, String profileIdentifier) {+ String appId = resolveApplication(applicationIdentifier).getId();+ return resolveConfigurationProfile(appId, profileIdentifier);}/** The configuration profile of {@code appId} whose ID or name is {@code idOrName}. */@@ -158,23 +159,35 @@ public class AppConfigService {.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Configuration profile not found", 404));}- public List<ConfigurationProfile> listConfigurationProfiles(String appId) {+ public List<ConfigurationProfile> listConfigurationProfiles(String applicationIdentifier) {+ String appId = resolveApplication(applicationIdentifier).getId();return profileStore.scan(k -> true).stream().filter(p -> p.getApplicationId().equals(appId)).toList();}- public void deleteConfigurationProfile(String appId, String profileId) {+ public void deleteConfigurationProfile(String applicationIdentifier, String profileIdentifier) {+ String appId = resolveApplication(applicationIdentifier).getId();// Unlike deleteApplication (a single, unscoped ID), a profile is nested under an// application - a mismatched appId must not be able to delete another application's// profile just because its bare profileId is guessed/known. A profileId that doesn't// exist at all is still an idempotent no-op, matching deleteApplication's convention;// only an existing-but-wrongly-scoped one is rejected.- profileStore.get(profileId).ifPresent(profile -> {- if (!profile.getApplicationId().equals(appId)) {- throw new AwsException("ResourceNotFoundException", "Configuration profile not found in this application", 404);- }- });+ ConfigurationProfile exactProfile = profileStore.get(profileIdentifier).orElse(null);+ ConfigurationProfile profile = exactProfile != null && appId.equals(exactProfile.getApplicationId())+ ? exactProfile+ : listConfigurationProfiles(appId).stream()+ .filter(candidate -> profileIdentifier.equals(candidate.getName()))+ .findFirst()+ .orElse(null);+ if (profile == null && exactProfile != null) {+ throw new AwsException("ResourceNotFoundException",+ "Configuration profile not found in this application", 404);+ }+ if (profile == null) {+ return;+ }+ String profileId = profile.getId();// 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.@@ -184,8 +197,13 @@ public class AppConfigService {// ──────────────────────────── Hosted Configuration Version ────────────────────────────- public HostedConfigurationVersion createHostedConfigurationVersion(String appId, String profileId, byte[] content, String contentType, String description) {- getConfigurationProfile(appId, profileId);+ public HostedConfigurationVersion createHostedConfigurationVersion(String applicationIdentifier,+ String profileIdentifier,+ byte[] content,+ String contentType,+ String description) {+ String appId = resolveApplication(applicationIdentifier).getId();+ String profileId = resolveConfigurationProfile(appId, profileIdentifier).getId();String prefix = appId + "::" + profileId + "::";int nextVersion = versionStore.scan(k -> k.startsWith(prefix)).stream().mapToInt(HostedConfigurationVersion::getVersionNumber).max().orElse(0) + 1;@@ -202,12 +220,19 @@ public class AppConfigService {return version;}- public HostedConfigurationVersion getHostedConfigurationVersion(String appId, String profileId, int versionNumber) {+ public HostedConfigurationVersion getHostedConfigurationVersion(String applicationIdentifier,+ String profileIdentifier,+ int versionNumber) {+ String appId = resolveApplication(applicationIdentifier).getId();+ String profileId = resolveConfigurationProfile(appId, profileIdentifier).getId();return versionStore.get(appId + "::" + profileId + "::" + versionNumber).orElseThrow(() -> new AwsException("ResourceNotFoundException", "Hosted configuration version not found", 404));}- public List<HostedConfigurationVersionSummary> listHostedConfigurationVersions(String appId, String profileId) {+ public List<HostedConfigurationVersionSummary> listHostedConfigurationVersions(String applicationIdentifier,+ String profileIdentifier) {+ String appId = resolveApplication(applicationIdentifier).getId();+ String profileId = resolveConfigurationProfile(appId, profileIdentifier).getId();String prefix = appId + "::" + profileId + "::";return versionStore.scan(k -> k.startsWith(prefix)).stream()@@ -224,7 +249,11 @@ public class AppConfigService {.toList();}- public void deleteHostedConfigurationVersion(String appId, String profileId, int versionNumber) {+ public void deleteHostedConfigurationVersion(String applicationIdentifier,+ String profileIdentifier,+ int versionNumber) {+ String appId = resolveApplication(applicationIdentifier).getId();+ String profileId = resolveConfigurationProfile(appId, profileIdentifier).getId();versionStore.delete(appId + "::" + profileId + "::" + versionNumber);}@@ -310,13 +339,16 @@ public class AppConfigService {// ──────────────────────────── Deployment ────────────────────────────- public synchronized Deployment startDeployment(String appId, String envId, Map<String, Object> request) {- getEnvironment(appId, envId);- String profileId = (String) request.get("ConfigurationProfileId");+ public synchronized Deployment startDeployment(String applicationIdentifier,+ String environmentIdentifier,+ Map<String, Object> request) {+ String appId = resolveApplication(applicationIdentifier).getId();+ String envId = resolveEnvironment(appId, environmentIdentifier).getId();+ String profileId = resolveConfigurationProfile(+ appId, (String) request.get("ConfigurationProfileId")).getId();String version = (String) request.get("ConfigurationVersion");String strategyId = (String) request.get("DeploymentStrategyId");- getConfigurationProfile(appId, profileId);getDeploymentStrategy(strategyId);Deployment deployment = new Deployment();@@ -342,14 +374,21 @@ public class AppConfigService {return deployment;}- public Deployment getDeployment(String appId, String envId, int deploymentNumber) {- getEnvironment(appId, envId);+ public Deployment getDeployment(String applicationIdentifier,+ String environmentIdentifier,+ int deploymentNumber) {+ String appId = resolveApplication(applicationIdentifier).getId();+ String envId = resolveEnvironment(appId, environmentIdentifier).getId();return deploymentStore.get(appId + "::" + envId + "::" + deploymentNumber).orElseThrow(() -> new AwsException("ResourceNotFoundException", "Deployment not found", 404));}- public DeploymentPage listDeployments(String appId, String envId, Integer maxResults, String nextToken) {- getEnvironment(appId, envId);+ public DeploymentPage listDeployments(String applicationIdentifier,+ String environmentIdentifier,+ Integer maxResults,+ String nextToken) {+ String appId = resolveApplication(applicationIdentifier).getId();+ String envId = resolveEnvironment(appId, environmentIdentifier).getId();int pageSize = maxResults == null ? 50 : maxResults;if (pageSize < 1 || pageSize > 50) {throw new AwsException("BadRequestException", "max_results must be between 1 and 50", 400);@@ -442,13 +481,13 @@ public class AppConfigService {public void tagApplication(String appId, Map<String, String> tags) {Application app = getApplication(appId);app.getTags().putAll(tags);- applicationStore.put(appId, app);+ applicationStore.put(app.getId(), app);}public void untagApplication(String appId, List<String> tagKeys) {Application app = getApplication(appId);tagKeys.forEach(app.getTags()::remove);- applicationStore.put(appId, app);+ applicationStore.put(app.getId(), app);}private static String shortId(int length) {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.javaindex 2f783b4fbd..860d1fd788 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@@ -868,16 +868,18 @@ class AppConfigIntegrationTest {@Test @Order(46)void startConfigurationSessionPrefersIdOverAnotherResourcesName() {// A second environment and profile named after the first ones' IDs: the ID must still select the original.- given()+ String decoyEnvironmentId = given().contentType(ContentType.JSON).body("{\"Name\": \"" + envId + "\"}").when().post("/applications/" + appId + "/environments")- .then().statusCode(201);- given()+ .then().statusCode(201)+ .extract().path("Id");+ String decoyProfileId = given().contentType(ContentType.JSON).body("{\"Name\": \"" + profileId + "\", \"LocationUri\": \"hosted\", \"Type\": \"AWS.Freeform\"}").when().post("/applications/" + appId + "/configurationprofiles")- .then().statusCode(201);+ .then().statusCode(201)+ .extract().path("Id");String token = given().contentType(ContentType.JSON)@@ -893,6 +895,13 @@ class AppConfigIntegrationTest {.then().statusCode(200).body("foo", notNullValue());++ given()+ .when().delete("/applications/" + appId + "/environments/" + decoyEnvironmentId)+ .then().statusCode(204);+ given()+ .when().delete("/applications/" + appId + "/configurationprofiles/" + decoyProfileId)+ .then().statusCode(204);}@Test @Order(47)diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigNameIdentifiersIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigNameIdentifiersIntegrationTest.javanew file mode 100644index 0000000000..29f56c0a6e--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigNameIdentifiersIntegrationTest.java@@ -0,0 +1,183 @@+package io.github.hectorvent.floci.services.appconfig;++import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;+import io.quarkus.test.junit.QuarkusTest;+import io.restassured.http.ContentType;+import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.Test;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.equalTo;+import static org.hamcrest.Matchers.hasItem;++@QuarkusTest+class AppConfigNameIdentifiersIntegrationTest {++ private static final String APPLICATION_NAME = "management-name-app";+ private static final String ENVIRONMENT_NAME = "management-name-env";+ private static final String PROFILE_NAME = "management-name-profile";++ @BeforeAll+ static void setup() {+ RestAssuredJsonUtils.configureAwsContentTypes();+ }++ @Test+ void managementOperationsAcceptResourceNames() {+ String applicationId = given()+ .contentType(ContentType.JSON)+ .body("{\"Name\":\"" + APPLICATION_NAME + "\"}")+ .when().post("/applications")+ .then().statusCode(201)+ .extract().path("Id");++ given()+ .when().get("/applications/" + APPLICATION_NAME)+ .then().statusCode(200)+ .body("Id", equalTo(applicationId));++ String environmentId = given()+ .contentType(ContentType.JSON)+ .body("{\"Name\":\"" + ENVIRONMENT_NAME + "\"}")+ .when().post("/applications/" + APPLICATION_NAME + "/environments")+ .then().statusCode(201)+ .body("ApplicationId", equalTo(applicationId))+ .extract().path("Id");++ given()+ .when().get("/applications/" + APPLICATION_NAME + "/environments/" + ENVIRONMENT_NAME)+ .then().statusCode(200)+ .body("Id", equalTo(environmentId));++ given()+ .when().get("/applications/" + APPLICATION_NAME + "/environments")+ .then().statusCode(200)+ .body("Items.Id", hasItem(environmentId));++ String profileId = given()+ .contentType(ContentType.JSON)+ .body("{\"Name\":\"" + PROFILE_NAME+ + "\",\"LocationUri\":\"hosted\",\"Type\":\"AWS.Freeform\"}")+ .when().post("/applications/" + APPLICATION_NAME + "/configurationprofiles")+ .then().statusCode(201)+ .body("ApplicationId", equalTo(applicationId))+ .extract().path("Id");++ given()+ .when().get("/applications/" + APPLICATION_NAME + "/configurationprofiles/" + PROFILE_NAME)+ .then().statusCode(200)+ .body("Id", equalTo(profileId));++ given()+ .when().get("/applications/" + APPLICATION_NAME + "/configurationprofiles")+ .then().statusCode(200)+ .body("Items.Id", hasItem(profileId));++ given()+ .header("Content-Type", "application/json")+ .body("{\"enabled\":true}")+ .when().post("/applications/" + APPLICATION_NAME + "/configurationprofiles/"+ + PROFILE_NAME + "/hostedconfigurationversions")+ .then().statusCode(201)+ .header("Application-Id", equalTo(applicationId))+ .header("Configuration-Profile-Id", equalTo(profileId))+ .header("Version-Number", equalTo("1"));++ given()+ .when().get("/applications/" + APPLICATION_NAME + "/configurationprofiles/"+ + PROFILE_NAME + "/hostedconfigurationversions/1")+ .then().statusCode(200)+ .header("Application-Id", equalTo(applicationId))+ .header("Configuration-Profile-Id", equalTo(profileId));++ given()+ .when().get("/applications/" + APPLICATION_NAME + "/configurationprofiles/"+ + PROFILE_NAME + "/hostedconfigurationversions")+ .then().statusCode(200)+ .body("Items[0].ApplicationId", equalTo(applicationId))+ .body("Items[0].ConfigurationProfileId", equalTo(profileId));++ int deploymentNumber = given()+ .contentType(ContentType.JSON)+ .body("{\"ConfigurationProfileId\":\"" + PROFILE_NAME+ + "\",\"ConfigurationVersion\":\"1\","+ + "\"DeploymentStrategyId\":\"AppConfig.AllAtOnce\"}")+ .when().post("/applications/" + APPLICATION_NAME + "/environments/"+ + ENVIRONMENT_NAME + "/deployments")+ .then().statusCode(201)+ .body("ApplicationId", equalTo(applicationId))+ .body("EnvironmentId", equalTo(environmentId))+ .body("ConfigurationProfileId", equalTo(profileId))+ .extract().path("DeploymentNumber");++ given()+ .when().get("/applications/" + APPLICATION_NAME + "/environments/"+ + ENVIRONMENT_NAME + "/deployments/" + deploymentNumber)+ .then().statusCode(200)+ .body("ApplicationId", equalTo(applicationId))+ .body("EnvironmentId", equalTo(environmentId));++ given()+ .when().get("/applications/" + APPLICATION_NAME + "/environments/"+ + ENVIRONMENT_NAME + "/deployments")+ .then().statusCode(200)+ .body("Items.DeploymentNumber", hasItem(deploymentNumber));++ given()+ .when().delete("/applications/" + APPLICATION_NAME + "/configurationprofiles/"+ + PROFILE_NAME + "/hostedconfigurationversions/1")+ .then().statusCode(204);++ given()+ .when().delete("/applications/" + APPLICATION_NAME + "/configurationprofiles/" + PROFILE_NAME)+ .then().statusCode(204);++ given()+ .when().delete("/applications/" + APPLICATION_NAME)+ .then().statusCode(204);++ given()+ .when().get("/applications/" + applicationId)+ .then().statusCode(404);+ }++ @Test+ void deleteProfileByNameIgnoresAnotherApplicationsMatchingProfileId() {+ String applicationId = given()+ .contentType(ContentType.JSON)+ .body("{\"Name\":\"profile-collision-target-app\"}")+ .when().post("/applications")+ .then().statusCode(201)+ .extract().path("Id");+ String otherApplicationId = given()+ .contentType(ContentType.JSON)+ .body("{\"Name\":\"profile-collision-other-app\"}")+ .when().post("/applications")+ .then().statusCode(201)+ .extract().path("Id");+ String otherProfileId = given()+ .contentType(ContentType.JSON)+ .body("{\"Name\":\"other-profile\",\"LocationUri\":\"hosted\",\"Type\":\"AWS.Freeform\"}")+ .when().post("/applications/" + otherApplicationId + "/configurationprofiles")+ .then().statusCode(201)+ .extract().path("Id");+ String targetProfileId = given()+ .contentType(ContentType.JSON)+ .body("{\"Name\":\"" + otherProfileId+ + "\",\"LocationUri\":\"hosted\",\"Type\":\"AWS.Freeform\"}")+ .when().post("/applications/" + applicationId + "/configurationprofiles")+ .then().statusCode(201)+ .extract().path("Id");++ given()+ .when().delete("/applications/" + applicationId + "/configurationprofiles/" + otherProfileId)+ .then().statusCode(204);++ given()+ .when().get("/applications/" + applicationId + "/configurationprofiles/" + targetProfileId)+ .then().statusCode(404);+ given()+ .when().get("/applications/" + otherApplicationId + "/configurationprofiles/" + otherProfileId)+ .then().statusCode(200);+ }+}