Search in sources :

Example 66 with AwsCredentialView

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

the class AwsCloudWatchServiceTest method testDeleteCloudWatchAlarmsForSystemFailuresDoesNotBatchDeletionWhenDescribeAlarmPermissionIsMissing.

@Test
void testDeleteCloudWatchAlarmsForSystemFailuresDoesNotBatchDeletionWhenDescribeAlarmPermissionIsMissing() {
    List<CloudInstance> cloudInstances = new LinkedList<>();
    List<List<String>> deleteAlarmNames = new LinkedList<>();
    List<String> alarmNames1 = new LinkedList<>();
    List<String> alarmNames2 = new LinkedList<>();
    List<String> alarmNames3 = new LinkedList<>();
    for (int i = 1; i <= 100; i++) {
        String alarmName = "i-" + i + "-Status-Check-Failed-System";
        alarmNames1.add(alarmName);
        deleteAlarmNames.add(List.of(alarmName));
    }
    for (int i = 101; i <= 200; i++) {
        String alarmName = "i-" + i + "-Status-Check-Failed-System";
        alarmNames2.add(alarmName);
        deleteAlarmNames.add(List.of(alarmName));
    }
    for (int i = 201; i <= 210; i++) {
        String alarmName = "i-" + i + "-Status-Check-Failed-System";
        alarmNames3.add(alarmName);
        deleteAlarmNames.add(List.of(alarmName));
    }
    for (int i = 1; i <= 210; i++) {
        String instanceId = "i-" + i;
        CloudInstance cloudInstance = mock(CloudInstance.class);
        when(cloudInstance.getInstanceId()).thenReturn(instanceId);
        cloudInstances.add(cloudInstance);
    }
    AmazonCloudWatchClient cloudWatchClient = mock(AmazonCloudWatchClient.class);
    AwsCredentialView credentialView = mock(AwsCredentialView.class);
    CloudStack stack = mock(CloudStack.class);
    Group group = mock(Group.class);
    List<Group> groups = List.of(group);
    when(stack.getGroups()).thenReturn(groups);
    when(group.getInstances()).thenReturn(cloudInstances);
    when(awsClient.createCloudWatchClient(credentialView, REGION)).thenReturn(cloudWatchClient);
    AmazonCloudWatchException exception = new AmazonCloudWatchException("No permissions to describe cloudwatch alarms");
    when(cloudWatchClient.describeAlarms(any())).thenThrow(exception);
    underTest.deleteAllCloudWatchAlarmsForSystemFailures(stack, REGION, credentialView);
    ArgumentCaptor<DescribeAlarmsRequest> captorDescribe = ArgumentCaptor.forClass(DescribeAlarmsRequest.class);
    ArgumentCaptor<DeleteAlarmsRequest> captorDelete = ArgumentCaptor.forClass(DeleteAlarmsRequest.class);
    verify(cloudWatchClient, times(3)).describeAlarms(captorDescribe.capture());
    verify(cloudWatchClient, times(210)).deleteAlarms(captorDelete.capture());
    assertEquals(List.of(alarmNames1, alarmNames2, alarmNames3), captorDescribe.getAllValues().stream().map(DescribeAlarmsRequest::getAlarmNames).collect(Collectors.toList()));
    assertEquals(deleteAlarmNames, captorDelete.getAllValues().stream().map(DeleteAlarmsRequest::getAlarmNames).collect(Collectors.toList()));
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) AmazonCloudWatchClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) DescribeAlarmsRequest(com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) LinkedList(java.util.LinkedList) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DeleteAlarmsRequest(com.amazonaws.services.cloudwatch.model.DeleteAlarmsRequest) AmazonCloudWatchException(com.amazonaws.services.cloudwatch.model.AmazonCloudWatchException) LinkedList(java.util.LinkedList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 67 with AwsCredentialView

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

the class AwsCloudWatchServiceTest method testAddCloudWatchAlarmsForSystemFailuresWithRecoveryNotSupportedType.

@Test
void testAddCloudWatchAlarmsForSystemFailuresWithRecoveryNotSupportedType() {
    CloudResource firstInst = CloudResource.builder().instanceId("i-1").name("i-1").type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED).build();
    CloudResource secondInst = CloudResource.builder().instanceId("i-2").name("i-2").type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED).build();
    secondInst.putParameter(INSTANCE_TYPE, "m5d.2xlarge");
    AwsCredentialView credentialView = mock(AwsCredentialView.class);
    AmazonCloudWatchClient cloudWatchClient = mock(AmazonCloudWatchClient.class);
    when(awsClient.createCloudWatchClient(eq(credentialView), eq(REGION))).thenReturn(cloudWatchClient);
    underTest.addCloudWatchAlarmsForSystemFailures(List.of(firstInst, secondInst), REGION, credentialView);
    ArgumentCaptor<PutMetricAlarmRequest> captorPut = ArgumentCaptor.forClass(PutMetricAlarmRequest.class);
    verify(cloudWatchClient, times(1)).putMetricAlarm(captorPut.capture());
    assertEquals(1, captorPut.getAllValues().size());
    assertEquals("i-1" + ALARM_SUFFIX, captorPut.getAllValues().get(0).getAlarmName());
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AmazonCloudWatchClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient) PutMetricAlarmRequest(com.amazonaws.services.cloudwatch.model.PutMetricAlarmRequest) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Test(org.junit.jupiter.api.Test)

Example 68 with AwsCredentialView

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

the class AwsCloudWatchServiceTest method testAddCloudWatchAlarmsForSystemFailures.

@Test
void testAddCloudWatchAlarmsForSystemFailures() {
    CloudResource firstInst = CloudResource.builder().instanceId("i-1").name("i-1").type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED).build();
    CloudResource secondInst = CloudResource.builder().instanceId("i-2").name("i-2").type(ResourceType.AWS_INSTANCE).status(CommonStatus.CREATED).build();
    AwsCredentialView credentialView = mock(AwsCredentialView.class);
    AmazonCloudWatchClient cloudWatchClient = mock(AmazonCloudWatchClient.class);
    when(awsClient.createCloudWatchClient(eq(credentialView), eq(REGION))).thenReturn(cloudWatchClient);
    underTest.addCloudWatchAlarmsForSystemFailures(List.of(firstInst, secondInst), REGION, credentialView);
    ArgumentCaptor<PutMetricAlarmRequest> captorPut = ArgumentCaptor.forClass(PutMetricAlarmRequest.class);
    verify(cloudWatchClient, times(2)).putMetricAlarm(captorPut.capture());
    assertEquals(2, captorPut.getAllValues().size());
    assertEquals("i-1" + ALARM_SUFFIX, captorPut.getAllValues().get(0).getAlarmName());
    assertEquals("i-2" + ALARM_SUFFIX, captorPut.getAllValues().get(1).getAlarmName());
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AmazonCloudWatchClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient) PutMetricAlarmRequest(com.amazonaws.services.cloudwatch.model.PutMetricAlarmRequest) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Test(org.junit.jupiter.api.Test)

Example 69 with AwsCredentialView

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

the class AwsCloudWatchServiceTest method testDeleteCloudWatchAlarmsForSystemFailuresWhenOneAlarmHasAlreadyBeenDeleted.

@Test
void testDeleteCloudWatchAlarmsForSystemFailuresWhenOneAlarmHasAlreadyBeenDeleted() {
    String alarm1Name = "i-1-Status-Check-Failed-System";
    String alarm2Name = "i-2-Status-Check-Failed-System";
    String alarm3Name = "i-3-Status-Check-Failed-System";
    String instanceId1 = "i-1";
    String instanceId2 = "i-2";
    String instanceId3 = "i-3";
    MetricAlarm alarm1 = mock(MetricAlarm.class);
    MetricAlarm alarm2 = mock(MetricAlarm.class);
    DescribeAlarmsResult describeAlarmsResult = mock(DescribeAlarmsResult.class);
    AmazonCloudWatchClient cloudWatchClient = mock(AmazonCloudWatchClient.class);
    AwsCredentialView credentialView = mock(AwsCredentialView.class);
    CloudStack stack = mock(CloudStack.class);
    Group group = mock(Group.class);
    CloudInstance instance1 = mock(CloudInstance.class);
    CloudInstance instance2 = mock(CloudInstance.class);
    CloudInstance instance3 = mock(CloudInstance.class);
    List<Group> groups = List.of(group);
    when(stack.getGroups()).thenReturn(groups);
    when(group.getInstances()).thenReturn(List.of(instance1, instance2, instance3));
    when(instance1.getInstanceId()).thenReturn(instanceId1);
    when(instance2.getInstanceId()).thenReturn(instanceId2);
    when(instance3.getInstanceId()).thenReturn(instanceId3);
    when(awsClient.createCloudWatchClient(eq(credentialView), eq(REGION))).thenReturn(cloudWatchClient);
    when(cloudWatchClient.describeAlarms(any())).thenReturn(describeAlarmsResult);
    // alarm 3 was already deleted
    when(describeAlarmsResult.getMetricAlarms()).thenReturn(List.of(alarm1, alarm2));
    when(alarm1.getAlarmName()).thenReturn(alarm1Name);
    when(alarm2.getAlarmName()).thenReturn(alarm2Name);
    underTest.deleteAllCloudWatchAlarmsForSystemFailures(stack, REGION, credentialView);
    ArgumentCaptor<DescribeAlarmsRequest> captorDescribe = ArgumentCaptor.forClass(DescribeAlarmsRequest.class);
    ArgumentCaptor<DeleteAlarmsRequest> captorDelete = ArgumentCaptor.forClass(DeleteAlarmsRequest.class);
    verify(cloudWatchClient, times(1)).describeAlarms(captorDescribe.capture());
    verify(cloudWatchClient, times(1)).deleteAlarms(captorDelete.capture());
    assertEquals(List.of(alarm1Name, alarm2Name, alarm3Name), captorDescribe.getValue().getAlarmNames());
    // only delete alarms that were not already deleted
    assertEquals(List.of(alarm1Name, alarm2Name), captorDelete.getValue().getAlarmNames());
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) AmazonCloudWatchClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) DescribeAlarmsRequest(com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) MetricAlarm(com.amazonaws.services.cloudwatch.model.MetricAlarm) DescribeAlarmsResult(com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult) DeleteAlarmsRequest(com.amazonaws.services.cloudwatch.model.DeleteAlarmsRequest) Test(org.junit.jupiter.api.Test)

Example 70 with AwsCredentialView

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

the class AwsCloudWatchServiceTest method testDeleteCloudWatchAlarmsWhenInstanceDeletedOnProvider.

@Test
void testDeleteCloudWatchAlarmsWhenInstanceDeletedOnProvider() {
    String alarm1Name = "i-1-Status-Check-Failed-System";
    String alarm2Name = "i-2-Status-Check-Failed-System";
    String deletedAlarmName = "i-deleted-Status-Check-Failed-System";
    String instanceId1 = "i-1";
    String instanceId2 = "i-2";
    String deletedInstanceId = "i-deleted";
    MetricAlarm alarm1 = mock(MetricAlarm.class);
    MetricAlarm alarm2 = mock(MetricAlarm.class);
    MetricAlarm deletedInstanceAlarm = mock(MetricAlarm.class);
    DescribeAlarmsResult describeAlarmsResult = mock(DescribeAlarmsResult.class);
    AmazonCloudWatchClient cloudWatchClient = mock(AmazonCloudWatchClient.class);
    AwsCredentialView credentialView = mock(AwsCredentialView.class);
    CloudStack stack = mock(CloudStack.class);
    Group group = mock(Group.class);
    Group deletedGroup = mock(Group.class);
    CloudInstance instance1 = mock(CloudInstance.class);
    CloudInstance instance2 = mock(CloudInstance.class);
    CloudInstance deletedInstance = mock(CloudInstance.class);
    List<Group> groups = List.of(group, deletedGroup);
    when(stack.getGroups()).thenReturn(groups);
    when(group.getInstances()).thenReturn(List.of(instance1, instance2));
    when(deletedGroup.getInstances()).thenReturn(List.of(deletedInstance));
    when(instance1.getInstanceId()).thenReturn(instanceId1);
    when(instance2.getInstanceId()).thenReturn(instanceId2);
    when(deletedInstance.getInstanceId()).thenReturn(deletedInstanceId);
    when(awsClient.createCloudWatchClient(eq(credentialView), eq(REGION))).thenReturn(cloudWatchClient);
    when(cloudWatchClient.describeAlarms(any())).thenReturn(describeAlarmsResult);
    when(describeAlarmsResult.getMetricAlarms()).thenReturn(List.of(alarm1, alarm2, deletedInstanceAlarm));
    when(alarm1.getAlarmName()).thenReturn(alarm1Name);
    when(alarm2.getAlarmName()).thenReturn(alarm2Name);
    when(deletedInstanceAlarm.getAlarmName()).thenReturn(deletedAlarmName);
    underTest.deleteAllCloudWatchAlarmsForSystemFailures(stack, REGION, credentialView);
    ArgumentCaptor<DescribeAlarmsRequest> captorDescribe = ArgumentCaptor.forClass(DescribeAlarmsRequest.class);
    ArgumentCaptor<DeleteAlarmsRequest> captorDelete = ArgumentCaptor.forClass(DeleteAlarmsRequest.class);
    verify(cloudWatchClient, times(1)).describeAlarms(captorDescribe.capture());
    verify(cloudWatchClient, times(1)).deleteAlarms(captorDelete.capture());
    assertEquals(List.of(alarm1Name, alarm2Name, deletedAlarmName), captorDescribe.getValue().getAlarmNames());
    assertEquals(List.of(alarm1Name, alarm2Name, deletedAlarmName), captorDelete.getValue().getAlarmNames());
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) AmazonCloudWatchClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) DescribeAlarmsRequest(com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) MetricAlarm(com.amazonaws.services.cloudwatch.model.MetricAlarm) DescribeAlarmsResult(com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult) DeleteAlarmsRequest(com.amazonaws.services.cloudwatch.model.DeleteAlarmsRequest) Test(org.junit.jupiter.api.Test)

Aggregations

AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)94 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)32 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)32 List (java.util.List)25 AmazonServiceException (com.amazonaws.AmazonServiceException)22 AmazonCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)21 Logger (org.slf4j.Logger)21 Inject (javax.inject.Inject)20 ArrayList (java.util.ArrayList)19 Collectors (java.util.stream.Collectors)19 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)18 Group (com.sequenceiq.cloudbreak.cloud.model.Group)18 Set (java.util.Set)18 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)17 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)17 Map (java.util.Map)16 LoggerFactory (org.slf4j.LoggerFactory)16 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)15 Service (org.springframework.stereotype.Service)15 AmazonAutoScalingClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient)14