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