mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
feat(ec2): implement CreateVolume, DescribeVolumes, DeleteVolume (#455)
Adds EBS volume support to the EC2 service — the minimum viable volume API needed to unblock AWS metadata workflows. New model classes: - Volume (volumeId, volumeType, size, state, availabilityZone, encrypted, iops, snapshotId, createTime, tags, attachments) - VolumeAttachment (instanceId, device, state, attachTime, deleteOnTermination, volumeId) Service changes (Ec2Service): - In-memory ConcurrentHashMap store keyed by region::volumeId - createVolume() with TagSpecification support - describeVolumes() with ID list + filter support - deleteVolume() with InvalidVolume.NotFound on missing ID - Filter support: volume-id, status, volume-type, availability-zone, encrypted, tag:*, tag-key, tag-value Handler changes (Ec2QueryHandler): - CreateVolume, DescribeVolumes, DeleteVolume cases in switch - volumeXml() serializer matching AWS EC2 Query Protocol wire format (volumeSet/item, attachmentSet, nextToken) - TagSpecification.N.ResourceType=volume parsing on create Integration tests (Ec2IntegrationTest): - create → describe by ID - filter by status and volume-type - delete → NotFound on subsequent describe - InvalidVolume.NotFound error case
This commit is contained in:
1 parent
b9ddb8ee5d
commit
c030af9a0d
5 files changed
+378
No files matched your search
@@ -107,6 +107,10 @@ public class Ec2QueryHandler {
|
||||
case "DescribeAccountAttributes" -> handleDescribeAccountAttributes(params, region);
|
||||
// Instance Types
|
||||
case "DescribeInstanceTypes" -> handleDescribeInstanceTypes(params, region);
|
||||
// Volumes
|
||||
case "CreateVolume" -> handleCreateVolume(params, region);
|
||||
case "DescribeVolumes" -> handleDescribeVolumes(params, region);
|
||||
case "DeleteVolume" -> handleDeleteVolume(params, region);
|
||||
default -> ec2Error("UnsupportedOperation",
|
||||
"Operation " + action + " is not supported.", 400);
|
||||
};
|
||||
@@ -1268,6 +1272,100 @@ public class Ec2QueryHandler {
|
||||
return xml.build();
|
||||
}
|
||||
|
||||
// ─── Volume handlers ──────────────────────────────────────────────────────
|
||||
|
||||
private Response handleCreateVolume(MultivaluedMap<String, String> p, String region) {
|
||||
String availabilityZone = p.getFirst("AvailabilityZone");
|
||||
String volumeType = p.getFirst("VolumeType");
|
||||
String sizeStr = p.getFirst("Size");
|
||||
int size = sizeStr != null ? Integer.parseInt(sizeStr) : 8;
|
||||
String encryptedStr = p.getFirst("Encrypted");
|
||||
boolean encrypted = "true".equalsIgnoreCase(encryptedStr);
|
||||
String iopsStr = p.getFirst("Iops");
|
||||
int iops = iopsStr != null ? Integer.parseInt(iopsStr) : 0;
|
||||
String snapshotId = p.getFirst("SnapshotId");
|
||||
|
||||
List<Tag> volumeTags = new ArrayList<>();
|
||||
for (int i = 1; ; i++) {
|
||||
String resType = p.getFirst("TagSpecification." + i + ".ResourceType");
|
||||
if (resType == null) break;
|
||||
if ("volume".equals(resType)) {
|
||||
for (int j = 1; ; j++) {
|
||||
String k = p.getFirst("TagSpecification." + i + ".Tag." + j + ".Key");
|
||||
if (k == null) break;
|
||||
String v = p.getFirst("TagSpecification." + i + ".Tag." + j + ".Value");
|
||||
volumeTags.add(new Tag(k, v));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Volume vol = service.createVolume(region, availabilityZone, volumeType, size,
|
||||
encrypted, iops, snapshotId, volumeTags);
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("CreateVolumeResponse", AwsNamespaces.EC2)
|
||||
.elem("requestId", UUID.randomUUID().toString())
|
||||
.raw(volumeXml(vol))
|
||||
.end("CreateVolumeResponse");
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
private Response handleDescribeVolumes(MultivaluedMap<String, String> p, String region) {
|
||||
List<String> ids = getList(p, "VolumeId");
|
||||
Map<String, List<String>> filters = getFilters(p);
|
||||
List<Volume> volList = service.describeVolumes(region, ids, filters);
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("DescribeVolumesResponse", AwsNamespaces.EC2)
|
||||
.elem("requestId", UUID.randomUUID().toString())
|
||||
.start("volumeSet");
|
||||
for (Volume vol : volList) {
|
||||
xml.start("item").raw(volumeXml(vol)).end("item");
|
||||
}
|
||||
xml.end("volumeSet")
|
||||
.elem("nextToken", "")
|
||||
.end("DescribeVolumesResponse");
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
private Response handleDeleteVolume(MultivaluedMap<String, String> p, String region) {
|
||||
service.deleteVolume(region, p.getFirst("VolumeId"));
|
||||
return booleanResponse("DeleteVolume");
|
||||
}
|
||||
|
||||
private String volumeXml(Volume vol) {
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.elem("volumeId", vol.getVolumeId())
|
||||
.elem("size", String.valueOf(vol.getSize()))
|
||||
.elem("volumeType", vol.getVolumeType())
|
||||
.elem("status", vol.getState())
|
||||
.elem("availabilityZone", vol.getAvailabilityZone())
|
||||
.elem("encrypted", String.valueOf(vol.isEncrypted()));
|
||||
if (vol.getIops() > 0) {
|
||||
xml.elem("iops", String.valueOf(vol.getIops()));
|
||||
}
|
||||
if (vol.getSnapshotId() != null) {
|
||||
xml.elem("snapshotId", vol.getSnapshotId());
|
||||
}
|
||||
if (vol.getCreateTime() != null) {
|
||||
xml.elem("createTime", ISO_FMT.format(vol.getCreateTime()));
|
||||
}
|
||||
xml.start("attachmentSet");
|
||||
for (VolumeAttachment att : vol.getAttachments()) {
|
||||
xml.start("item")
|
||||
.elem("volumeId", att.getVolumeId())
|
||||
.elem("instanceId", att.getInstanceId())
|
||||
.elem("device", att.getDevice())
|
||||
.elem("status", att.getState())
|
||||
.elem("deleteOnTermination", String.valueOf(att.isDeleteOnTermination()));
|
||||
if (att.getAttachTime() != null) {
|
||||
xml.elem("attachTime", ISO_FMT.format(att.getAttachTime()));
|
||||
}
|
||||
xml.end("item");
|
||||
}
|
||||
xml.end("attachmentSet")
|
||||
.raw(tagSetXml(vol.getTags()));
|
||||
return xml.build();
|
||||
}
|
||||
|
||||
private String ipPermissionsXml(List<IpPermission> perms, String wrapperTag) {
|
||||
XmlBuilder xml = new XmlBuilder().start(wrapperTag);
|
||||
for (IpPermission perm : perms) {
|
||||
|
||||
@@ -30,6 +30,7 @@ public class Ec2Service {
|
||||
private final Map<String, KeyPair> keyPairs = new ConcurrentHashMap<>();
|
||||
private final Map<String, Address> addresses = new ConcurrentHashMap<>();
|
||||
private final Map<String, Instance> instances = new ConcurrentHashMap<>();
|
||||
private final Map<String, Volume> volumes = new ConcurrentHashMap<>();
|
||||
// resourceId → List<Tag>
|
||||
private final Map<String, List<Tag>> tags = new ConcurrentHashMap<>();
|
||||
private final Set<String> seededRegions = ConcurrentHashMap.newKeySet();
|
||||
@@ -1196,6 +1197,16 @@ public class Ec2Service {
|
||||
default -> true;
|
||||
};
|
||||
}
|
||||
if (resource instanceof Volume vol) {
|
||||
return switch (filterName) {
|
||||
case "volume-id" -> values.contains(vol.getVolumeId());
|
||||
case "status" -> values.contains(vol.getState());
|
||||
case "volume-type" -> values.contains(vol.getVolumeType());
|
||||
case "availability-zone" -> values.contains(vol.getAvailabilityZone());
|
||||
case "encrypted" -> values.contains(String.valueOf(vol.isEncrypted()));
|
||||
default -> true;
|
||||
};
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1209,6 +1220,54 @@ public class Ec2Service {
|
||||
if (resource instanceof RouteTable rt) return rt.getTags();
|
||||
if (resource instanceof KeyPair kp) return kp.getTags();
|
||||
if (resource instanceof Address addr) return addr.getTags();
|
||||
if (resource instanceof Volume vol) return vol.getTags();
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// ─── Volumes ───────────────────────────────────────────────────────────────
|
||||
|
||||
public Volume createVolume(String region, String availabilityZone, String volumeType,
|
||||
int size, boolean encrypted, int iops, String snapshotId,
|
||||
List<Tag> volumeTags) {
|
||||
ensureDefaultResources(region);
|
||||
String volumeId = "vol-" + randomHex(17);
|
||||
Volume vol = new Volume();
|
||||
vol.setVolumeId(volumeId);
|
||||
vol.setAvailabilityZone(availabilityZone != null ? availabilityZone : region + "a");
|
||||
vol.setVolumeType(volumeType != null ? volumeType : "gp2");
|
||||
vol.setSize(size > 0 ? size : 8);
|
||||
vol.setEncrypted(encrypted);
|
||||
vol.setIops(iops > 0 ? iops : (volumeType != null && volumeType.startsWith("io") ? iops : 0));
|
||||
vol.setSnapshotId(snapshotId);
|
||||
vol.setCreateTime(Instant.now());
|
||||
vol.setState("available");
|
||||
vol.setRegion(region);
|
||||
if (volumeTags != null) vol.setTags(new ArrayList<>(volumeTags));
|
||||
volumes.put(key(region, volumeId), vol);
|
||||
return vol;
|
||||
}
|
||||
|
||||
public List<Volume> describeVolumes(String region, List<String> volumeIds,
|
||||
Map<String, List<String>> filters) {
|
||||
if (volumeIds != null && !volumeIds.isEmpty()) {
|
||||
for (String id : volumeIds) {
|
||||
if (volumes.get(key(region, id)) == null) {
|
||||
throw new AwsException("InvalidVolume.NotFound",
|
||||
"The volume '" + id + "' does not exist.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
return volumes.values().stream()
|
||||
.filter(v -> v.getRegion().equals(region))
|
||||
.filter(v -> volumeIds == null || volumeIds.isEmpty() || volumeIds.contains(v.getVolumeId()))
|
||||
.filter(v -> matchesFilters(v, filters, region))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void deleteVolume(String region, String volumeId) {
|
||||
if (volumes.remove(key(region, volumeId)) == null) {
|
||||
throw new AwsException("InvalidVolume.NotFound",
|
||||
"The volume '" + volumeId + "' does not exist.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package io.github.hectorvent.floci.services.ec2.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class Volume {
|
||||
|
||||
private String volumeId;
|
||||
private String volumeType; // gp2, gp3, io1, io2, st1, sc1, standard
|
||||
private int size; // GiB
|
||||
private String state; // creating, available, in-use, deleting, deleted, error
|
||||
private String availabilityZone;
|
||||
private boolean encrypted;
|
||||
private int iops;
|
||||
private String snapshotId;
|
||||
private Instant createTime;
|
||||
private String region;
|
||||
private List<Tag> tags = new ArrayList<>();
|
||||
private List<VolumeAttachment> attachments = new ArrayList<>();
|
||||
|
||||
public Volume() {}
|
||||
|
||||
public String getVolumeId() { return volumeId; }
|
||||
public void setVolumeId(String volumeId) { this.volumeId = volumeId; }
|
||||
|
||||
public String getVolumeType() { return volumeType; }
|
||||
public void setVolumeType(String volumeType) { this.volumeType = volumeType; }
|
||||
|
||||
public int getSize() { return size; }
|
||||
public void setSize(int size) { this.size = size; }
|
||||
|
||||
public String getState() { return state; }
|
||||
public void setState(String state) { this.state = state; }
|
||||
|
||||
public String getAvailabilityZone() { return availabilityZone; }
|
||||
public void setAvailabilityZone(String availabilityZone) { this.availabilityZone = availabilityZone; }
|
||||
|
||||
public boolean isEncrypted() { return encrypted; }
|
||||
public void setEncrypted(boolean encrypted) { this.encrypted = encrypted; }
|
||||
|
||||
public int getIops() { return iops; }
|
||||
public void setIops(int iops) { this.iops = iops; }
|
||||
|
||||
public String getSnapshotId() { return snapshotId; }
|
||||
public void setSnapshotId(String snapshotId) { this.snapshotId = snapshotId; }
|
||||
|
||||
public Instant getCreateTime() { return createTime; }
|
||||
public void setCreateTime(Instant createTime) { this.createTime = createTime; }
|
||||
|
||||
public String getRegion() { return region; }
|
||||
public void setRegion(String region) { this.region = region; }
|
||||
|
||||
public List<Tag> getTags() { return tags; }
|
||||
public void setTags(List<Tag> tags) { this.tags = tags; }
|
||||
|
||||
public List<VolumeAttachment> getAttachments() { return attachments; }
|
||||
public void setAttachments(List<VolumeAttachment> attachments) { this.attachments = attachments; }
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package io.github.hectorvent.floci.services.ec2.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class VolumeAttachment {
|
||||
|
||||
private String instanceId;
|
||||
private String device;
|
||||
private String state; // attaching, attached, detaching, detached
|
||||
private Instant attachTime;
|
||||
private boolean deleteOnTermination;
|
||||
private String volumeId;
|
||||
|
||||
public VolumeAttachment() {}
|
||||
|
||||
public String getInstanceId() { return instanceId; }
|
||||
public void setInstanceId(String instanceId) { this.instanceId = instanceId; }
|
||||
|
||||
public String getDevice() { return device; }
|
||||
public void setDevice(String device) { this.device = device; }
|
||||
|
||||
public String getState() { return state; }
|
||||
public void setState(String state) { this.state = state; }
|
||||
|
||||
public Instant getAttachTime() { return attachTime; }
|
||||
public void setAttachTime(Instant attachTime) { this.attachTime = attachTime; }
|
||||
|
||||
public boolean isDeleteOnTermination() { return deleteOnTermination; }
|
||||
public void setDeleteOnTermination(boolean deleteOnTermination) { this.deleteOnTermination = deleteOnTermination; }
|
||||
|
||||
public String getVolumeId() { return volumeId; }
|
||||
public void setVolumeId(String volumeId) { this.volumeId = volumeId; }
|
||||
}
|
||||
Reference in new issue
Block a user