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);
}
Aggregations