Search in sources :

Example 1 with DetachInstancesRequest

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);
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) ArrayList(java.util.ArrayList) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) DetachInstancesRequest(com.amazonaws.services.autoscaling.model.DetachInstancesRequest) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)1 DetachInstancesRequest (com.amazonaws.services.autoscaling.model.DetachInstancesRequest)1 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)1 TerminateInstancesRequest (com.amazonaws.services.ec2.model.TerminateInstancesRequest)1 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)1 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)1 ArrayList (java.util.ArrayList)1