Search in sources :

Example 1 with AmazonCloudWatchClient

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

the class AwsCloudWatchService method deleteCloudWatchAlarms.

private void deleteCloudWatchAlarms(String regionName, AwsCredentialView credentialView, List<String> alarmNames) {
    LOGGER.info("Attempting to delete cloudwatch alarms [{}]", alarmNames);
    try {
        DeleteAlarmsRequest deleteAlarmsRequest = new DeleteAlarmsRequest().withAlarmNames(alarmNames);
        AmazonCloudWatchClient amazonCloudWatchClient = awsClient.createCloudWatchClient(credentialView, regionName);
        amazonCloudWatchClient.deleteAlarms(deleteAlarmsRequest);
        LOGGER.info("Deleted cloudwatch alarms [{}]", alarmNames);
    } catch (AmazonCloudWatchException acwe) {
        LOGGER.error("Unable to delete cloudwatch alarms [{}]: {}", alarmNames, acwe.getLocalizedMessage());
        throw new CloudConnectorException("Unable to delete cloud watch alarms: " + acwe.getLocalizedMessage(), acwe);
    }
}
Also used : AmazonCloudWatchClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient) DeleteAlarmsRequest(com.amazonaws.services.cloudwatch.model.DeleteAlarmsRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonCloudWatchException(com.amazonaws.services.cloudwatch.model.AmazonCloudWatchException)

Example 2 with AmazonCloudWatchClient

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

the class AwsCloudWatchServiceTest method testDeleteCloudWatchAlarmsForSystemFailuresBatchesDeletion.

@Test
void testDeleteCloudWatchAlarmsForSystemFailuresBatchesDeletion() {
    List<CloudInstance> cloudInstances = new LinkedList<>();
    List<String> alarmNames1 = new LinkedList<>();
    List<String> alarmNames2 = new LinkedList<>();
    List<String> alarmNames3 = new LinkedList<>();
    List<MetricAlarm> alarms1 = new LinkedList<>();
    List<MetricAlarm> alarms2 = new LinkedList<>();
    List<MetricAlarm> alarms3 = new LinkedList<>();
    for (int i = 1; i <= 100; i++) {
        String alarmName = "i-" + i + "-Status-Check-Failed-System";
        alarmNames1.add(alarmName);
        MetricAlarm alarm = mock(MetricAlarm.class);
        when(alarm.getAlarmName()).thenReturn(alarmName);
        alarms1.add(alarm);
    }
    for (int i = 101; i <= 200; i++) {
        String alarmName = "i-" + i + "-Status-Check-Failed-System";
        alarmNames2.add(alarmName);
        MetricAlarm alarm = mock(MetricAlarm.class);
        when(alarm.getAlarmName()).thenReturn(alarmName);
        alarms2.add(alarm);
    }
    for (int i = 201; i <= 210; i++) {
        String alarmName = "i-" + i + "-Status-Check-Failed-System";
        alarmNames3.add(alarmName);
        MetricAlarm alarm = mock(MetricAlarm.class);
        when(alarm.getAlarmName()).thenReturn(alarmName);
        alarms3.add(alarm);
    }
    for (int i = 1; i <= 210; i++) {
        String instanceId = "i-" + i;
        CloudInstance cloudInstance = mock(CloudInstance.class);
        when(cloudInstance.getInstanceId()).thenReturn(instanceId);
        cloudInstances.add(cloudInstance);
    }
    DescribeAlarmsResult describeAlarmsResult1 = mock(DescribeAlarmsResult.class);
    DescribeAlarmsResult describeAlarmsResult2 = mock(DescribeAlarmsResult.class);
    DescribeAlarmsResult describeAlarmsResult3 = mock(DescribeAlarmsResult.class);
    when(describeAlarmsResult1.getMetricAlarms()).thenReturn(alarms1);
    when(describeAlarmsResult2.getMetricAlarms()).thenReturn(alarms2);
    when(describeAlarmsResult3.getMetricAlarms()).thenReturn(alarms3);
    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);
    when(cloudWatchClient.describeAlarms(any())).thenReturn(describeAlarmsResult1, describeAlarmsResult2, describeAlarmsResult3);
    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(3)).deleteAlarms(captorDelete.capture());
    assertEquals(List.of(alarmNames1, alarmNames2, alarmNames3), captorDescribe.getAllValues().stream().map(DescribeAlarmsRequest::getAlarmNames).collect(Collectors.toList()));
    assertEquals(List.of(alarmNames1, alarmNames2, alarmNames3), 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) 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 3 with AmazonCloudWatchClient

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient 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 4 with AmazonCloudWatchClient

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient 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 5 with AmazonCloudWatchClient

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient 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)

Aggregations

AmazonCloudWatchClient (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient)9 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)7 DeleteAlarmsRequest (com.amazonaws.services.cloudwatch.model.DeleteAlarmsRequest)6 DescribeAlarmsRequest (com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest)6 Test (org.junit.jupiter.api.Test)6 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)5 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)5 AmazonCloudWatchException (com.amazonaws.services.cloudwatch.model.AmazonCloudWatchException)4 MetricAlarm (com.amazonaws.services.cloudwatch.model.MetricAlarm)4 Group (com.sequenceiq.cloudbreak.cloud.model.Group)4 DescribeAlarmsResult (com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult)3 PutMetricAlarmRequest (com.amazonaws.services.cloudwatch.model.PutMetricAlarmRequest)3 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)3 List (java.util.List)3 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)2 LinkedList (java.util.LinkedList)2 Dimension (com.amazonaws.services.cloudwatch.model.Dimension)1 AwsCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient)1 INSTANCE_TYPE (com.sequenceiq.cloudbreak.cloud.model.CloudResource.INSTANCE_TYPE)1 Arrays (java.util.Arrays)1