use of com.amazonaws.services.ec2.model.DescribeNatGatewaysResult in project photon-model by vmware.
the class AWSTaskStatusChecker method buildHandler.
private AsyncHandler buildHandler(T type) {
return new AsyncHandler<AmazonWebServiceRequest, AmazonWebServiceResult>() {
@Override
public void onError(Exception exception) {
// particular instanceId.
if (exception instanceof AmazonServiceException && ((AmazonServiceException) exception).getErrorCode().equalsIgnoreCase(AWS_INVALID_INSTANCE_ID_ERROR_CODE)) {
AWSTaskStatusChecker.this.service.logWarning("Could not retrieve status for instance %s. Retrying... Exception on AWS is %s", AWSTaskStatusChecker.this.instanceId, exception);
AWSTaskStatusChecker.create(AWSTaskStatusChecker.this.amazonEC2Client, AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, AWSTaskStatusChecker.this.failureStates, AWSTaskStatusChecker.this.consumer, AWSTaskStatusChecker.this.taskManager, AWSTaskStatusChecker.this.service, AWSTaskStatusChecker.this.expirationTimeMicros).start(type);
return;
} else if (exception instanceof AmazonEC2Exception && ((AmazonEC2Exception) exception).getErrorCode().equalsIgnoreCase(AWS_INVALID_VOLUME_ID_ERROR_CODE)) {
AWSTaskStatusChecker.this.consumer.accept(null);
return;
}
AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(exception);
return;
}
@Override
public void onSuccess(AmazonWebServiceRequest request, AmazonWebServiceResult result) {
String status;
Object instance;
String failureMessage = null;
String stateReason = null;
if (result instanceof DescribeInstancesResult) {
instance = ((DescribeInstancesResult) result).getReservations().get(0).getInstances().get(0);
Instance vm = (Instance) instance;
status = vm.getState().getName();
stateReason = vm.getStateReason() != null ? vm.getStateReason().getMessage() : null;
} else if (result instanceof DescribeNatGatewaysResult) {
instance = ((DescribeNatGatewaysResult) result).getNatGateways().get(0);
status = ((NatGateway) instance).getState();
// if NAT gateway creation fails, the status is still "pending";
// rather than keep checking for status and eventually time out, get the
// failure message and fail the task
failureMessage = ((NatGateway) instance).getFailureMessage();
} else if (result instanceof DescribeVolumesResult) {
instance = ((DescribeVolumesResult) result).getVolumes().get(0);
status = ((Volume) instance).getState().toLowerCase();
} else {
AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalArgumentException("Invalid type " + result));
return;
}
if (failureMessage != null) {
// operation failed; no need to keep checking for desired status
AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalStateException(failureMessage));
return;
} else if (AWSTaskStatusChecker.this.failureStates.contains(status)) {
// operation failed; no need to keep checking for desired status
AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalStateException("Resource is state:[" + status + "]," + "reason:" + stateReason));
return;
} else if (!status.equals(AWSTaskStatusChecker.this.desiredState)) {
AWSTaskStatusChecker.this.service.logInfo("Instance %s not yet in desired state %s. Current state %s, failure states %s, waiting 5s", AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, status, AWSTaskStatusChecker.this.failureStates);
// if the instance is not in the desired state, schedule thread
// to run again in 5 seconds
AWSTaskStatusChecker.this.service.getHost().schedule(() -> {
AWSTaskStatusChecker.create(AWSTaskStatusChecker.this.amazonEC2Client, AWSTaskStatusChecker.this.instanceId, AWSTaskStatusChecker.this.desiredState, AWSTaskStatusChecker.this.failureStates, AWSTaskStatusChecker.this.consumer, AWSTaskStatusChecker.this.taskManager, AWSTaskStatusChecker.this.service, AWSTaskStatusChecker.this.expirationTimeMicros).start(type);
}, 5, TimeUnit.SECONDS);
return;
}
AWSTaskStatusChecker.this.consumer.accept(instance);
return;
}
};
}
use of com.amazonaws.services.ec2.model.DescribeNatGatewaysResult in project photon-model by vmware.
the class AWSRemoteCleanup method deleteNATGateway.
private void deleteNATGateway(String vpcId, AmazonEC2 usEastEc2Client) {
DescribeNatGatewaysRequest natGatewaysRequest = new DescribeNatGatewaysRequest().withFilter(new Filter(VPC_KEY, Collections.singletonList(vpcId)));
DescribeNatGatewaysResult natGatewaysResult = usEastEc2Client.describeNatGateways(natGatewaysRequest);
natGatewaysResult.getNatGateways().forEach(natGateway -> {
DeleteNatGatewayRequest deleteNatGatewayRequest = new DeleteNatGatewayRequest().withNatGatewayId(natGateway.getNatGatewayId());
this.host.log("Terminating stale NAT gateway: %s", natGateway.getNatGatewayId());
usEastEc2Client.deleteNatGateway(deleteNatGatewayRequest);
});
}
Aggregations