@Test public void testInvalidInstancesReboot() throws Exception { Mockito.when(stackService.getByEnvironmentCrnAndAccountIdWithLists(anyString(), anyString())).thenReturn(stack1); Mockito.when(healthDetailsService.getHealthDetails(any(), any())).thenReturn(getMockDetails1()); RebootInstancesRequest rebootInstancesRequest = new RebootInstancesRequest(); rebootInstancesRequest.setEnvironmentCrn(ENVIRONMENT_ID1); rebootInstancesRequest.setInstanceIds(Arrays.asList("bad_instance")); Exception expected = assertThrows(BadRequestException.class, () -> { underTest.rebootInstances(ACCOUNT_ID, rebootInstancesRequest); }); Assert.assertTrue(expected.getLocalizedMessage().equals("Invalid instanceIds in request bad_instance.")); }
public OperationStatus rebootInstances(String accountId, RebootInstancesRequest request) { Stack stack = stackService.getByEnvironmentCrnAndAccountIdWithLists(request.getEnvironmentCrn(), accountId); MDCBuilder.buildMdcContext(stack); Map<String, InstanceMetaData> allInstancesByInstanceId = getAllInstancesFromStack(stack); Map<String, InstanceStatus> healthMap = request.isForceReboot() ? Collections.emptyMap() : getInstanceHealthMap(accountId, request.getEnvironmentCrn()); Map<String, InstanceMetaData> instancesToReboot = getInstancesToRepair(healthMap, allInstancesByInstanceId, request.getInstanceIds(), request.isForceReboot(), true); if (instancesToReboot.keySet().isEmpty()) { throw new NotFoundException("No unhealthy instances to reboot. Maybe use the force option."); } Operation operation = operationService.startOperation(accountId, OperationType.REBOOT, Set.of(stack.getEnvironmentCrn()), Collections.emptySet()); if (operation.getStatus() == OperationState.RUNNING) { stackUpdater.updateStackStatus(stack.getId(), DetailedStackStatus.REPAIR_REQUESTED, "Reboot requested"); flowManager.notify(REBOOT_EVENT.event(), new RebootInstanceEvent(REBOOT_EVENT.event(), stack.getId(), instancesToReboot.keySet().stream().collect(Collectors.toList()), operation.getOperationId())); } return operationToOperationStatusConverter.convert(operation); }
RepairInstancesService { public OperationStatus rebootInstances(String accountId, RebootInstancesRequest request) { Stack stack = stackService.getByEnvironmentCrnAndAccountIdWithLists(request.getEnvironmentCrn(), accountId); MDCBuilder.buildMdcContext(stack); Map<String, InstanceMetaData> allInstancesByInstanceId = getAllInstancesFromStack(stack); Map<String, InstanceStatus> healthMap = request.isForceReboot() ? Collections.emptyMap() : getInstanceHealthMap(accountId, request.getEnvironmentCrn()); Map<String, InstanceMetaData> instancesToReboot = getInstancesToRepair(healthMap, allInstancesByInstanceId, request.getInstanceIds(), request.isForceReboot(), true); if (instancesToReboot.keySet().isEmpty()) { throw new NotFoundException("No unhealthy instances to reboot. Maybe use the force option."); } Operation operation = operationService.startOperation(accountId, OperationType.REBOOT, Set.of(stack.getEnvironmentCrn()), Collections.emptySet()); if (operation.getStatus() == OperationState.RUNNING) { stackUpdater.updateStackStatus(stack.getId(), DetailedStackStatus.REPAIR_REQUESTED, "Reboot requested"); flowManager.notify(REBOOT_EVENT.event(), new RebootInstanceEvent(REBOOT_EVENT.event(), stack.getId(), instancesToReboot.keySet().stream().collect(Collectors.toList()), operation.getOperationId())); } return operationToOperationStatusConverter.convert(operation); } }
RepairInstancesService { public OperationStatus rebootInstances(String accountId, RebootInstancesRequest request) { Stack stack = stackService.getByEnvironmentCrnAndAccountIdWithLists(request.getEnvironmentCrn(), accountId); MDCBuilder.buildMdcContext(stack); Map<String, InstanceMetaData> allInstancesByInstanceId = getAllInstancesFromStack(stack); Map<String, InstanceStatus> healthMap = request.isForceReboot() ? Collections.emptyMap() : getInstanceHealthMap(accountId, request.getEnvironmentCrn()); Map<String, InstanceMetaData> instancesToReboot = getInstancesToRepair(healthMap, allInstancesByInstanceId, request.getInstanceIds(), request.isForceReboot(), true); if (instancesToReboot.keySet().isEmpty()) { throw new NotFoundException("No unhealthy instances to reboot. Maybe use the force option."); } Operation operation = operationService.startOperation(accountId, OperationType.REBOOT, Set.of(stack.getEnvironmentCrn()), Collections.emptySet()); if (operation.getStatus() == OperationState.RUNNING) { stackUpdater.updateStackStatus(stack.getId(), DetailedStackStatus.REPAIR_REQUESTED, "Reboot requested"); flowManager.notify(REBOOT_EVENT.event(), new RebootInstanceEvent(REBOOT_EVENT.event(), stack.getId(), instancesToReboot.keySet().stream().collect(Collectors.toList()), operation.getOperationId())); } return operationToOperationStatusConverter.convert(operation); }  }
RepairInstancesService { public OperationStatus rebootInstances(String accountId, RebootInstancesRequest request) { Stack stack = stackService.getByEnvironmentCrnAndAccountIdWithLists(request.getEnvironmentCrn(), accountId); MDCBuilder.buildMdcContext(stack); Map<String, InstanceMetaData> allInstancesByInstanceId = getAllInstancesFromStack(stack); Map<String, InstanceStatus> healthMap = request.isForceReboot() ? Collections.emptyMap() : getInstanceHealthMap(accountId, request.getEnvironmentCrn()); Map<String, InstanceMetaData> instancesToReboot = getInstancesToRepair(healthMap, allInstancesByInstanceId, request.getInstanceIds(), request.isForceReboot(), true); if (instancesToReboot.keySet().isEmpty()) { throw new NotFoundException("No unhealthy instances to reboot. Maybe use the force option."); } Operation operation = operationService.startOperation(accountId, OperationType.REBOOT, Set.of(stack.getEnvironmentCrn()), Collections.emptySet()); if (operation.getStatus() == OperationState.RUNNING) { stackUpdater.updateStackStatus(stack.getId(), DetailedStackStatus.REPAIR_REQUESTED, "Reboot requested"); flowManager.notify(REBOOT_EVENT.event(), new RebootInstanceEvent(REBOOT_EVENT.event(), stack.getId(), instancesToReboot.keySet().stream().collect(Collectors.toList()), operation.getOperationId())); } return operationToOperationStatusConverter.convert(operation); }  OperationStatus repairInstances(String accountId, RepairInstancesRequest request); OperationStatus rebootInstances(String accountId, RebootInstancesRequest request); }
RepairInstancesService { public OperationStatus rebootInstances(String accountId, RebootInstancesRequest request) { Stack stack = stackService.getByEnvironmentCrnAndAccountIdWithLists(request.getEnvironmentCrn(), accountId); MDCBuilder.buildMdcContext(stack); Map<String, InstanceMetaData> allInstancesByInstanceId = getAllInstancesFromStack(stack); Map<String, InstanceStatus> healthMap = request.isForceReboot() ? Collections.emptyMap() : getInstanceHealthMap(accountId, request.getEnvironmentCrn()); Map<String, InstanceMetaData> instancesToReboot = getInstancesToRepair(healthMap, allInstancesByInstanceId, request.getInstanceIds(), request.isForceReboot(), true); if (instancesToReboot.keySet().isEmpty()) { throw new NotFoundException("No unhealthy instances to reboot. Maybe use the force option."); } Operation operation = operationService.startOperation(accountId, OperationType.REBOOT, Set.of(stack.getEnvironmentCrn()), Collections.emptySet()); if (operation.getStatus() == OperationState.RUNNING) { stackUpdater.updateStackStatus(stack.getId(), DetailedStackStatus.REPAIR_REQUESTED, "Reboot requested"); flowManager.notify(REBOOT_EVENT.event(), new RebootInstanceEvent(REBOOT_EVENT.event(), stack.getId(), instancesToReboot.keySet().stream().collect(Collectors.toList()), operation.getOperationId())); } return operationToOperationStatusConverter.convert(operation); }  OperationStatus repairInstances(String accountId, RepairInstancesRequest request); OperationStatus rebootInstances(String accountId, RebootInstancesRequest request);  }