Search in sources :

Example 1 with CreateExternalDatabaseRequest

use of com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.CreateExternalDatabaseRequest in project cloudbreak by hortonworks.

the class CreateExternalDatabaseHandlerTest method acceptCatchErrors.

@ParameterizedTest
@ValueSource(classes = { UserBreakException.class, PollerStoppedException.class, PollerException.class, Exception.class })
@MockitoSettings(strictness = Strictness.LENIENT)
void acceptCatchErrors(Class<? extends Exception> exceptionClass) {
    doAnswer(a -> {
        throw exceptionClass.getDeclaredConstructor().newInstance();
    }).when(provisionService).provisionDatabase(any(), any(DatabaseAvailabilityType.class), any());
    DetailedEnvironmentResponse environment = new DetailedEnvironmentResponse();
    environment.setCloudPlatform("cloudplatform");
    when(environmentClientService.getByCrn(anyString())).thenReturn(environment);
    when(stackService.getById(anyLong())).thenReturn(buildStack(DatabaseAvailabilityType.HA));
    CreateExternalDatabaseRequest request = new CreateExternalDatabaseRequest(STACK_ID, "selector", "resourceName", "crn");
    Event<CreateExternalDatabaseRequest> event = new Event<>(EVENT_HEADERS, request);
    underTest.accept(event);
    verify(provisionService).provisionDatabase(any(), eq(DatabaseAvailabilityType.HA), eq(environment));
    verify(environmentValidator).checkValidEnvironment(eq(STACK_NAME), eq(DatabaseAvailabilityType.HA), eq(environment));
    verify(stackUpdaterService).updateStatus(eq(STACK_ID), eq(DetailedStackStatus.EXTERNAL_DATABASE_CREATION_IN_PROGRESS), eq(ResourceEvent.CLUSTER_EXTERNAL_DATABASE_CREATION_STARTED), eq("External database creation in progress"));
    verify(stackUpdaterService, never()).updateStatus(eq(STACK_ID), eq(DetailedStackStatus.PROVISION_REQUESTED), eq(ResourceEvent.CLUSTER_EXTERNAL_DATABASE_CREATION_FINISHED), anyString());
    verify(eventBus).notify(eq("CreateExternalDatabaseFailed"), any(Event.class));
}
Also used : CreateExternalDatabaseRequest(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.CreateExternalDatabaseRequest) DetailedEnvironmentResponse(com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse) Event(reactor.bus.Event) ResourceEvent(com.sequenceiq.cloudbreak.event.ResourceEvent) DatabaseAvailabilityType(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.database.DatabaseAvailabilityType) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings)

Example 2 with CreateExternalDatabaseRequest

use of com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.CreateExternalDatabaseRequest in project cloudbreak by hortonworks.

the class CreateExternalDatabaseHandlerTest method acceptDbNone.

@Test
void acceptDbNone() {
    when(stackService.getById(anyLong())).thenReturn(buildStack(DatabaseAvailabilityType.NONE));
    CreateExternalDatabaseRequest request = new CreateExternalDatabaseRequest(STACK_ID, "selector", "resourceName", "crn");
    Event<CreateExternalDatabaseRequest> event = new Event<>(EVENT_HEADERS, request);
    underTest.accept(event);
    verifyNoMoreInteractions(provisionService);
    verifyNoMoreInteractions(stackUpdaterService);
    verifyNoMoreInteractions(environmentValidator);
    ArgumentCaptor<Event<CreateExternalDatabaseResult>> eventCaptor = ArgumentCaptor.forClass(Event.class);
    verify(eventBus).notify(eq("CreateExternalDatabaseResult"), eventCaptor.capture());
    Event<CreateExternalDatabaseResult> value = eventCaptor.getValue();
    assertThat(value.getHeaders()).isEqualTo(EVENT_HEADERS);
    assertThat(value.getData().getResourceCrn()).isNull();
}
Also used : CreateExternalDatabaseRequest(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.CreateExternalDatabaseRequest) Event(reactor.bus.Event) ResourceEvent(com.sequenceiq.cloudbreak.event.ResourceEvent) CreateExternalDatabaseResult(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.CreateExternalDatabaseResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with CreateExternalDatabaseRequest

use of com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.CreateExternalDatabaseRequest in project cloudbreak by hortonworks.

the class CreateExternalDatabaseHandler method accept.

@Override
public void accept(Event<CreateExternalDatabaseRequest> createExternalDatabaseRequest) {
    LOGGER.debug("In CreateExternalDatabaseHandler.accept");
    CreateExternalDatabaseRequest request = createExternalDatabaseRequest.getData();
    Stack stack = stackService.getById(request.getResourceId());
    DatabaseAvailabilityType externalDatabase = ObjectUtils.defaultIfNull(stack.getExternalDatabaseCreationType(), DatabaseAvailabilityType.NONE);
    LOGGER.debug("External database: {} for stack {}", externalDatabase.name(), stack.getName());
    Selectable result;
    try {
        String resourceCrn = null;
        if (externalDatabase.isEmbedded()) {
            LOGGER.info("External database for stack {} is not requested.", stack.getName());
        } else {
            LOGGER.debug("Updating stack {} status from {} to {}", stack.getName(), stack.getStatus().name(), DetailedStackStatus.EXTERNAL_DATABASE_CREATION_IN_PROGRESS.name());
            stackUpdaterService.updateStatus(stack.getId(), DetailedStackStatus.EXTERNAL_DATABASE_CREATION_IN_PROGRESS, ResourceEvent.CLUSTER_EXTERNAL_DATABASE_CREATION_STARTED, "External database creation in progress");
            LOGGER.debug("Getting environment CRN for stack {}", stack.getName());
            DetailedEnvironmentResponse environment = environmentClientService.getByCrn(stack.getEnvironmentCrn());
            environmentValidator.checkValidEnvironment(stack.getName(), externalDatabase, environment);
            provisionService.provisionDatabase(stack.getCluster(), externalDatabase, environment);
            LOGGER.debug("Updating stack {} status from {} to {}", stack.getName(), stack.getStatus().name(), DetailedStackStatus.PROVISION_REQUESTED.name());
            stackUpdaterService.updateStatus(stack.getId(), DetailedStackStatus.PROVISION_REQUESTED, ResourceEvent.CLUSTER_EXTERNAL_DATABASE_CREATION_FINISHED, "External database creation finished");
            resourceCrn = stack.getCluster().getDatabaseServerCrn();
        }
        result = new CreateExternalDatabaseResult(stack.getId(), EXTERNAL_DATABASE_WAIT_SUCCESS_EVENT.event(), stack.getName(), resourceCrn);
    } catch (UserBreakException e) {
        LOGGER.info("Database polling exited before timeout. Cause: ", e);
        result = createFailedEvent(stack, e);
    } catch (PollerStoppedException e) {
        LOGGER.info(String.format("Database poller stopped for stack: %s", stack.getName()), e);
        result = createFailedEvent(stack, e);
    } catch (PollerException e) {
        LOGGER.info(String.format("Database polling failed for stack: %s", stack.getName()), e);
        result = createFailedEvent(stack, e);
    } catch (Exception e) {
        LOGGER.error(String.format("Exception during DB creation for stack/cluster: %s", stack.getName()), e);
        result = createFailedEvent(stack, e);
    }
    LOGGER.debug("Sending reactor notification for selector {}", result.selector());
    eventBus.notify(result.selector(), new Event<>(createExternalDatabaseRequest.getHeaders(), result));
}
Also used : UserBreakException(com.dyngr.exception.UserBreakException) Selectable(com.sequenceiq.cloudbreak.common.event.Selectable) CreateExternalDatabaseRequest(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.CreateExternalDatabaseRequest) PollerException(com.dyngr.exception.PollerException) DetailedEnvironmentResponse(com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse) CreateExternalDatabaseResult(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.CreateExternalDatabaseResult) PollerStoppedException(com.dyngr.exception.PollerStoppedException) UserBreakException(com.dyngr.exception.UserBreakException) PollerException(com.dyngr.exception.PollerException) PollerStoppedException(com.dyngr.exception.PollerStoppedException) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) DatabaseAvailabilityType(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.database.DatabaseAvailabilityType)

Example 4 with CreateExternalDatabaseRequest

use of com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.CreateExternalDatabaseRequest in project cloudbreak by hortonworks.

the class CreateExternalDatabaseHandlerTest method accept.

@Test
void accept() {
    DetailedEnvironmentResponse environment = new DetailedEnvironmentResponse();
    environment.setCloudPlatform("cloudplatform");
    when(environmentClientService.getByCrn(anyString())).thenReturn(environment);
    doAnswer(a -> {
        Cluster cluster = a.getArgument(0);
        cluster.setDatabaseServerCrn(DATABASE_CRN);
        return null;
    }).when(provisionService).provisionDatabase(any(), any(), any());
    when(stackService.getById(anyLong())).thenReturn(buildStack(DatabaseAvailabilityType.HA));
    CreateExternalDatabaseRequest request = new CreateExternalDatabaseRequest(STACK_ID, "selector", "resourceName", "crn");
    Event<CreateExternalDatabaseRequest> event = new Event<>(EVENT_HEADERS, request);
    underTest.accept(event);
    verify(provisionService).provisionDatabase(any(), eq(DatabaseAvailabilityType.HA), eq(environment));
    verify(environmentValidator).checkValidEnvironment(eq(STACK_NAME), eq(DatabaseAvailabilityType.HA), eq(environment));
    verify(stackUpdaterService).updateStatus(eq(STACK_ID), eq(DetailedStackStatus.EXTERNAL_DATABASE_CREATION_IN_PROGRESS), eq(ResourceEvent.CLUSTER_EXTERNAL_DATABASE_CREATION_STARTED), eq("External database creation in progress"));
    verify(stackUpdaterService).updateStatus(eq(STACK_ID), eq(DetailedStackStatus.PROVISION_REQUESTED), eq(ResourceEvent.CLUSTER_EXTERNAL_DATABASE_CREATION_FINISHED), eq("External database creation finished"));
    ArgumentCaptor<Event<CreateExternalDatabaseResult>> eventCaptor = ArgumentCaptor.forClass(Event.class);
    verify(eventBus).notify(eq("CreateExternalDatabaseResult"), eventCaptor.capture());
    Event<CreateExternalDatabaseResult> value = eventCaptor.getValue();
    assertThat(value.getHeaders()).isEqualTo(EVENT_HEADERS);
    assertThat(value.getData().getResourceCrn()).isEqualTo(DATABASE_CRN);
}
Also used : CreateExternalDatabaseRequest(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.CreateExternalDatabaseRequest) DetailedEnvironmentResponse(com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse) Cluster(com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster) Event(reactor.bus.Event) ResourceEvent(com.sequenceiq.cloudbreak.event.ResourceEvent) CreateExternalDatabaseResult(com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.CreateExternalDatabaseResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

CreateExternalDatabaseRequest (com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.CreateExternalDatabaseRequest)4 ResourceEvent (com.sequenceiq.cloudbreak.event.ResourceEvent)3 CreateExternalDatabaseResult (com.sequenceiq.cloudbreak.reactor.api.event.externaldatabase.CreateExternalDatabaseResult)3 DetailedEnvironmentResponse (com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 Event (reactor.bus.Event)3 DatabaseAvailabilityType (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.database.DatabaseAvailabilityType)2 Test (org.junit.jupiter.api.Test)2 PollerException (com.dyngr.exception.PollerException)1 PollerStoppedException (com.dyngr.exception.PollerStoppedException)1 UserBreakException (com.dyngr.exception.UserBreakException)1 Selectable (com.sequenceiq.cloudbreak.common.event.Selectable)1 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)1 Cluster (com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster)1 ValueSource (org.junit.jupiter.params.provider.ValueSource)1 MockitoSettings (org.mockito.junit.jupiter.MockitoSettings)1