mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(ec2): added attach and detach volume support (#1787)
* feat(ec2): added attach and detach volume support * fix(ec2): added same AZ check and fixed unit tests fragility * fix(ec2): added instance state check for EBS flow * fix(ec2): updated action set and adjusted exception precedence
This commit is contained in:
1 parent
9f0dec9317
commit
2c743299ec
4 files changed
+265
-1
No files matched your search
@@ -170,7 +170,7 @@ public class AwsQueryController {
|
||||
"ModifyLaunchTemplate", "DeleteLaunchTemplate",
|
||||
"DescribeNetworkInterfaces",
|
||||
"CreateFlowLogs", "DescribeFlowLogs", "DeleteFlowLogs",
|
||||
"CreateVolume", "DescribeVolumes", "DeleteVolume"
|
||||
"CreateVolume", "DescribeVolumes", "DeleteVolume", "AttachVolume", "DetachVolume"
|
||||
);
|
||||
|
||||
private final CloudFormationQueryHandler cloudFormationQueryHandler;
|
||||
|
||||
@@ -158,6 +158,8 @@ public class Ec2QueryHandler {
|
||||
case "CreateVolume" -> handleCreateVolume(params, region);
|
||||
case "DescribeVolumes" -> handleDescribeVolumes(params, region);
|
||||
case "DeleteVolume" -> handleDeleteVolume(params, region);
|
||||
case "AttachVolume" -> handleAttachVolume(params, region);
|
||||
case "DetachVolume" -> handleDetachVolume(params, region);
|
||||
// Spot Instances
|
||||
case "RequestSpotInstances" -> handleRequestSpotInstances(params, region);
|
||||
case "DescribeSpotInstanceRequests" -> handleDescribeSpotInstanceRequests(params, region);
|
||||
@@ -2587,6 +2589,39 @@ public class Ec2QueryHandler {
|
||||
return booleanResponse("DeleteVolume");
|
||||
}
|
||||
|
||||
private Response handleAttachVolume(MultivaluedMap<String, String> p, String region) {
|
||||
String volumeId = p.getFirst("VolumeId");
|
||||
String instanceId = p.getFirst("InstanceId");
|
||||
String device = p.getFirst("Device");
|
||||
VolumeAttachment attachment = service.attachVolume(region, volumeId, instanceId, device);
|
||||
return volumeAttachmentResponse("AttachVolume", attachment, "attaching");
|
||||
}
|
||||
|
||||
private Response handleDetachVolume(MultivaluedMap<String, String> p, String region) {
|
||||
String volumeId = p.getFirst("VolumeId");
|
||||
String instanceId = p.getFirst("InstanceId");
|
||||
String device = p.getFirst("Device");
|
||||
boolean force = "true".equalsIgnoreCase(p.getFirst("Force"));
|
||||
VolumeAttachment attachment = service.detachVolume(region, volumeId, instanceId, device, force);
|
||||
return volumeAttachmentResponse("DetachVolume", attachment, "detaching");
|
||||
}
|
||||
|
||||
private Response volumeAttachmentResponse(String action, VolumeAttachment attachment, String status) {
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start(action + "Response", AwsNamespaces.EC2)
|
||||
.elem("requestId", UUID.randomUUID().toString())
|
||||
.elem("volumeId", attachment.getVolumeId())
|
||||
.elem("instanceId", attachment.getInstanceId())
|
||||
.elem("device", attachment.getDevice())
|
||||
.elem("status", status)
|
||||
.elem("deleteOnTermination", String.valueOf(attachment.isDeleteOnTermination()));
|
||||
if (attachment.getAttachTime() != null) {
|
||||
xml.elem("attachTime", ISO_FMT.format(attachment.getAttachTime()));
|
||||
}
|
||||
xml.end(action + "Response");
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
private String volumeXml(Volume vol) {
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.elem("volumeId", vol.getVolumeId())
|
||||
|
||||
@@ -2918,6 +2918,95 @@ public class Ec2Service implements ContainerTeardown {
|
||||
volumes.delete(key(region, volumeId));
|
||||
}
|
||||
|
||||
public VolumeAttachment attachVolume(String region, String volumeId, String instanceId, String device) {
|
||||
ensureDefaultResources(region);
|
||||
if (volumeId == null || volumeId.isEmpty()) {
|
||||
throw new AwsException("MissingParameter",
|
||||
"The parameter VolumeId is missing", 400);
|
||||
}
|
||||
if (instanceId == null || instanceId.isEmpty()) {
|
||||
throw new AwsException("MissingParameter",
|
||||
"The parameter InstanceId is missing", 400);
|
||||
}
|
||||
if (device == null || device.isEmpty()) {
|
||||
throw new AwsException("MissingParameter",
|
||||
"The parameter Device is missing", 400);
|
||||
}
|
||||
Volume volume = getRequiredVolume(region, volumeId);
|
||||
Instance inst = getRequiredInstance(region, instanceId);
|
||||
if (!List.of("running", "stopped").contains(inst.getState().getName())) {
|
||||
throw new AwsException("IncorrectInstanceState",
|
||||
"The instance '" + inst.getInstanceId() + "' is not in a state from which it can be attached", 400);
|
||||
}
|
||||
if (!inst.getPlacement().getAvailabilityZone().equals(volume.getAvailabilityZone())) {
|
||||
throw new AwsException(
|
||||
"InvalidParameterValue",
|
||||
"The volume '" + volume.getVolumeId() +
|
||||
"' and instance '" + inst.getInstanceId() +
|
||||
"' must be in the same Availability Zone", 400);
|
||||
}
|
||||
if (!"available".equals(volume.getState())) {
|
||||
throw new AwsException("VolumeInUse",
|
||||
"Volume '" + volumeId + "' is already attached", 400);
|
||||
}
|
||||
|
||||
VolumeAttachment attachment = new VolumeAttachment();
|
||||
attachment.setVolumeId(volumeId);
|
||||
attachment.setInstanceId(instanceId);
|
||||
attachment.setDevice(device);
|
||||
attachment.setState("attached");
|
||||
attachment.setAttachTime(Instant.now());
|
||||
attachment.setDeleteOnTermination(false); // Default for attached volumes
|
||||
|
||||
volume.getAttachments().add(attachment);
|
||||
volume.setState("in-use");
|
||||
volumes.put(key(region, volumeId), volume);
|
||||
return attachment;
|
||||
}
|
||||
|
||||
public VolumeAttachment detachVolume(String region, String volumeId, String instanceId, String device, boolean force) {
|
||||
if (volumeId == null || volumeId.isEmpty()) {
|
||||
throw new AwsException("MissingParameter", "The parameter VolumeId is missing", 400);
|
||||
}
|
||||
ensureDefaultResources(region);
|
||||
Volume volume = getRequiredVolume(region, volumeId);
|
||||
|
||||
if ("available".equals(volume.getState()) || volume.getAttachments().isEmpty()) {
|
||||
throw new AwsException("InvalidVolume.NotAttached",
|
||||
"Volume '" + volumeId + "' is not attached", 400);
|
||||
}
|
||||
VolumeAttachment target = volume.getAttachments().getFirst();
|
||||
if (instanceId != null && !target.getInstanceId().equals(instanceId)) {
|
||||
throw new AwsException("InvalidAttachment.NotFound",
|
||||
"Volume '" + volumeId + "' is not attached to instance '" + instanceId + "'", 400);
|
||||
}
|
||||
if (device != null && !target.getDevice().equals(device)) {
|
||||
throw new AwsException("InvalidAttachment.NotFound",
|
||||
"Volume '" + volumeId + "' is not attached with device '" + device + "'", 400);
|
||||
}
|
||||
Instance inst = getRequiredInstance(region, target.getInstanceId());
|
||||
if (!inst.getState().getName().equals("stopped") && target.getDevice().equals(inst.getRootDeviceName())) {
|
||||
throw new AwsException("OperationNotPermitted",
|
||||
"The root volume of an instance cannot be detached while the instance is running", 400);
|
||||
}
|
||||
if (!force && target.getDevice().equals(inst.getRootDeviceName())) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"Device " + inst.getRootDeviceName() + " has the root partition on it. Detaching it will damage the " +
|
||||
"filesystem/partition tables. To force detachment, use the force parameter", 400);
|
||||
}
|
||||
target.setState("detached");
|
||||
volume.getAttachments().clear();
|
||||
volume.setState("available");
|
||||
volumes.put(key(region, volumeId), volume);
|
||||
return target;
|
||||
}
|
||||
|
||||
private Volume getRequiredVolume(String region, String volumeId) {
|
||||
return volumes.get(key(region, volumeId)).orElseThrow(() ->
|
||||
new AwsException("InvalidVolume.NotFound", "The volume '" + volumeId + "' does not exist", 400)
|
||||
);
|
||||
}
|
||||
|
||||
// ─── Network Interfaces ─────────────────────────────────────────────────────
|
||||
|
||||
public NetworkInterfaceListResult describeNetworkInterfaces(String region, List<String> networkInterfaceIds,
|
||||
|
||||
Reference in new issue
Block a user