Search in sources :

Example 41 with AmazonEc2Client

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

the class AwsDefaultRegionSelector method describeRegionsViaEc2Region.

private boolean describeRegionsViaEc2Region(AwsCredentialView awsCredential, String region) {
    boolean regionIsViable = false;
    try {
        LOGGER.debug("Describing regions on EC2 API in '{}'", region);
        AmazonEc2Client access = awsClient.createAccessWithMinimalRetries(awsCredential, region);
        DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
        access.describeRegions(describeRegionsRequest);
        regionIsViable = true;
    } catch (AmazonEC2Exception ec2Exception) {
        String errorMessage = String.format("Unable to describe regions via using EC2 region '%s' APIs, due to: '%s'", region, ec2Exception.getMessage());
        LOGGER.debug(errorMessage, ec2Exception);
        if (!EC2_AUTH_FAILURE_ERROR_CODE.equals(ec2Exception.getErrorCode())) {
            throw ec2Exception;
        }
    } catch (RuntimeException e) {
        String errorMessage = String.format("Unable to describe EC2 regions from AWS: %s", e.getMessage());
        LOGGER.warn(errorMessage, e);
        throw e;
    }
    return regionIsViable;
}
Also used : DescribeRegionsRequest(com.amazonaws.services.ec2.model.DescribeRegionsRequest) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Example 42 with AmazonEc2Client

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

the class AwsInstanceConnector method getConsoleOutput.

@Override
public String getConsoleOutput(AuthenticatedContext authenticatedContext, CloudInstance vm) {
    if (!verifyHostKey) {
        throw new CloudOperationNotSupportedException("Host key verification is disabled on AWS");
    }
    AmazonEc2Client amazonEC2Client = new AuthenticatedContextView(authenticatedContext).getAmazonEC2Client();
    GetConsoleOutputRequest getConsoleOutputRequest = new GetConsoleOutputRequest().withInstanceId(vm.getInstanceId());
    GetConsoleOutputResult getConsoleOutputResult = amazonEC2Client.getConsoleOutput(getConsoleOutputRequest);
    try {
        return getConsoleOutputResult.getOutput() == null ? "" : getConsoleOutputResult.getDecodedOutput();
    } catch (Exception ex) {
        LOGGER.warn(ex.getMessage(), ex);
        return "";
    }
}
Also used : CloudOperationNotSupportedException(com.sequenceiq.cloudbreak.cloud.exception.CloudOperationNotSupportedException) AuthenticatedContextView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AuthenticatedContextView) GetConsoleOutputResult(com.amazonaws.services.ec2.model.GetConsoleOutputResult) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) GetConsoleOutputRequest(com.amazonaws.services.ec2.model.GetConsoleOutputRequest) SdkClientException(com.amazonaws.SdkClientException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) CloudOperationNotSupportedException(com.sequenceiq.cloudbreak.cloud.exception.CloudOperationNotSupportedException)

Example 43 with AmazonEc2Client

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

the class AwsDownscaleServiceTest method downscaleASGWhenAllInstancesHaveBeenRemovedFromASG.

@Test
void downscaleASGWhenAllInstancesHaveBeenRemovedFromASG() {
    CloudStack stack = mock(CloudStack.class);
    List<CloudResource> resources = List.of(new CloudResource.Builder().name("i-1").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-2").type(ResourceType.AWS_INSTANCE).build(), new CloudResource.Builder().name("i-3").type(ResourceType.AWS_INSTANCE).build());
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    List<CloudInstance> cloudInstances = new ArrayList<>();
    InstanceTemplate workerTemplate = mock(InstanceTemplate.class);
    when(workerTemplate.getGroupName()).thenReturn("worker");
    CloudInstance workerInstance1 = new CloudInstance("i-worker1", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance2 = new CloudInstance("i-worker2", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    CloudInstance workerInstance3 = new CloudInstance("i-worker3", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    cloudInstances.add(workerInstance1);
    cloudInstances.add(workerInstance2);
    cloudInstances.add(workerInstance3);
    CloudContext context = CloudContext.Builder.builder().withId(1L).withName("teststack").withCrn("crn").withPlatform("AWS").withVariant("AWS").withLocation(Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a"))).withAccountId("1").build();
    AuthenticatedContext authenticatedContext = new AuthenticatedContext(context, new CloudCredential());
    AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
    when(awsClient.createAutoScalingClient(any(), anyString())).thenReturn(amazonAutoScalingClient);
    AmazonEc2Client amazonEC2Client = mock(AmazonEc2Client.class);
    when(awsClient.createEc2Client(any(), anyString())).thenReturn(amazonEC2Client);
    AmazonEC2Waiters amazonEC2Waiters = mock(AmazonEC2Waiters.class);
    when(amazonEC2Client.waiters()).thenReturn(amazonEC2Waiters);
    Waiter waiter = mock(Waiter.class);
    when(amazonEC2Waiters.instanceTerminated()).thenReturn(waiter);
    when(cfStackUtil.getAutoscalingGroupName(any(), (String) any(), any())).thenReturn("autoscalegroup-1");
    DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
    AutoScalingGroup autoScalingGroup = new AutoScalingGroup();
    autoScalingGroup.setInstances(List.of());
    describeAutoScalingGroupsResult.setAutoScalingGroups(List.of(autoScalingGroup));
    ArgumentCaptor<DescribeAutoScalingGroupsRequest> describeAutoScalingGroupsRequest = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class);
    when(amazonAutoScalingClient.describeAutoScalingGroups(describeAutoScalingGroupsRequest.capture())).thenReturn(describeAutoScalingGroupsResult);
    mockDescribeInstances(amazonEC2Client);
    underTest.downscale(authenticatedContext, stack, resources, cloudInstances);
    verify(amazonAutoScalingClient, never()).detachInstances(any());
    verify(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
    assertEquals(describeAutoScalingGroupsRequest.getValue().getAutoScalingGroupNames(), List.of("autoscalegroup-1"));
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) AmazonEC2Waiters(com.amazonaws.services.ec2.waiters.AmazonEC2Waiters) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Waiter(com.amazonaws.waiters.Waiter) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Test(org.junit.jupiter.api.Test)

Example 44 with AmazonEc2Client

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

the class AwsDownscaleServiceTest method downscaleOrderTest.

@Test
void downscaleOrderTest() {
    // We need to invoke  detach, terminate and update ASG in this order othervise a strange sporadic concurrency issue can occure on AWS side
    // The proper order is:
    // amazonASClient.detachInstances(...);
    // amazonEC2Client.terminateInstances(...);
    // amazonASClient.updateAutoScalingGroup(...);
    CloudStack stack = mock(CloudStack.class);
    List<CloudResource> resources = List.of(new CloudResource.Builder().name("i-1").type(ResourceType.AWS_INSTANCE).build());
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    List<CloudInstance> cloudInstances = new ArrayList<>();
    InstanceTemplate workerTemplate = mock(InstanceTemplate.class);
    when(workerTemplate.getGroupName()).thenReturn("worker");
    CloudInstance workerInstance1 = new CloudInstance("i-worker1", workerTemplate, instanceAuthentication, "subnet-1", "az1");
    cloudInstances.add(workerInstance1);
    CloudContext context = CloudContext.Builder.builder().withId(1L).withName("teststack").withCrn("crn").withPlatform("AWS").withVariant("AWS").withLocation(Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a"))).withAccountId("1").build();
    AuthenticatedContext authenticatedContext = new AuthenticatedContext(context, new CloudCredential());
    AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
    DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
    AutoScalingGroup autoScalingGroup = new AutoScalingGroup();
    autoScalingGroup.setInstances(List.of(new Instance().withInstanceId("i-worker1")));
    describeAutoScalingGroupsResult.setAutoScalingGroups(List.of(autoScalingGroup));
    when(amazonAutoScalingClient.describeAutoScalingGroups(any())).thenReturn(describeAutoScalingGroupsResult);
    when(awsClient.createAutoScalingClient(any(), anyString())).thenReturn(amazonAutoScalingClient);
    AmazonEc2Client amazonEC2Client = mock(AmazonEc2Client.class);
    when(awsClient.createEc2Client(any(), anyString())).thenReturn(amazonEC2Client);
    AmazonEC2Waiters amazonEC2Waiters = mock(AmazonEC2Waiters.class);
    when(amazonEC2Client.waiters()).thenReturn(amazonEC2Waiters);
    Waiter waiter = mock(Waiter.class);
    when(amazonEC2Waiters.instanceTerminated()).thenReturn(waiter);
    when(amazonAutoScalingClient.detachInstances(any())).thenReturn(new DetachInstancesResult());
    when(amazonEC2Client.terminateInstances(any())).thenReturn(new TerminateInstancesResult());
    when(cfStackUtil.getAutoscalingGroupName(any(), (String) any(), any())).thenReturn("autoscalegroup-1");
    mockDescribeInstances(amazonEC2Client);
    // create inOrder object passing any mocks that need to be verified in order
    InOrder inOrder = Mockito.inOrder(amazonAutoScalingClient, amazonEC2Client);
    underTest.downscale(authenticatedContext, stack, resources, cloudInstances);
    verify(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
    // Following will make sure that detach, ivoked before terminate and terminate invoked before update ASG!
    inOrder.verify(amazonAutoScalingClient).detachInstances(any());
    inOrder.verify(amazonEC2Client).terminateInstances(any());
    inOrder.verify(amazonAutoScalingClient).updateAutoScalingGroup(any());
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) InOrder(org.mockito.InOrder) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.autoscaling.model.Instance) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) AmazonEC2Waiters(com.amazonaws.services.ec2.waiters.AmazonEC2Waiters) DetachInstancesResult(com.amazonaws.services.autoscaling.model.DetachInstancesResult) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Waiter(com.amazonaws.waiters.Waiter) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Test(org.junit.jupiter.api.Test)

Example 45 with AmazonEc2Client

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

the class AwsDownscaleServiceTest method downscaleWithLoadBalancers.

@Test
void downscaleWithLoadBalancers() {
    CloudStack stack = mock(CloudStack.class);
    List<CloudResource> resources = Collections.emptyList();
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    InstanceTemplate workerTemplate = mock(InstanceTemplate.class);
    when(workerTemplate.getGroupName()).thenReturn("worker");
    List<CloudInstance> cloudInstances = List.of(new CloudInstance("i-worker1", workerTemplate, instanceAuthentication, "subnet-1", "az1"));
    CloudLoadBalancer privateLoadBalancer = new CloudLoadBalancer(LoadBalancerType.PRIVATE);
    CloudLoadBalancer publicLoadBalancer = new CloudLoadBalancer(LoadBalancerType.PUBLIC);
    CloudContext context = CloudContext.Builder.builder().withId(1L).withName("teststack").withCrn("crn").withPlatform("AWS").withVariant("AWS").withLocation(Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a"))).withAccountId("1").build();
    AuthenticatedContext authenticatedContext = new AuthenticatedContext(context, new CloudCredential());
    AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
    when(awsClient.createAutoScalingClient(any(), anyString())).thenReturn(amazonAutoScalingClient);
    AmazonEc2Client amazonEC2Client = mock(AmazonEc2Client.class);
    when(awsClient.createEc2Client(any(), anyString())).thenReturn(amazonEC2Client);
    AmazonEC2Waiters amazonEC2Waiters = mock(AmazonEC2Waiters.class);
    when(amazonEC2Client.waiters()).thenReturn(amazonEC2Waiters);
    Waiter waiter = mock(Waiter.class);
    when(amazonEC2Waiters.instanceTerminated()).thenReturn(waiter);
    when(cfStackUtil.getAutoscalingGroupName(any(), (String) any(), any())).thenReturn("autoscalegroup-1");
    when(stack.getLoadBalancers()).thenReturn(List.of(privateLoadBalancer, publicLoadBalancer));
    doNothing().when(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
    DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
    AutoScalingGroup autoScalingGroup = new AutoScalingGroup();
    autoScalingGroup.setInstances(List.of(new Instance().withInstanceId("i-worker1")));
    describeAutoScalingGroupsResult.setAutoScalingGroups(List.of(autoScalingGroup));
    ArgumentCaptor<DescribeAutoScalingGroupsRequest> describeAutoScalingGroupsRequest = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class);
    ArgumentCaptor<DetachInstancesRequest> detachInstancesRequestArgumentCaptor = ArgumentCaptor.forClass(DetachInstancesRequest.class);
    when(amazonAutoScalingClient.describeAutoScalingGroups(describeAutoScalingGroupsRequest.capture())).thenReturn(describeAutoScalingGroupsResult);
    when(amazonAutoScalingClient.detachInstances(detachInstancesRequestArgumentCaptor.capture())).thenReturn(new DetachInstancesResult());
    mockDescribeInstances(amazonEC2Client);
    underTest.downscale(authenticatedContext, stack, resources, cloudInstances);
    verify(loadBalancerService).removeLoadBalancerTargets(any(), any(), any());
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.autoscaling.model.Instance) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) AmazonEC2Waiters(com.amazonaws.services.ec2.waiters.AmazonEC2Waiters) DetachInstancesResult(com.amazonaws.services.autoscaling.model.DetachInstancesResult) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Waiter(com.amazonaws.waiters.Waiter) DetachInstancesRequest(com.amazonaws.services.autoscaling.model.DetachInstancesRequest) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Test(org.junit.jupiter.api.Test)

Aggregations

AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)97 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)44 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)41 Test (org.junit.Test)31 ArrayList (java.util.ArrayList)30 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)29 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)29 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)28 HashMap (java.util.HashMap)28 Group (com.sequenceiq.cloudbreak.cloud.model.Group)24 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)24 DescribeSubnetsResult (com.amazonaws.services.ec2.model.DescribeSubnetsResult)23 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)23 Network (com.sequenceiq.cloudbreak.cloud.model.Network)23 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)22 AmazonServiceException (com.amazonaws.AmazonServiceException)21 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)21 List (java.util.List)21 Vpc (com.amazonaws.services.ec2.model.Vpc)20 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)20