use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project cloudbreak by hortonworks.
the class AwsMetadataCollector method collectInstancesForGroup.
public List<Instance> collectInstancesForGroup(AuthenticatedContext ac, AmazonAutoScalingClient amazonASClient, AmazonEc2Client amazonEC2Client, AmazonCloudFormationClient amazonCFClient, String group) {
LOGGER.debug("Collect aws instances for group: {}", group);
String asGroupName = cloudFormationStackUtil.getAutoscalingGroupName(ac, amazonCFClient, group);
List<String> instanceIds = cloudFormationStackUtil.getInstanceIds(amazonASClient, asGroupName);
DescribeInstancesRequest instancesRequest = cloudFormationStackUtil.createDescribeInstancesRequest(instanceIds);
DescribeInstancesResult instancesResult = amazonEC2Client.describeInstances(instancesRequest);
return instancesResult.getReservations().stream().flatMap(reservation -> reservation.getInstances().stream()).collect(Collectors.toList());
}
use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project cloudbreak by hortonworks.
the class AwsRepairTest method upscaleStack.
private void upscaleStack() throws Exception {
AuthenticatedContext authenticatedContext = componentTestUtil.getAuthenticatedContext();
CloudStack stack = componentTestUtil.getStack(InstanceStatus.CREATE_REQUESTED, InstanceStatus.STARTED);
List<CloudResource> cloudResources = List.of(CloudResource.builder().name(AWS_SUBNET_ID).type(ResourceType.AWS_SUBNET).build(), createVolumeResource(VOLUME_ID_1, INSTANCE_ID_1, SIZE_DISK_1, FSTAB_1, CommonStatus.DETACHED), createVolumeResource(VOLUME_ID_2, INSTANCE_ID_2, SIZE_DISK_2, FSTAB_2, CommonStatus.DETACHED), createVolumeResource(VOLUME_ID_3, INSTANCE_ID_3, SIZE_DISK_2, FSTAB_2, CommonStatus.CREATED));
InMemoryStateStore.putStack(1L, PollGroup.POLLABLE);
when(amazonCloudFormationClient.describeStackResource(any())).thenReturn(new DescribeStackResourceResult().withStackResourceDetail(new StackResourceDetail().withPhysicalResourceId(AUTOSCALING_GROUP_NAME)));
when(amazonAutoScalingClient.describeAutoScalingGroups(any())).thenReturn(new DescribeAutoScalingGroupsResult().withAutoScalingGroups(new AutoScalingGroup().withAutoScalingGroupName(AUTOSCALING_GROUP_NAME).withInstances(List.of(new Instance().withInstanceId(INSTANCE_ID_1).withLifecycleState(LifecycleState.InService), new Instance().withInstanceId(INSTANCE_ID_2).withLifecycleState(LifecycleState.InService)))));
when(amazonEC2Client.describeVolumes(any())).thenReturn(new DescribeVolumesResult().withVolumes(new com.amazonaws.services.ec2.model.Volume().withVolumeId(VOLUME_ID_1).withState(VolumeState.Available), new com.amazonaws.services.ec2.model.Volume().withVolumeId(VOLUME_ID_2).withState(VolumeState.Available), new com.amazonaws.services.ec2.model.Volume().withVolumeId(VOLUME_ID_3).withState(VolumeState.InUse)));
when(amazonEC2Client.describeInstances(any())).thenReturn(new DescribeInstancesResult().withReservations(new Reservation().withInstances(new com.amazonaws.services.ec2.model.Instance().withInstanceId("i-instance"))));
DescribeScalingActivitiesResult result = new DescribeScalingActivitiesResult();
result.setActivities(List.of());
when(amazonAutoScalingClient.describeScalingActivities(any(DescribeScalingActivitiesRequest.class))).thenReturn(result);
AmazonEC2Waiters waiters = mock(AmazonEC2Waiters.class);
when(amazonEC2Client.waiters()).thenReturn(waiters);
Waiter<DescribeInstancesRequest> instanceWaiter = mock(Waiter.class);
when(waiters.instanceRunning()).thenReturn(instanceWaiter);
when(amazonAutoScalingClient.waiters()).thenReturn(asWaiters);
when(asWaiters.groupInService()).thenReturn(describeAutoScalingGroupsRequestWaiter);
underTest.upscale(authenticatedContext, stack, cloudResources, new AdjustmentTypeWithThreshold(AdjustmentType.EXACT, 0L));
verify(amazonAutoScalingClient).resumeProcesses(argThat(argument -> AUTOSCALING_GROUP_NAME.equals(argument.getAutoScalingGroupName()) && argument.getScalingProcesses().contains("Launch")));
verify(amazonAutoScalingClient).updateAutoScalingGroup(argThat(argument -> {
Group workerGroup = stack.getGroups().get(1);
return AUTOSCALING_GROUP_NAME.equals(argument.getAutoScalingGroupName()) && workerGroup.getInstancesSize().equals(argument.getMaxSize()) && workerGroup.getInstancesSize().equals(argument.getDesiredCapacity());
}));
verify(amazonAutoScalingClient, times(stack.getGroups().size())).suspendProcesses(argThat(argument -> AUTOSCALING_GROUP_NAME.equals(argument.getAutoScalingGroupName()) && SUSPENDED_PROCESSES.equals(argument.getScalingProcesses())));
ArgumentCaptor<CloudResource> updatedCloudResourceArgumentCaptor = ArgumentCaptor.forClass(CloudResource.class);
verify(resourceNotifier, times(2)).notifyUpdate(updatedCloudResourceArgumentCaptor.capture(), any());
assertVolumeResource(updatedCloudResourceArgumentCaptor.getAllValues(), INSTANCE_ID_1, SIZE_DISK_1, FSTAB_1);
assertVolumeResource(updatedCloudResourceArgumentCaptor.getAllValues(), INSTANCE_ID_2, SIZE_DISK_2, FSTAB_2);
}
use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project cloudbreak by hortonworks.
the class AwsDownscaleServiceTest method mockDescribeInstances.
private void mockDescribeInstances(AmazonEc2Client amazonEC2Client) {
when(amazonEC2Client.describeInstances(any(DescribeInstancesRequest.class))).thenAnswer(a -> {
DescribeInstancesRequest request = a.getArgument(0, DescribeInstancesRequest.class);
List<com.amazonaws.services.ec2.model.Instance> instances = request.getInstanceIds().stream().map(i -> new com.amazonaws.services.ec2.model.Instance().withInstanceId(i)).collect(Collectors.toList());
return new DescribeInstancesResult().withReservations(new Reservation().withInstances(instances));
});
}
use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project cloudbreak by hortonworks.
the class AwsDownscaleServiceTest method downscaleButInstanceNotFoundOnAWS.
@Test
public void downscaleButInstanceNotFoundOnAWS() {
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(), new CloudResource.Builder().name("i-4").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");
CloudInstance workerInstance4 = new CloudInstance("i-worker4", workerTemplate, instanceAuthentication, "subnet-1", "az1");
cloudInstances.add(workerInstance1);
cloudInstances.add(workerInstance2);
cloudInstances.add(workerInstance3);
cloudInstances.add(workerInstance4);
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");
AmazonServiceException amazonServiceException = new AmazonServiceException("Cannot execute method: terminateInstances. Invalid id: " + "\"i-worker1\",\"i-worker2\"");
amazonServiceException.setErrorCode(INSTANCE_NOT_FOUND_ERROR_CODE);
when(amazonEC2Client.terminateInstances(any())).thenThrow(amazonServiceException).thenReturn(new TerminateInstancesResult());
AmazonServiceException amazonServiceExceptionForWaiter = new AmazonServiceException("Cannot execute method: terminateInstances. Invalid id: " + "\"i-worker3\"");
amazonServiceExceptionForWaiter.setErrorCode(INSTANCE_NOT_FOUND_ERROR_CODE);
doThrow(amazonServiceExceptionForWaiter).doNothing().when(waiter).run(any());
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(cfStackUtil, times(0)).removeLoadBalancerTargets(any(), any(), any());
ArgumentCaptor<TerminateInstancesRequest> terminateInstancesRequestArgumentCaptor = ArgumentCaptor.forClass(TerminateInstancesRequest.class);
verify(amazonEC2Client, times(2)).terminateInstances(terminateInstancesRequestArgumentCaptor.capture());
List<TerminateInstancesRequest> allValues = terminateInstancesRequestArgumentCaptor.getAllValues();
List<String> firstTerminateInstanceIds = allValues.get(0).getInstanceIds();
assertEquals(4, firstTerminateInstanceIds.size());
assertTrue(firstTerminateInstanceIds.contains("i-worker1"));
assertTrue(firstTerminateInstanceIds.contains("i-worker2"));
assertTrue(firstTerminateInstanceIds.contains("i-worker3"));
assertTrue(firstTerminateInstanceIds.contains("i-worker4"));
List<String> secondTerminateInstanceIds = allValues.get(1).getInstanceIds();
assertEquals(2, secondTerminateInstanceIds.size());
assertTrue(firstTerminateInstanceIds.contains("i-worker3"));
assertTrue(firstTerminateInstanceIds.contains("i-worker4"));
ArgumentCaptor<WaiterParameters> waiterParametersArgumentCaptor = ArgumentCaptor.forClass(WaiterParameters.class);
verify(waiter, times(2)).run(waiterParametersArgumentCaptor.capture());
List<WaiterParameters> waiterParametersList = waiterParametersArgumentCaptor.getAllValues();
DescribeInstancesRequest firstDescribeInstancesRequest = (DescribeInstancesRequest) waiterParametersList.get(0).getRequest();
assertEquals(2, firstDescribeInstancesRequest.getInstanceIds().size());
assertTrue(firstDescribeInstancesRequest.getInstanceIds().contains("i-worker3"));
assertTrue(firstDescribeInstancesRequest.getInstanceIds().contains("i-worker4"));
DescribeInstancesRequest secondDescribeInstancesRequest = (DescribeInstancesRequest) waiterParametersList.get(1).getRequest();
assertEquals(1, secondDescribeInstancesRequest.getInstanceIds().size());
assertTrue(secondDescribeInstancesRequest.getInstanceIds().contains("i-worker4"));
assertEquals(describeAutoScalingGroupsRequest.getValue().getAutoScalingGroupNames(), List.of("autoscalegroup-1"));
}
use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project cloudbreak by hortonworks.
the class EC2ClientActions method deleteHostGroupInstances.
public void deleteHostGroupInstances(List<String> instanceIds) {
AmazonEC2 ec2Client = buildEC2Client();
TerminateInstancesResult terminateInstancesResult = ec2Client.terminateInstances(new TerminateInstancesRequest().withInstanceIds(instanceIds));
for (String instanceId : instanceIds) {
try {
Log.log(LOGGER, format(" EC2 instance [%s] state is [%s] ", instanceId, Objects.requireNonNull(terminateInstancesResult.getTerminatingInstances().stream().filter(instance -> instance.getInstanceId().equals(instanceId)).findAny().orElse(null)).getCurrentState().getName()));
ec2Client.waiters().instanceTerminated().run(new WaiterParameters<DescribeInstancesRequest>(new DescribeInstancesRequest().withInstanceIds(instanceId)).withPollingStrategy(new PollingStrategy(new MaxAttemptsRetryStrategy(80), new FixedDelayStrategy(30))));
DescribeInstancesResult describeInstanceResult = ec2Client.describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceId));
InstanceState actualInstanceState = describeInstanceResult.getReservations().get(0).getInstances().get(0).getState();
if (TERMINATED_STATE.equals(actualInstanceState.getName())) {
Log.log(LOGGER, format(" EC2 Instance: %s state is: %s ", instanceId, TERMINATED_STATE));
} else {
LOGGER.error("EC2 Instance: {} termination has not been successful. So the actual state is: {} ", instanceId, actualInstanceState.getName());
throw new TestFailException(" EC2 Instance: " + instanceId + " termination has not been successful, because of the actual state is: " + actualInstanceState.getName());
}
} catch (WaiterUnrecoverableException e) {
LOGGER.error("EC2 Instance {} termination has not been successful, because of WaiterUnrecoverableException: {}", instanceId, e);
} catch (WaiterTimedOutException e) {
LOGGER.error("EC2 Instance {} termination has not been successful, because of WaiterTimedOutException: {}", instanceId, e);
} catch (EC2UnexpectedException e) {
LOGGER.error("EC2 Instance {} termination has not been successful, because of EC2UnexpectedException: {}", instanceId, e);
}
}
}
Aggregations