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());
}
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());
}
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;
}
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());
}
});
}
Aggregations