Search in sources :

Example 26 with AwsCredentialView

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());
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) MetricAlarm(com.amazonaws.services.cloudwatch.model.MetricAlarm) Group(com.sequenceiq.cloudbreak.cloud.model.Group) DescribeAlarmsResult(com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult) DeleteAlarmsRequest(com.amazonaws.services.cloudwatch.model.DeleteAlarmsRequest) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) DescribeAlarmsRequest(com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Test(org.junit.jupiter.api.Test)

Example 27 with AwsCredentialView

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;
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)

Example 28 with AwsCredentialView

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");
    }
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) LoggerFactory(org.slf4j.LoggerFactory) Validator(com.sequenceiq.cloudbreak.cloud.Validator) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) HashSet(java.util.HashSet) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) GroupSubnet(com.sequenceiq.cloudbreak.cloud.model.GroupSubnet) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) GroupNetwork(com.sequenceiq.cloudbreak.cloud.model.GroupNetwork) Collectors.mapping(java.util.stream.Collectors.mapping) Map(java.util.Map) Subnet(com.amazonaws.services.ec2.model.Subnet) Collectors.toSet(java.util.stream.Collectors.toSet) Logger(org.slf4j.Logger) AmazonServiceException(com.amazonaws.AmazonServiceException) Set(java.util.Set) Collectors(java.util.stream.Collectors) InstanceGroupType(com.sequenceiq.common.api.type.InstanceGroupType) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Component(org.springframework.stereotype.Component) Group(com.sequenceiq.cloudbreak.cloud.model.Group) Optional(java.util.Optional) CommonAwsClient(com.sequenceiq.cloudbreak.cloud.aws.common.CommonAwsClient) Group(com.sequenceiq.cloudbreak.cloud.model.Group) HashSet(java.util.HashSet) Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)

Example 29 with AwsCredentialView

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);
    }
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)

Example 30 with AwsCredentialView

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;
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)

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