mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(cloudformation): provision RDS resources (#1367)
Inject RdsService and add provision + delete handling for AWS::RDS::DBInstance, DBCluster, DBSubnetGroup, DBParameterGroup and DBClusterParameterGroup. DBInstance/DBCluster start real RDS containers (same path as the direct API). Ref returns the resource identifier; Fn::GetAtt exposes Endpoint.Address, Endpoint.Port and the resource ARN (plus ReadEndpoint.Address for clusters). Stack deletion removes each resource so create/delete is symmetric.
This commit is contained in:
1 parent
feae421666
commit
eb730a455e
4 files changed
+395
-2
No files matched your search
+82
@@ -0,0 +1,82 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
||||
/**
|
||||
* End-to-end check that CloudFormation provisions RDS resources for real. Uses DBSubnetGroup
|
||||
* because it does not start a container, so the test stays Docker-free (DBInstance/DBCluster
|
||||
* provisioning is covered by the mocked-service {@code RdsCfnProvisionerTest}).
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CloudFormationRdsIntegrationTest {
|
||||
|
||||
private static final String CFN_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";
|
||||
private static final String RDS_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/rds/aws4_request";
|
||||
|
||||
@Test
|
||||
void createStackProvisionsDbSubnetGroupVisibleToRds() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String groupName = "cfn-rds-subnets-" + suffix;
|
||||
String stackName = "cfn-rds-stack-" + suffix;
|
||||
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"DbSubnets": {
|
||||
"Type": "AWS::RDS::DBSubnetGroup",
|
||||
"Properties": {
|
||||
"DBSubnetGroupName": "%s",
|
||||
"DBSubnetGroupDescription": "managed by cfn",
|
||||
"SubnetIds": ["subnet-aaaa1111", "subnet-bbbb2222"]
|
||||
}
|
||||
}
|
||||
},
|
||||
"Outputs": {
|
||||
"GroupName": {"Value": {"Ref": "DbSubnets"}}
|
||||
}
|
||||
}
|
||||
""".formatted(groupName);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
// Stack reaches CREATE_COMPLETE and Ref(DbSubnets) exports the subnet group name.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"))
|
||||
.body(containsString(groupName));
|
||||
|
||||
// The subnet group really exists in RDS (provisioned, not stubbed).
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", RDS_AUTH)
|
||||
.formParam("Action", "DescribeDBSubnetGroups")
|
||||
.formParam("DBSubnetGroupName", groupName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString(groupName));
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -48,7 +48,7 @@ class CustomResourceProvisionerTest {
|
||||
|
||||
provisioner = new CloudFormationResourceProvisioner(
|
||||
null, null, null, null, lambdaService, null, null, null, null, null,
|
||||
null, null, null, null, null, null, mapper, store, endpoint, null, null, null, null, null);
|
||||
null, null, null, null, null, null, mapper, store, endpoint, null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
private CloudFormationTemplateEngine engine() {
|
||||
|
||||
+167
@@ -0,0 +1,167 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.rds.RdsService;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbCluster;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbEndpoint;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbInstance;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbParameterGroup;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbSubnetGroup;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Verifies CloudFormation provisions RDS resources by delegating to {@link RdsService} (mocked, so
|
||||
* no containers start) and maps CFN properties to the right create-method arguments, with
|
||||
* Ref/GetAtt set from the returned resource.
|
||||
*/
|
||||
class RdsCfnProvisionerTest {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private RdsService rdsService;
|
||||
private CloudFormationResourceProvisioner provisioner;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
rdsService = mock(RdsService.class);
|
||||
provisioner = new CloudFormationResourceProvisioner(
|
||||
null, null, null, null, null, null, null, null, null, null,
|
||||
null, null, null, null, null, null,
|
||||
mapper,
|
||||
null, null, null, null, null, null, null,
|
||||
rdsService);
|
||||
}
|
||||
|
||||
private CloudFormationTemplateEngine engine() {
|
||||
return new CloudFormationTemplateEngine("000000000000", "us-east-1", "my-stack",
|
||||
"stack/id", Map.of(), Map.of(), Map.of(), Map.of(), Map.of(), mapper,
|
||||
(Function<String, String>) name -> null);
|
||||
}
|
||||
|
||||
private JsonNode props(String json) {
|
||||
try {
|
||||
return mapper.readTree(json);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private StackResource provision(String logicalId, String type, String json) {
|
||||
return provisioner.provision(logicalId, type, props(json), engine(),
|
||||
"us-east-1", "000000000000", "my-stack");
|
||||
}
|
||||
|
||||
@Test
|
||||
void provisionsDbInstanceWithMappedArgsAndEndpointAttributes() {
|
||||
DbInstance instance = mock(DbInstance.class);
|
||||
when(instance.getDbInstanceIdentifier()).thenReturn("mydb");
|
||||
when(instance.getEndpoint()).thenReturn(new DbEndpoint("mydb.local", 5432));
|
||||
when(instance.getDbInstanceArn()).thenReturn("arn:aws:rds:us-east-1:000000000000:db:mydb");
|
||||
when(rdsService.createDbInstance(any(), any(), any(), any(), any(), any(), any(),
|
||||
anyInt(), anyBoolean(), any(), any(), any())).thenReturn(instance);
|
||||
|
||||
StackResource r = provision("Db", "AWS::RDS::DBInstance", """
|
||||
{"DBInstanceIdentifier":"mydb","Engine":"postgres","EngineVersion":"16",
|
||||
"MasterUsername":"admin","MasterUserPassword":"secret","DBName":"appdb",
|
||||
"AllocatedStorage":50,"DBInstanceClass":"db.t3.small"}
|
||||
""");
|
||||
|
||||
assertEquals("CREATE_COMPLETE", r.getStatus());
|
||||
assertEquals("mydb", r.getPhysicalId());
|
||||
assertEquals("mydb.local", r.getAttributes().get("Endpoint.Address"));
|
||||
assertEquals("5432", r.getAttributes().get("Endpoint.Port"));
|
||||
assertEquals("arn:aws:rds:us-east-1:000000000000:db:mydb", r.getAttributes().get("DBInstanceArn"));
|
||||
// CFN properties mapped to the create-method arguments; absent optionals are null.
|
||||
verify(rdsService).createDbInstance("mydb", "postgres", "16", "admin", "secret",
|
||||
"appdb", "db.t3.small", 50, false, null, null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void provisionsDbClusterWithReaderEndpoint() {
|
||||
DbCluster cluster = mock(DbCluster.class);
|
||||
when(cluster.getDbClusterIdentifier()).thenReturn("mycluster");
|
||||
when(cluster.getEndpoint()).thenReturn(new DbEndpoint("mycluster.local", 5432));
|
||||
when(cluster.getReaderEndpoint()).thenReturn(new DbEndpoint("mycluster-ro.local", 5432));
|
||||
when(cluster.getDbClusterArn()).thenReturn("arn:aws:rds:us-east-1:000000000000:cluster:mycluster");
|
||||
when(rdsService.createDbCluster(any(), any(), any(), any(), any(), any(), anyBoolean(), any()))
|
||||
.thenReturn(cluster);
|
||||
|
||||
StackResource r = provision("Cluster", "AWS::RDS::DBCluster", """
|
||||
{"DBClusterIdentifier":"mycluster","Engine":"aurora-postgresql","EngineVersion":"16.3",
|
||||
"MasterUsername":"admin","MasterUserPassword":"secret","DatabaseName":"appdb"}
|
||||
""");
|
||||
|
||||
assertEquals("mycluster", r.getPhysicalId());
|
||||
assertEquals("mycluster.local", r.getAttributes().get("Endpoint.Address"));
|
||||
assertEquals("mycluster-ro.local", r.getAttributes().get("ReadEndpoint.Address"));
|
||||
assertEquals("5432", r.getAttributes().get("Endpoint.Port"));
|
||||
verify(rdsService).createDbCluster("mycluster", "aurora-postgresql", "16.3",
|
||||
"admin", "secret", "appdb", false, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void provisionsDbSubnetGroupWithResolvedSubnetIds() {
|
||||
DbSubnetGroup group = mock(DbSubnetGroup.class);
|
||||
when(group.getDbSubnetGroupName()).thenReturn("my-subnet-group");
|
||||
when(rdsService.createDbSubnetGroup(any(), any(), anyList())).thenReturn(group);
|
||||
|
||||
StackResource r = provision("Sg", "AWS::RDS::DBSubnetGroup", """
|
||||
{"DBSubnetGroupName":"my-subnet-group","DBSubnetGroupDescription":"db subnets",
|
||||
"SubnetIds":["subnet-a","subnet-b"]}
|
||||
""");
|
||||
|
||||
assertEquals("my-subnet-group", r.getPhysicalId());
|
||||
assertEquals("my-subnet-group", r.getAttributes().get("DBSubnetGroupName"));
|
||||
verify(rdsService).createDbSubnetGroup("my-subnet-group", "db subnets",
|
||||
List.of("subnet-a", "subnet-b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void provisionsDbParameterGroup() {
|
||||
DbParameterGroup group = mock(DbParameterGroup.class);
|
||||
when(group.getDbParameterGroupName()).thenReturn("my-pg");
|
||||
when(rdsService.createDbParameterGroup(any(), any(), any())).thenReturn(group);
|
||||
|
||||
StackResource r = provision("Pg", "AWS::RDS::DBParameterGroup", """
|
||||
{"DBParameterGroupName":"my-pg","Family":"postgres16","Description":"params"}
|
||||
""");
|
||||
|
||||
assertEquals("my-pg", r.getPhysicalId());
|
||||
assertEquals("my-pg", r.getAttributes().get("DBParameterGroupName"));
|
||||
verify(rdsService).createDbParameterGroup("my-pg", "postgres16", "params");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteDelegatesToRdsServiceForEachRdsType() {
|
||||
// Stack deletion tears down RDS resources via the physical id set at provision time.
|
||||
provisioner.delete("AWS::RDS::DBInstance", "mydb", "us-east-1");
|
||||
verify(rdsService).deleteDbInstance("mydb");
|
||||
|
||||
provisioner.delete("AWS::RDS::DBCluster", "mycluster", "us-east-1");
|
||||
verify(rdsService).deleteDbCluster("mycluster");
|
||||
|
||||
provisioner.delete("AWS::RDS::DBSubnetGroup", "my-subnet-group", "us-east-1");
|
||||
verify(rdsService).deleteDbSubnetGroup("my-subnet-group");
|
||||
|
||||
provisioner.delete("AWS::RDS::DBParameterGroup", "my-pg", "us-east-1");
|
||||
verify(rdsService).deleteDbParameterGroup("my-pg");
|
||||
|
||||
provisioner.delete("AWS::RDS::DBClusterParameterGroup", "my-cpg", "us-east-1");
|
||||
verify(rdsService).deleteDbClusterParameterGroup("my-cpg");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user