use of software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class AWSTaskStatusChecker method runSearch.
private void runSearch(T type) {
AmazonWebServiceRequest descRequest = buildRequest(type);
AsyncHandler describeHandler = buildHandler(type);
if (type instanceof Instance) {
this.amazonEC2Client.describeInstancesAsync((DescribeInstancesRequest) descRequest, describeHandler);
} else if (type instanceof NatGateway) {
this.amazonEC2Client.describeNatGatewaysAsync((DescribeNatGatewaysRequest) descRequest, describeHandler);
} else if (type instanceof Volume) {
this.amazonEC2Client.describeVolumesAsync((DescribeVolumesRequest) descRequest, describeHandler);
} else {
AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalArgumentException("Invalid type " + type));
}
}
use of software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class AWSUtils method waitForTransitionCompletion.
public static void waitForTransitionCompletion(ServiceHost host, List<InstanceStateChange> stateChangeList, final String desiredState, AmazonEC2AsyncClient client, BiConsumer<InstanceState, Exception> callback) {
InstanceStateChange stateChange = stateChangeList.get(0);
try {
DescribeInstancesRequest request = new DescribeInstancesRequest();
request.withInstanceIds(stateChange.getInstanceId());
DescribeInstancesResult result = client.describeInstances(request);
Instance instance = result.getReservations().stream().flatMap(r -> r.getInstances().stream()).filter(i -> i.getInstanceId().equalsIgnoreCase(stateChange.getInstanceId())).findFirst().orElseThrow(() -> new IllegalArgumentException(String.format("%s instance not found", stateChange.getInstanceId())));
String state = instance.getState().getName();
if (state.equals(desiredState)) {
callback.accept(instance.getState(), null);
} else {
host.schedule(() -> waitForTransitionCompletion(host, stateChangeList, desiredState, client, callback), 5, TimeUnit.SECONDS);
}
} catch (AmazonServiceException | IllegalArgumentException ase) {
callback.accept(null, ase);
}
}
use of software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class AWSRemoteCleanup method deleteStaleAwsEc2instances.
/**
* Delete stale AWS EC2 Instances.
*/
@Test
public void deleteStaleAwsEc2instances() {
AmazonEC2 usEastEc2Client = this.ec2Clients.get(US_EAST_1_TAG);
List<String> instanceIdsToBeDeleted = new ArrayList<>();
DescribeInstancesResult instancesResult = usEastEc2Client.describeInstances();
List<Reservation> reservations = instancesResult.getReservations();
for (Reservation reservation : reservations) {
List<Instance> instances = reservation.getInstances();
for (Instance instance : instances) {
long instanceLaunchTimeMicros = TimeUnit.MILLISECONDS.toMicros(instance.getLaunchTime().getTime());
long timeDifference = Utils.getNowMicrosUtc() - instanceLaunchTimeMicros;
if (timeDifference > TimeUnit.HOURS.toMicros(1) && shouldDelete(instance)) {
this.host.log(Level.INFO, "Marking %s instance for deletion", instance.getInstanceId());
instanceIdsToBeDeleted.add(instance.getInstanceId());
}
}
}
triggerEC2Deletion(instanceIdsToBeDeleted, usEastEc2Client);
}
use of software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class AWSResetServiceTest method testReset.
// Test the reset operation.
@Test
public void testReset() throws Throwable {
provisionSingleAWS();
// check that the VM has been created
ProvisioningUtils.queryComputeInstances(this.host, 2);
ComputeState compute = getCompute(this.host, this.vmState.documentSelfLink);
if (!this.isMock) {
List<Instance> instances = getAwsInstancesByIds(this.client, this.host, Collections.singletonList(compute.id));
Instance instance = instances.get(0);
ComputeState vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
setVMSecurityGroupsToBeDeleted(instance, vm);
}
String taskLink = UUID.randomUUID().toString();
ResourceOperationRequest request = new ResourceOperationRequest();
request.isMockRequest = this.isMock;
request.operation = ResourceOperation.RESET.operation;
request.payload = new HashMap<>();
request.resourceReference = UriUtils.buildUri(this.host, compute.documentSelfLink);
request.taskReference = UriUtils.buildUri(this.host, taskLink);
TestContext ctx = this.host.testCreate(1);
createTaskResultListener(this.host, taskLink, (u) -> {
if (u.getAction() != Service.Action.PATCH) {
return false;
}
ResourceOperationResponse response = u.getBody(ResourceOperationResponse.class);
if (TaskState.isFailed(response.taskInfo)) {
ctx.failIteration(new IllegalStateException(response.taskInfo.failure.message));
} else if (TaskState.isFinished(response.taskInfo)) {
ctx.completeIteration();
}
return true;
});
TestContext ctx2 = this.host.testCreate(1);
Operation resetOp = Operation.createPatch(UriUtils.buildUri(this.host, AWSResetService.SELF_LINK)).setBody(request).setReferer(this.host.getReferer()).setCompletion((o, e) -> {
if (e != null) {
ctx2.failIteration(e);
return;
}
ctx2.completeIteration();
});
this.host.send(resetOp);
ctx2.await();
// in EC2 documentation
if (!this.isMock) {
// Waiting for power off
this.host.waitFor("Timed out waiting for EC2 to power off", () -> {
String state = getVMState(this.client, compute.id);
if (("stopping".equals(state) || "stopped".equals(state))) {
this.host.log(Level.INFO, "EC2 is being powered off");
return true;
} else {
return false;
}
});
// Waiting for power on
this.host.waitFor("Timed out waiting for EC2 to power on", () -> {
String state = getVMState(this.client, compute.id);
if ("running".equals(state)) {
this.host.log(Level.INFO, "EC2 is being powered on");
return true;
} else {
return false;
}
});
}
}
use of software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class TestAWSEnumerationUtils method testGetComputeDescriptionKeyFromAWSInstance.
@Test
public void testGetComputeDescriptionKeyFromAWSInstance() throws Throwable {
Map<String, ZoneData> zones = new HashMap<>();
zones.put(AWS_ZONE_ID, ZoneData.build(AWS_REGION_ID, AWS_ZONE_ID, ""));
Instance awsInstance = new Instance();
awsInstance.setInstanceId(AWS_INSTANCE_ID);
Placement placement = new Placement();
placement.setAvailabilityZone(AWS_ZONE_ID);
awsInstance.setPlacement(placement);
String regionId = getRegionId(awsInstance);
awsInstance.setInstanceType(AWS_INSTANCE_TYPE);
awsInstance.setVpcId(AWS_VPC_ID);
assertEquals(AWS_REGION_ID, regionId);
InstanceDescKey computeDescriptionKey = getKeyForComputeDescriptionFromInstance(awsInstance, zones);
assertEquals(AWS_COMPUTE_DESCRIPTION_KEY, computeDescriptionKey);
}
Aggregations