refactor(ec2): dedupe instance-state-change XML building (#3709)

handleTerminateInstances, handleStartInstances, and handleStopInstances
in Ec2QueryHandler each hand-wrote the same instancesSet/item/
currentState/previousState response shape, a 17-line block repeated
three times, found via CPD (#3703). Extracted it into
buildInstanceStateChangeXml(responseElementName, changes), used by
all three handlers.

No behavior change: the XML shape is identical, only its construction
is now shared. All three response shapes already have direct
RestAssured coverage in Ec2IntegrationTest.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-15 21:57:03 -05:00
1 parent 328b6f6c17
commit 6145fbcb24
1 file changed
+14 -41
@@ -1205,8 +1205,17 @@ public class Ec2QueryHandler {
private Response handleTerminateInstances(MultivaluedMap<String, String> p, String region) {
List<String> ids = getList(p, "InstanceId");
List<Map<String, String>> changes = service.terminateInstances(region, ids);
return xmlResponse(buildInstanceStateChangeXml("TerminateInstancesResponse", changes));
}
/**
* The shared {@code instancesSet}/{@code item}/{@code currentState}/{@code previousState}
* shape that {@code TerminateInstances}, {@code StartInstances}, and {@code StopInstances}
* each return, keyed only by the top-level response element name.
*/
private String buildInstanceStateChangeXml(String responseElementName, List<Map<String, String>> changes) {
XmlBuilder xml = new XmlBuilder()
.start("TerminateInstancesResponse", AwsNamespaces.EC2)
.start(responseElementName, AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
.start("instancesSet");
for (Map<String, String> c : changes) {
@@ -1222,8 +1231,8 @@ public class Ec2QueryHandler {
.end("previousState")
.end("item");
}
xml.end("instancesSet").end("TerminateInstancesResponse");
return xmlResponse(xml.build());
xml.end("instancesSet").end(responseElementName);
return xml.build();
}
/**
@@ -1256,49 +1265,13 @@ public class Ec2QueryHandler {
private Response handleStartInstances(MultivaluedMap<String, String> p, String region) {
List<String> ids = getList(p, "InstanceId");
List<Map<String, String>> changes = service.startInstances(region, ids);
XmlBuilder xml = new XmlBuilder()
.start("StartInstancesResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
.start("instancesSet");
for (Map<String, String> c : changes) {
xml.start("item")
.elem("instanceId", c.get("instanceId"))
.start("currentState")
.elem("code", c.get("currentCode"))
.elem("name", c.get("currentState"))
.end("currentState")
.start("previousState")
.elem("code", c.get("previousCode"))
.elem("name", c.get("previousState"))
.end("previousState")
.end("item");
}
xml.end("instancesSet").end("StartInstancesResponse");
return xmlResponse(xml.build());
return xmlResponse(buildInstanceStateChangeXml("StartInstancesResponse", changes));
}
private Response handleStopInstances(MultivaluedMap<String, String> p, String region) {
List<String> ids = getList(p, "InstanceId");
List<Map<String, String>> changes = service.stopInstances(region, ids);
XmlBuilder xml = new XmlBuilder()
.start("StopInstancesResponse", AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
.start("instancesSet");
for (Map<String, String> c : changes) {
xml.start("item")
.elem("instanceId", c.get("instanceId"))
.start("currentState")
.elem("code", c.get("currentCode"))
.elem("name", c.get("currentState"))
.end("currentState")
.start("previousState")
.elem("code", c.get("previousCode"))
.elem("name", c.get("previousState"))
.end("previousState")
.end("item");
}
xml.end("instancesSet").end("StopInstancesResponse");
return xmlResponse(xml.build());
return xmlResponse(buildInstanceStateChangeXml("StopInstancesResponse", changes));
}
private Response handleRebootInstances(MultivaluedMap<String, String> p, String region) {