Search in sources :

Example 11 with RepairInstancesRequest

use of com.sequenceiq.freeipa.api.v1.freeipa.stack.model.repair.RepairInstancesRequest in project cloudbreak by hortonworks.

the class RepairInstancesServiceTest method testRepairAndAutodetectWith2InstancesWithNoInstanceIds.

@Test
public void testRepairAndAutodetectWith2InstancesWithNoInstanceIds() throws Exception {
    Stack stack = createStack(Status.UNHEALTHY, List.of(InstanceStatus.DELETED_ON_PROVIDER_SIDE, InstanceStatus.CREATED), 2);
    OperationStatus operationStatus = new OperationStatus();
    RepairInstancesRequest repairInstancesRequest = new RepairInstancesRequest();
    repairInstancesRequest.setEnvironmentCrn(ENVIRONMENT_ID1);
    repairInstancesRequest.setForceRepair(false);
    when(stackService.getByEnvironmentCrnAndAccountIdWithListsAndMdcContext(ENVIRONMENT_ID1, ACCOUNT_ID)).thenReturn(stack);
    when(healthDetailsService.getHealthDetails(ENVIRONMENT_ID1, ACCOUNT_ID)).thenReturn(createHealthDetails(InstanceStatus.DELETED_ON_PROVIDER_SIDE, InstanceStatus.CREATED));
    when(entitlementService.freeIpaHaRepairEnabled(any())).thenReturn(Boolean.TRUE);
    when(operationService.startOperation(any(), any(), any(), any())).thenReturn(createOperation());
    when(operationToOperationStatusConverter.convert(any())).thenReturn(operationStatus);
    assertEquals(operationStatus, underTest.repairInstances(ACCOUNT_ID, repairInstancesRequest));
    verify(flowManager, times(1)).notify(eq(REPAIR_TRIGGER_EVENT), any());
}
Also used : OperationStatus(com.sequenceiq.freeipa.api.v1.operation.model.OperationStatus) RepairInstancesRequest(com.sequenceiq.freeipa.api.v1.freeipa.stack.model.repair.RepairInstancesRequest) Stack(com.sequenceiq.freeipa.entity.Stack) Test(org.junit.jupiter.api.Test)

Example 12 with RepairInstancesRequest

use of com.sequenceiq.freeipa.api.v1.freeipa.stack.model.repair.RepairInstancesRequest in project cloudbreak by hortonworks.

the class RepairInstancesServiceTest method testRepairInstancesWithGoodInstancesShouldThrowException.

@Test
void testRepairInstancesWithGoodInstancesShouldThrowException() {
    Stack stack = createStack(Status.AVAILABLE, List.of(InstanceStatus.CREATED, InstanceStatus.CREATED));
    List<String> instanceIds = List.of("i-2");
    when(stackService.getByEnvironmentCrnAndAccountIdWithListsAndMdcContext(ENVIRONMENT_ID1, ACCOUNT_ID)).thenReturn(stack);
    when(healthDetailsService.getHealthDetails(ENVIRONMENT_ID1, ACCOUNT_ID)).thenReturn(createHealthDetails(InstanceStatus.CREATED, InstanceStatus.CREATED));
    when(entitlementService.freeIpaHaRepairEnabled(any())).thenReturn(Boolean.TRUE);
    RepairInstancesRequest request = new RepairInstancesRequest();
    request.setForceRepair(false);
    request.setInstanceIds(instanceIds);
    request.setEnvironmentCrn(ENVIRONMENT_ID1);
    assertThrows(NotFoundException.class, () -> underTest.repairInstances(ACCOUNT_ID, request));
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RepairInstancesRequest(com.sequenceiq.freeipa.api.v1.freeipa.stack.model.repair.RepairInstancesRequest) Stack(com.sequenceiq.freeipa.entity.Stack) Test(org.junit.jupiter.api.Test)

Example 13 with RepairInstancesRequest

use of com.sequenceiq.freeipa.api.v1.freeipa.stack.model.repair.RepairInstancesRequest in project cloudbreak by hortonworks.

the class RepairInstancesServiceTest method testRepairWithForceThrowsWhenNoInstanceIdsAreProvided.

@Test
void testRepairWithForceThrowsWhenNoInstanceIdsAreProvided() {
    Stack stack = createStack(Status.AVAILABLE, List.of(InstanceStatus.CREATED, InstanceStatus.CREATED));
    when(stackService.getByEnvironmentCrnAndAccountIdWithListsAndMdcContext(ENVIRONMENT_ID1, ACCOUNT_ID)).thenReturn(stack);
    RepairInstancesRequest request = new RepairInstancesRequest();
    request.setForceRepair(true);
    request.setEnvironmentCrn(ENVIRONMENT_ID1);
    assertThrows(UnsupportedOperationException.class, () -> {
        underTest.repairInstances(ACCOUNT_ID, request);
    });
}
Also used : RepairInstancesRequest(com.sequenceiq.freeipa.api.v1.freeipa.stack.model.repair.RepairInstancesRequest) Stack(com.sequenceiq.freeipa.entity.Stack) Test(org.junit.jupiter.api.Test)

Example 14 with RepairInstancesRequest

use of com.sequenceiq.freeipa.api.v1.freeipa.stack.model.repair.RepairInstancesRequest in project cloudbreak by hortonworks.

the class RepairInstancesService method repairInstances.

/**
 * If no instance passed in request, repair all bad instances (at least 1 instance must be good)
 * If instances passed in request, repair all valid passed bad instances (at least 1 instance must remain)
 * If force and instances passed in request, repair all valid passed instances (at least 1 instance must remain)
 * If force and no instances passed in request then report an error
 *
 * @param accountId - The account id for the instance to repair.
 * @param request   - A RepairInstanceRequest containing request parameters.
 */
public OperationStatus repairInstances(String accountId, RepairInstancesRequest request) {
    Stack stack = stackService.getByEnvironmentCrnAndAccountIdWithListsAndMdcContext(request.getEnvironmentCrn(), accountId);
    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, new ArrayList<>(instancesToRepair.keySet()), additionalTerminatedInstanceIds));
    }
    return operationToOperationStatusConverter.convert(operation);
}
Also used : ArrayList(java.util.ArrayList) Operation(com.sequenceiq.freeipa.entity.Operation) Stack(com.sequenceiq.freeipa.entity.Stack) InstanceMetaData(com.sequenceiq.freeipa.entity.InstanceMetaData) InstanceStatus(com.sequenceiq.freeipa.api.v1.freeipa.stack.model.common.instance.InstanceStatus) RepairEvent(com.sequenceiq.freeipa.flow.freeipa.repair.event.RepairEvent)

Aggregations

RepairInstancesRequest (com.sequenceiq.freeipa.api.v1.freeipa.stack.model.repair.RepairInstancesRequest)13 Stack (com.sequenceiq.freeipa.entity.Stack)12 Test (org.junit.jupiter.api.Test)12 OperationStatus (com.sequenceiq.freeipa.api.v1.operation.model.OperationStatus)6 RepairEvent (com.sequenceiq.freeipa.flow.freeipa.repair.event.RepairEvent)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 ArgumentCaptor (org.mockito.ArgumentCaptor)4 InstanceGroupResponse (com.sequenceiq.freeipa.api.v1.freeipa.stack.model.common.instance.InstanceGroupResponse)1 InstanceStatus (com.sequenceiq.freeipa.api.v1.freeipa.stack.model.common.instance.InstanceStatus)1 InstanceMetaData (com.sequenceiq.freeipa.entity.InstanceMetaData)1 Operation (com.sequenceiq.freeipa.entity.Operation)1 ArrayList (java.util.ArrayList)1