use of com.amazonaws.services.ec2.model.InstanceStatus in project camel by apache.
the class AmazonEC2ClientMock method describeInstanceStatus.
@Override
public DescribeInstanceStatusResult describeInstanceStatus(DescribeInstanceStatusRequest describeInstanceStatusRequest) {
DescribeInstanceStatusResult result = new DescribeInstanceStatusResult();
Collection<InstanceStatus> instanceStatuses = new ArrayList();
if (describeInstanceStatusRequest.getInstanceIds().isEmpty()) {
InstanceStatus status = new InstanceStatus();
status.setInstanceId("test-1");
status.setInstanceState(new InstanceState().withName(InstanceStateName.Running));
instanceStatuses.add(status);
status.setInstanceId("test-2");
status.setInstanceState(new InstanceState().withName(InstanceStateName.Stopped));
instanceStatuses.add(status);
} else {
if (describeInstanceStatusRequest.getInstanceIds().contains("test-1")) {
InstanceStatus status = new InstanceStatus();
status.setInstanceId("test-1");
status.setInstanceState(new InstanceState().withName(InstanceStateName.Running));
instanceStatuses.add(status);
}
if (describeInstanceStatusRequest.getInstanceIds().contains("test-2")) {
InstanceStatus status = new InstanceStatus();
status.setInstanceId("test-2");
status.setInstanceState(new InstanceState().withName(InstanceStateName.Stopped));
instanceStatuses.add(status);
}
}
result.setInstanceStatuses(instanceStatuses);
return result;
}
use of com.amazonaws.services.ec2.model.InstanceStatus in project cloudbreak by hortonworks.
the class ASGroupStatusCheckerTaskTest method successTest.
@Test
public void successTest() throws Exception {
int requiredInstances = 160;
AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
CloudContext cloudContext = mock(CloudContext.class);
String regionName = "eu-west-1";
when(cloudContext.getLocation()).thenReturn(Location.location(Region.region(regionName)));
when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
CloudCredential cloudCredential = mock(CloudCredential.class);
when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
String asGroupName = "as-group";
AwsClient awsClient = mock(AwsClient.class);
AmazonEC2Client amazonEC2Client = mock(AmazonEC2Client.class);
when(awsClient.createAccess(any(AwsCredentialView.class), eq(regionName))).thenReturn(amazonEC2Client);
DescribeInstanceStatusResult firstDescribeInstanceStatusResult = new DescribeInstanceStatusResult();
List<InstanceStatus> firstInstanceStatuses = returnInstanceStatus(0, 100);
firstDescribeInstanceStatusResult.setInstanceStatuses(firstInstanceStatuses);
DescribeInstanceStatusResult secondDescribeInstanceStatusResult = new DescribeInstanceStatusResult();
List<InstanceStatus> secondInstanceStatuses = returnInstanceStatus(100, 160);
secondDescribeInstanceStatusResult.setInstanceStatuses(secondInstanceStatuses);
when(amazonEC2Client.describeInstanceStatus(any(DescribeInstanceStatusRequest.class))).thenReturn(firstDescribeInstanceStatusResult).thenReturn(secondDescribeInstanceStatusResult);
CloudFormationStackUtil cloudFormationStackUtil = mock(CloudFormationStackUtil.class);
List<String> instancIds = new ArrayList<>();
for (int i = 0; i < requiredInstances; i++) {
instancIds.add(Integer.toString(i));
}
AmazonAutoScalingClient autoScalingClient = mock(AmazonAutoScalingClient.class);
when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), anyString())).thenReturn(autoScalingClient);
when(autoScalingClient.describeScalingActivities(any(DescribeScalingActivitiesRequest.class))).thenReturn(new DescribeScalingActivitiesResult());
when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq(asGroupName))).thenReturn(instancIds);
ASGroupStatusCheckerTask asGroupStatusCheckerTask = new ASGroupStatusCheckerTask(authenticatedContext, asGroupName, requiredInstances, awsClient, cloudFormationStackUtil);
Boolean taskResult = asGroupStatusCheckerTask.call();
ArgumentCaptor<DescribeInstanceStatusRequest> instanceStatusRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeInstanceStatusRequest.class);
verify(amazonEC2Client, times(2)).describeInstanceStatus(instanceStatusRequestArgumentCaptor.capture());
List<DescribeInstanceStatusRequest> allValues = instanceStatusRequestArgumentCaptor.getAllValues();
assertEquals(100, allValues.get(0).getInstanceIds().size());
assertEquals(60, allValues.get(1).getInstanceIds().size());
assertTrue(taskResult);
}
use of com.amazonaws.services.ec2.model.InstanceStatus in project cloudbreak by hortonworks.
the class ASGroupStatusCheckerTaskTest method returnInstanceStatus.
private List<InstanceStatus> returnInstanceStatus(int start, int end) {
List<InstanceStatus> instanceStatuses = new ArrayList<>();
for (int i = start; i < end; i++) {
InstanceStatus instanceStatus = new InstanceStatus();
InstanceState instanceState = new InstanceState();
instanceState.setCode(16);
instanceStatus.setInstanceState(instanceState);
instanceStatus.setInstanceId(Integer.toString(i));
instanceStatuses.add(instanceStatus);
}
return instanceStatuses;
}
use of com.amazonaws.services.ec2.model.InstanceStatus in project cloudbreak by hortonworks.
the class ASGroupStatusCheckerTaskTest method failTest.
@Test
public void failTest() throws Exception {
int requiredInstances = 160;
AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
CloudContext cloudContext = mock(CloudContext.class);
String regionName = "eu-west-1";
when(cloudContext.getLocation()).thenReturn(Location.location(Region.region(regionName)));
when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
CloudCredential cloudCredential = mock(CloudCredential.class);
when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
String asGroupName = "as-group";
AwsClient awsClient = mock(AwsClient.class);
AmazonEC2Client amazonEC2Client = mock(AmazonEC2Client.class);
when(awsClient.createAccess(any(AwsCredentialView.class), eq(regionName))).thenReturn(amazonEC2Client);
DescribeInstanceStatusResult firstDescribeInstanceStatusResult = new DescribeInstanceStatusResult();
List<InstanceStatus> firstInstanceStatuses = returnInstanceStatus(0, 99);
firstDescribeInstanceStatusResult.setInstanceStatuses(firstInstanceStatuses);
DescribeInstanceStatusResult secondDescribeInstanceStatusResult = new DescribeInstanceStatusResult();
List<InstanceStatus> secondInstanceStatuses = returnInstanceStatus(100, 160);
secondDescribeInstanceStatusResult.setInstanceStatuses(secondInstanceStatuses);
when(amazonEC2Client.describeInstanceStatus(any(DescribeInstanceStatusRequest.class))).thenReturn(firstDescribeInstanceStatusResult).thenReturn(secondDescribeInstanceStatusResult);
CloudFormationStackUtil cloudFormationStackUtil = mock(CloudFormationStackUtil.class);
List<String> instancIds = new ArrayList<>();
for (int i = 0; i < requiredInstances; i++) {
instancIds.add(Integer.toString(i));
}
AmazonAutoScalingClient autoScalingClient = mock(AmazonAutoScalingClient.class);
when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), anyString())).thenReturn(autoScalingClient);
when(autoScalingClient.describeScalingActivities(any(DescribeScalingActivitiesRequest.class))).thenReturn(new DescribeScalingActivitiesResult());
when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq(asGroupName))).thenReturn(instancIds);
ASGroupStatusCheckerTask asGroupStatusCheckerTask = new ASGroupStatusCheckerTask(authenticatedContext, asGroupName, requiredInstances, awsClient, cloudFormationStackUtil);
Boolean taskResult = asGroupStatusCheckerTask.call();
ArgumentCaptor<DescribeInstanceStatusRequest> instanceStatusRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeInstanceStatusRequest.class);
verify(amazonEC2Client, times(2)).describeInstanceStatus(instanceStatusRequestArgumentCaptor.capture());
List<DescribeInstanceStatusRequest> allValues = instanceStatusRequestArgumentCaptor.getAllValues();
assertEquals(100, allValues.get(0).getInstanceIds().size());
assertEquals(60, allValues.get(1).getInstanceIds().size());
assertFalse(taskResult);
}
use of com.amazonaws.services.ec2.model.InstanceStatus in project cloudbreak by hortonworks.
the class ASGroupStatusCheckerTask method doCall.
@Override
protected Boolean doCall() {
LOGGER.info("Checking status of Auto Scaling group '{}'", autoScalingGroupName);
AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(getAuthenticatedContext().getCloudCredential()), getAuthenticatedContext().getCloudContext().getLocation().getRegion().value());
List<String> instanceIds = cloudFormationStackUtil.getInstanceIds(autoScalingClient, autoScalingGroupName);
if (instanceIds.size() < requiredInstances) {
LOGGER.debug("Instances in AS group: {}, needed: {}", instanceIds.size(), requiredInstances);
List<Activity> activities = getAutoScalingActivities();
if (latestActivity.isPresent()) {
checkForSpotRequest(latestActivity.get(), amazonEC2Client);
activities = activities.stream().filter(activity -> activity.getStartTime().after(latestActivity.get().getStartTime())).collect(Collectors.toList());
}
updateLatestActivity(activities);
checkFailedActivities(activities);
return false;
}
Collection<DescribeInstanceStatusResult> describeInstanceStatusResultList = new ArrayList<>();
List<List<String>> partitionedInstanceIdsList = Lists.partition(instanceIds, MAX_INSTANCE_ID_SIZE);
for (List<String> partitionedInstanceIds : partitionedInstanceIdsList) {
DescribeInstanceStatusRequest describeInstanceStatusRequest = new DescribeInstanceStatusRequest().withInstanceIds(partitionedInstanceIds);
DescribeInstanceStatusResult describeResult = amazonEC2Client.describeInstanceStatus(describeInstanceStatusRequest);
describeInstanceStatusResultList.add(describeResult);
}
List<InstanceStatus> instanceStatusList = describeInstanceStatusResultList.stream().flatMap(describeInstanceStatusResult -> describeInstanceStatusResult.getInstanceStatuses().stream()).collect(Collectors.toList());
if (instanceStatusList.size() < requiredInstances) {
LOGGER.debug("Instances up: {}, needed: {}", instanceStatusList.size(), requiredInstances);
return false;
}
for (InstanceStatus status : instanceStatusList) {
if (INSTANCE_RUNNING != status.getInstanceState().getCode()) {
LOGGER.debug("Instances are up but not all of them are in running state.");
return false;
}
}
return true;
}
Aggregations