Light, fluffy, and always free - The AWS Local Emulator alternative
feat(cloudcontrol): include EC2 resource tags (#1933)
Jason van Zyl205a8f174b92
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 5f7f1e74d0..29920292d7 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@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.services.ec2.Ec2Service;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;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;@@ -66,6 +67,7 @@ public class CloudControlService {properties.put("VpcId", vpc.getVpcId());properties.put("CidrBlock", vpc.getCidrBlock());properties.put("InstanceTenancy", vpc.getInstanceTenancy());+ addTags(properties, vpc.getTags());resources.add(new ResourceDescription(vpc.getVpcId(), propertiesString(properties)));}return resources;@@ -79,6 +81,7 @@ public class CloudControlService {properties.put("VpcId", subnet.getVpcId());properties.put("CidrBlock", subnet.getCidrBlock());properties.put("AvailabilityZone", subnet.getAvailabilityZone());+ addTags(properties, subnet.getTags());resources.add(new ResourceDescription(subnet.getSubnetId(), propertiesString(properties)));}return resources;@@ -92,6 +95,7 @@ public class CloudControlService {properties.put("GroupName", group.getGroupName());properties.put("GroupDescription", group.getDescription());properties.put("VpcId", group.getVpcId());+ addTags(properties, group.getTags());resources.add(new ResourceDescription(group.getGroupId(), propertiesString(properties)));}return resources;@@ -130,5 +134,23 @@ public class CloudControlService {}}+ private void addTags(ObjectNode properties, List<Tag> tags) {+ if (tags == null || tags.isEmpty()) {+ return;+ }+ List<Tag> validTags = tags.stream()+ .filter(tag -> tag != null && tag.getKey() != null && !tag.getKey().isBlank())+ .toList();+ if (validTags.isEmpty()) {+ return;+ }+ var tagArray = properties.putArray("Tags");+ for (Tag tag : validTags) {+ tagArray.addObject()+ .put("Key", tag.getKey())+ .put("Value", tag.getValue() == null ? "" : tag.getValue());+ }+ }+public record ResourceDescription(String identifier, String properties) {}}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 f6261ed638..978ca777cf 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@@ -1,5 +1,8 @@package io.github.hectorvent.floci.services.cloudcontrol;+import com.fasterxml.jackson.core.JsonProcessingException;+import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;import io.quarkus.test.junit.QuarkusTest;import io.restassured.RestAssured;import io.restassured.parsing.Parser;@@ -12,10 +15,14 @@ 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.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertNotNull;+import static org.junit.jupiter.api.Assertions.assertTrue;@QuarkusTestclass CloudControlIntegrationTest {+ private static final ObjectMapper MAPPER = new ObjectMapper();private static final String EC2_AUTH ="AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";private static final String IAM_AUTH =@@ -31,13 +38,16 @@ class CloudControlIntegrationTest {}@Test- void listResourcesReturnsCreatedS3Ec2AndIamResources() {+ void listResourcesReturnsCreatedS3Ec2AndIamResources() throws JsonProcessingException {String bucket = "cloudcontrol-test-bucket";given().when().put("/" + bucket).then().statusCode(200);String vpcId = given().formParam("Action", "CreateVpc").formParam("CidrBlock", "10.42.0.0/16")+ .formParam("TagSpecification.1.ResourceType", "vpc")+ .formParam("TagSpecification.1.Tag.1.Key", "Name")+ .formParam("TagSpecification.1.Tag.1.Value", "cloudcontrol-vpc").header("Authorization", EC2_AUTH).when().post("/").then().statusCode(200)@@ -47,6 +57,9 @@ class CloudControlIntegrationTest {.formParam("Action", "CreateSubnet").formParam("VpcId", vpcId).formParam("CidrBlock", "10.42.1.0/24")+ .formParam("TagSpecification.1.ResourceType", "subnet")+ .formParam("TagSpecification.1.Tag.1.Key", "Name")+ .formParam("TagSpecification.1.Tag.1.Value", "cloudcontrol-subnet").header("Authorization", EC2_AUTH).when().post("/").then().statusCode(200)@@ -57,6 +70,9 @@ class CloudControlIntegrationTest {.formParam("GroupName", "cloudcontrol-sg").formParam("GroupDescription", "cloudcontrol sg").formParam("VpcId", vpcId)+ .formParam("TagSpecification.1.ResourceType", "security-group")+ .formParam("TagSpecification.1.Tag.1.Key", "Name")+ .formParam("TagSpecification.1.Tag.1.Value", "cloudcontrol-sg").header("Authorization", EC2_AUTH).when().post("/").then().statusCode(200)@@ -78,9 +94,11 @@ class CloudControlIntegrationTest {.then().statusCode(200);assertListed("AWS::S3::Bucket", bucket, "BucketName");+ assertListedWithTag("AWS::EC2::VPC", vpcId, "Name", "cloudcontrol-vpc");assertListed("AWS::EC2::VPC", vpcId, "CidrBlock");+ assertListedWithTag("AWS::EC2::Subnet", subnetId, "Name", "cloudcontrol-subnet");assertListed("AWS::EC2::Subnet", subnetId, "VpcId");- assertListed("AWS::EC2::SecurityGroup", groupId, "GroupName");+ assertListedWithTag("AWS::EC2::SecurityGroup", groupId, "Name", "cloudcontrol-sg");assertListed("AWS::EC2::SecurityGroup", groupId, "GroupName", "application/x-amz-json-1.0");assertListed("AWS::IAM::User", "cloudcontrol-user", "UserName");assertListed("AWS::IAM::Role", "CloudControlRole", "RoleName");@@ -91,7 +109,37 @@ class CloudControlIntegrationTest {}private void assertListed(String typeName, String identifier, String propertyName, String contentType) {- String body = given()+ String body = listResources(typeName, contentType);++ assertThat(body, containsString("\"TypeName\":\"" + typeName + "\""));+ assertThat(body, containsString("\"Identifier\":\"" + identifier + "\""));+ assertThat(body, containsString(propertyName));+ }++ private void assertListedWithTag(+ String typeName, String identifier, String key, String value) throws JsonProcessingException {+ JsonNode response = MAPPER.readTree(listResources(typeName, "application/x-amz-json-1.1"));+ JsonNode description = null;+ for (JsonNode candidate : response.path("ResourceDescriptions")) {+ if (identifier.equals(candidate.path("Identifier").asText())) {+ description = candidate;+ break;+ }+ }++ assertNotNull(description);+ assertEquals(identifier, description.path("Identifier").asText());+ JsonNode tags = MAPPER.readTree(description.path("Properties").asText()).path("Tags");+ assertTrue(tags.isArray());+ assertTrue(tags.valueStream().anyMatch(tag ->+ key.equals(tag.path("Key").asText())+ && tag.path("Key").isTextual()+ && value.equals(tag.path("Value").asText())+ && tag.path("Value").isTextual()));+ }++ private String listResources(String typeName, String contentType) {+ return given().config(config().encoderConfig(encoderConfig().encodeContentTypeAs(contentType, TEXT))).contentType(contentType)@@ -102,9 +150,5 @@ class CloudControlIntegrationTest {.then().statusCode(200).extract().asString();-- assertThat(body, containsString("\"TypeName\":\"" + typeName + "\""));- assertThat(body, containsString("\"Identifier\":\"" + identifier + "\""));- assertThat(body, containsString(propertyName));}}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.javanew file mode 100644index 0000000000..131a15351b--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlServiceTest.java@@ -0,0 +1,51 @@+package io.github.hectorvent.floci.services.cloudcontrol;++import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;+import io.github.hectorvent.floci.services.ec2.Ec2Service;+import io.github.hectorvent.floci.services.ec2.model.Tag;+import io.github.hectorvent.floci.services.ec2.model.Vpc;+import io.github.hectorvent.floci.services.iam.IamService;+import io.github.hectorvent.floci.services.s3.S3Service;+import org.junit.jupiter.api.Test;++import java.util.List;+import java.util.Map;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.when;++class CloudControlServiceTest {++ @Test+ void emitsOnlyAwsShapedTagsForMalformedPersistedData() throws Exception {+ Ec2Service ec2Service = mock(Ec2Service.class);+ Vpc vpc = new Vpc();+ vpc.setVpcId("vpc-test");+ vpc.setTags(List.of(+ new Tag(null, "ignored-null"),+ new Tag("", "ignored-empty"),+ new Tag(" ", "ignored-blank"),+ new Tag("Name", null)));+ when(ec2Service.describeVpcs("us-east-1", List.of(), Map.of())).thenReturn(List.of(vpc));+ ObjectMapper mapper = new ObjectMapper();+ CloudControlService service = new CloudControlService(+ mock(S3Service.class), ec2Service, mock(IamService.class), mapper);++ String properties = service.listResources("us-east-1", "AWS::EC2::VPC").getFirst().properties();+ JsonNode tags = mapper.readTree(properties).path("Tags");++ assertTrue(tags.isArray());+ assertEquals(1, tags.size());+ assertTrue(tags.get(0).path("Key").isTextual());+ assertEquals("Name", tags.get(0).path("Key").asText());+ assertTrue(tags.get(0).path("Value").isTextual());+ assertEquals("", tags.get(0).path("Value").asText());+ assertFalse(properties.contains("ignored-null"));+ assertFalse(properties.contains("ignored-empty"));+ assertFalse(properties.contains("ignored-blank"));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.javaindex 0b01be4cfa..d023bfa316 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java@@ -1954,7 +1954,8 @@ class Ec2IntegrationTest {.post("/").then().statusCode(200)- .body("DescribeTagsResponse.tagSet.item.find { it.key == 'Name' }.value", equalTo("test-instance"));+ .body("DescribeTagsResponse.tagSet.item.find { it.key == 'Name' && it.resourceId == '"+ + instanceId + "' }.value", equalTo("test-instance"));}@Test@@ -1985,7 +1986,8 @@ class Ec2IntegrationTest {.post("/").then().statusCode(200)- .body("DescribeTagsResponse.tagSet.item.key", equalTo("Name"));+ .body("DescribeTagsResponse.tagSet.item.size()", greaterThanOrEqualTo(1))+ .body("DescribeTagsResponse.tagSet.item.findAll { it.key != 'Name' }.size()", equalTo(0));}@Test