@Test void testRepairInstancesWithNoneSpecified() { Stack stack = createStack(List.of(InstanceStatus.CREATED, InstanceStatus.UNREACHABLE)); List<String> instanceIds = List.of("i-2"); OperationStatus operationStatus = new OperationStatus(); when(stackService.getByEnvironmentCrnAndAccountIdWithLists(ENVIRONMENT_ID1, ACCOUNT_ID)).thenReturn(stack); when(healthDetailsService.getHealthDetails(ENVIRONMENT_ID1, ACCOUNT_ID)) .thenReturn(createHealthDetails(InstanceStatus.CREATED, InstanceStatus.UNREACHABLE)); when(operationService.startOperation(any(), any(), any(), any())).thenReturn(createOperation()); when(operationToOperationStatusConverter.convert(any())).thenReturn(operationStatus); when(entitlementService.freeIpaHaRepairEnabled(any(), any())).thenReturn(Boolean.TRUE); when(stackUpdater.updateStackStatus(anyLong(), any(), any())).thenReturn(stack); RepairInstancesRequest request = new RepairInstancesRequest(); request.setEnvironmentCrn(ENVIRONMENT_ID1); assertEquals(operationStatus, underTest.repairInstances(ACCOUNT_ID, request)); ArgumentCaptor acAcceptable = ArgumentCaptor.forClass(Acceptable.class); verify(flowManager).notify(eq("REPAIR_TRIGGER_EVENT"), (Acceptable) acAcceptable.capture()); assertTrue(acAcceptable.getValue() instanceof RepairEvent); RepairEvent repairEvent = (RepairEvent) acAcceptable.getValue(); assertEquals(instanceIds, repairEvent.getRepairInstanceIds()); verify(stackUpdater).updateStackStatus(eq(STACK_ID), eq(DetailedStackStatus.REPAIR_REQUESTED), any()); }
public OperationStatus repairInstances(String accountId, RepairInstancesRequest request) { Stack stack = stackService.getByEnvironmentCrnAndAccountIdWithLists(request.getEnvironmentCrn(), accountId); MDCBuilder.buildMdcContext(stack); if (request.isForceRepair() && CollectionUtils.isEmpty(request.getInstanceIds())) { throw new UnsupportedOperationException("Force repair requires the instance IDs to be provided."); } Map<String, InstanceStatus> healthMap = request.isForceRepair() ? Collections.emptyMap() : getInstanceHealthMap(accountId, request.getEnvironmentCrn()); Map<String, InstanceMetaData> allInstancesByInstanceId = getAllInstancesFromStack(stack); Map<String, InstanceMetaData> instancesToRepair = getInstancesToRepair(healthMap, allInstancesByInstanceId, request.getInstanceIds(), request.isForceRepair(), false); Set<InstanceMetaData> remainingGoodInstances = getRemainingGoodInstances(allInstancesByInstanceId, instancesToRepair, healthMap, request.isForceRepair()); Set<InstanceMetaData> remainingBadInstances = getRemainingBadInstances(allInstancesByInstanceId, instancesToRepair, healthMap, request.isForceRepair()); validate(accountId, stack, remainingGoodInstances, remainingBadInstances, instancesToRepair.values()); int nodeCount = stack.getInstanceGroups().stream().findFirst().get().getNodeCount(); List<String> additionalTerminatedInstanceIds = getAdditionalTerminatedInstanceIds(allInstancesByInstanceId.values(), request.getInstanceIds()); Operation operation = operationService.startOperation(accountId, OperationType.REPAIR, Set.of(stack.getEnvironmentCrn()), Collections.emptySet()); if (operation.getStatus() == OperationState.RUNNING) { stackUpdater.updateStackStatus(stack.getId(), DetailedStackStatus.REPAIR_REQUESTED, "Repair requested"); flowManager.notify(FlowChainTriggers.REPAIR_TRIGGER_EVENT, new RepairEvent(FlowChainTriggers.REPAIR_TRIGGER_EVENT, stack.getId(), operation.getOperationId(), nodeCount, instancesToRepair.keySet().stream().collect(Collectors.toList()), additionalTerminatedInstanceIds)); } return operationToOperationStatusConverter.convert(operation); }
RepairInstancesService { public OperationStatus repairInstances(String accountId, RepairInstancesRequest request) { Stack stack = stackService.getByEnvironmentCrnAndAccountIdWithLists(request.getEnvironmentCrn(), accountId); MDCBuilder.buildMdcContext(stack); if (request.isForceRepair() && CollectionUtils.isEmpty(request.getInstanceIds())) { throw new UnsupportedOperationException("Force repair requires the instance IDs to be provided."); } Map<String, InstanceStatus> healthMap = request.isForceRepair() ? Collections.emptyMap() : getInstanceHealthMap(accountId, request.getEnvironmentCrn()); Map<String, InstanceMetaData> allInstancesByInstanceId = getAllInstancesFromStack(stack); Map<String, InstanceMetaData> instancesToRepair = getInstancesToRepair(healthMap, allInstancesByInstanceId, request.getInstanceIds(), request.isForceRepair(), false); Set<InstanceMetaData> remainingGoodInstances = getRemainingGoodInstances(allInstancesByInstanceId, instancesToRepair, healthMap, request.isForceRepair()); Set<InstanceMetaData> remainingBadInstances = getRemainingBadInstances(allInstancesByInstanceId, instancesToRepair, healthMap, request.isForceRepair()); validate(accountId, stack, remainingGoodInstances, remainingBadInstances, instancesToRepair.values()); int nodeCount = stack.getInstanceGroups().stream().findFirst().get().getNodeCount(); List<String> additionalTerminatedInstanceIds = getAdditionalTerminatedInstanceIds(allInstancesByInstanceId.values(), request.getInstanceIds()); Operation operation = operationService.startOperation(accountId, OperationType.REPAIR, Set.of(stack.getEnvironmentCrn()), Collections.emptySet()); if (operation.getStatus() == OperationState.RUNNING) { stackUpdater.updateStackStatus(stack.getId(), DetailedStackStatus.REPAIR_REQUESTED, "Repair requested"); flowManager.notify(FlowChainTriggers.REPAIR_TRIGGER_EVENT, new RepairEvent(FlowChainTriggers.REPAIR_TRIGGER_EVENT, stack.getId(), operation.getOperationId(), nodeCount, instancesToRepair.keySet().stream().collect(Collectors.toList()), additionalTerminatedInstanceIds)); } return operationToOperationStatusConverter.convert(operation); } }
RepairInstancesService { public OperationStatus repairInstances(String accountId, RepairInstancesRequest request) { Stack stack = stackService.getByEnvironmentCrnAndAccountIdWithLists(request.getEnvironmentCrn(), accountId); MDCBuilder.buildMdcContext(stack); if (request.isForceRepair() && CollectionUtils.isEmpty(request.getInstanceIds())) { throw new UnsupportedOperationException("Force repair requires the instance IDs to be provided."); } Map<String, InstanceStatus> healthMap = request.isForceRepair() ? Collections.emptyMap() : getInstanceHealthMap(accountId, request.getEnvironmentCrn()); Map<String, InstanceMetaData> allInstancesByInstanceId = getAllInstancesFromStack(stack); Map<String, InstanceMetaData> instancesToRepair = getInstancesToRepair(healthMap, allInstancesByInstanceId, request.getInstanceIds(), request.isForceRepair(), false); Set<InstanceMetaData> remainingGoodInstances = getRemainingGoodInstances(allInstancesByInstanceId, instancesToRepair, healthMap, request.isForceRepair()); Set<InstanceMetaData> remainingBadInstances = getRemainingBadInstances(allInstancesByInstanceId, instancesToRepair, healthMap, request.isForceRepair()); validate(accountId, stack, remainingGoodInstances, remainingBadInstances, instancesToRepair.values()); int nodeCount = stack.getInstanceGroups().stream().findFirst().get().getNodeCount(); List<String> additionalTerminatedInstanceIds = getAdditionalTerminatedInstanceIds(allInstancesByInstanceId.values(), request.getInstanceIds()); Operation operation = operationService.startOperation(accountId, OperationType.REPAIR, Set.of(stack.getEnvironmentCrn()), Collections.emptySet()); if (operation.getStatus() == OperationState.RUNNING) { stackUpdater.updateStackStatus(stack.getId(), DetailedStackStatus.REPAIR_REQUESTED, "Repair requested"); flowManager.notify(FlowChainTriggers.REPAIR_TRIGGER_EVENT, new RepairEvent(FlowChainTriggers.REPAIR_TRIGGER_EVENT, stack.getId(), operation.getOperationId(), nodeCount, instancesToRepair.keySet().stream().collect(Collectors.toList()), additionalTerminatedInstanceIds)); } return operationToOperationStatusConverter.convert(operation); }  }
RepairInstancesService { public OperationStatus repairInstances(String accountId, RepairInstancesRequest request) { Stack stack = stackService.getByEnvironmentCrnAndAccountIdWithLists(request.getEnvironmentCrn(), accountId); MDCBuilder.buildMdcContext(stack); if (request.isForceRepair() && CollectionUtils.isEmpty(request.getInstanceIds())) { throw new UnsupportedOperationException("Force repair requires the instance IDs to be provided."); } Map<String, InstanceStatus> healthMap = request.isForceRepair() ? Collections.emptyMap() : getInstanceHealthMap(accountId, request.getEnvironmentCrn()); Map<String, InstanceMetaData> allInstancesByInstanceId = getAllInstancesFromStack(stack); Map<String, InstanceMetaData> instancesToRepair = getInstancesToRepair(healthMap, allInstancesByInstanceId, request.getInstanceIds(), request.isForceRepair(), false); Set<InstanceMetaData> remainingGoodInstances = getRemainingGoodInstances(allInstancesByInstanceId, instancesToRepair, healthMap, request.isForceRepair()); Set<InstanceMetaData> remainingBadInstances = getRemainingBadInstances(allInstancesByInstanceId, instancesToRepair, healthMap, request.isForceRepair()); validate(accountId, stack, remainingGoodInstances, remainingBadInstances, instancesToRepair.values()); int nodeCount = stack.getInstanceGroups().stream().findFirst().get().getNodeCount(); List<String> additionalTerminatedInstanceIds = getAdditionalTerminatedInstanceIds(allInstancesByInstanceId.values(), request.getInstanceIds()); Operation operation = operationService.startOperation(accountId, OperationType.REPAIR, Set.of(stack.getEnvironmentCrn()), Collections.emptySet()); if (operation.getStatus() == OperationState.RUNNING) { stackUpdater.updateStackStatus(stack.getId(), DetailedStackStatus.REPAIR_REQUESTED, "Repair requested"); flowManager.notify(FlowChainTriggers.REPAIR_TRIGGER_EVENT, new RepairEvent(FlowChainTriggers.REPAIR_TRIGGER_EVENT, stack.getId(), operation.getOperationId(), nodeCount, instancesToRepair.keySet().stream().collect(Collectors.toList()), additionalTerminatedInstanceIds)); } return operationToOperationStatusConverter.convert(operation); }  OperationStatus repairInstances(String accountId, RepairInstancesRequest request); OperationStatus rebootInstances(String accountId, RebootInstancesRequest request); }
RepairInstancesService { public OperationStatus repairInstances(String accountId, RepairInstancesRequest request) { Stack stack = stackService.getByEnvironmentCrnAndAccountIdWithLists(request.getEnvironmentCrn(), accountId); MDCBuilder.buildMdcContext(stack); if (request.isForceRepair() && CollectionUtils.isEmpty(request.getInstanceIds())) { throw new UnsupportedOperationException("Force repair requires the instance IDs to be provided."); } Map<String, InstanceStatus> healthMap = request.isForceRepair() ? Collections.emptyMap() : getInstanceHealthMap(accountId, request.getEnvironmentCrn()); Map<String, InstanceMetaData> allInstancesByInstanceId = getAllInstancesFromStack(stack); Map<String, InstanceMetaData> instancesToRepair = getInstancesToRepair(healthMap, allInstancesByInstanceId, request.getInstanceIds(), request.isForceRepair(), false); Set<InstanceMetaData> remainingGoodInstances = getRemainingGoodInstances(allInstancesByInstanceId, instancesToRepair, healthMap, request.isForceRepair()); Set<InstanceMetaData> remainingBadInstances = getRemainingBadInstances(allInstancesByInstanceId, instancesToRepair, healthMap, request.isForceRepair()); validate(accountId, stack, remainingGoodInstances, remainingBadInstances, instancesToRepair.values()); int nodeCount = stack.getInstanceGroups().stream().findFirst().get().getNodeCount(); List<String> additionalTerminatedInstanceIds = getAdditionalTerminatedInstanceIds(allInstancesByInstanceId.values(), request.getInstanceIds()); Operation operation = operationService.startOperation(accountId, OperationType.REPAIR, Set.of(stack.getEnvironmentCrn()), Collections.emptySet()); if (operation.getStatus() == OperationState.RUNNING) { stackUpdater.updateStackStatus(stack.getId(), DetailedStackStatus.REPAIR_REQUESTED, "Repair requested"); flowManager.notify(FlowChainTriggers.REPAIR_TRIGGER_EVENT, new RepairEvent(FlowChainTriggers.REPAIR_TRIGGER_EVENT, stack.getId(), operation.getOperationId(), nodeCount, instancesToRepair.keySet().stream().collect(Collectors.toList()), additionalTerminatedInstanceIds)); } return operationToOperationStatusConverter.convert(operation); }  OperationStatus repairInstances(String accountId, RepairInstancesRequest request); OperationStatus rebootInstances(String accountId, RebootInstancesRequest request);  }