Search in sources :

Example 1 with RedbeamsContext

use of com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsContext in project cloudbreak by hortonworks.

the class AbstractRedbeamsProvisionActionTest method testCreateFlowContext.

@Test
public void testCreateFlowContext() {
    when(dbStackService.getById(RESOURCE_ID)).thenReturn(dbStack);
    when(credentialService.getCredentialByEnvCrn(ENVIRONMENT_CRN)).thenReturn(credential);
    when(credentialConverter.convert(credential)).thenReturn(cloudCredential);
    when(databaseStackConverter.convert(dbStack)).thenReturn(databaseStack);
    RedbeamsContext ctx = underTest.createFlowContext(flowParameters, stateContext, new TestPayload());
    assertEquals(dbStack.getId(), ctx.getCloudContext().getId());
    assertEquals(dbStack.getName(), ctx.getCloudContext().getName());
    assertEquals(Platform.platform(dbStack.getCloudPlatform()), ctx.getCloudContext().getPlatform());
    assertEquals(Variant.variant(dbStack.getPlatformVariant()), ctx.getCloudContext().getVariant());
    assertEquals(Location.location(Region.region(dbStack.getRegion()), AvailabilityZone.availabilityZone(dbStack.getAvailabilityZone())), ctx.getCloudContext().getLocation());
    assertEquals(dbStack.getOwnerCrn().getAccountId(), ctx.getCloudContext().getAccountId());
    assertEquals(cloudCredential, ctx.getCloudCredential());
    assertEquals(databaseStack, ctx.getDatabaseStack());
}
Also used : RedbeamsContext(com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsContext) Test(org.junit.jupiter.api.Test)

Example 2 with RedbeamsContext

use of com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsContext in project cloudbreak by hortonworks.

the class AbstractRedbeamsTerminationAction method createFlowContext.

@Override
protected RedbeamsContext createFlowContext(FlowParameters flowParameters, StateContext<RedbeamsTerminationState, RedbeamsTerminationEvent> stateContext, P payload) {
    Optional<DBStack> optionalDBStack = dbStackService.findById(payload.getResourceId());
    CloudContext cloudContext = null;
    CloudCredential cloudCredential = null;
    DatabaseStack databaseStack = null;
    DBStack dbStack = null;
    if (optionalDBStack.isPresent()) {
        dbStack = optionalDBStack.get();
        MDCBuilder.buildMdcContext(dbStack);
        Location location = location(region(dbStack.getRegion()), availabilityZone(dbStack.getAvailabilityZone()));
        String userName = dbStack.getOwnerCrn().getUserId();
        String accountId = dbStack.getOwnerCrn().getAccountId();
        cloudContext = CloudContext.Builder.builder().withId(dbStack.getId()).withName(dbStack.getName()).withCrn(dbStack.getResourceCrn()).withPlatform(dbStack.getCloudPlatform()).withVariant(dbStack.getPlatformVariant()).withUserName(userName).withLocation(location).withAccountId(accountId).build();
        try {
            Credential credential = credentialService.getCredentialByEnvCrn(dbStack.getEnvironmentId());
            cloudCredential = credentialConverter.convert(credential);
        } catch (Exception ex) {
            LOGGER.warn("Could not detect credential for environment: {}", dbStack.getEnvironmentId());
        }
        databaseStack = databaseStackConverter.convert(dbStack);
    } else {
        LOGGER.warn("DBStack for {} id is not found in the database, it seems to be only possible if the redbeams process was killed during the execution" + " of the termination finished action", payload.getResourceId());
    }
    return new RedbeamsContext(flowParameters, cloudContext, cloudCredential, databaseStack, dbStack);
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Credential(com.sequenceiq.redbeams.dto.Credential) DBStack(com.sequenceiq.redbeams.domain.stack.DBStack) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) RedbeamsContext(com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsContext) RedbeamsException(com.sequenceiq.redbeams.exception.RedbeamsException) Location(com.sequenceiq.cloudbreak.cloud.model.Location)

Example 3 with RedbeamsContext

use of com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsContext in project cloudbreak by hortonworks.

the class RedbeamsProvisionActions method provisionFailed.

@Bean(name = "REDBEAMS_PROVISION_FAILED_STATE")
public Action<?, ?> provisionFailed() {
    return new AbstractRedbeamsProvisionAction<>(RedbeamsFailureEvent.class) {

        // A lot here - some of this could go into some sort of failure handler class
        // compare to core StackCreationService::handleStackCreationFailure
        @Override
        protected void prepareExecution(RedbeamsFailureEvent payload, Map<Object, Object> variables) {
            Exception failureException = payload.getException();
            LOGGER.info("Error during database stack creation flow:", failureException);
            if (failureException instanceof CancellationException || ExceptionUtils.getRootCause(failureException) instanceof CancellationException) {
                LOGGER.debug("The flow has been cancelled");
            } else {
                // StackCreationActions / StackCreationService only update status if stack isn't mid-deletion
                String errorReason = failureException == null ? "Unknown error" : failureException.getMessage();
                Optional<DBStack> dbStack = dbStackStatusUpdater.updateStatus(payload.getResourceId(), DetailedDBStackStatus.PROVISION_FAILED, errorReason);
                metricService.incrementMetricCounter(MetricType.DB_PROVISION_FAILED, dbStack);
            }
        }

        @Override
        protected RedbeamsContext createFlowContext(FlowParameters flowParameters, StateContext<RedbeamsProvisionState, RedbeamsProvisionEvent> stateContext, RedbeamsFailureEvent payload) {
            Flow flow = getFlow(flowParameters.getFlowId());
            flow.setFlowFailed(payload.getException());
            return super.createFlowContext(flowParameters, stateContext, payload);
        }

        @Override
        protected Selectable createRequest(RedbeamsContext context) {
            return new RedbeamsEvent(RedbeamsProvisionEvent.REDBEAMS_PROVISION_FAILURE_HANDLED_EVENT.event(), 0L);
        }
    };
}
Also used : DBStack(com.sequenceiq.redbeams.domain.stack.DBStack) StateContext(org.springframework.statemachine.StateContext) AbstractRedbeamsProvisionAction(com.sequenceiq.redbeams.flow.redbeams.provision.AbstractRedbeamsProvisionAction) CancellationException(com.sequenceiq.cloudbreak.cloud.scheduler.CancellationException) Flow(com.sequenceiq.flow.core.Flow) RedbeamsFailureEvent(com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsFailureEvent) FlowParameters(com.sequenceiq.flow.core.FlowParameters) CancellationException(com.sequenceiq.cloudbreak.cloud.scheduler.CancellationException) RedbeamsEvent(com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsEvent) Map(java.util.Map) RedbeamsContext(com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsContext) Bean(org.springframework.context.annotation.Bean)

Example 4 with RedbeamsContext

use of com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsContext in project cloudbreak by hortonworks.

the class RedbeamsTerminationActions method failedAction.

@Bean(name = "REDBEAMS_TERMINATION_FAILED_STATE")
public Action<?, ?> failedAction() {
    return new AbstractRedbeamsTerminationAction<>(RedbeamsFailureEvent.class, false) {

        // A lot here - some of this could go into some sort of failure handler class
        // compare to core StackCreationService::handleStackCreationFailure
        @Override
        protected void doExecute(RedbeamsContext context, RedbeamsFailureEvent payload, Map<Object, Object> variables) {
            Exception failureException = payload.getException();
            LOGGER.info("Error during database stack termination flow:", failureException);
            if (failureException instanceof CancellationException || ExceptionUtils.getRootCause(failureException) instanceof CancellationException) {
                LOGGER.debug("The flow has been cancelled");
            } else {
                // StackCreationActions / StackCreationService only update status if stack isn't mid-deletion
                String errorReason = failureException == null ? "Unknown error" : failureException.getMessage();
                Optional<DBStack> dbStack = dbStackStatusUpdater.updateStatus(payload.getResourceId(), DetailedDBStackStatus.DELETE_FAILED, errorReason);
                metricService.incrementMetricCounter(MetricType.DB_TERMINATION_FAILED, dbStack);
            }
            sendEvent(context, REDBEAMS_TERMINATION_FAILURE_HANDLED_EVENT.event(), payload);
        }

        @Override
        protected RedbeamsContext createFlowContext(FlowParameters flowParameters, StateContext<RedbeamsTerminationState, RedbeamsTerminationEvent> stateContext, RedbeamsFailureEvent payload) {
            Flow flow = getFlow(flowParameters.getFlowId());
            flow.setFlowFailed(payload.getException());
            return super.createFlowContext(flowParameters, stateContext, payload);
        }

        @Override
        protected Object getFailurePayload(RedbeamsFailureEvent payload, Optional<RedbeamsContext> flowContext, Exception ex) {
            return payload;
        }
    };
}
Also used : Optional(java.util.Optional) DBStack(com.sequenceiq.redbeams.domain.stack.DBStack) StateContext(org.springframework.statemachine.StateContext) CancellationException(com.sequenceiq.cloudbreak.cloud.scheduler.CancellationException) Flow(com.sequenceiq.flow.core.Flow) AbstractRedbeamsTerminationAction(com.sequenceiq.redbeams.flow.redbeams.termination.AbstractRedbeamsTerminationAction) RedbeamsFailureEvent(com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsFailureEvent) FlowParameters(com.sequenceiq.flow.core.FlowParameters) CancellationException(com.sequenceiq.cloudbreak.cloud.scheduler.CancellationException) RedbeamsContext(com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsContext) Map(java.util.Map) Bean(org.springframework.context.annotation.Bean)

Example 5 with RedbeamsContext

use of com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsContext in project cloudbreak by hortonworks.

the class AbstractRedbeamsProvisionAction method createFlowContext.

@Override
protected RedbeamsContext createFlowContext(FlowParameters flowParameters, StateContext<RedbeamsProvisionState, RedbeamsProvisionEvent> stateContext, P payload) {
    DBStack dbStack = dbStackService.getById(payload.getResourceId());
    MDCBuilder.buildMdcContext(dbStack);
    Location location = location(region(dbStack.getRegion()), availabilityZone(dbStack.getAvailabilityZone()));
    String accountId = dbStack.getOwnerCrn().getAccountId();
    CloudContext cloudContext = CloudContext.Builder.builder().withId(dbStack.getId()).withName(dbStack.getName()).withCrn(dbStack.getResourceCrn()).withPlatform(dbStack.getCloudPlatform()).withVariant(dbStack.getPlatformVariant()).withLocation(location).withUserName(dbStack.getUserName()).withAccountId(accountId).build();
    Credential credential = credentialService.getCredentialByEnvCrn(dbStack.getEnvironmentId());
    CloudCredential cloudCredential = credentialConverter.convert(credential);
    DatabaseStack databaseStack = databaseStackConverter.convert(dbStack);
    return new RedbeamsContext(flowParameters, cloudContext, cloudCredential, databaseStack, dbStack);
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Credential(com.sequenceiq.redbeams.dto.Credential) DBStack(com.sequenceiq.redbeams.domain.stack.DBStack) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) RedbeamsContext(com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsContext) Location(com.sequenceiq.cloudbreak.cloud.model.Location)

Aggregations

RedbeamsContext (com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsContext)7 DBStack (com.sequenceiq.redbeams.domain.stack.DBStack)4 Map (java.util.Map)3 Bean (org.springframework.context.annotation.Bean)3 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)2 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)2 DatabaseStack (com.sequenceiq.cloudbreak.cloud.model.DatabaseStack)2 Location (com.sequenceiq.cloudbreak.cloud.model.Location)2 CancellationException (com.sequenceiq.cloudbreak.cloud.scheduler.CancellationException)2 Flow (com.sequenceiq.flow.core.Flow)2 FlowParameters (com.sequenceiq.flow.core.FlowParameters)2 Credential (com.sequenceiq.redbeams.dto.Credential)2 RedbeamsEvent (com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsEvent)2 RedbeamsFailureEvent (com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsFailureEvent)2 AbstractRedbeamsProvisionAction (com.sequenceiq.redbeams.flow.redbeams.provision.AbstractRedbeamsProvisionAction)2 StateContext (org.springframework.statemachine.StateContext)2 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)1 RedbeamsException (com.sequenceiq.redbeams.exception.RedbeamsException)1 AllocateDatabaseServerSuccess (com.sequenceiq.redbeams.flow.redbeams.provision.event.allocate.AllocateDatabaseServerSuccess)1 UpdateDatabaseServerRegistrationRequest (com.sequenceiq.redbeams.flow.redbeams.provision.event.register.UpdateDatabaseServerRegistrationRequest)1