Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(rds): keep the tags given to CreateDBSubnetGroup (#2627)

Michal Schreiber20ecb8915567
diff --git a/compatibility-tests/sdk-test-awscli/test/rds.bats b/compatibility-tests/sdk-test-awscli/test/rds.bats
index 05eeb313b0..ed72204432 100644
--- a/compatibility-tests/sdk-test-awscli/test/rds.bats
+++ b/compatibility-tests/sdk-test-awscli/test/rds.bats
@@ -192,3 +192,30 @@ teardown() {
aws_cmd docdb delete-db-cluster --db-cluster-identifier "$CLUSTER_ID" --skip-final-snapshot >/dev/null 2>&1 || true
}
+
+@test "rds: tags given to create-db-subnet-group are readable back" {
+ VPC_ID=$(aws_cmd ec2 create-vpc --cidr-block 10.77.0.0/16 --query Vpc.VpcId --output text)
+ S1=$(aws_cmd ec2 create-subnet --vpc-id "$VPC_ID" --cidr-block 10.77.1.0/24 --availability-zone us-east-1a --query Subnet.SubnetId --output text)
+ S2=$(aws_cmd ec2 create-subnet --vpc-id "$VPC_ID" --cidr-block 10.77.2.0/24 --availability-zone us-east-1b --query Subnet.SubnetId --output text)
+ GROUP="bats-sng-$(unique_name)"
+
+ run aws_cmd rds create-db-subnet-group --db-subnet-group-name "$GROUP" --db-subnet-group-description d \
+ --subnet-ids "$S1" "$S2" --tags "Key=Name,Value=$GROUP" Key=env,Value=tst
+ assert_success
+ arn=$(json_get "$output" '.DBSubnetGroup.DBSubnetGroupArn')
+
+ run aws_cmd rds list-tags-for-resource --resource-name "$arn"
+ assert_success
+ assert_output --partial '"Key": "env"'
+ assert_output --partial "\"Value\": \"$GROUP\""
+
+ # the DocumentDB CLI reaches the same group through the same scope
+ run aws_cmd docdb list-tags-for-resource --resource-name "$arn"
+ assert_success
+ assert_output --partial '"Key": "env"'
+
+ aws_cmd rds delete-db-subnet-group --db-subnet-group-name "$GROUP" >/dev/null 2>&1 || true
+ aws_cmd ec2 delete-subnet --subnet-id "$S1" >/dev/null 2>&1 || true
+ aws_cmd ec2 delete-subnet --subnet-id "$S2" >/dev/null 2>&1 || true
+ aws_cmd ec2 delete-vpc --vpc-id "$VPC_ID" >/dev/null 2>&1 || true
+}
diff --git a/docs/services/rds.md b/docs/services/rds.md
index a9d9d8a6a2..cc9c95d202 100644
--- a/docs/services/rds.md
+++ b/docs/services/rds.md
@@ -19,7 +19,7 @@ RDS Data API (`rds-data`) is documented separately because it uses REST JSON rou
| `ModifyDBInstance` | Update instance settings |
| `RebootDBInstance` | Restart a database instance |
| `DescribeOrderableDBInstanceOptions` | List deterministic instance class options |
-| `CreateDBSubnetGroup` | Create a DB subnet group |
+| `CreateDBSubnetGroup` | Create a DB subnet group; tags given here are readable through `ListTagsForResource` |
| `DescribeDBSubnetGroups` | List DB subnet groups |
| `ModifyDBSubnetGroup` | Update DB subnet group description and subnet list |
| `DeleteDBSubnetGroup` | Delete a DB subnet group |
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java
index c6e5840d0b..58c786e6c1 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java
@@ -304,7 +304,8 @@ public class RdsQueryHandler {
String description = params.getFirst("DBSubnetGroupDescription");
List<String> subnetIds = memberList(params, "SubnetIds");
try {
- DbSubnetGroup group = service.createDbSubnetGroup(name, description, subnetIds, region);
+ DbSubnetGroup group = service.createDbSubnetGroup(name, description, subnetIds, region,
+ parseTags(params));
return Response.ok(AwsQueryResponse.envelope("CreateDBSubnetGroup",
AwsNamespaces.RDS, dbSubnetGroupXml(group))).build();
} catch (AwsException e) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java
index fa5cfa87bf..66e6fc1641 100644
--- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java
@@ -2261,6 +2261,15 @@ public class RdsService implements Resettable, ResourceProvider {
}
public DbSubnetGroup createDbSubnetGroup(String name, String description, List<String> subnetIds, String region) {
+ return createDbSubnetGroup(name, description, subnetIds, region, Map.of());
+ }
+
+ /**
+ * Tags given at create are stored with the group in the same write — a live account reads
+ * them back from ListTagsForResource straight after CreateDBSubnetGroup.
+ */
+ public DbSubnetGroup createDbSubnetGroup(String name, String description, List<String> subnetIds,
+ String region, Map<String, String> tags) {
if (name == null || name.isBlank()) {
throw new AwsException("MissingParameter", "The request must contain the parameter DBSubnetGroupName.", 400);
}
@@ -2277,6 +2286,9 @@ public class RdsService implements Resettable, ResourceProvider {
}
DbSubnetGroup group = buildSubnetGroup(name, description, subnetIds, effectiveRegion);
+ if (tags != null && !tags.isEmpty()) {
+ group.setTags(new LinkedHashMap<>(tags));
+ }
putSubnetGroupForScope(currentAccountId(), effectiveRegion, name, group);
return group;
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java
index bc35f470da..d5539577b7 100644
--- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java
@@ -633,7 +633,7 @@ class RdsQueryHandlerTest {
@Test
void createDbSubnetGroup_passesSubnetMembersToService() {
- when(service.createDbSubnetGroup("sample-db-subnets", "test", List.of("subnet-aaa", "subnet-bbb"), null))
+ when(service.createDbSubnetGroup("sample-db-subnets", "test", List.of("subnet-aaa", "subnet-bbb"), null, java.util.Map.of()))
.thenReturn(new DbSubnetGroup(
"sample-db-subnets", "test", "vpc-123", List.of("subnet-aaa", "subnet-bbb"),
Map.of("subnet-aaa", "us-east-1a", "subnet-bbb", "us-east-1b")));
@@ -645,7 +645,7 @@ class RdsQueryHandlerTest {
p.add("SubnetIds.SubnetIdentifier.2", "subnet-bbb");
Response response = handler.handle("CreateDBSubnetGroup", p);
- verify(service).createDbSubnetGroup("sample-db-subnets", "test", List.of("subnet-aaa", "subnet-bbb"), null);
+ verify(service).createDbSubnetGroup("sample-db-subnets", "test", List.of("subnet-aaa", "subnet-bbb"), null, java.util.Map.of());
String body = (String) response.getEntity();
assertEquals(200, response.getStatus());
assertTrue(body.contains("<DBSubnetGroupName>sample-db-subnets</DBSubnetGroupName>"));
@@ -657,7 +657,7 @@ class RdsQueryHandlerTest {
@Test
void createDbSubnetGroupPassesRequestRegionToService() {
- when(service.createDbSubnetGroup("sample-db-subnets", "test", List.of("subnet-aaa", "subnet-bbb"), "us-west-2"))
+ when(service.createDbSubnetGroup("sample-db-subnets", "test", List.of("subnet-aaa", "subnet-bbb"), "us-west-2", java.util.Map.of()))
.thenReturn(new DbSubnetGroup(
"sample-db-subnets", "test", "vpc-123", List.of("subnet-aaa", "subnet-bbb"),
Map.of("subnet-aaa", "us-west-2a", "subnet-bbb", "us-west-2b")));
@@ -671,7 +671,7 @@ class RdsQueryHandlerTest {
Response response = handler.handle("CreateDBSubnetGroup", p, "us-west-2");
assertEquals(200, response.getStatus());
- verify(service).createDbSubnetGroup("sample-db-subnets", "test", List.of("subnet-aaa", "subnet-bbb"), "us-west-2");
+ verify(service).createDbSubnetGroup("sample-db-subnets", "test", List.of("subnet-aaa", "subnet-bbb"), "us-west-2", java.util.Map.of());
}
@Test
@@ -862,7 +862,7 @@ class RdsQueryHandlerTest {
group.setVpcId("vpc-12345678");
group.setSubnetIds(List.of("subnet-a", "subnet-b"));
group.setSubnetAvailabilityZones(Map.of("subnet-a", "us-east-1a", "subnet-b", "us-east-1b"));
- when(service.createDbSubnetGroup("my-subnet-group", "test subnet group", List.of("subnet-a", "subnet-b"), null))
+ when(service.createDbSubnetGroup("my-subnet-group", "test subnet group", List.of("subnet-a", "subnet-b"), null, java.util.Map.of()))
.thenReturn(group);
MultivaluedMap<String, String> p = params();
@@ -2004,4 +2004,27 @@ class RdsQueryHandlerTest {
c.setMasterUsername("admin");
return c;
}
+
+ @Test
+ void createDbSubnetGroup_passesCreateTagsToService() {
+ DbSubnetGroup group = new DbSubnetGroup();
+ group.setDbSubnetGroupName("tagged");
+ group.setDbSubnetGroupArn("arn:aws:rds:us-east-1:123456789012:subgrp:tagged");
+ when(service.createDbSubnetGroup(eq("tagged"), eq("d"), eq(List.of("subnet-aaa", "subnet-bbb")), isNull(),
+ eq(java.util.Map.of("Name", "tagged", "env", "tst")))).thenReturn(group);
+
+ MultivaluedMap<String, String> p = params();
+ p.add("DBSubnetGroupName", "tagged");
+ p.add("DBSubnetGroupDescription", "d");
+ p.add("SubnetIds.SubnetIdentifier.1", "subnet-aaa");
+ p.add("SubnetIds.SubnetIdentifier.2", "subnet-bbb");
+ p.add("Tags.Tag.1.Key", "Name");
+ p.add("Tags.Tag.1.Value", "tagged");
+ p.add("Tags.Tag.2.Key", "env");
+ p.add("Tags.Tag.2.Value", "tst");
+
+ assertEquals(200, handler.handle("CreateDBSubnetGroup", p).getStatus());
+ verify(service).createDbSubnetGroup("tagged", "d", List.of("subnet-aaa", "subnet-bbb"), null,
+ java.util.Map.of("Name", "tagged", "env", "tst"));
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java
index 7eaab9b251..cb4947efb5 100644
--- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java
@@ -4920,4 +4920,22 @@ class RdsServiceTest {
subnet.setAvailabilityZone(availabilityZone);
return subnet;
}
+
+ @Test
+ void createDbSubnetGroupStoresTheTagsItIsGiven() {
+ rdsService.createDbSubnetGroup("tagged", "d", List.of("subnet-default-a", "subnet-default-b"), "us-east-1",
+ Map.of("Name", "tagged", "env", "tst"));
+ String arn = "arn:aws:rds:us-east-1:123456789012:subgrp:tagged";
+
+ assertEquals(Map.of("Name", "tagged", "env", "tst"), rdsService.listTagsForResource(arn, "us-east-1"));
+
+ // later tag calls build on the create-time tags rather than replacing them
+ rdsService.addTagsToResource(arn, Map.of("env", "stg", "extra", "yes"), "us-east-1");
+ assertEquals(Map.of("Name", "tagged", "env", "stg", "extra", "yes"),
+ rdsService.listTagsForResource(arn, "us-east-1"));
+
+ // and the tags are what a restart reads back, not a side table
+ assertEquals(Map.of("Name", "tagged", "env", "stg", "extra", "yes"),
+ rdsService.getDbSubnetGroup("tagged", "us-east-1").getTags());
+ }
}