mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(ec2): implement DescribeSpotPriceHistory (#3359)
* feat(ec2): implement DescribeSpotPriceHistory * test(ec2): verify DescribeSpotPriceHistory with SDK * fix(ec2): honor spot price history dry run
This commit is contained in:
1 parent
7c08b35f1d
commit
ee23e26bc3
3 files changed
+66
-1
No files matched your search
+45
@@ -0,0 +1,45 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
|
||||
import software.amazon.awssdk.services.ec2.model.DescribeSpotPriceHistoryRequest;
|
||||
import software.amazon.awssdk.services.ec2.model.InstanceType;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class Ec2DescribeSpotPriceHistoryTest {
|
||||
|
||||
@Test
|
||||
void describeSpotPriceHistoryReturnsEmptyHistoryWhenNoSnapshotIsConfigured() {
|
||||
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
|
||||
var response = ec2.describeSpotPriceHistory(DescribeSpotPriceHistoryRequest.builder()
|
||||
.instanceTypes(InstanceType.M5_LARGE, InstanceType.fromValue("t4g.medium"))
|
||||
.productDescriptions("Linux/UNIX")
|
||||
.availabilityZone("us-east-1a")
|
||||
.startTime(Instant.parse("2026-01-01T00:00:00Z"))
|
||||
.endTime(Instant.parse("2026-01-02T00:00:00Z"))
|
||||
.maxResults(100)
|
||||
.build());
|
||||
|
||||
assertThat(response.spotPriceHistory()).isEmpty();
|
||||
assertThat(response.nextToken()).isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeSpotPriceHistoryHonorsDryRun() {
|
||||
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
|
||||
Ec2Exception exception = assertThrows(Ec2Exception.class,
|
||||
() -> ec2.describeSpotPriceHistory(DescribeSpotPriceHistoryRequest.builder()
|
||||
.dryRun(true)
|
||||
.build()));
|
||||
|
||||
assertThat(exception.statusCode()).isEqualTo(412);
|
||||
assertThat(exception.awsErrorDetails().errorCode()).isEqualTo("DryRunOperation");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -171,7 +171,7 @@ public class AwsQueryController {
|
||||
"DescribeAddressesAttribute",
|
||||
"DescribeIamInstanceProfileAssociations",
|
||||
"DescribeAvailabilityZones", "DescribeRegions", "DescribeAccountAttributes",
|
||||
"DescribeInstanceTypes", "DescribeInstanceTypeOfferings",
|
||||
"DescribeInstanceTypes", "DescribeInstanceTypeOfferings", "DescribeSpotPriceHistory",
|
||||
"CreateLaunchTemplate", "CreateLaunchTemplateVersion", "DescribeLaunchTemplates", "DescribeLaunchTemplateVersions",
|
||||
"ModifyLaunchTemplate", "DeleteLaunchTemplate",
|
||||
"DescribeNetworkInterfaces",
|
||||
|
||||
@@ -256,6 +256,7 @@ public class Ec2QueryHandler {
|
||||
case "RequestSpotInstances" -> handleRequestSpotInstances(params, region);
|
||||
case "DescribeSpotInstanceRequests" -> handleDescribeSpotInstanceRequests(params, region);
|
||||
case "CancelSpotInstanceRequests" -> handleCancelSpotInstanceRequests(params, region);
|
||||
case "DescribeSpotPriceHistory" -> handleDescribeSpotPriceHistory(params, region);
|
||||
// IPAM
|
||||
case "EnableIpamOrganizationAdminAccount" -> handleEnableIpamOrgAdmin(params);
|
||||
case "DisableIpamOrganizationAdminAccount" -> handleDisableIpamOrgAdmin(params);
|
||||
@@ -5376,6 +5377,25 @@ public class Ec2QueryHandler {
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the EC2 Query response shape for spot price history.
|
||||
*
|
||||
* <p>Floci does not currently maintain a spot-price snapshot. AWS returns an empty
|
||||
* {@code spotPriceHistorySet} when no matching records exist, which is sufficient for
|
||||
* clients such as Karpenter to distinguish an empty result from an unsupported action.</p>
|
||||
*/
|
||||
private Response handleDescribeSpotPriceHistory(MultivaluedMap<String, String> p, String region) {
|
||||
checkDryRun(p);
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("DescribeSpotPriceHistoryResponse", AwsNamespaces.EC2)
|
||||
.elem("requestId", UUID.randomUUID().toString())
|
||||
.elem("nextToken", "")
|
||||
.start("spotPriceHistorySet")
|
||||
.end("spotPriceHistorySet")
|
||||
.end("DescribeSpotPriceHistoryResponse");
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
private String spotInstanceRequestXml(SpotInstanceRequest sir) {
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.elem("spotInstanceRequestId", sir.getSpotInstanceRequestId())
|
||||
|
||||
Reference in new issue
Block a user