Search in sources :

Example 11 with AuthenticatedContextView

use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AuthenticatedContextView in project cloudbreak by hortonworks.

the class AwsDownscaleService method downscale.

public List<CloudResourceStatus> downscale(AuthenticatedContext auth, CloudStack stack, List<CloudResource> resources, List<CloudInstance> vmsToDownscale) {
    if (!vmsToDownscale.isEmpty()) {
        List<String> instanceIdsToDownscale = new ArrayList<>();
        for (CloudInstance vm : vmsToDownscale) {
            instanceIdsToDownscale.add(vm.getInstanceId());
        }
        AwsCredentialView credentialView = new AwsCredentialView(auth.getCloudCredential());
        AuthenticatedContextView authenticatedContextView = new AuthenticatedContextView(auth);
        String regionName = authenticatedContextView.getRegion();
        LOGGER.debug("Calling deleteCloudWatchAlarmsForSystemFailures from AwsDownscaleService");
        awsCloudWatchService.deleteCloudWatchAlarmsForSystemFailures(stack, regionName, credentialView, instanceIdsToDownscale);
        List<CloudResource> resourcesToDownscale = resources.stream().filter(resource -> instanceIdsToDownscale.contains(resource.getInstanceId())).collect(Collectors.toList());
        awsComputeResourceService.deleteComputeResources(auth, stack, resourcesToDownscale);
        AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, auth.getCloudContext().getLocation().getRegion().value());
        AmazonEc2Client amazonEC2Client = awsClient.createEc2Client(credentialView, auth.getCloudContext().getLocation().getRegion().value());
        Map<String, List<CloudInstance>> downscaledGroupsWithCloudInstances = vmsToDownscale.stream().collect(Collectors.groupingBy(cloudInstance -> cloudInstance.getTemplate().getGroupName()));
        Long stackId = auth.getCloudContext().getId();
        List<String> terminatedInstances = terminateInstances(auth, amazonASClient, amazonEC2Client, downscaledGroupsWithCloudInstances, stackId);
        if (!terminatedInstances.isEmpty()) {
            waitForTerminateInstances(stackId, terminatedInstances, amazonEC2Client);
        }
        updateAutoscalingGroups(auth, amazonASClient, downscaledGroupsWithCloudInstances);
        List<String> targetGroupArns = getTargetGroupArns(stack.getLoadBalancers(), auth);
        loadBalancerService.removeLoadBalancerTargets(auth, targetGroupArns, resourcesToDownscale);
    }
    return awsResourceConnector.check(auth, resources);
}
Also used : LoadBalancerService(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.LoadBalancerService) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair) LoggerFactory(org.slf4j.LoggerFactory) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) INSTANCE_NOT_FOUND_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.common.AwsInstanceConnector.INSTANCE_NOT_FOUND_ERROR_CODE) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) UpdateAutoScalingGroupRequest(com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Service(org.springframework.stereotype.Service) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) AwsTargetGroup(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsTargetGroup) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) Waiter(com.amazonaws.waiters.Waiter) Logger(org.slf4j.Logger) AuthenticatedContextView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AuthenticatedContextView) AmazonServiceException(com.amazonaws.AmazonServiceException) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) DetachInstancesRequest(com.amazonaws.services.autoscaling.model.DetachInstancesRequest) AwsCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Collection(java.util.Collection) CloudFormationStackUtil(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationStackUtil) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Set(java.util.Set) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) Collectors(java.util.stream.Collectors) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) Group(com.sequenceiq.cloudbreak.cloud.model.Group) WaiterRunner.run(com.sequenceiq.cloudbreak.cloud.aws.scheduler.WaiterRunner.run) Instance(com.amazonaws.services.autoscaling.model.Instance) LoadBalancerTypeConverter(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.LoadBalancerTypeConverter) DetachInstancesResult(com.amazonaws.services.autoscaling.model.DetachInstancesResult) ArrayList(java.util.ArrayList) AuthenticatedContextView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AuthenticatedContextView) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) ArrayList(java.util.ArrayList) List(java.util.List) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)

Aggregations

AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)11 AuthenticatedContextView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AuthenticatedContextView)11 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)5 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)5 AmazonServiceException (com.amazonaws.AmazonServiceException)3 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)3 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)3 SdkClientException (com.amazonaws.SdkClientException)2 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)2 DescribeImagesRequest (com.amazonaws.services.ec2.model.DescribeImagesRequest)2 DescribeImagesResult (com.amazonaws.services.ec2.model.DescribeImagesResult)2 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)2 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)2 GetConsoleOutputRequest (com.amazonaws.services.ec2.model.GetConsoleOutputRequest)2 GetConsoleOutputResult (com.amazonaws.services.ec2.model.GetConsoleOutputResult)2 Instance (com.amazonaws.services.ec2.model.Instance)2 AmazonAutoScalingClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient)2 AmazonCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)2 AmazonElasticLoadBalancingClient (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient)2 CloudOperationNotSupportedException (com.sequenceiq.cloudbreak.cloud.exception.CloudOperationNotSupportedException)2