use of com.amazonaws.services.autoscaling.model.DetachInstancesRequest in project cloudbreak by hortonworks.
the class AwsResourceConnector method downscale.
@Override
public List<CloudResourceStatus> downscale(AuthenticatedContext auth, CloudStack stack, List<CloudResource> resources, List<CloudInstance> vms, Object resourcesToRemove) {
Collection<String> instanceIds = new ArrayList<>();
for (CloudInstance vm : vms) {
instanceIds.add(vm.getInstanceId());
}
String asGroupName = cfStackUtil.getAutoscalingGroupName(auth, vms.get(0).getTemplate().getGroupName(), auth.getCloudContext().getLocation().getRegion().value());
DetachInstancesRequest detachInstancesRequest = new DetachInstancesRequest().withAutoScalingGroupName(asGroupName).withInstanceIds(instanceIds).withShouldDecrementDesiredCapacity(true);
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(auth.getCloudCredential()), auth.getCloudContext().getLocation().getRegion().value());
try {
amazonASClient.detachInstances(detachInstancesRequest);
} catch (AmazonServiceException e) {
if (!"ValidationError".equals(e.getErrorCode()) || !e.getErrorMessage().contains("not part of Auto Scaling") || instanceIds.stream().anyMatch(id -> !e.getErrorMessage().contains(id))) {
throw e;
}
LOGGER.info(e.getErrorMessage());
}
AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(auth.getCloudCredential()), auth.getCloudContext().getLocation().getRegion().value());
try {
amazonEC2Client.terminateInstances(new TerminateInstancesRequest().withInstanceIds(instanceIds));
} catch (AmazonServiceException e) {
if (!"InvalidInstanceID.NotFound".equals(e.getErrorCode())) {
throw e;
}
LOGGER.info(e.getErrorMessage());
}
LOGGER.info("Terminated instances in stack '{}': '{}'", auth.getCloudContext().getId(), instanceIds);
return check(auth, resources);
}
Aggregations