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());
}
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);
}
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);
}
};
}
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;
}
};
}
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);
}
Aggregations