@Test public void shouldFinalizeInstancesMarkedAsTerminated() { Stack stack = TestUtil.stack(Status.AVAILABLE, TestUtil.awsCredential()); CloudCredential cloudCredential = mock(CloudCredential.class); when(stackUtil.getCloudCredential(stack)).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(1L, unhealthyInstances.size()); assertTrue(unhealthyInstances.contains(instanceId2)); }
public Set<String> finalizeUnhealthyInstances(Stack stack, Iterable<InstanceMetaData> candidateUnhealthyInstances) { Location location = location(region(stack.getRegion()), availabilityZone(stack.getAvailabilityZone())); CloudContext cloudContext = new CloudContext(stack.getId(), stack.getName(), stack.cloudPlatform(), stack.getPlatformVariant(), location, stack.getCreator().getUserId(), stack.getWorkspace().getId()); CloudCredential cloudCredential = stackUtil.getCloudCredential(stack); List<CloudInstance> cloudInstances = cloudInstanceConverter.convert(candidateUnhealthyInstances); List<CloudVmInstanceStatus> cloudVmInstanceStatuses = instanceStateQuery.getCloudVmInstanceStatuses( cloudCredential, cloudContext, cloudInstances); Map<String, CloudVmInstanceStatus> cloudVmInstanceStatusesById = new HashMap<>(); cloudVmInstanceStatuses.forEach(c -> cloudVmInstanceStatusesById.put(c.getCloudInstance().getInstanceId(), c)); Set<String> unhealthyInstances = new HashSet<>(); for (InstanceMetaData i : candidateUnhealthyInstances) { CloudVmInstanceStatus instanceStatus = cloudVmInstanceStatusesById.get(i.getInstanceId()); if ((instanceStatus == null) || (instanceStatus.getStatus().equals(InstanceStatus.TERMINATED))) { unhealthyInstances.add(i.getInstanceId()); } } return unhealthyInstances; }
UnhealthyInstancesFinalizer { public Set<String> finalizeUnhealthyInstances(Stack stack, Iterable<InstanceMetaData> candidateUnhealthyInstances) { Location location = location(region(stack.getRegion()), availabilityZone(stack.getAvailabilityZone())); CloudContext cloudContext = new CloudContext(stack.getId(), stack.getName(), stack.cloudPlatform(), stack.getPlatformVariant(), location, stack.getCreator().getUserId(), stack.getWorkspace().getId()); CloudCredential cloudCredential = stackUtil.getCloudCredential(stack); List<CloudInstance> cloudInstances = cloudInstanceConverter.convert(candidateUnhealthyInstances); List<CloudVmInstanceStatus> cloudVmInstanceStatuses = instanceStateQuery.getCloudVmInstanceStatuses( cloudCredential, cloudContext, cloudInstances); Map<String, CloudVmInstanceStatus> cloudVmInstanceStatusesById = new HashMap<>(); cloudVmInstanceStatuses.forEach(c -> cloudVmInstanceStatusesById.put(c.getCloudInstance().getInstanceId(), c)); Set<String> unhealthyInstances = new HashSet<>(); for (InstanceMetaData i : candidateUnhealthyInstances) { CloudVmInstanceStatus instanceStatus = cloudVmInstanceStatusesById.get(i.getInstanceId()); if ((instanceStatus == null) || (instanceStatus.getStatus().equals(InstanceStatus.TERMINATED))) { unhealthyInstances.add(i.getInstanceId()); } } return unhealthyInstances; } }
UnhealthyInstancesFinalizer { public Set<String> finalizeUnhealthyInstances(Stack stack, Iterable<InstanceMetaData> candidateUnhealthyInstances) { Location location = location(region(stack.getRegion()), availabilityZone(stack.getAvailabilityZone())); CloudContext cloudContext = new CloudContext(stack.getId(), stack.getName(), stack.cloudPlatform(), stack.getPlatformVariant(), location, stack.getCreator().getUserId(), stack.getWorkspace().getId()); CloudCredential cloudCredential = stackUtil.getCloudCredential(stack); List<CloudInstance> cloudInstances = cloudInstanceConverter.convert(candidateUnhealthyInstances); List<CloudVmInstanceStatus> cloudVmInstanceStatuses = instanceStateQuery.getCloudVmInstanceStatuses( cloudCredential, cloudContext, cloudInstances); Map<String, CloudVmInstanceStatus> cloudVmInstanceStatusesById = new HashMap<>(); cloudVmInstanceStatuses.forEach(c -> cloudVmInstanceStatusesById.put(c.getCloudInstance().getInstanceId(), c)); Set<String> unhealthyInstances = new HashSet<>(); for (InstanceMetaData i : candidateUnhealthyInstances) { CloudVmInstanceStatus instanceStatus = cloudVmInstanceStatusesById.get(i.getInstanceId()); if ((instanceStatus == null) || (instanceStatus.getStatus().equals(InstanceStatus.TERMINATED))) { unhealthyInstances.add(i.getInstanceId()); } } return unhealthyInstances; }  }
UnhealthyInstancesFinalizer { public Set<String> finalizeUnhealthyInstances(Stack stack, Iterable<InstanceMetaData> candidateUnhealthyInstances) { Location location = location(region(stack.getRegion()), availabilityZone(stack.getAvailabilityZone())); CloudContext cloudContext = new CloudContext(stack.getId(), stack.getName(), stack.cloudPlatform(), stack.getPlatformVariant(), location, stack.getCreator().getUserId(), stack.getWorkspace().getId()); CloudCredential cloudCredential = stackUtil.getCloudCredential(stack); List<CloudInstance> cloudInstances = cloudInstanceConverter.convert(candidateUnhealthyInstances); List<CloudVmInstanceStatus> cloudVmInstanceStatuses = instanceStateQuery.getCloudVmInstanceStatuses( cloudCredential, cloudContext, cloudInstances); Map<String, CloudVmInstanceStatus> cloudVmInstanceStatusesById = new HashMap<>(); cloudVmInstanceStatuses.forEach(c -> cloudVmInstanceStatusesById.put(c.getCloudInstance().getInstanceId(), c)); Set<String> unhealthyInstances = new HashSet<>(); for (InstanceMetaData i : candidateUnhealthyInstances) { CloudVmInstanceStatus instanceStatus = cloudVmInstanceStatusesById.get(i.getInstanceId()); if ((instanceStatus == null) || (instanceStatus.getStatus().equals(InstanceStatus.TERMINATED))) { unhealthyInstances.add(i.getInstanceId()); } } return unhealthyInstances; }  Set<String> finalizeUnhealthyInstances(Stack stack, Iterable<InstanceMetaData> candidateUnhealthyInstances); }
UnhealthyInstancesFinalizer { public Set<String> finalizeUnhealthyInstances(Stack stack, Iterable<InstanceMetaData> candidateUnhealthyInstances) { Location location = location(region(stack.getRegion()), availabilityZone(stack.getAvailabilityZone())); CloudContext cloudContext = new CloudContext(stack.getId(), stack.getName(), stack.cloudPlatform(), stack.getPlatformVariant(), location, stack.getCreator().getUserId(), stack.getWorkspace().getId()); CloudCredential cloudCredential = stackUtil.getCloudCredential(stack); List<CloudInstance> cloudInstances = cloudInstanceConverter.convert(candidateUnhealthyInstances); List<CloudVmInstanceStatus> cloudVmInstanceStatuses = instanceStateQuery.getCloudVmInstanceStatuses( cloudCredential, cloudContext, cloudInstances); Map<String, CloudVmInstanceStatus> cloudVmInstanceStatusesById = new HashMap<>(); cloudVmInstanceStatuses.forEach(c -> cloudVmInstanceStatusesById.put(c.getCloudInstance().getInstanceId(), c)); Set<String> unhealthyInstances = new HashSet<>(); for (InstanceMetaData i : candidateUnhealthyInstances) { CloudVmInstanceStatus instanceStatus = cloudVmInstanceStatusesById.get(i.getInstanceId()); if ((instanceStatus == null) || (instanceStatus.getStatus().equals(InstanceStatus.TERMINATED))) { unhealthyInstances.add(i.getInstanceId()); } } return unhealthyInstances; }  Set<String> finalizeUnhealthyInstances(Stack stack, Iterable<InstanceMetaData> candidateUnhealthyInstances);  }