mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(ec2): honor RunInstances dry run before provisioning (#3599)
* fix(ec2): honor RunInstances dry run before provisioning * fix(ec2): validate launch input before dry-run success * fix(ec2): validate dry runs through the launch service
This commit is contained in:
1 parent
872463f83a
commit
46b66ffa40
6 files changed
+256
-5
No files matched your search
@@ -674,8 +674,11 @@ public class Ec2QueryHandler {
|
||||
private Response handleRunInstances(MultivaluedMap<String, String> p, String region) {
|
||||
String imageId = p.getFirst("ImageId");
|
||||
String instanceType = p.getFirst("InstanceType");
|
||||
int minCount = Integer.parseInt(p.getOrDefault("MinCount", List.of("1")).get(0));
|
||||
int maxCount = Integer.parseInt(p.getOrDefault("MaxCount", List.of("1")).get(0));
|
||||
int minCount = parseRunInstancesCount(p, "MinCount");
|
||||
int maxCount = parseRunInstancesCount(p, "MaxCount");
|
||||
if (maxCount < minCount) {
|
||||
throw new AwsException("InvalidParameterValue", "MaxCount must be greater than or equal to MinCount", 400);
|
||||
}
|
||||
String keyName = p.getFirst("KeyName");
|
||||
String subnetId = p.getFirst("SubnetId");
|
||||
// The launch-time public-IP override arrives either on the primary
|
||||
@@ -767,7 +770,7 @@ public class Ec2QueryHandler {
|
||||
Reservation res = service.runInstances(region, imageId, instanceType, minCount, maxCount,
|
||||
keyName, sgIds, subnetId, clientToken, instanceTags, userData, iamInstanceProfileArn,
|
||||
associatePublicIp, networkInterfaceId, networkInterfaceDeviceIndex, null, metadataOptions,
|
||||
creditSpecificationCpuCredits, userDataEncoded);
|
||||
creditSpecificationCpuCredits, userDataEncoded, Boolean.parseBoolean(p.getFirst("DryRun")));
|
||||
|
||||
if (!networkInterfaceTags.isEmpty()) {
|
||||
List<String> eniIds = new ArrayList<>();
|
||||
@@ -794,6 +797,22 @@ public class Ec2QueryHandler {
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
private int parseRunInstancesCount(MultivaluedMap<String, String> p, String name) {
|
||||
String value = p.getFirst(name);
|
||||
if (value == null) {
|
||||
return 1;
|
||||
}
|
||||
try {
|
||||
int count = Integer.parseInt(value);
|
||||
if (count > 0) {
|
||||
return count;
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
// Malformed and non-positive counts share the same EC2 validation error.
|
||||
}
|
||||
throw new AwsException("InvalidParameterValue", name + " must be a positive integer", 400);
|
||||
}
|
||||
|
||||
private LaunchTemplateData resolveRunInstancesLaunchTemplateData(MultivaluedMap<String, String> p, String region) {
|
||||
String id = p.getFirst("LaunchTemplate.LaunchTemplateId");
|
||||
String name = p.getFirst("LaunchTemplate.LaunchTemplateName");
|
||||
|
||||
@@ -2487,6 +2487,21 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
int networkInterfaceDeviceIndex, String availabilityZone,
|
||||
LaunchTemplateData.MetadataOptions metadataOptions,
|
||||
String creditSpecificationCpuCredits, String encodedUserData) {
|
||||
return runInstances(region, imageId, instanceType, minCount, maxCount, keyName,
|
||||
securityGroupIds, subnetId, clientToken, instanceTags, userData, iamInstanceProfileArn,
|
||||
associatePublicIp, networkInterfaceId, networkInterfaceDeviceIndex, availabilityZone,
|
||||
metadataOptions, creditSpecificationCpuCredits, encodedUserData, false);
|
||||
}
|
||||
|
||||
public Reservation runInstances(String region, String imageId, String instanceType,
|
||||
int minCount, int maxCount, String keyName,
|
||||
List<String> securityGroupIds, String subnetId,
|
||||
String clientToken, List<Tag> instanceTags,
|
||||
String userData, String iamInstanceProfileArn,
|
||||
Boolean associatePublicIp, String networkInterfaceId,
|
||||
int networkInterfaceDeviceIndex, String availabilityZone,
|
||||
LaunchTemplateData.MetadataOptions metadataOptions,
|
||||
String creditSpecificationCpuCredits, String encodedUserData, boolean dryRun) {
|
||||
if (imageId == null || imageId.isBlank()) {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter ImageId", 400);
|
||||
}
|
||||
@@ -2597,6 +2612,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
dockerImage.cloudInit(), dockerImage.dockerPlatform());
|
||||
}
|
||||
}
|
||||
if (dryRun) {
|
||||
throw new AwsException("DryRunOperation", "Request would have succeeded, but DryRun flag is set.", 412);
|
||||
}
|
||||
for (int i = 0; i < count; i++) {
|
||||
String instanceId = "i-" + randomHex(17);
|
||||
String privateIp = suppliedEni != null
|
||||
|
||||
Reference in new issue
Block a user