fix(redshift): keep tags, VPC and subnet detail on cluster subnet groups (#7211)

* fix(redshift): keep tags, VPC and subnet detail on cluster subnet groups

CreateClusterSubnetGroup dropped Tags and passed a null VPC, so
DescribeClusterSubnetGroups returned no Tags, VpcId, SubnetGroupStatus or
SubnetAvailabilityZone and the Terraform provider saw drift on every plan.

Tags are now parsed (Tags.member.N and Tags.Tag.N) and stored on the group,
where DescribeTags/CreateTags already find them under the subnetgroup ARN.
The VPC and each subnet's zone are resolved from EC2 on create and modify;
subnets EC2 does not know are kept unresolved, as before. Groups persisted
earlier load unchanged (the new zone map is nullable).

* fix(redshift): inject Ec2Service through the constructor

Replace the Ec2Service field injection with a constructor parameter held
in a final field. The previous @Inject signature stays as a package-private
delegating constructor passing null, so hand-built test services still
skip subnet resolution.

* test(redshift): build the EC2 setup requests with Ec2Requests

ec2Id now only extracts the id from the response; the auth header and
Action come from the shared Ec2Requests.ec2 helper.

* test(redshift): make ec2Id a plain path extraction

Call sites build the EC2 request with Ec2Requests.ec2 and pass it in;
the account and region are named constants.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-11-02 10:09:50 -04:00
1 parent 3963e03797
commit 4438e3df49
6 files changed
+122 -7

No files matched your search

@@ -30,6 +30,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import static io.github.hectorvent.floci.testing.Ec2Requests.ec2;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
@@ -49,6 +50,9 @@ public class RedshiftOperationsTest {
private static final String AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=test/20260822/us-east-1/redshift/aws4_request";
private static final String EC2_ACCOUNT = "000000000000";
private static final String EC2_REGION = "us-east-1";
private static final String GRANT_NAMES_PATH = "DescribeSnapshotCopyGrantsResponse"
+ ".DescribeSnapshotCopyGrantsResult.SnapshotCopyGrants.SnapshotCopyGrant.SnapshotCopyGrantName";
private static final String MARKER_PATH =
@@ -523,6 +527,57 @@ public class RedshiftOperationsTest {
.body(containsString("<ClusterIdentifier>cluster-restored</ClusterIdentifier>"));
}
private String ec2Id(String path, RequestSpecification req) {
return req.post("/").then().statusCode(200).extract().path(path);
}
private XmlPath describeSubnetGroup(String name) {
return XmlPath.from(redshift("DescribeClusterSubnetGroups", "ClusterSubnetGroupName", name).asString())
.setRootPath("DescribeClusterSubnetGroupsResponse.DescribeClusterSubnetGroupsResult.ClusterSubnetGroups.ClusterSubnetGroup");
}
@Test
@Order(4)
void clusterSubnetGroupKeepsTagsVpcAndSubnetDetail() {
String vpc = ec2Id("CreateVpcResponse.vpc.vpcId", ec2("CreateVpc", EC2_ACCOUNT, EC2_REGION)
.formParam("CidrBlock", "10.77.0.0/16"));
String subnet = ec2Id("CreateSubnetResponse.subnet.subnetId", ec2("CreateSubnet", EC2_ACCOUNT, EC2_REGION)
.formParam("VpcId", vpc).formParam("CidrBlock", "10.77.1.0/24")
.formParam("AvailabilityZone", "us-east-1b"));
assertEquals(200, redshift("CreateClusterSubnetGroup", "ClusterSubnetGroupName", "sng-tagged",
"Description", "d", "SubnetIds.SubnetIdentifier.1", subnet,
"Tags.Tag.1.Key", "env", "Tags.Tag.1.Value", "dev").statusCode());
// Read back through a separate call, not the create response.
XmlPath group = describeSubnetGroup("sng-tagged");
assertEquals(vpc, group.getString("VpcId"));
assertEquals("Complete", group.getString("SubnetGroupStatus"));
assertEquals(subnet, group.getString("Subnets.Subnet.SubnetIdentifier"));
assertEquals("us-east-1b", group.getString("Subnets.Subnet.SubnetAvailabilityZone.Name"));
assertEquals("Active", group.getString("Subnets.Subnet.SubnetStatus"));
assertEquals("env", group.getString("Tags.Tag.Key"));
assertEquals("dev", group.getString("Tags.Tag.Value"));
XmlPath tagged = XmlPath.from(redshift("DescribeTags", "ResourceName",
"arn:aws:redshift:us-east-1:000000000000:subnetgroup:sng-tagged").asString())
.setRootPath("DescribeTagsResponse.DescribeTagsResult.TaggedResources");
assertEquals("dev", tagged.getString("TaggedResource.Tag.Value"));
// Modify keeps tags and recomputes the VPC from the new subnet list.
String vpc2 = ec2Id("CreateVpcResponse.vpc.vpcId", ec2("CreateVpc", EC2_ACCOUNT, EC2_REGION)
.formParam("CidrBlock", "10.78.0.0/16"));
String subnet2 = ec2Id("CreateSubnetResponse.subnet.subnetId", ec2("CreateSubnet", EC2_ACCOUNT, EC2_REGION)
.formParam("VpcId", vpc2).formParam("CidrBlock", "10.78.1.0/24")
.formParam("AvailabilityZone", "us-east-1c"));
assertEquals(200, redshift("ModifyClusterSubnetGroup", "ClusterSubnetGroupName", "sng-tagged",
"SubnetIds.SubnetIdentifier.1", subnet2).statusCode());
group = describeSubnetGroup("sng-tagged");
assertEquals(vpc2, group.getString("VpcId"));
assertEquals("us-east-1c", group.getString("Subnets.Subnet.SubnetAvailabilityZone.Name"));
assertEquals("dev", group.getString("Tags.Tag.Value"));
}
@Test
@Order(4)
void clusterSubnetGroupLifecycle() {
@@ -792,14 +792,14 @@ class RedshiftQueryHandlerTest {
params.putSingle("SubnetIds.SubnetIdentifier.2", "subnet-bbb");
ClusterSubnetGroup group = new ClusterSubnetGroup("test-sng", "desc", null, List.of("subnet-aaa", "subnet-bbb"));
when(service.createClusterSubnetGroup(any(), any(), any(), any())).thenReturn(group);
when(service.createClusterSubnetGroup(any(), any(), any(), any(), any())).thenReturn(group);
Response response = handler.handle("CreateClusterSubnetGroup", params);
assertEquals(200, response.getStatus());
@SuppressWarnings("unchecked")
ArgumentCaptor<List<String>> captor = ArgumentCaptor.forClass(List.class);
verify(service).createClusterSubnetGroup(eq("test-sng"), eq("desc"), any(), captor.capture());
verify(service).createClusterSubnetGroup(eq("test-sng"), eq("desc"), any(), captor.capture(), any());
assertEquals(List.of("subnet-aaa", "subnet-bbb"), captor.getValue());
}