Search in sources :

Example 11 with AmazonAutoScalingClient

use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient in project cloudbreak by hortonworks.

the class AwsCloudFormationSetup method scalingPrerequisites.

@Override
public void scalingPrerequisites(AuthenticatedContext ac, CloudStack stack, boolean upscale) {
    if (!upscale) {
        return;
    }
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    AwsCredentialView awsCredential = new AwsCredentialView(ac.getCloudCredential());
    AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(awsCredential, regionName);
    AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(awsCredential, regionName);
    List<Group> groups = stack.getGroups().stream().filter(g -> g.getInstances().stream().anyMatch(inst -> InstanceStatus.CREATE_REQUESTED == inst.getTemplate().getStatus())).collect(Collectors.toList());
    Map<String, Group> groupMap = groups.stream().collect(Collectors.toMap(g -> cfStackUtil.getAutoscalingGroupName(ac, cloudFormationClient, g.getName()), g -> g));
    DescribeAutoScalingGroupsResult result = amazonASClient.describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(groupMap.keySet()));
    for (AutoScalingGroup asg : result.getAutoScalingGroups()) {
        Group group = groupMap.get(asg.getAutoScalingGroupName());
        List<CloudInstance> groupInstances = group.getInstances().stream().filter(inst -> InstanceStatus.CREATED.equals(inst.getTemplate().getStatus())).collect(Collectors.toList());
        List<CloudVmInstanceStatus> instanceStatuses = instanceConnector.check(ac, groupInstances);
        if (checkInstanceStatuses(instanceStatuses, ac.getCloudCredential().getAccountId())) {
            String errorMessage = "Not all the existing instances are in allowed state, upscale is not possible!";
            LOGGER.info(errorMessage);
            throw new CloudConnectorException(errorMessage);
        }
        List<Instance> asgOnlyInstances = asg.getInstances().stream().filter(inst -> groupInstances.stream().noneMatch(gi -> gi.getInstanceId().equals(inst.getInstanceId()))).collect(Collectors.toList());
        List<CloudInstance> cbOnlyInstances = groupInstances.stream().filter(gi -> asg.getInstances().stream().noneMatch(inst -> inst.getInstanceId().equals(gi.getInstanceId()))).collect(Collectors.toList());
        if (!asgOnlyInstances.isEmpty() || !cbOnlyInstances.isEmpty()) {
            String errorMessage = "The instances in the autoscaling group are not in sync with the instances in cloudbreak! Cloudbreak only instances: [" + cbOnlyInstances.stream().map(CloudInstance::getInstanceId).collect(Collectors.joining(",")) + "], AWS only instances: [" + asgOnlyInstances.stream().map(Instance::getInstanceId).collect(Collectors.joining(",")) + "]. Upscale is not possible!";
            LOGGER.info(errorMessage);
            throw new CloudConnectorException(errorMessage);
        }
        if (groupInstances.size() != asg.getDesiredCapacity()) {
            String errorMessage = String.format("The autoscale group's desired instance count is not match with the instance count in the cloudbreak." + " Desired count: %d <> cb instance count: %d. Upscale is not possible!", asg.getDesiredCapacity(), groupInstances.size());
            LOGGER.info(errorMessage);
            throw new CloudConnectorException(errorMessage);
        }
    }
}
Also used : EntitlementService(com.sequenceiq.cloudbreak.auth.altus.EntitlementService) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Map(java.util.Map) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) EnumSet(java.util.EnumSet) AwsInstanceConnector(com.sequenceiq.cloudbreak.cloud.aws.common.AwsInstanceConnector) AwsSetup(com.sequenceiq.cloudbreak.cloud.aws.common.AwsSetup) Logger(org.slf4j.Logger) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) Set(java.util.Set) Collectors(java.util.stream.Collectors) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) Component(org.springframework.stereotype.Component) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) Group(com.sequenceiq.cloudbreak.cloud.model.Group) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Instance(com.amazonaws.services.autoscaling.model.Instance) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.autoscaling.model.Instance) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)

Example 12 with AmazonAutoScalingClient

use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient in project cloudbreak by hortonworks.

the class AwsUpscaleServiceTest method upscaleTest.

@Test
void upscaleTest() throws AmazonAutoscalingFailed {
    AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
    AmazonCloudFormationClient amazonCloudFormationClient = mock(AmazonCloudFormationClient.class);
    DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult().withAutoScalingGroups(newAutoScalingGroup("masterASG", List.of("i-master1", "i-master2")), newAutoScalingGroup("workerASG", List.of("i-worker1", "i-worker2", "i-worker3")));
    when(amazonAutoScalingClient.describeAutoScalingGroups(any(DescribeAutoScalingGroupsRequest.class))).thenReturn(describeAutoScalingGroupsResult);
    when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), anyString())).thenReturn(amazonAutoScalingClient);
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), anyString())).thenReturn(amazonCloudFormationClient);
    when(awsClient.createEc2Client(any(), any())).thenReturn(mock(AmazonEc2Client.class));
    when(cfStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("worker"))).thenReturn("workerASG");
    CloudContext cloudContext = CloudContext.Builder.builder().withId(1L).withName("teststack").withCrn("crn").withPlatform("AWS").withVariant("AWS").withLocation(Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a"))).withAccountId("1").build();
    AuthenticatedContext authenticatedContext = new AuthenticatedContext(cloudContext, new CloudCredential());
    List<CloudResource> allInstances = new ArrayList<>();
    allInstances.add(newInstanceResource("worker1", "worker", "i-worker1"));
    allInstances.add(newInstanceResource("worker2", "worker", "i-worker2"));
    allInstances.add(newInstanceResource("worker3", "worker", "i-worker3"));
    CloudResource workerInstance4 = newInstanceResource("worker4", "worker", "i-worker4");
    allInstances.add(workerInstance4);
    CloudResource workerInstance5 = newInstanceResource("worker5", "worker", "i-worker5");
    allInstances.add(workerInstance5);
    when(cfStackUtil.getInstanceCloudResources(eq(authenticatedContext), eq(amazonCloudFormationClient), eq(amazonAutoScalingClient), anyList())).thenReturn(allInstances);
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    List<Group> groups = new ArrayList<>();
    groups.add(getMasterGroup(instanceAuthentication));
    Group worker = getWorkerGroup(instanceAuthentication);
    groups.add(worker);
    Map<String, String> tags = new HashMap<>();
    tags.put("owner", "cbuser");
    tags.put("created", "yesterday");
    CloudStack cloudStack = new CloudStack(groups, getNetwork(), null, emptyMap(), tags, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    List<CloudResource> cloudResourceList = Collections.emptyList();
    AdjustmentTypeWithThreshold adjustmentTypeWithThreshold = new AdjustmentTypeWithThreshold(AdjustmentType.EXACT, 0L);
    awsUpscaleService.upscale(authenticatedContext, cloudStack, cloudResourceList, adjustmentTypeWithThreshold);
    verify(awsAutoScalingService, times(1)).updateAutoscalingGroup(any(AmazonAutoScalingClient.class), eq("workerASG"), eq(5));
    verify(awsAutoScalingService, times(1)).scheduleStatusChecks(eq(List.of(worker)), eq(authenticatedContext), eq(amazonCloudFormationClient), any(), any());
    verify(awsAutoScalingService, times(1)).suspendAutoScaling(eq(authenticatedContext), eq(cloudStack));
    ArgumentCaptor<List<CloudResource>> captor = ArgumentCaptor.forClass(List.class);
    verify(awsComputeResourceService, times(1)).buildComputeResourcesForUpscale(eq(authenticatedContext), eq(cloudStack), anyList(), captor.capture(), any(), any(), eq(adjustmentTypeWithThreshold));
    verify(awsTaggingService, times(1)).tagRootVolumes(eq(authenticatedContext), any(AmazonEc2Client.class), eq(allInstances), eq(tags));
    verify(awsCloudWatchService, times(1)).addCloudWatchAlarmsForSystemFailures(any(), eq("eu-west-1"), any(AwsCredentialView.class));
    List<CloudResource> newInstances = captor.getValue();
    assertEquals("Two new instances should be created", 2, newInstances.size());
    assertThat(newInstances, hasItem(workerInstance4));
    assertThat(newInstances, hasItem(workerInstance5));
    verify(cfStackUtil, times(0)).addLoadBalancerTargets(any(), any(), any());
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) HashMap(java.util.HashMap) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AdjustmentTypeWithThreshold(com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) List(java.util.List) ArrayList(java.util.ArrayList) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) Test(org.junit.jupiter.api.Test)

Example 13 with AmazonAutoScalingClient

use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient in project cloudbreak by hortonworks.

the class AwsUpscaleServiceTest method upscaleWithLoadBalancers.

@Test
void upscaleWithLoadBalancers() {
    AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
    AmazonCloudFormationClient amazonCloudFormationClient = mock(AmazonCloudFormationClient.class);
    when(amazonAutoScalingClient.describeAutoScalingGroups(any(DescribeAutoScalingGroupsRequest.class))).thenReturn(new DescribeAutoScalingGroupsResult().withAutoScalingGroups(newAutoScalingGroup("masterASG", List.of("i-master1", "i-master2")), newAutoScalingGroup("workerASG", List.of("i-worker1", "i-worker2", "i-worker3"))));
    when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), anyString())).thenReturn(amazonAutoScalingClient);
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), anyString())).thenReturn(amazonCloudFormationClient);
    when(awsClient.createEc2Client(any(), any())).thenReturn(mock(AmazonEc2Client.class));
    when(cfStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("worker"))).thenReturn("workerASG");
    CloudContext cloudContext = CloudContext.Builder.builder().withId(1L).withName("teststack").withCrn("crn").withPlatform("AWS").withVariant("AWS").withLocation(Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a"))).withAccountId("1").build();
    AuthenticatedContext authenticatedContext = new AuthenticatedContext(cloudContext, new CloudCredential());
    List<CloudResource> allInstances = List.of(newInstanceResource("worker1", "worker", "i-worker1"), newInstanceResource("worker2", "worker", "i-worker2"), newInstanceResource("worker3", "worker", "i-worker3"), newInstanceResource("worker4", "worker", "i-worker4"), newInstanceResource("worker5", "worker", "i-worker5"));
    when(cfStackUtil.getInstanceCloudResources(eq(authenticatedContext), eq(amazonCloudFormationClient), eq(amazonAutoScalingClient), anyList())).thenReturn(allInstances);
    doNothing().when(cfStackUtil).addLoadBalancerTargets(any(), any(), any());
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    List<Group> groups = new ArrayList<>();
    List<CloudLoadBalancer> loadBalancers = List.of(new CloudLoadBalancer(LoadBalancerType.PRIVATE), new CloudLoadBalancer(LoadBalancerType.PUBLIC));
    Group master = getMasterGroup(instanceAuthentication);
    groups.add(master);
    Group worker = getWorkerGroup(instanceAuthentication);
    groups.add(worker);
    Map<String, String> tags = new HashMap<>();
    tags.put("owner", "cbuser");
    tags.put("created", "yesterday");
    CloudStack cloudStack = new CloudStack(groups, getNetwork(), null, emptyMap(), tags, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null, loadBalancers);
    List<CloudResource> cloudResourceList = Collections.emptyList();
    AdjustmentTypeWithThreshold adjustmentTypeWithThreshold = new AdjustmentTypeWithThreshold(AdjustmentType.EXACT, 0L);
    awsUpscaleService.upscale(authenticatedContext, cloudStack, cloudResourceList, adjustmentTypeWithThreshold);
    verify(cfStackUtil, times(2)).addLoadBalancerTargets(any(), any(), any());
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) HashMap(java.util.HashMap) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AdjustmentTypeWithThreshold(com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) Test(org.junit.jupiter.api.Test)

Example 14 with AmazonAutoScalingClient

use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient in project cloudbreak by hortonworks.

the class AwsUpscaleServiceTest method upscaleAwsVolumeFail.

@Test
void upscaleAwsVolumeFail() throws AmazonAutoscalingFailed {
    AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
    AmazonCloudFormationClient amazonCloudFormationClient = mock(AmazonCloudFormationClient.class);
    DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest();
    request.setAutoScalingGroupNames(new ArrayList<>(Collections.singletonList("workerASG")));
    DescribeAutoScalingGroupsResult describeScaledAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult().withAutoScalingGroups(newAutoScalingGroup("workerASG", List.of("i-worker1", "i-worker2", "i-worker3")));
    when(amazonAutoScalingClient.describeAutoScalingGroups(eq(request))).thenReturn(describeScaledAutoScalingGroupsResult);
    when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), anyString())).thenReturn(amazonAutoScalingClient);
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), anyString())).thenReturn(amazonCloudFormationClient);
    when(cfStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("worker"))).thenReturn("workerASG");
    CloudContext cloudContext = CloudContext.Builder.builder().withId(1L).withName("teststack").withCrn("crn").withPlatform("AWS").withVariant("AWS").withLocation(Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a"))).withAccountId("1").build();
    AuthenticatedContext authenticatedContext = new AuthenticatedContext(cloudContext, new CloudCredential());
    List<CloudResource> allInstances = new ArrayList<>();
    allInstances.add(newInstanceResource("worker1", "worker", "i-worker1"));
    allInstances.add(newInstanceResource("worker2", "worker", "i-worker2"));
    allInstances.add(newInstanceResource("worker3", "worker", "i-worker3"));
    CloudResource workerInstance4 = newInstanceResource("worker4", "worker", "i-worker4");
    allInstances.add(workerInstance4);
    CloudResource workerInstance5 = newInstanceResource("worker5", "worker", "i-worker5");
    allInstances.add(workerInstance5);
    when(cfStackUtil.getInstanceCloudResources(eq(authenticatedContext), eq(amazonCloudFormationClient), eq(amazonAutoScalingClient), anyList())).thenReturn(allInstances);
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    List<Group> groups = new ArrayList<>();
    groups.add(getMasterGroup(instanceAuthentication));
    Group worker = getWorkerGroup(instanceAuthentication);
    groups.add(worker);
    CloudStack cloudStack = new CloudStack(groups, getNetwork(), null, emptyMap(), emptyMap(), null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    List<CloudResource> cloudResourceList = Collections.emptyList();
    AutoScalingGroup newWorkerASGroup = newAutoScalingGroup("workerASG", List.of("i-worker1", "i-worker2", "i-worker3", "i-worker4", "i-worker5"));
    when(awsAutoScalingService.getAutoscalingGroups(eq(amazonAutoScalingClient), any())).thenReturn(List.of(newWorkerASGroup));
    AdjustmentTypeWithThreshold adjustmentTypeWithThreshold = new AdjustmentTypeWithThreshold(AdjustmentType.EXACT, 0L);
    when(awsComputeResourceService.buildComputeResourcesForUpscale(any(), any(), anyList(), anyList(), anyList(), anyList(), eq(adjustmentTypeWithThreshold))).thenThrow(new CloudConnectorException("volume create error"));
    CloudConnectorException exception = assertThrows(CloudConnectorException.class, () -> awsUpscaleService.upscale(authenticatedContext, cloudStack, cloudResourceList, adjustmentTypeWithThreshold));
    Assertions.assertEquals("Failed to create some resource on AWS for upscaled nodes, please check your quotas on AWS. " + "Original autoscaling group state has been recovered. Exception: volume create error", exception.getMessage());
    verify(awsAutoScalingService, times(1)).updateAutoscalingGroup(any(AmazonAutoScalingClient.class), eq("workerASG"), eq(5));
    verify(awsAutoScalingService, times(1)).scheduleStatusChecks(eq(List.of(worker)), eq(authenticatedContext), eq(amazonCloudFormationClient), any(), any());
    verify(awsComputeResourceService, times(1)).buildComputeResourcesForUpscale(eq(authenticatedContext), eq(cloudStack), anyList(), anyList(), any(), any(), eq(adjustmentTypeWithThreshold));
    verify(awsAutoScalingService, times(2)).suspendAutoScaling(eq(authenticatedContext), eq(cloudStack));
    verify(awsAutoScalingService, times(1)).terminateInstance(eq(amazonAutoScalingClient), eq("i-worker4"));
    verify(awsAutoScalingService, times(1)).terminateInstance(eq(amazonAutoScalingClient), eq("i-worker5"));
    verify(cfStackUtil, times(0)).addLoadBalancerTargets(any(), any(), any());
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AdjustmentTypeWithThreshold(com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) Test(org.junit.jupiter.api.Test)

Example 15 with AmazonAutoScalingClient

use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient in project cloudbreak by hortonworks.

the class AwsUpscaleServiceTest method upscaleAwsASGroupFail.

@Test
void upscaleAwsASGroupFail() throws AmazonAutoscalingFailed {
    AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
    AmazonCloudFormationClient amazonCloudFormationClient = mock(AmazonCloudFormationClient.class);
    DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest();
    request.setAutoScalingGroupNames(List.of("workerASG"));
    DescribeAutoScalingGroupsResult describeScaledAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult().withAutoScalingGroups(newAutoScalingGroup("workerASG", List.of("i-worker1", "i-worker2", "i-worker3")));
    when(amazonAutoScalingClient.describeAutoScalingGroups(eq(request))).thenReturn(describeScaledAutoScalingGroupsResult);
    when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), anyString())).thenReturn(amazonAutoScalingClient);
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), anyString())).thenReturn(amazonCloudFormationClient);
    when(cfStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("worker"))).thenReturn("workerASG");
    CloudContext cloudContext = CloudContext.Builder.builder().withId(1L).withName("teststack").withCrn("crn").withPlatform("AWS").withVariant("AWS").withLocation(Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a"))).withAccountId("1").build();
    AuthenticatedContext authenticatedContext = new AuthenticatedContext(cloudContext, new CloudCredential());
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    List<Group> groups = new ArrayList<>();
    groups.add(getMasterGroup(instanceAuthentication));
    Group worker = getWorkerGroup(instanceAuthentication);
    groups.add(worker);
    CloudStack cloudStack = new CloudStack(groups, getNetwork(), null, emptyMap(), emptyMap(), null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null);
    List<CloudResource> cloudResourceList = Collections.emptyList();
    AutoScalingGroup newWorkerASGroup = newAutoScalingGroup("workerASG", List.of("i-worker1", "i-worker2", "i-worker3", "i-worker4", "i-worker5"));
    when(awsAutoScalingService.getAutoscalingGroups(eq(amazonAutoScalingClient), any())).thenReturn(Collections.singletonList(newWorkerASGroup));
    doThrow(new AmazonAutoscalingFailed("autoscaling failed")).when(awsAutoScalingService).scheduleStatusChecks(eq(List.of(worker)), eq(authenticatedContext), eq(amazonCloudFormationClient), any(Date.class), any());
    AdjustmentTypeWithThreshold adjustmentTypeWithThreshold = new AdjustmentTypeWithThreshold(AdjustmentType.EXACT, 0L);
    CloudConnectorException exception = assertThrows(CloudConnectorException.class, () -> awsUpscaleService.upscale(authenticatedContext, cloudStack, cloudResourceList, adjustmentTypeWithThreshold));
    Assertions.assertEquals("Autoscaling group update failed: Amazon Autoscaling Group was not able to reach the desired state (3 instances instead of 5). " + "Original autoscaling group state has been recovered. Failure reason: autoscaling failed", exception.getMessage());
    verify(awsAutoScalingService, times(1)).updateAutoscalingGroup(any(AmazonAutoScalingClient.class), eq("workerASG"), eq(5));
    verify(awsAutoScalingService, times(1)).scheduleStatusChecks(eq(List.of(worker)), eq(authenticatedContext), eq(amazonCloudFormationClient), any(), any());
    verify(awsComputeResourceService, times(0)).buildComputeResourcesForUpscale(eq(authenticatedContext), eq(cloudStack), anyList(), anyList(), any(), any(), eq(adjustmentTypeWithThreshold));
    verify(awsAutoScalingService, times(1)).suspendAutoScaling(eq(authenticatedContext), eq(cloudStack));
    verify(awsAutoScalingService, times(1)).terminateInstance(eq(amazonAutoScalingClient), eq("i-worker4"));
    verify(awsAutoScalingService, times(1)).terminateInstance(eq(amazonAutoScalingClient), eq("i-worker5"));
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AdjustmentTypeWithThreshold(com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Date(java.util.Date) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) Test(org.junit.jupiter.api.Test)

Aggregations

AmazonAutoScalingClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient)28 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)24 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)20 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)20 DescribeAutoScalingGroupsRequest (com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest)19 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)19 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)19 AmazonCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)18 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)18 Group (com.sequenceiq.cloudbreak.cloud.model.Group)15 ArrayList (java.util.ArrayList)15 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)14 DescribeAutoScalingGroupsResult (com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult)13 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)12 List (java.util.List)12 Waiter (com.amazonaws.waiters.Waiter)11 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)10 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)10 Test (org.junit.jupiter.api.Test)10 AmazonServiceException (com.amazonaws.AmazonServiceException)9