Search in sources :

Example 16 with AmazonAutoScalingClient

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

the class AwsRepairTest method upscaleStack.

private void upscaleStack() throws Exception {
    AuthenticatedContext authenticatedContext = componentTestUtil.getAuthenticatedContext();
    CloudStack stack = componentTestUtil.getStack(InstanceStatus.CREATE_REQUESTED, InstanceStatus.STARTED);
    List<CloudResource> cloudResources = List.of(CloudResource.builder().name(AWS_SUBNET_ID).type(ResourceType.AWS_SUBNET).build(), createVolumeResource(VOLUME_ID_1, INSTANCE_ID_1, SIZE_DISK_1, FSTAB_1, CommonStatus.DETACHED), createVolumeResource(VOLUME_ID_2, INSTANCE_ID_2, SIZE_DISK_2, FSTAB_2, CommonStatus.DETACHED), createVolumeResource(VOLUME_ID_3, INSTANCE_ID_3, SIZE_DISK_2, FSTAB_2, CommonStatus.CREATED));
    InMemoryStateStore.putStack(1L, PollGroup.POLLABLE);
    when(amazonCloudFormationClient.describeStackResource(any())).thenReturn(new DescribeStackResourceResult().withStackResourceDetail(new StackResourceDetail().withPhysicalResourceId(AUTOSCALING_GROUP_NAME)));
    when(amazonAutoScalingClient.describeAutoScalingGroups(any())).thenReturn(new DescribeAutoScalingGroupsResult().withAutoScalingGroups(new AutoScalingGroup().withAutoScalingGroupName(AUTOSCALING_GROUP_NAME).withInstances(List.of(new Instance().withInstanceId(INSTANCE_ID_1).withLifecycleState(LifecycleState.InService), new Instance().withInstanceId(INSTANCE_ID_2).withLifecycleState(LifecycleState.InService)))));
    when(amazonEC2Client.describeVolumes(any())).thenReturn(new DescribeVolumesResult().withVolumes(new com.amazonaws.services.ec2.model.Volume().withVolumeId(VOLUME_ID_1).withState(VolumeState.Available), new com.amazonaws.services.ec2.model.Volume().withVolumeId(VOLUME_ID_2).withState(VolumeState.Available), new com.amazonaws.services.ec2.model.Volume().withVolumeId(VOLUME_ID_3).withState(VolumeState.InUse)));
    when(amazonEC2Client.describeInstances(any())).thenReturn(new DescribeInstancesResult().withReservations(new Reservation().withInstances(new com.amazonaws.services.ec2.model.Instance().withInstanceId("i-instance"))));
    DescribeScalingActivitiesResult result = new DescribeScalingActivitiesResult();
    result.setActivities(List.of());
    when(amazonAutoScalingClient.describeScalingActivities(any(DescribeScalingActivitiesRequest.class))).thenReturn(result);
    AmazonEC2Waiters waiters = mock(AmazonEC2Waiters.class);
    when(amazonEC2Client.waiters()).thenReturn(waiters);
    Waiter<DescribeInstancesRequest> instanceWaiter = mock(Waiter.class);
    when(waiters.instanceRunning()).thenReturn(instanceWaiter);
    when(amazonAutoScalingClient.waiters()).thenReturn(asWaiters);
    when(asWaiters.groupInService()).thenReturn(describeAutoScalingGroupsRequestWaiter);
    underTest.upscale(authenticatedContext, stack, cloudResources, new AdjustmentTypeWithThreshold(AdjustmentType.EXACT, 0L));
    verify(amazonAutoScalingClient).resumeProcesses(argThat(argument -> AUTOSCALING_GROUP_NAME.equals(argument.getAutoScalingGroupName()) && argument.getScalingProcesses().contains("Launch")));
    verify(amazonAutoScalingClient).updateAutoScalingGroup(argThat(argument -> {
        Group workerGroup = stack.getGroups().get(1);
        return AUTOSCALING_GROUP_NAME.equals(argument.getAutoScalingGroupName()) && workerGroup.getInstancesSize().equals(argument.getMaxSize()) && workerGroup.getInstancesSize().equals(argument.getDesiredCapacity());
    }));
    verify(amazonAutoScalingClient, times(stack.getGroups().size())).suspendProcesses(argThat(argument -> AUTOSCALING_GROUP_NAME.equals(argument.getAutoScalingGroupName()) && SUSPENDED_PROCESSES.equals(argument.getScalingProcesses())));
    ArgumentCaptor<CloudResource> updatedCloudResourceArgumentCaptor = ArgumentCaptor.forClass(CloudResource.class);
    verify(resourceNotifier, times(2)).notifyUpdate(updatedCloudResourceArgumentCaptor.capture(), any());
    assertVolumeResource(updatedCloudResourceArgumentCaptor.getAllValues(), INSTANCE_ID_1, SIZE_DISK_1, FSTAB_1);
    assertVolumeResource(updatedCloudResourceArgumentCaptor.getAllValues(), INSTANCE_ID_2, SIZE_DISK_2, FSTAB_2);
}
Also used : Arrays(java.util.Arrays) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) AmazonCloudWatchClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient) AmazonEC2Waiters(com.amazonaws.services.ec2.waiters.AmazonEC2Waiters) Arrays.asList(java.util.Arrays.asList) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) MockBean(org.springframework.boot.test.mock.mockito.MockBean) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) LifeCycleState(com.amazonaws.services.elasticfilesystem.model.LifeCycleState) CloudVolumeUsageType(com.sequenceiq.cloudbreak.cloud.model.CloudVolumeUsageType) DescribeFileSystemsResult(com.amazonaws.services.elasticfilesystem.model.DescribeFileSystemsResult) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) TestPropertySource(org.springframework.test.context.TestPropertySource) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes) Retry(com.sequenceiq.cloudbreak.service.Retry) AwsResourceConnector(com.sequenceiq.cloudbreak.cloud.aws.connector.resource.AwsResourceConnector) CreateFileSystemResult(com.amazonaws.services.elasticfilesystem.model.CreateFileSystemResult) AdjustmentType(com.sequenceiq.common.api.type.AdjustmentType) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) VolumeState(com.amazonaws.services.ec2.model.VolumeState) SIZE_DISK_1(com.sequenceiq.cloudbreak.cloud.aws.component.ComponentTestUtil.SIZE_DISK_1) SIZE_DISK_2(com.sequenceiq.cloudbreak.cloud.aws.component.ComponentTestUtil.SIZE_DISK_2) Mockito.mock(org.mockito.Mockito.mock) LocationHelper(com.sequenceiq.cloudbreak.cloud.storage.LocationHelper) Mock(org.mockito.Mock) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) AVAILABILITY_ZONE(com.sequenceiq.cloudbreak.cloud.aws.component.ComponentTestUtil.AVAILABILITY_ZONE) RunWith(org.junit.runner.RunWith) AmazonEfsClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEfsClient) Supplier(java.util.function.Supplier) StackResourceDetail(com.amazonaws.services.cloudformation.model.StackResourceDetail) ResourceNotifier(com.sequenceiq.cloudbreak.cloud.notification.ResourceNotifier) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) DeleteMountTargetResult(com.amazonaws.services.elasticfilesystem.model.DeleteMountTargetResult) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) DescribeScalingActivitiesResult(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesResult) AwsCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient) InMemoryStateStore(com.sequenceiq.cloudbreak.cloud.store.InMemoryStateStore) Assert.assertTrue(org.junit.Assert.assertTrue) Import(org.springframework.context.annotation.Import) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) LifecycleState(com.amazonaws.services.autoscaling.model.LifecycleState) INSTANCE_ID_1(com.sequenceiq.cloudbreak.cloud.aws.component.ComponentTestUtil.INSTANCE_ID_1) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) INSTANCE_ID_2(com.sequenceiq.cloudbreak.cloud.aws.component.ComponentTestUtil.INSTANCE_ID_2) INSTANCE_ID_3(com.sequenceiq.cloudbreak.cloud.aws.component.ComponentTestUtil.INSTANCE_ID_3) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) DescribeStackResourceResult(com.amazonaws.services.cloudformation.model.DescribeStackResourceResult) PollGroup(com.sequenceiq.cloudbreak.cloud.scheduler.PollGroup) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DescribeMountTargetsResult(com.amazonaws.services.elasticfilesystem.model.DescribeMountTargetsResult) EntitlementService(com.sequenceiq.cloudbreak.auth.altus.EntitlementService) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ActiveProfiles(org.springframework.test.context.ActiveProfiles) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) DescribeScalingActivitiesRequest(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesRequest) Assert.fail(org.junit.Assert.fail) SpringRunner(org.springframework.test.context.junit4.SpringRunner) AmazonAutoScalingWaiters(com.amazonaws.services.autoscaling.waiters.AmazonAutoScalingWaiters) Waiter(com.amazonaws.waiters.Waiter) Reservation(com.amazonaws.services.ec2.model.Reservation) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AmazonCloudFormationWaiters(com.amazonaws.services.cloudformation.waiters.AmazonCloudFormationWaiters) Collectors(java.util.stream.Collectors) TemporaryStorage(com.sequenceiq.cloudbreak.common.type.TemporaryStorage) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) Group(com.sequenceiq.cloudbreak.cloud.model.Group) DeleteFileSystemResult(com.amazonaws.services.elasticfilesystem.model.DeleteFileSystemResult) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HashMap(java.util.HashMap) AwsMetadataCollector(com.sequenceiq.cloudbreak.cloud.aws.AwsMetadataCollector) ResourceRetriever(com.sequenceiq.cloudbreak.cloud.service.ResourceRetriever) Inject(javax.inject.Inject) ArgumentCaptor(org.mockito.ArgumentCaptor) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) ResourceType(com.sequenceiq.common.api.type.ResourceType) FileSystemDescription(com.amazonaws.services.elasticfilesystem.model.FileSystemDescription) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AdjustmentTypeWithThreshold(com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) Mockito(org.mockito.Mockito) MountTargetDescription(com.amazonaws.services.elasticfilesystem.model.MountTargetDescription) CustomAmazonWaiterProvider(com.sequenceiq.cloudbreak.cloud.aws.scheduler.CustomAmazonWaiterProvider) DescribeAlarmsResult(com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult) Instance(com.amazonaws.services.autoscaling.model.Instance) Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) CommonAwsClient(com.sequenceiq.cloudbreak.cloud.aws.common.CommonAwsClient) Collections(java.util.Collections) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) PollGroup(com.sequenceiq.cloudbreak.cloud.scheduler.PollGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.autoscaling.model.Instance) AdjustmentTypeWithThreshold(com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) AmazonEC2Waiters(com.amazonaws.services.ec2.waiters.AmazonEC2Waiters) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) StackResourceDetail(com.amazonaws.services.cloudformation.model.StackResourceDetail) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) DescribeScalingActivitiesResult(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesResult) Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) DescribeScalingActivitiesRequest(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesRequest) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) DescribeStackResourceResult(com.amazonaws.services.cloudformation.model.DescribeStackResourceResult)

Example 17 with AmazonAutoScalingClient

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

the class AwsDownscaleServiceTest method downscaleASG.

@Test
void downscaleASG() {
    CloudStack stack = mock(CloudStack.class);
    List<CloudResource> resources = List.of(new CloudResource.Builder().name("i-1").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-2").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-3").type(ResourceType.AWS_INSTANCE).build());
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    List<CloudInstance> cloudInstances = new ArrayList<>();
    InstanceTemplate workerTemplate = mock(InstanceTemplate.class);
    when(workerTemplate.getGroupName()).thenReturn("worker");
    CloudInstance workerInstance1 = new CloudInstance("i-worker1", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance2 = new CloudInstance("i-worker2", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance3 = new CloudInstance("i-worker3", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    cloudInstances.add(workerInstance1);
    cloudInstances.add(workerInstance2);
    cloudInstances.add(workerInstance3);
    AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
    when(awsClient.createAutoScalingClient(any(), anyString())).thenReturn(amazonAutoScalingClient);
    AmazonEc2Client amazonEC2Client = mock(AmazonEc2Client.class);
    when(awsClient.createEc2Client(any(), anyString())).thenReturn(amazonEC2Client);
    CloudContext context = 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(context, new CloudCredential());
    AmazonEC2Waiters amazonEC2Waiters = mock(AmazonEC2Waiters.class);
    when(amazonEC2Client.waiters()).thenReturn(amazonEC2Waiters);
    Waiter waiter = mock(Waiter.class);
    when(amazonEC2Waiters.instanceTerminated()).thenReturn(waiter);
    when(cfStackUtil.getAutoscalingGroupName(any(), (String) any(), any())).thenReturn("autoscalegroup-1");
    DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
    AutoScalingGroup autoScalingGroup = new AutoScalingGroup();
    autoScalingGroup.setInstances(List.of(new Instance().withInstanceId("i-worker1")));
    describeAutoScalingGroupsResult.setAutoScalingGroups(List.of(autoScalingGroup));
    ArgumentCaptor<DescribeAutoScalingGroupsRequest> describeAutoScalingGroupsRequest = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class);
    ArgumentCaptor<DetachInstancesRequest> detachInstancesRequestArgumentCaptor = ArgumentCaptor.forClass(DetachInstancesRequest.class);
    when(amazonAutoScalingClient.describeAutoScalingGroups(describeAutoScalingGroupsRequest.capture())).thenReturn(describeAutoScalingGroupsResult);
    when(amazonAutoScalingClient.detachInstances(detachInstancesRequestArgumentCaptor.capture())).thenReturn(new DetachInstancesResult());
    mockDescribeInstances(amazonEC2Client);
    underTest.downscale(authenticatedContext, stack, resources, cloudInstances);
    List<DetachInstancesRequest> allValues = detachInstancesRequestArgumentCaptor.getAllValues();
    assertThat(allValues.get(0).getInstanceIds(), contains("i-worker1"));
    verify(amazonAutoScalingClient, times(1)).detachInstances(any());
    verify(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
    assertEquals(describeAutoScalingGroupsRequest.getValue().getAutoScalingGroupNames(), List.of("autoscalegroup-1"));
}
Also used : CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.autoscaling.model.Instance) ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) DetachInstancesResult(com.amazonaws.services.autoscaling.model.DetachInstancesResult) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) AmazonEC2Waiters(com.amazonaws.services.ec2.waiters.AmazonEC2Waiters) 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) Waiter(com.amazonaws.waiters.Waiter) DetachInstancesRequest(com.amazonaws.services.autoscaling.model.DetachInstancesRequest) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Test(org.junit.jupiter.api.Test)

Example 18 with AmazonAutoScalingClient

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

the class AwsDownscaleServiceTest method downscaleNoWaiterIfNoInstanceLeftOnAWS.

@Test
public void downscaleNoWaiterIfNoInstanceLeftOnAWS() {
    CloudStack stack = mock(CloudStack.class);
    List<CloudResource> resources = List.of(new CloudResource.Builder().name("i-1").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-2").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-3").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-4").type(ResourceType.AWS_INSTANCE).build());
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    List<CloudInstance> cloudInstances = new ArrayList<>();
    InstanceTemplate workerTemplate = mock(InstanceTemplate.class);
    when(workerTemplate.getGroupName()).thenReturn("worker");
    CloudInstance workerInstance1 = new CloudInstance("i-worker1", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance2 = new CloudInstance("i-worker2", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance3 = new CloudInstance("i-worker3", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance4 = new CloudInstance("i-worker4", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    cloudInstances.add(workerInstance1);
    cloudInstances.add(workerInstance2);
    cloudInstances.add(workerInstance3);
    cloudInstances.add(workerInstance4);
    CloudContext context = 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(context, new CloudCredential());
    AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
    when(awsClient.createAutoScalingClient(any(), anyString())).thenReturn(amazonAutoScalingClient);
    AmazonEc2Client amazonEC2Client = mock(AmazonEc2Client.class);
    when(awsClient.createEc2Client(any(), anyString())).thenReturn(amazonEC2Client);
    Waiter waiter = mock(Waiter.class);
    when(cfStackUtil.getAutoscalingGroupName(any(), (String) any(), any())).thenReturn("autoscalegroup-1");
    AmazonServiceException amazonServiceException = new AmazonServiceException("Cannot execute method: terminateInstances. Invalid id: " + "\"i-worker1\",\"i-worker2\",\"i-worker3\",\"i-worker4\"");
    amazonServiceException.setErrorCode(INSTANCE_NOT_FOUND_ERROR_CODE);
    when(amazonEC2Client.terminateInstances(any())).thenThrow(amazonServiceException).thenReturn(new TerminateInstancesResult());
    DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
    AutoScalingGroup autoScalingGroup = new AutoScalingGroup();
    autoScalingGroup.setInstances(List.of());
    describeAutoScalingGroupsResult.setAutoScalingGroups(List.of(autoScalingGroup));
    ArgumentCaptor<DescribeAutoScalingGroupsRequest> describeAutoScalingGroupsRequest = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class);
    when(amazonAutoScalingClient.describeAutoScalingGroups(describeAutoScalingGroupsRequest.capture())).thenReturn(describeAutoScalingGroupsResult);
    mockDescribeInstances(amazonEC2Client);
    underTest.downscale(authenticatedContext, stack, resources, cloudInstances);
    verify(amazonAutoScalingClient, never()).detachInstances(any());
    verify(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
    ArgumentCaptor<TerminateInstancesRequest> terminateInstancesRequestArgumentCaptor = ArgumentCaptor.forClass(TerminateInstancesRequest.class);
    verify(amazonEC2Client, times(1)).terminateInstances(terminateInstancesRequestArgumentCaptor.capture());
    List<TerminateInstancesRequest> allValues = terminateInstancesRequestArgumentCaptor.getAllValues();
    List<String> firstTerminateInstanceIds = allValues.get(0).getInstanceIds();
    assertEquals(4, firstTerminateInstanceIds.size());
    assertTrue(firstTerminateInstanceIds.contains("i-worker1"));
    assertTrue(firstTerminateInstanceIds.contains("i-worker2"));
    assertTrue(firstTerminateInstanceIds.contains("i-worker3"));
    assertTrue(firstTerminateInstanceIds.contains("i-worker4"));
    verify(waiter, times(0)).run(any());
}
Also used : ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Waiter(com.amazonaws.waiters.Waiter) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Test(org.junit.jupiter.api.Test)

Example 19 with AmazonAutoScalingClient

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

the class AwsDownscaleServiceTest method downscaleButInstanceNotFoundOnAWS.

@Test
public void downscaleButInstanceNotFoundOnAWS() {
    CloudStack stack = mock(CloudStack.class);
    List<CloudResource> resources = List.of(new CloudResource.Builder().name("i-1").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-2").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-3").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-4").type(ResourceType.AWS_INSTANCE).build());
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    List<CloudInstance> cloudInstances = new ArrayList<>();
    InstanceTemplate workerTemplate = mock(InstanceTemplate.class);
    when(workerTemplate.getGroupName()).thenReturn("worker");
    CloudInstance workerInstance1 = new CloudInstance("i-worker1", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance2 = new CloudInstance("i-worker2", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance3 = new CloudInstance("i-worker3", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance4 = new CloudInstance("i-worker4", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    cloudInstances.add(workerInstance1);
    cloudInstances.add(workerInstance2);
    cloudInstances.add(workerInstance3);
    cloudInstances.add(workerInstance4);
    CloudContext context = 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(context, new CloudCredential());
    AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
    when(awsClient.createAutoScalingClient(any(), anyString())).thenReturn(amazonAutoScalingClient);
    AmazonEc2Client amazonEC2Client = mock(AmazonEc2Client.class);
    when(awsClient.createEc2Client(any(), anyString())).thenReturn(amazonEC2Client);
    AmazonEC2Waiters amazonEC2Waiters = mock(AmazonEC2Waiters.class);
    when(amazonEC2Client.waiters()).thenReturn(amazonEC2Waiters);
    Waiter waiter = mock(Waiter.class);
    when(amazonEC2Waiters.instanceTerminated()).thenReturn(waiter);
    when(cfStackUtil.getAutoscalingGroupName(any(), (String) any(), any())).thenReturn("autoscalegroup-1");
    AmazonServiceException amazonServiceException = new AmazonServiceException("Cannot execute method: terminateInstances. Invalid id: " + "\"i-worker1\",\"i-worker2\"");
    amazonServiceException.setErrorCode(INSTANCE_NOT_FOUND_ERROR_CODE);
    when(amazonEC2Client.terminateInstances(any())).thenThrow(amazonServiceException).thenReturn(new TerminateInstancesResult());
    AmazonServiceException amazonServiceExceptionForWaiter = new AmazonServiceException("Cannot execute method: terminateInstances. Invalid id: " + "\"i-worker3\"");
    amazonServiceExceptionForWaiter.setErrorCode(INSTANCE_NOT_FOUND_ERROR_CODE);
    doThrow(amazonServiceExceptionForWaiter).doNothing().when(waiter).run(any());
    DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
    AutoScalingGroup autoScalingGroup = new AutoScalingGroup();
    autoScalingGroup.setInstances(List.of());
    describeAutoScalingGroupsResult.setAutoScalingGroups(List.of(autoScalingGroup));
    ArgumentCaptor<DescribeAutoScalingGroupsRequest> describeAutoScalingGroupsRequest = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class);
    when(amazonAutoScalingClient.describeAutoScalingGroups(describeAutoScalingGroupsRequest.capture())).thenReturn(describeAutoScalingGroupsResult);
    mockDescribeInstances(amazonEC2Client);
    underTest.downscale(authenticatedContext, stack, resources, cloudInstances);
    verify(amazonAutoScalingClient, never()).detachInstances(any());
    verify(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
    ArgumentCaptor<TerminateInstancesRequest> terminateInstancesRequestArgumentCaptor = ArgumentCaptor.forClass(TerminateInstancesRequest.class);
    verify(amazonEC2Client, times(2)).terminateInstances(terminateInstancesRequestArgumentCaptor.capture());
    List<TerminateInstancesRequest> allValues = terminateInstancesRequestArgumentCaptor.getAllValues();
    List<String> firstTerminateInstanceIds = allValues.get(0).getInstanceIds();
    assertEquals(4, firstTerminateInstanceIds.size());
    assertTrue(firstTerminateInstanceIds.contains("i-worker1"));
    assertTrue(firstTerminateInstanceIds.contains("i-worker2"));
    assertTrue(firstTerminateInstanceIds.contains("i-worker3"));
    assertTrue(firstTerminateInstanceIds.contains("i-worker4"));
    List<String> secondTerminateInstanceIds = allValues.get(1).getInstanceIds();
    assertEquals(2, secondTerminateInstanceIds.size());
    assertTrue(firstTerminateInstanceIds.contains("i-worker3"));
    assertTrue(firstTerminateInstanceIds.contains("i-worker4"));
    ArgumentCaptor<WaiterParameters> waiterParametersArgumentCaptor = ArgumentCaptor.forClass(WaiterParameters.class);
    verify(waiter, times(2)).run(waiterParametersArgumentCaptor.capture());
    List<WaiterParameters> waiterParametersList = waiterParametersArgumentCaptor.getAllValues();
    DescribeInstancesRequest firstDescribeInstancesRequest = (DescribeInstancesRequest) waiterParametersList.get(0).getRequest();
    assertEquals(2, firstDescribeInstancesRequest.getInstanceIds().size());
    assertTrue(firstDescribeInstancesRequest.getInstanceIds().contains("i-worker3"));
    assertTrue(firstDescribeInstancesRequest.getInstanceIds().contains("i-worker4"));
    DescribeInstancesRequest secondDescribeInstancesRequest = (DescribeInstancesRequest) waiterParametersList.get(1).getRequest();
    assertEquals(1, secondDescribeInstancesRequest.getInstanceIds().size());
    assertTrue(secondDescribeInstancesRequest.getInstanceIds().contains("i-worker4"));
    assertEquals(describeAutoScalingGroupsRequest.getValue().getAutoScalingGroupNames(), List.of("autoscalegroup-1"));
}
Also used : ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) AmazonEC2Waiters(com.amazonaws.services.ec2.waiters.AmazonEC2Waiters) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) WaiterParameters(com.amazonaws.waiters.WaiterParameters) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Waiter(com.amazonaws.waiters.Waiter) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Test(org.junit.jupiter.api.Test)

Example 20 with AmazonAutoScalingClient

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

the class AwsAutoScalingService method checkLastScalingActivity.

@VisibleForTesting
void checkLastScalingActivity(AmazonAutoScalingClient asClient, String autoScalingGroupName, Date timeBeforeASUpdate, Group group) throws AmazonAutoscalingFailed {
    if (group.getInstancesSize() > 0) {
        LOGGER.debug("Check last activity after AS update. Wait for the first if it doesn't exist. [asGroup: {}]", autoScalingGroupName);
        Optional<Activity> firstActivity = Optional.empty();
        try {
            AutoScalingGroup scalingGroup = asClient.describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(autoScalingGroupName)).getAutoScalingGroups().stream().findFirst().orElseThrow(() -> new AmazonAutoscalingFailed("Can not find autoscaling group by name: " + autoScalingGroupName));
            if (group.getInstancesSize() > scalingGroup.getInstances().size()) {
                Waiter<DescribeScalingActivitiesRequest> autoscalingActivitiesWaiter = customAmazonWaiterProvider.getAutoscalingActivitiesWaiter(asClient, timeBeforeASUpdate);
                autoscalingActivitiesWaiter.run(new WaiterParameters<>(new DescribeScalingActivitiesRequest().withAutoScalingGroupName(autoScalingGroupName)));
                DescribeScalingActivitiesResult describeScalingActivitiesResult = asClient.describeScalingActivities(new DescribeScalingActivitiesRequest().withAutoScalingGroupName(autoScalingGroupName));
                // if we run into InsufficientInstanceCapacity we can skip to waitForGroup because that method will wait for the required instance count
                firstActivity = describeScalingActivitiesResult.getActivities().stream().findFirst().filter(activity -> "failed".equals(activity.getStatusCode().toLowerCase()) && !activity.getStatusMessage().contains("InsufficientInstanceCapacity"));
            } else {
                LOGGER.info("Skip checking activities because the AS group contains the desired instance count");
            }
        } catch (Exception e) {
            LOGGER.error("Failed to list activities: {}", e.getMessage(), e);
            throw new AmazonAutoscalingFailed(e.getMessage(), e);
        }
        if (firstActivity.isPresent()) {
            Activity activity = firstActivity.get();
            LOGGER.error("Cannot execute autoscale, because last activity is failed: {}", activity);
            throw new AmazonAutoscalingFailed(activity.getDescription() + " " + activity.getCause());
        }
    }
}
Also used : Activity(com.amazonaws.services.autoscaling.model.Activity) Date(java.util.Date) WaiterParameters(com.amazonaws.waiters.WaiterParameters) LoggerFactory(org.slf4j.LoggerFactory) TerminateInstanceInAutoScalingGroupRequest(com.amazonaws.services.autoscaling.model.TerminateInstanceInAutoScalingGroupRequest) SuspendProcessesRequest(com.amazonaws.services.autoscaling.model.SuspendProcessesRequest) Inject(javax.inject.Inject) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Lists(com.google.common.collect.Lists) 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) SUSPENDED_PROCESSES(com.sequenceiq.cloudbreak.cloud.aws.connector.resource.AwsResourceConstants.SUSPENDED_PROCESSES) DescribeScalingActivitiesRequest(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesRequest) ResumeProcessesRequest(com.amazonaws.services.autoscaling.model.ResumeProcessesRequest) BackoffCancellablePollingStrategy.getBackoffCancellablePollingStrategy(com.sequenceiq.cloudbreak.cloud.aws.scheduler.BackoffCancellablePollingStrategy.getBackoffCancellablePollingStrategy) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) Waiter(com.amazonaws.waiters.Waiter) Logger(org.slf4j.Logger) AmazonServiceException(com.amazonaws.AmazonServiceException) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) DescribeScalingActivitiesResult(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesResult) AwsCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient) Collection(java.util.Collection) CloudFormationStackUtil(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationStackUtil) Set(java.util.Set) WaiterTimedOutException(com.amazonaws.waiters.WaiterTimedOutException) Collectors(java.util.stream.Collectors) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) PollingStrategy(com.amazonaws.waiters.PollingStrategy) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) CustomAmazonWaiterProvider(com.sequenceiq.cloudbreak.cloud.aws.scheduler.CustomAmazonWaiterProvider) Group(com.sequenceiq.cloudbreak.cloud.model.Group) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) DescribeScalingActivitiesResult(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesResult) DescribeScalingActivitiesRequest(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesRequest) Activity(com.amazonaws.services.autoscaling.model.Activity) AmazonServiceException(com.amazonaws.AmazonServiceException) WaiterTimedOutException(com.amazonaws.waiters.WaiterTimedOutException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

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