use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsNativeCloudWatchServiceTest 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);
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(commonAwsClient.createCloudWatchClient(eq(credentialView), eq(REGION))).thenReturn(mockAmazonCloudWatchClient);
when(mockAmazonCloudWatchClient.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(mockAmazonCloudWatchClient, times(1)).describeAlarms(captorDescribe.capture());
verify(mockAmazonCloudWatchClient, 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.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsNativeCloudWatchResourceBuilder method delete.
@Override
public CloudResource delete(AwsContext context, AuthenticatedContext auth, CloudResource resource) throws Exception {
String region = context.getLocation().getRegion().getRegionName();
AwsCredentialView credential = new AwsCredentialView(auth.getCloudCredential());
String instanceId = resource.getInstanceId() == null ? resource.getReference() : resource.getInstanceId();
boolean instanceHasAlarm = !nativeCloudWatchService.getMetricAlarmsForInstances(region, credential, Set.of(instanceId)).isEmpty();
if (instanceHasAlarm) {
LOGGER.info("About to remove CloudWatch alarm for instance: {}", instanceId);
nativeCloudWatchService.deleteCloudWatchAlarmsForSystemFailures(region, credential, List.of(instanceId));
return resource;
}
LOGGER.info("No alarm has found for instance: {}", instanceId);
return null;
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsGatewaySubnetMultiAzValidator method validate.
@Override
public void validate(AuthenticatedContext ac, CloudStack cloudStack) {
LOGGER.debug("Validating that only one subnet per availability zone have been provided for the gateway group.");
AwsCredentialView awsCredential = new AwsCredentialView(ac.getCloudCredential());
String region = ac.getCloudContext().getLocation().getRegion().value();
Optional<Group> gatewayGroupOptional = getGatewayGroup(cloudStack);
if (gatewayGroupOptional.isPresent()) {
Group group = gatewayGroupOptional.get();
Set<String> subnetIds = getInstanceGroupNetworSubnetIds(group);
if (!subnetIds.isEmpty()) {
Map<String, Set<String>> subnetIdSetsByAvailabilityZone = fetchingSubnetsAndGroupingByAvailabilityZone(awsCredential, subnetIds, region);
Set<String> availabilityZonesWithMultipleSubnets = subnetIdSetsByAvailabilityZone.entrySet().stream().filter(entry -> entry.getValue().size() > 1).map(e -> String.format("subnets: '%s' from availability zone: '%s'", String.join(",", e.getValue()), e.getKey())).collect(toSet());
if (!availabilityZonesWithMultipleSubnets.isEmpty()) {
LOGGER.debug("Validation error found there are multiple subnets associated with an availability zone");
String validationError = String.format("Only one subnet per Availability Zone can be specified for a Gateway group. The request contains %s", String.join(", ", availabilityZonesWithMultipleSubnets));
LOGGER.info(validationError);
throw new CloudConnectorException(validationError);
}
} else {
LOGGER.debug("No group network subnet specified, there is no info to be validated");
}
LOGGER.debug("There is no validation error found for gateway's group network's subnets");
}
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsNativeMetadataCollector method collect.
@Override
public List<CloudVmMetaDataStatus> collect(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms, List<CloudInstance> allInstances) {
LOGGER.debug("Collect AWS instance metadata, for cluster {} resources: {}, VMs: {}, allInstances: {}", ac.getCloudContext().getName(), resources.size(), vms.size(), allInstances.size());
try {
String region = ac.getCloudContext().getLocation().getRegion().value();
AmazonEc2Client ec2Client = awsClient.createEc2Client(new AwsCredentialView(ac.getCloudCredential()), region);
return collectInstances(vms, resources, ec2Client);
} catch (RuntimeException e) {
LOGGER.warn("Collection of instance metadata failed", e);
throw new CloudConnectorException(e.getMessage(), e);
}
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsNativeResourceConnector method upscale.
@Override
public List<CloudResourceStatus> upscale(AuthenticatedContext auth, CloudStack stack, List<CloudResource> resources, AdjustmentTypeWithThreshold adjustmentTypeWithThreshold) throws QuotaExceededException {
List<CloudResourceStatus> upscale = super.upscale(auth, stack, resources, adjustmentTypeWithThreshold);
LOGGER.info("Launching elastic load balancers");
CloudCredential cloudCredential = auth.getCloudCredential();
String region = auth.getCloudContext().getLocation().getRegion().value();
AwsCredentialView awsCredentialView = new AwsCredentialView(cloudCredential);
AmazonElasticLoadBalancingClient elasticLoadBalancingClient = commonAwsClient.createElasticLoadBalancingClient(awsCredentialView, region);
loadBalancerLaunchService.launchLoadBalancerResources(auth, stack, persistenceNotifier, elasticLoadBalancingClient, true);
return upscale;
}
Aggregations