Search in sources :

Example 6 with AmazonCloudWatchClient

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

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

Example 8 with AmazonCloudWatchClient

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

the class AwsCloudWatchService method getExistingCloudWatchAlarms.

private Stream<List<String>> getExistingCloudWatchAlarms(String regionName, AwsCredentialView credentialView, List<String> alarmNames) {
    Stream<List<String>> filteredAlarmNamesStream;
    AmazonCloudWatchClient amazonCloudWatchClient = awsClient.createCloudWatchClient(credentialView, regionName);
    LOGGER.info("Searching for cloudwatch alarms [{}]", alarmNames);
    try {
        DescribeAlarmsRequest request = new DescribeAlarmsRequest().withAlarmNames(alarmNames).withMaxRecords(maxBatchsize);
        List<String> filteredAlarmNames = amazonCloudWatchClient.describeAlarms(request).getMetricAlarms().stream().map(MetricAlarm::getAlarmName).collect(Collectors.toList());
        filteredAlarmNamesStream = Stream.of(filteredAlarmNames);
        LOGGER.debug("Checking cloudwatch alarms [{}] for existence and found [{}]", alarmNames, filteredAlarmNames);
    } catch (AmazonCloudWatchException acwe) {
        LOGGER.error("Unable to describe cloudwatch alarms falling back to delete all alarms individually [{}]: {}", alarmNames, acwe.getLocalizedMessage());
        filteredAlarmNamesStream = alarmNames.stream().map(alarmName -> List.of(alarmName));
    }
    return filteredAlarmNamesStream;
}
Also used : AmazonCloudWatchClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient) AmazonCloudWatchException(com.amazonaws.services.cloudwatch.model.AmazonCloudWatchException) DescribeAlarmsRequest(com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest) List(java.util.List)

Example 9 with AmazonCloudWatchClient

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

the class AwsCloudWatchService method addCloudWatchAlarmsForSystemFailures.

public void addCloudWatchAlarmsForSystemFailures(List<CloudResource> instances, String regionName, AwsCredentialView credentialView) {
    AmazonCloudWatchClient amazonCloudWatchClient = awsClient.createCloudWatchClient(credentialView, regionName);
    instances.stream().filter(instance -> {
        String instanceType = instance.getStringParameter(INSTANCE_TYPE);
        if (instanceType == null) {
            LOGGER.debug("Cannot determine if recovery is supported by instance type, attempting to set it up.");
            return true;
        }
        String family = instanceType.contains(".") ? instanceType.split("\\.")[0] : instanceType;
        return RECOVERABLE_INSTANCE_TYPES.contains(family);
    }).forEach(instance -> {
        try {
            PutMetricAlarmRequest metricAlarmRequest = new PutMetricAlarmRequest();
            String segment = credentialView.isGovernmentCloudEnabled() ? "aws-us-gov" : "aws";
            metricAlarmRequest.setAlarmActions(Arrays.asList("arn:" + segment + ":automate:" + regionName + ":ec2:recover"));
            metricAlarmRequest.setAlarmName(instance.getInstanceId() + alarmSuffix);
            metricAlarmRequest.setMetricName("StatusCheckFailed_System");
            metricAlarmRequest.setStatistic("Maximum");
            metricAlarmRequest.setNamespace("AWS/EC2");
            metricAlarmRequest.setDimensions(Arrays.asList(new Dimension().withName("InstanceId").withValue(instance.getInstanceId())));
            metricAlarmRequest.setPeriod(cloudwatchPeriod);
            metricAlarmRequest.setEvaluationPeriods(cloudwatchEvaluationPeriods);
            metricAlarmRequest.setThreshold(cloudwatchThreshhold);
            metricAlarmRequest.setComparisonOperator("GreaterThanOrEqualToThreshold");
            amazonCloudWatchClient.putMetricAlarm(metricAlarmRequest);
            LOGGER.debug("Created cloudwatch alarm for instanceId {}.", instance.getInstanceId());
        } catch (AmazonCloudWatchException acwe) {
            LOGGER.info("Unable to create cloudwatch alarm for instanceId {} (instance type: {}): {}", instance.getInstanceId(), instance.getStringParameter(INSTANCE_TYPE), acwe.getLocalizedMessage());
        }
    });
}
Also used : Arrays(java.util.Arrays) INSTANCE_TYPE(com.sequenceiq.cloudbreak.cloud.model.CloudResource.INSTANCE_TYPE) LoggerFactory(org.slf4j.LoggerFactory) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AmazonCloudWatchClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient) Inject(javax.inject.Inject) Value(org.springframework.beans.factory.annotation.Value) Dimension(com.amazonaws.services.cloudwatch.model.Dimension) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Service(org.springframework.stereotype.Service) Map(java.util.Map) DeleteAlarmsRequest(com.amazonaws.services.cloudwatch.model.DeleteAlarmsRequest) Logger(org.slf4j.Logger) DescribeAlarmsRequest(com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest) MetricAlarm(com.amazonaws.services.cloudwatch.model.MetricAlarm) AwsCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Collectors(java.util.stream.Collectors) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) PutMetricAlarmRequest(com.amazonaws.services.cloudwatch.model.PutMetricAlarmRequest) Stream(java.util.stream.Stream) AmazonCloudWatchException(com.amazonaws.services.cloudwatch.model.AmazonCloudWatchException) AmazonCloudWatchClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient) PutMetricAlarmRequest(com.amazonaws.services.cloudwatch.model.PutMetricAlarmRequest) AmazonCloudWatchException(com.amazonaws.services.cloudwatch.model.AmazonCloudWatchException) Dimension(com.amazonaws.services.cloudwatch.model.Dimension)

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