use of com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus in project cloudbreak by hortonworks.
the class UnhealthyInstancesFinalizerTest method shouldFinalizeInstancesThatAreNotFound.
@Test
public void shouldFinalizeInstancesThatAreNotFound() {
Stack stack = TestUtil.stack(Status.AVAILABLE, TestUtil.awsCredential());
CloudCredential cloudCredential = mock(CloudCredential.class);
when(credentialConverter.convert(stack.getCredential())).thenReturn(cloudCredential);
String instanceId1 = "i-0f1e0605506aaaaaa";
String instanceId2 = "i-0f1e0605506bbbbbb";
Set<InstanceMetaData> candidateUnhealthyInstances = new HashSet<>();
setupInstanceMetaData(instanceId1, candidateUnhealthyInstances);
setupInstanceMetaData(instanceId2, candidateUnhealthyInstances);
List<CloudInstance> cloudInstances = new ArrayList<>();
CloudInstance cloudInstance1 = setupCloudInstance(instanceId1, cloudInstances);
when(cloudInstanceConverter.convert(candidateUnhealthyInstances)).thenReturn(cloudInstances);
List<CloudVmInstanceStatus> cloudVmInstanceStatusList = new ArrayList<>();
setupCloudVmInstanceStatus(cloudInstance1, InstanceStatus.TERMINATED, cloudVmInstanceStatusList);
when(instanceStateQuery.getCloudVmInstanceStatuses(eq(cloudCredential), any(CloudContext.class), eq(cloudInstances))).thenReturn(cloudVmInstanceStatusList);
Set<String> unhealthyInstances = underTest.finalizeUnhealthyInstances(stack, candidateUnhealthyInstances);
assertEquals(2, unhealthyInstances.size());
assertTrue(unhealthyInstances.contains(instanceId1));
assertTrue(unhealthyInstances.contains(instanceId2));
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus in project cloudbreak by hortonworks.
the class AwsInstanceConnector method stop.
@Override
public List<CloudVmInstanceStatus> stop(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms) {
List<CloudVmInstanceStatus> statuses = new ArrayList<>();
AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
for (String group : getGroups(vms)) {
Collection<String> instances = new ArrayList<>();
Collection<CloudInstance> cloudInstances = new ArrayList<>();
for (CloudInstance vm : vms) {
if (vm.getTemplate().getGroupName().equals(group)) {
instances.add(vm.getInstanceId());
cloudInstances.add(vm);
}
}
try {
instances = removeInstanceIdsWhichAreNotInCorrectState(instances, amazonEC2Client, "Stopped");
if (!instances.isEmpty()) {
amazonEC2Client.stopInstances(new StopInstancesRequest().withInstanceIds(instances));
}
for (CloudInstance cloudInstance : cloudInstances) {
statuses.add(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.IN_PROGRESS));
}
} catch (RuntimeException e) {
LOGGER.error("Stop instances failed on AWS", e);
String message = e instanceof AmazonEC2Exception ? ((AmazonEC2Exception) e).getErrorCode() : e.getMessage();
for (CloudInstance cloudInstance : cloudInstances) {
statuses.add(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.FAILED, message));
}
}
}
return statuses;
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus in project cloudbreak by hortonworks.
the class AwsInstanceConnector method start.
@Override
public List<CloudVmInstanceStatus> start(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms) {
List<CloudVmInstanceStatus> statuses = new ArrayList<>();
AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
for (String group : getGroups(vms)) {
Collection<String> instances = new ArrayList<>();
Collection<CloudInstance> cloudInstances = new ArrayList<>();
for (CloudInstance vm : vms) {
if (vm.getTemplate().getGroupName().equals(group)) {
instances.add(vm.getInstanceId());
cloudInstances.add(vm);
}
}
try {
instances = removeInstanceIdsWhichAreNotInCorrectState(instances, amazonEC2Client, "Running");
if (!instances.isEmpty()) {
amazonEC2Client.startInstances(new StartInstancesRequest().withInstanceIds(instances));
}
for (CloudInstance cloudInstance : cloudInstances) {
statuses.add(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.IN_PROGRESS));
}
} catch (RuntimeException e) {
LOGGER.error("Start instances failed on AWS", e);
String message = e instanceof AmazonEC2Exception ? ((AmazonEC2Exception) e).getErrorCode() : e.getMessage();
for (CloudInstance cloudInstance : cloudInstances) {
statuses.add(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.FAILED, message));
}
}
}
return statuses;
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus in project cloudbreak by hortonworks.
the class UnhealthyInstancesFinalizerTest method setupCloudVmInstanceStatus.
private CloudVmInstanceStatus setupCloudVmInstanceStatus(CloudInstance cloudInstance1, InstanceStatus stopped, List<CloudVmInstanceStatus> cloudVmInstanceStatusList) {
CloudVmInstanceStatus status1 = mock(CloudVmInstanceStatus.class);
when(status1.getCloudInstance()).thenReturn(cloudInstance1);
when(status1.getStatus()).thenReturn(stopped);
cloudVmInstanceStatusList.add(status1);
return status1;
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus in project cloudbreak by hortonworks.
the class UnhealthyInstancesFinalizerTest method shouldFinalizeInstancesMarkedAsTerminated.
@Test
public void shouldFinalizeInstancesMarkedAsTerminated() {
Stack stack = TestUtil.stack(Status.AVAILABLE, TestUtil.awsCredential());
CloudCredential cloudCredential = mock(CloudCredential.class);
when(credentialConverter.convert(stack.getCredential())).thenReturn(cloudCredential);
String instanceId1 = "i-0f1e0605506aaaaaa";
String instanceId2 = "i-0f1e0605506bbbbbb";
Set<InstanceMetaData> candidateUnhealthyInstances = new HashSet<>();
setupInstanceMetaData(instanceId1, candidateUnhealthyInstances);
setupInstanceMetaData(instanceId2, candidateUnhealthyInstances);
List<CloudInstance> cloudInstances = new ArrayList<>();
CloudInstance cloudInstance1 = setupCloudInstance(instanceId1, cloudInstances);
CloudInstance cloudInstance2 = setupCloudInstance(instanceId2, cloudInstances);
when(cloudInstanceConverter.convert(candidateUnhealthyInstances)).thenReturn(cloudInstances);
List<CloudVmInstanceStatus> cloudVmInstanceStatusList = new ArrayList<>();
setupCloudVmInstanceStatus(cloudInstance1, InstanceStatus.STOPPED, cloudVmInstanceStatusList);
setupCloudVmInstanceStatus(cloudInstance2, InstanceStatus.TERMINATED, cloudVmInstanceStatusList);
when(instanceStateQuery.getCloudVmInstanceStatuses(eq(cloudCredential), any(CloudContext.class), eq(cloudInstances))).thenReturn(cloudVmInstanceStatusList);
Set<String> unhealthyInstances = underTest.finalizeUnhealthyInstances(stack, candidateUnhealthyInstances);
assertEquals(1, unhealthyInstances.size());
assertTrue(unhealthyInstances.contains(instanceId2));
}
Aggregations