Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(cloudcontrol): move library usages to imports (#3902)
Vũ Thế Huy44fa11448386
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlService.java b/src/main/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlService.javaindex 2d56fd4235..f34488c9d2 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlService.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlService.java@@ -1,22 +1,22 @@package io.github.hectorvent.floci.services.cloudcontrol;import com.fasterxml.jackson.core.JsonProcessingException;+import com.fasterxml.jackson.core.type.TypeReference;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.ArrayNode;import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.RequestScopes;import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;import io.github.hectorvent.floci.core.storage.StorageFactory;-import com.fasterxml.jackson.core.type.TypeReference;import io.github.hectorvent.floci.services.cloudformation.CloudFormationResourceProvisioner;-import io.quarkus.runtime.annotations.RegisterForReflection;+import io.github.hectorvent.floci.services.cloudformation.model.StackResource;import io.github.hectorvent.floci.services.ec2.Ec2Service;import io.github.hectorvent.floci.services.ec2.model.GroupIdentifier;import io.github.hectorvent.floci.services.ec2.model.Instance;import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;import io.github.hectorvent.floci.services.ec2.model.Reservation;-import io.github.hectorvent.floci.services.iam.model.InstanceProfile;import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;import io.github.hectorvent.floci.services.ec2.model.Subnet;import io.github.hectorvent.floci.services.ec2.model.Tag;@@ -24,8 +24,11 @@ import io.github.hectorvent.floci.services.ec2.model.Vpc;import io.github.hectorvent.floci.services.iam.IamService;import io.github.hectorvent.floci.services.iam.model.IamRole;import io.github.hectorvent.floci.services.iam.model.IamUser;+import io.github.hectorvent.floci.services.iam.model.InstanceProfile;import io.github.hectorvent.floci.services.s3.S3Service;import io.github.hectorvent.floci.services.s3.model.Bucket;+import io.quarkus.runtime.annotations.RegisterForReflection;+import jakarta.annotation.PreDestroy;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;@@ -33,8 +36,12 @@ import java.util.ArrayList;import java.util.Comparator;import java.util.List;import java.util.Map;+import java.util.Set;import java.util.UUID;import java.util.concurrent.ConcurrentHashMap;+import java.util.concurrent.ConcurrentLinkedQueue;+import java.util.concurrent.ExecutorService;+import java.util.concurrent.Executors;@ApplicationScopedpublic class CloudControlService {@@ -56,24 +63,24 @@ public class CloudControlService {* properties, a nodegroup's cluster name, an inline policy's principals. Deleting one of these* from type and identifier alone is a no-op, so Cloud Control must not report SUCCESS for it.*/- private static final java.util.Set<String> ATTRIBUTE_BACKED_DELETES =- java.util.Set.of("AWS::EKS::Nodegroup", "AWS::IAM::Policy");+ private static final Set<String> ATTRIBUTE_BACKED_DELETES =+ Set.of("AWS::EKS::Nodegroup", "AWS::IAM::Policy");/** RequestToken → ProgressEvent. Cloud Control is async; clients poll by token. */private final Map<String, ProgressEvent> requests = new ConcurrentHashMap<>();/** Token insertion order, so the map can be bounded without losing in-flight requests. */- private final java.util.concurrent.ConcurrentLinkedQueue<String> requestOrder =- new java.util.concurrent.ConcurrentLinkedQueue<>();+ private final ConcurrentLinkedQueue<String> requestOrder =+ new ConcurrentLinkedQueue<>();/*** What CreateResource provisioned, keyed by region/type/identifier. Carries the attributes the* delete path needs and the model the read path returns for types outside {@link #listResources}.* Entries are dropped when the resource is deleted.*/private final Map<String, CreatedResource> created = new ConcurrentHashMap<>();- private final java.util.concurrent.ExecutorService executor =- java.util.concurrent.Executors.newFixedThreadPool(4);+ private final ExecutorService executor =+ Executors.newFixedThreadPool(4);- @jakarta.annotation.PreDestroy+ @PreDestroyvoid shutdown() {executor.shutdownNow();}@@ -250,7 +257,7 @@ public class CloudControlService {persistRequest(new PersistedRequest(pending, region, desiredStateJson, System.currentTimeMillis()));executor.submit(() -> RequestScopes.runAs(accountId, () -> {try {- var resource = provisioner.provisionStandalone(typeName, props, region, accountId);+ StackResource resource = provisioner.provisionStandalone(typeName, props, region, accountId);if (resource == null || resource.getPhysicalId() == null) {record(pending.failed("CreateResource is not supported for " + typeName + "."));} else {@@ -501,7 +508,7 @@ public class CloudControlService {putIfPresent(properties, "State", instance.getState().getName());}if (instance.getSecurityGroups() != null && !instance.getSecurityGroups().isEmpty()) {- var groups = properties.putArray("SecurityGroupIds");+ ArrayNode groups = properties.putArray("SecurityGroupIds");for (GroupIdentifier g : instance.getSecurityGroups()) {if (g.getGroupId() != null) groups.add(g.getGroupId());}@@ -636,7 +643,7 @@ public class CloudControlService {if (validTags.isEmpty()) {return;}- var tagArray = properties.putArray("Tags");+ ArrayNode tagArray = properties.putArray("Tags");for (Tag tag : validTags) {tagArray.addObject().put("Key", tag.getKey())diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlIntegrationTest.javaindex bbe725c59a..bd4ce95bda 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlIntegrationTest.java@@ -6,6 +6,9 @@ import com.fasterxml.jackson.databind.ObjectMapper;import io.quarkus.test.junit.QuarkusTest;import io.restassured.RestAssured;import io.restassured.parsing.Parser;+import io.restassured.response.ExtractableResponse;+import io.restassured.response.Response;+import io.restassured.specification.RequestSpecification;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.Test;@@ -15,6 +18,7 @@ import static io.restassured.config.EncoderConfig.encoderConfig;import static io.restassured.config.RestAssuredConfig.config;import static org.hamcrest.MatcherAssert.assertThat;import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.equalTo;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertNotNull;import static org.junit.jupiter.api.Assertions.assertTrue;@@ -125,7 +129,7 @@ class CloudControlIntegrationTest {// Poll GetResourceRequestStatus until the async provision reaches SUCCESS.String identifier = null;for (int i = 0; i < 20 && identifier == null; i++) {- var pe = given()+ ExtractableResponse<Response> pe = given().config(config().encoderConfig(encoderConfig().encodeContentTypeAs(ct, TEXT))).contentType(ct).header("X-Amz-Target", "CloudApiService.GetResourceRequestStatus")@@ -224,7 +228,7 @@ class CloudControlIntegrationTest {.body("{\"TypeName\":\"AWS::S3::Bucket\"}").when().post("/").then().statusCode(200)- .body("TypeName", org.hamcrest.Matchers.equalTo("AWS::S3::Bucket"));+ .body("TypeName", equalTo("AWS::S3::Bucket"));}private void assertErrorCode(String contentType, String target, String body) {@@ -284,7 +288,7 @@ class CloudControlIntegrationTest {.body("{\"TypeName\":\"AWS::IAM::Policy\",\"Identifier\":\"never-created\"}").when().post("/").then().statusCode(200)- .body("ProgressEvent.OperationStatus", org.hamcrest.Matchers.equalTo("FAILED"));+ .body("ProgressEvent.OperationStatus", equalTo("FAILED"));}private String createVpcThroughCloudControl(String ct, String auth, String cidr) {@@ -302,7 +306,7 @@ class CloudControlIntegrationTest {.header("X-Amz-Target", "CloudApiService.DeleteResource").body("{\"TypeName\":\"AWS::EC2::VPC\",\"Identifier\":\"" + identifier + "\"}").when().post("/").then().statusCode(200)- .body("ProgressEvent.OperationStatus", org.hamcrest.Matchers.equalTo(status));+ .body("ProgressEvent.OperationStatus", equalTo(status));}private String awaitIdentifier(String token, String ct) throws InterruptedException {@@ -311,10 +315,10 @@ class CloudControlIntegrationTest {private String awaitIdentifier(String token, String ct, String auth) throws InterruptedException {for (int i = 0; i < 20; i++) {- var request = given().config(config().encoderConfig(encoderConfig().encodeContentTypeAs(ct, TEXT)))+ RequestSpecification request = given().config(config().encoderConfig(encoderConfig().encodeContentTypeAs(ct, TEXT))).contentType(ct).header("X-Amz-Target", "CloudApiService.GetResourceRequestStatus");if (auth != null) request.header("Authorization", auth);- var pe = request.body("{\"RequestToken\":\"" + token + "\"}")+ ExtractableResponse<Response> pe = request.body("{\"RequestToken\":\"" + token + "\"}").when().post("/").then().statusCode(200).extract();if ("SUCCESS".equals(pe.path("ProgressEvent.OperationStatus"))) {return pe.path("ProgressEvent.Identifier");diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlServiceTest.javaindex 316135c371..e0289a7021 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlServiceTest.java@@ -3,8 +3,8 @@ package io.github.hectorvent.floci.services.cloudcontrol;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import io.github.hectorvent.floci.core.common.AwsException;-import io.github.hectorvent.floci.services.cloudformation.CloudFormationResourceProvisioner;import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;+import io.github.hectorvent.floci.services.cloudformation.CloudFormationResourceProvisioner;import io.github.hectorvent.floci.services.cloudformation.model.StackResource;import io.github.hectorvent.floci.services.ec2.Ec2Service;import io.github.hectorvent.floci.services.ec2.model.Tag;@@ -20,10 +20,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertThrows;import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.ArgumentMatchers.any;+import static org.mockito.ArgumentMatchers.anyMap;+import static org.mockito.ArgumentMatchers.anyString;+import static org.mockito.ArgumentMatchers.eq;import static org.mockito.Mockito.mock;-import static org.mockito.Mockito.when;-import static org.mockito.Mockito.verify;import static org.mockito.Mockito.never;+import static org.mockito.Mockito.verify;+import static org.mockito.Mockito.when;class CloudControlServiceTest {@@ -33,9 +37,9 @@ class CloudControlServiceTest {StackResource resource = new StackResource();resource.setPhysicalId("vpc-account-a");resource.setAttributes(Map.of("VpcId", "vpc-account-a"));- when(provisioner.provisionStandalone(org.mockito.ArgumentMatchers.eq("AWS::EC2::VPC"),- org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.eq("us-east-1"),- org.mockito.ArgumentMatchers.eq("111111111111"))).thenReturn(resource);+ when(provisioner.provisionStandalone(eq("AWS::EC2::VPC"),+ any(), eq("us-east-1"),+ eq("111111111111"))).thenReturn(resource);CloudControlService service = new CloudControlService(mock(S3Service.class), mock(Ec2Service.class), mock(IamService.class), provisioner,new ObjectMapper());@@ -57,9 +61,9 @@ class CloudControlServiceTest {CloudControlService.ProgressEvent deniedDelete = service.deleteResource("us-east-1", "222222222222", "AWS::EC2::VPC", "vpc-account-a");assertEquals("FAILED", deniedDelete.operationStatus());- verify(provisioner, never()).deleteStandalone(org.mockito.ArgumentMatchers.anyString(),- org.mockito.ArgumentMatchers.anyString(), org.mockito.ArgumentMatchers.anyString(),- org.mockito.ArgumentMatchers.anyString(), org.mockito.ArgumentMatchers.anyMap());+ verify(provisioner, never()).deleteStandalone(anyString(),+ anyString(), anyString(),+ anyString(), anyMap());CloudControlService.ProgressEvent deleted = service.deleteResource("us-east-1", "111111111111", "AWS::EC2::VPC", "vpc-account-a");@@ -73,9 +77,9 @@ class CloudControlServiceTest {CloudFormationResourceProvisioner provisioner = mock(CloudFormationResourceProvisioner.class);StackResource resource = new StackResource();resource.setPhysicalId("igw-persisted");- when(provisioner.provisionStandalone(org.mockito.ArgumentMatchers.eq("AWS::EC2::InternetGateway"),- org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.eq("us-east-1"),- org.mockito.ArgumentMatchers.eq("111111111111"))).thenReturn(resource);+ when(provisioner.provisionStandalone(eq("AWS::EC2::InternetGateway"),+ any(), eq("us-east-1"),+ eq("111111111111"))).thenReturn(resource);AccountAwareStorageBackend<CloudControlService.PersistedRequest> requests =AccountAwareStorageBackend.inMemory("000000000000");AccountAwareStorageBackend<CloudControlService.PersistedCreatedResource> created =