Search in sources :

Example 6 with FailureDetails

use of com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails in project cloudbreak by hortonworks.

the class UserSyncForEnvService method synchronizeUsers.

public void synchronizeUsers(String operationId, String accountId, List<Stack> stacks, UserSyncRequestFilter userSyncFilter, UserSyncOptions options, long startTime) {
    operationService.tryWithOperationCleanup(operationId, accountId, () -> {
        Set<String> environmentCrns = stacks.stream().map(Stack::getEnvironmentCrn).collect(Collectors.toSet());
        UserSyncLogEvent logUserSyncEvent = options.isFullSync() ? FULL_USER_SYNC : PARTIAL_USER_SYNC;
        LOGGER.info("Starting {} for environments {} with operationId {} ...", logUserSyncEvent, environmentCrns, operationId);
        if (options.isFullSync()) {
            umsVirtualGroupCreateService.createVirtualGroups(accountId, stacks);
        }
        Map<String, Future<SyncStatusDetail>> statusFutures = startAsyncSyncsForStacks(operationId, accountId, stacks, userSyncFilter, options, environmentCrns);
        List<SuccessDetails> success = new ArrayList<>();
        List<FailureDetails> failure = new ArrayList<>();
        statusFutures.forEach((envCrn, statusFuture) -> {
            try {
                SyncStatusDetail statusDetail = waitForSyncStatusDetailResult(startTime, statusFuture, accountId);
                switch(statusDetail.getStatus()) {
                    case COMPLETED:
                        success.add(new SuccessDetails(envCrn));
                        break;
                    case FAILED:
                        failure.add(createFailureDetails(envCrn, statusDetail.getDetails(), statusDetail.getWarnings()));
                        break;
                    default:
                        failure.add(createFailureDetails(envCrn, "Unexpected status: " + statusDetail.getStatus(), statusDetail.getWarnings()));
                        break;
                }
            } catch (TimeoutException e) {
                LOGGER.warn("Sync timed out for env: {}", envCrn, e);
                statusFuture.cancel(true);
                failure.add(new FailureDetails(envCrn, "Timed out"));
            } catch (InterruptedException | ExecutionException e) {
                LOGGER.error("Sync is interrupted for env: {}", envCrn, e);
                failure.add(new FailureDetails(envCrn, e.getLocalizedMessage()));
            }
        });
        operationService.completeOperation(accountId, operationId, success, failure);
        LOGGER.info("Finished {} for environments {} with operationId {}.", logUserSyncEvent, environmentCrns, operationId);
    });
}
Also used : SuccessDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.SuccessDetails) ArrayList(java.util.ArrayList) FailureDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails) SyncStatusDetail(com.sequenceiq.freeipa.service.freeipa.user.model.SyncStatusDetail) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 7 with FailureDetails

use of com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails in project cloudbreak by hortonworks.

the class RebootActions method rebootFailureAction.

@Bean(name = "REBOOT_FAILED_STATE")
public Action<?, ?> rebootFailureAction() {
    return new AbstractRebootAction<>(InstanceFailureEvent.class) {

        @Inject
        private OperationService operationService;

        @Override
        protected void doExecute(RebootContext context, InstanceFailureEvent payload, Map<Object, Object> variables) {
            addMdcOperationId(variables);
            rebootService.handleInstanceRebootError(context);
            String message = String.format("Rebooting failed for %s.", context.getInstanceIds());
            LOGGER.error(message);
            Stack stack = context.getStack();
            SuccessDetails successDetails = new SuccessDetails(stack.getEnvironmentCrn());
            FailureDetails failureDetails = new FailureDetails(stack.getEnvironmentCrn(), message);
            operationService.failOperation(stack.getAccountId(), getOperationId(variables), message, List.of(successDetails), List.of(failureDetails));
            sendEvent(context, new InstanceEvent(RebootEvent.REBOOT_FAIL_HANDLED_EVENT.event(), context.getStack().getId(), context.getInstanceIdList()));
        }

        @Override
        protected RebootContext createFlowContext(FlowParameters flowParameters, StateContext<RebootState, RebootEvent> stateContext, InstanceFailureEvent payload) {
            Long stackId = payload.getResourceId();
            Stack stack = stackService.getStackById(stackId);
            MDCBuilder.buildMdcContext(stack);
            return new RebootContext(flowParameters, stack, payload.getInstanceIds().stream().map(instanceId -> {
                InstanceMetaData md = new InstanceMetaData();
                md.setInstanceId(instanceId);
                return md;
            }).collect(Collectors.toList()), null, null);
        }

        @Override
        protected Object getFailurePayload(InstanceFailureEvent payload, Optional<RebootContext> flowContext, Exception ex) {
            return new InstanceFailureEvent(payload.getResourceId(), ex, payload.getInstanceIds());
        }

        @Override
        protected void initPayloadConverterMap(List<PayloadConverter<InstanceFailureEvent>> payloadConverters) {
            payloadConverters.add(new RebootInstancesResultToCleanupFailureEventConverter());
            payloadConverters.add(new WaitUntilAvailableFailedToInstanceFailureEventConverter());
        }
    };
}
Also used : SuccessDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.SuccessDetails) InstanceFailureEvent(com.sequenceiq.freeipa.flow.instance.InstanceFailureEvent) Optional(java.util.Optional) StateContext(org.springframework.statemachine.StateContext) FailureDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails) Stack(com.sequenceiq.freeipa.entity.Stack) RebootInstanceEvent(com.sequenceiq.freeipa.flow.instance.reboot.RebootInstanceEvent) InstanceEvent(com.sequenceiq.freeipa.flow.instance.InstanceEvent) RebootInstancesResultToCleanupFailureEventConverter(com.sequenceiq.freeipa.flow.freeipa.cleanup.event.failure.RebootInstancesResultToCleanupFailureEventConverter) InstanceMetaData(com.sequenceiq.freeipa.entity.InstanceMetaData) FlowParameters(com.sequenceiq.flow.core.FlowParameters) List(java.util.List) OperationService(com.sequenceiq.freeipa.service.operation.OperationService) WaitUntilAvailableFailedToInstanceFailureEventConverter(com.sequenceiq.freeipa.flow.instance.reboot.failure.WaitUntilAvailableFailedToInstanceFailureEventConverter) RebootContext(com.sequenceiq.freeipa.flow.instance.reboot.RebootContext) Map(java.util.Map) Bean(org.springframework.context.annotation.Bean)

Example 8 with FailureDetails

use of com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails in project cloudbreak by hortonworks.

the class SyncOperationToOperationStatusConverterTest method convertCompleted.

@Test
void convertCompleted() {
    OperationState operationState = OperationState.COMPLETED;
    SynchronizationStatus synchronizationStatus = SynchronizationStatus.fromOperationState(operationState);
    List<SuccessDetails> successDetails = List.of(new SuccessDetails("environment1"), new SuccessDetails("environment2"));
    List<FailureDetails> failureDetails = List.of(new FailureDetails("environment3", "failure message1"), new FailureDetails("environment4", "failure message2"));
    Operation operation = createSyncOperation(operationState);
    operation.setSuccessList(successDetails);
    operation.setFailureList(failureDetails);
    SyncOperationStatus actual = underTest.convert(operation);
    assertEqualsDefaults(actual);
    assertEquals(synchronizationStatus, actual.getStatus());
    assertEquals(successDetails, actual.getSuccess());
    assertEquals(failureDetails, actual.getFailure());
    assertNull(actual.getError());
}
Also used : SuccessDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.SuccessDetails) SynchronizationStatus(com.sequenceiq.freeipa.api.v1.freeipa.user.model.SynchronizationStatus) SyncOperationStatus(com.sequenceiq.freeipa.api.v1.freeipa.user.model.SyncOperationStatus) Operation(com.sequenceiq.freeipa.entity.Operation) FailureDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails) OperationState(com.sequenceiq.freeipa.api.v1.operation.model.OperationState) Test(org.junit.jupiter.api.Test)

Example 9 with FailureDetails

use of com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails in project cloudbreak by hortonworks.

the class FreeIpaCleanupActions method cleanupFailureAction.

@Bean(name = "CLEANUP_FAILED_STATE")
public Action<?, ?> cleanupFailureAction() {
    return new AbstractFreeIpaCleanupAction<>(CleanupFailureEvent.class) {

        @Inject
        private OperationService operationService;

        @Override
        protected void doExecute(FreeIpaContext context, CleanupFailureEvent payload, Map<Object, Object> variables) {
            LOGGER.error("Cleanup failed with payload: " + payload);
            String environmentCrn = payload.getEnvironmentCrn();
            SuccessDetails successDetails = new SuccessDetails(environmentCrn);
            successDetails.getAdditionalDetails().put(payload.getFailedPhase(), payload.getSuccess() == null ? List.of() : new ArrayList<>(payload.getSuccess()));
            String message = "Cleanup failed during " + payload.getFailedPhase();
            FailureDetails failureDetails = new FailureDetails(environmentCrn, message);
            if (payload.getFailureDetails() != null) {
                failureDetails.getAdditionalDetails().putAll(payload.getFailureDetails());
            }
            operationService.failOperation(payload.getAccountId(), payload.getOperationId(), message, List.of(successDetails), List.of(failureDetails));
            sendEvent(context, FreeIpaCleanupEvent.CLEANUP_FAILURE_HANDLED_EVENT.event(), payload);
        }

        @Override
        protected void initPayloadConverterMap(List<PayloadConverter<CleanupFailureEvent>> payloadConverters) {
            payloadConverters.add(new RemoveDnsResponseToCleanupFailureEventConverter());
            payloadConverters.add(new RemoveHostsResponseToCleanupFailureEventConverter());
            payloadConverters.add(new RemoveRolesResponseToCleanupFailureEventConverter());
            payloadConverters.add(new RemoveUsersResponseToCleanupFailureEventConverter());
            payloadConverters.add(new RevokeCertsResponseToCleanupFailureEventConverter());
            payloadConverters.add(new RemoveVaultEntriesResponseToCleanupFailureEventConverter());
        }
    };
}
Also used : SuccessDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.SuccessDetails) RemoveDnsResponseToCleanupFailureEventConverter(com.sequenceiq.freeipa.flow.freeipa.cleanup.event.failure.RemoveDnsResponseToCleanupFailureEventConverter) RemoveRolesResponseToCleanupFailureEventConverter(com.sequenceiq.freeipa.flow.freeipa.cleanup.event.failure.RemoveRolesResponseToCleanupFailureEventConverter) RemoveVaultEntriesResponseToCleanupFailureEventConverter(com.sequenceiq.freeipa.flow.freeipa.cleanup.event.failure.RemoveVaultEntriesResponseToCleanupFailureEventConverter) ArrayList(java.util.ArrayList) FailureDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails) RemoveUsersResponseToCleanupFailureEventConverter(com.sequenceiq.freeipa.flow.freeipa.cleanup.event.failure.RemoveUsersResponseToCleanupFailureEventConverter) RevokeCertsResponseToCleanupFailureEventConverter(com.sequenceiq.freeipa.flow.freeipa.cleanup.event.failure.RevokeCertsResponseToCleanupFailureEventConverter) RemoveHostsResponseToCleanupFailureEventConverter(com.sequenceiq.freeipa.flow.freeipa.cleanup.event.failure.RemoveHostsResponseToCleanupFailureEventConverter) ArrayList(java.util.ArrayList) List(java.util.List) OperationService(com.sequenceiq.freeipa.service.operation.OperationService) Map(java.util.Map) CleanupFailureEvent(com.sequenceiq.freeipa.flow.freeipa.cleanup.event.failure.CleanupFailureEvent) Bean(org.springframework.context.annotation.Bean)

Example 10 with FailureDetails

use of com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails in project cloudbreak by hortonworks.

the class UserSyncForEnvServiceTest method testSyncUsersInterrupted.

@Test
public void testSyncUsersInterrupted() {
    Stack stack1 = mock(Stack.class);
    when(stack1.getEnvironmentCrn()).thenReturn(ENV_CRN);
    UserSyncRequestFilter userSyncFilter = new UserSyncRequestFilter(Set.of(), Set.of(), Optional.empty());
    UserSyncOptions options = new UserSyncOptions(true, true, WorkloadCredentialsUpdateType.UPDATE_IF_CHANGED);
    doAnswer(inv -> {
        inv.getArgument(2, Runnable.class).run();
        return null;
    }).when(operationService).tryWithOperationCleanup(eq(OPERATION_ID), eq(ACCOUNT_ID), any(Runnable.class));
    UmsUsersState umsUsersState1 = mock(UmsUsersState.class);
    when(umsUsersStateProviderDispatcher.getEnvToUmsUsersStateMap(eq(ACCOUNT_ID), eq(Set.of(ENV_CRN)), eq(Set.of()), eq(Set.of()), any())).thenReturn(Map.of(ENV_CRN, umsUsersState1));
    when(asyncTaskExecutor.submit(any(Callable.class))).thenAnswer(inv -> {
        Future future = mock(Future.class);
        when(future.get()).thenThrow(new InterruptedException("interrupt"));
        return future;
    });
    when(umsEventGenerationIdsProvider.getEventGenerationIds(eq(ACCOUNT_ID), any(Optional.class))).thenReturn(new UmsEventGenerationIds());
    underTest.synchronizeUsers(OPERATION_ID, ACCOUNT_ID, List.of(stack1), userSyncFilter, options, System.currentTimeMillis());
    verifyNoInteractions(userSyncStatusService);
    ArgumentCaptor<Collection> successCaptor = ArgumentCaptor.forClass(Collection.class);
    ArgumentCaptor<Collection> failureCaptor = ArgumentCaptor.forClass(Collection.class);
    verify(operationService).completeOperation(eq(ACCOUNT_ID), eq(OPERATION_ID), successCaptor.capture(), failureCaptor.capture());
    assertTrue(successCaptor.getValue().isEmpty());
    List<FailureDetails> failures = (List<FailureDetails>) failureCaptor.getValue();
    assertThat(failures, allOf(hasItem(allOf(hasProperty("environment", is(ENV_CRN)), hasProperty("message", is("interrupt")), hasProperty("additionalDetails", anEmptyMap())))));
}
Also used : Optional(java.util.Optional) UmsUsersState(com.sequenceiq.freeipa.service.freeipa.user.model.UmsUsersState) FailureDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails) Callable(java.util.concurrent.Callable) Stack(com.sequenceiq.freeipa.entity.Stack) UmsEventGenerationIds(com.sequenceiq.freeipa.service.freeipa.user.model.UmsEventGenerationIds) UserSyncOptions(com.sequenceiq.freeipa.service.freeipa.user.model.UserSyncOptions) Future(java.util.concurrent.Future) Collection(java.util.Collection) List(java.util.List) Test(org.junit.jupiter.api.Test)

Aggregations

FailureDetails (com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails)15 SuccessDetails (com.sequenceiq.freeipa.api.v1.freeipa.user.model.SuccessDetails)9 Stack (com.sequenceiq.freeipa.entity.Stack)9 List (java.util.List)8 ArrayList (java.util.ArrayList)7 OperationService (com.sequenceiq.freeipa.service.operation.OperationService)5 Map (java.util.Map)5 Test (org.junit.jupiter.api.Test)5 Bean (org.springframework.context.annotation.Bean)5 FlowParameters (com.sequenceiq.flow.core.FlowParameters)4 Optional (java.util.Optional)4 StateContext (org.springframework.statemachine.StateContext)4 Flow (com.sequenceiq.flow.core.Flow)3 UmsEventGenerationIds (com.sequenceiq.freeipa.service.freeipa.user.model.UmsEventGenerationIds)3 UmsUsersState (com.sequenceiq.freeipa.service.freeipa.user.model.UmsUsersState)3 Future (java.util.concurrent.Future)3 Operation (com.sequenceiq.freeipa.entity.Operation)2 StackContext (com.sequenceiq.freeipa.flow.stack.StackContext)2 SyncStatusDetail (com.sequenceiq.freeipa.service.freeipa.user.model.SyncStatusDetail)2 UserSyncOptions (com.sequenceiq.freeipa.service.freeipa.user.model.UserSyncOptions)2