Search in sources :

Example 11 with EnvCreationFailureEvent

use of com.sequenceiq.environment.environment.flow.creation.event.EnvCreationFailureEvent in project cloudbreak by hortonworks.

the class EnvCreationActions method publickeyCreationAction.

@Bean(name = "PUBLICKEY_CREATION_STARTED_STATE")
public Action<?, ?> publickeyCreationAction() {
    return new AbstractEnvironmentCreationAction<>(EnvCreationEvent.class) {

        @Override
        protected void doExecute(CommonContext context, EnvCreationEvent payload, Map<Object, Object> variables) {
            environmentService.findEnvironmentById(payload.getResourceId()).ifPresentOrElse(environment -> {
                LOGGER.info("Creation of PublicKey has started. Current state is - PUBLICKEY_CREATION_STARTED_STATE");
                environment.setStatus(EnvironmentStatus.PUBLICKEY_CREATE_IN_PROGRESS);
                environment.setStatusReason(null);
                environment = environmentService.save(environment);
                EnvironmentDto environmentDto = environmentService.getEnvironmentDto(environment);
                eventService.sendEventAndNotification(environmentDto, context.getFlowTriggerUserCrn(), ENVIRONMENT_PUBLICKEY_CREATION_STARTED);
                sendEvent(context, CREATE_PUBLICKEY_EVENT.selector(), environmentDto);
            }, () -> {
                EnvCreationFailureEvent failureEvent = new EnvCreationFailureEvent(payload.getResourceId(), payload.getResourceName(), null, payload.getResourceCrn());
                LOGGER.debug("Environment public key creation action went failed with EnvCreationFailureEvent was: {}", failureEvent);
                eventService.sendEventAndNotificationForMissingEnv(payload, ENVIRONMENT_PUBLICKEY_CREATION_FAILED, context.getFlowTriggerUserCrn());
                LOGGER.warn("Failed to create public key for environment! No environment found with id '{}'.", payload.getResourceId());
                sendEvent(context, failureEvent);
            });
        }
    };
}
Also used : EnvironmentDto(com.sequenceiq.environment.environment.dto.EnvironmentDto) CommonContext(com.sequenceiq.flow.core.CommonContext) EnvCreationEvent(com.sequenceiq.environment.environment.flow.creation.event.EnvCreationEvent) EnvCreationFailureEvent(com.sequenceiq.environment.environment.flow.creation.event.EnvCreationFailureEvent) Map(java.util.Map) Bean(org.springframework.context.annotation.Bean)

Example 12 with EnvCreationFailureEvent

use of com.sequenceiq.environment.environment.flow.creation.event.EnvCreationFailureEvent in project cloudbreak by hortonworks.

the class EnvCreationActions method failedAction.

@Bean(name = "ENV_CREATION_FAILED_STATE")
public Action<?, ?> failedAction() {
    return new AbstractEnvironmentCreationAction<>(EnvCreationFailureEvent.class) {

        @Override
        protected void doExecute(CommonContext context, EnvCreationFailureEvent payload, Map<Object, Object> variables) {
            Exception exception = payload.getException();
            LOGGER.debug("Failed to create environment {} with payload {}", exception, payload);
            environmentService.findEnvironmentById(payload.getResourceId()).ifPresentOrElse(environment -> {
                environment.setStatusReason(exception.getMessage());
                environment.setStatus(EnvironmentStatus.CREATE_FAILED);
                environmentService.save(environment);
                EnvironmentDto environmentDto = environmentService.getEnvironmentDto(environment);
                metricService.incrementMetricCounter(MetricType.ENV_CREATION_FAILED, environmentDto, exception);
                eventService.sendEventAndNotification(environmentDto, context.getFlowTriggerUserCrn(), ENVIRONMENT_CREATION_FAILED);
            }, () -> LOGGER.error("Cannot finish the creation of env, because the environment does not exist: {}. " + "But the flow will continue, how can this happen?", payload.getResourceId()));
            LOGGER.info("Flow entered into ENV_CREATION_FAILED_STATE");
            sendEvent(context, HANDLED_FAILED_ENV_CREATION_EVENT.event(), payload);
        }
    };
}
Also used : EnvironmentDto(com.sequenceiq.environment.environment.dto.EnvironmentDto) CommonContext(com.sequenceiq.flow.core.CommonContext) EnvCreationFailureEvent(com.sequenceiq.environment.environment.flow.creation.event.EnvCreationFailureEvent) Map(java.util.Map) Bean(org.springframework.context.annotation.Bean)

Example 13 with EnvCreationFailureEvent

use of com.sequenceiq.environment.environment.flow.creation.event.EnvCreationFailureEvent in project cloudbreak by hortonworks.

the class EnvironmentValidationHandlerTest method sendEnvCreationFailureEventWhenNoEnvironmentFound.

@Test
void sendEnvCreationFailureEventWhenNoEnvironmentFound() {
    EnvironmentValidationDto environmentValidationDto = createEnvironmentValidationDto();
    when(environmentService.findEnvironmentById(anyLong())).thenReturn(Optional.empty());
    underTest.accept(Event.wrap(environmentValidationDto));
    ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
    verify(eventBus, times(1)).notify(eq(EnvCreationStateSelectors.FAILED_ENV_CREATION_EVENT.name()), eventCaptor.capture());
    EnvCreationFailureEvent envCreationFailureEvent = (EnvCreationFailureEvent) eventCaptor.getValue().getData();
    Assertions.assertEquals("Environment was not found with id '1'.", envCreationFailureEvent.getException().getMessage());
}
Also used : EnvCreationEvent(com.sequenceiq.environment.environment.flow.creation.event.EnvCreationEvent) Event(reactor.bus.Event) EnvCreationFailureEvent(com.sequenceiq.environment.environment.flow.creation.event.EnvCreationFailureEvent) EnvironmentValidationDto(com.sequenceiq.environment.environment.dto.EnvironmentValidationDto) EnvCreationFailureEvent(com.sequenceiq.environment.environment.flow.creation.event.EnvCreationFailureEvent) Test(org.junit.jupiter.api.Test)

Example 14 with EnvCreationFailureEvent

use of com.sequenceiq.environment.environment.flow.creation.event.EnvCreationFailureEvent in project cloudbreak by hortonworks.

the class EnvironmentValidationHandlerTest method sendEnvCreationFailureEventWhenValidationFailed.

@Test
void sendEnvCreationFailureEventWhenValidationFailed() {
    EnvironmentValidationDto environmentValidationDto = createEnvironmentValidationDto();
    Environment environment = new Environment();
    when(environmentService.findEnvironmentById(anyLong())).thenReturn(Optional.of(environment));
    when(validatorService.validateRegionsAndLocation(any(), any(), eq(environment), any())).thenReturn(new ValidationResultBuilder());
    when(cloudStorageValidator.validateCloudStorage(any(), any())).thenReturn(ObjectStorageValidateResponse.builder().withStatus(ResponseStatus.ERROR).withError("Validation failed.").build());
    underTest.accept(Event.wrap(environmentValidationDto));
    ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
    verify(eventBus, times(1)).notify(eq(EnvCreationStateSelectors.FAILED_ENV_CREATION_EVENT.name()), eventCaptor.capture());
    EnvCreationFailureEvent envCreationFailureEvent = (EnvCreationFailureEvent) eventCaptor.getValue().getData();
    Assertions.assertEquals("Validation failed.", envCreationFailureEvent.getException().getMessage());
}
Also used : ValidationResultBuilder(com.sequenceiq.cloudbreak.validation.ValidationResult.ValidationResultBuilder) Environment(com.sequenceiq.environment.environment.domain.Environment) EnvCreationEvent(com.sequenceiq.environment.environment.flow.creation.event.EnvCreationEvent) Event(reactor.bus.Event) EnvCreationFailureEvent(com.sequenceiq.environment.environment.flow.creation.event.EnvCreationFailureEvent) EnvironmentValidationDto(com.sequenceiq.environment.environment.dto.EnvironmentValidationDto) EnvCreationFailureEvent(com.sequenceiq.environment.environment.flow.creation.event.EnvCreationFailureEvent) Test(org.junit.jupiter.api.Test)

Example 15 with EnvCreationFailureEvent

use of com.sequenceiq.environment.environment.flow.creation.event.EnvCreationFailureEvent in project cloudbreak by hortonworks.

the class ResourceEncryptionInitializationHandler method accept.

@Override
public void accept(Event<EnvironmentDto> environmentDtoEvent) {
    LOGGER.debug("Accepting ResourceEncryptionInitialization event");
    EnvironmentDto environmentDto = environmentDtoEvent.getData();
    try {
        environmentService.findEnvironmentById(environmentDto.getId()).ifPresent(environment -> {
            if (AZURE.name().equalsIgnoreCase(environmentDto.getCloudPlatform())) {
                String encryptionKeyUrl = Optional.ofNullable(environmentDto.getParameters()).map(ParametersDto::getAzureParametersDto).map(AzureParametersDto::getAzureResourceEncryptionParametersDto).map(AzureResourceEncryptionParametersDto::getEncryptionKeyUrl).orElse(null);
                String environmentName = environment.getName();
                if (StringUtils.isNotEmpty(encryptionKeyUrl)) {
                    if (environment.getStatus() != EnvironmentStatus.ENVIRONMENT_ENCRYPTION_RESOURCES_INITIALIZED) {
                        initializeEncryptionResources(environmentDto, environment);
                    } else {
                        LOGGER.info("Initialization of encryption resources for environment \"{}\" has already been triggered, " + "continuing without new initialize trigger. Environment status: {}", environmentName, environment.getStatus());
                    }
                } else {
                    LOGGER.info("Environment \"{}\" has not requested for SSE with CMK.", environmentName);
                }
            }
        });
        EnvCreationEvent envCreationEvent = getEnvCreateEvent(environmentDto);
        eventSender().sendEvent(envCreationEvent, environmentDtoEvent.getHeaders());
    } catch (Exception e) {
        LOGGER.error("ResourceEncryptionInitialization failed with error.", e);
        EnvCreationFailureEvent failedEvent = new EnvCreationFailureEvent(environmentDto.getId(), environmentDto.getName(), e, environmentDto.getResourceCrn());
        Event<EnvCreationFailureEvent> ev = new Event<>(environmentDtoEvent.getHeaders(), failedEvent);
        eventBus.notify(failedEvent.selector(), ev);
    }
}
Also used : EnvironmentDto(com.sequenceiq.environment.environment.dto.EnvironmentDto) EnvCreationEvent(com.sequenceiq.environment.environment.flow.creation.event.EnvCreationEvent) Event(reactor.bus.Event) EnvCreationFailureEvent(com.sequenceiq.environment.environment.flow.creation.event.EnvCreationFailureEvent) AzureParametersDto(com.sequenceiq.environment.parameter.dto.AzureParametersDto) EnvCreationEvent(com.sequenceiq.environment.environment.flow.creation.event.EnvCreationEvent) EnvCreationFailureEvent(com.sequenceiq.environment.environment.flow.creation.event.EnvCreationFailureEvent) CloudbreakServiceException(com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException)

Aggregations

EnvCreationFailureEvent (com.sequenceiq.environment.environment.flow.creation.event.EnvCreationFailureEvent)19 EnvironmentDto (com.sequenceiq.environment.environment.dto.EnvironmentDto)14 EnvCreationEvent (com.sequenceiq.environment.environment.flow.creation.event.EnvCreationEvent)12 CommonContext (com.sequenceiq.flow.core.CommonContext)7 Map (java.util.Map)7 Bean (org.springframework.context.annotation.Bean)7 Event (reactor.bus.Event)7 Environment (com.sequenceiq.environment.environment.domain.Environment)3 EnvironmentValidationDto (com.sequenceiq.environment.environment.dto.EnvironmentValidationDto)3 Test (org.junit.jupiter.api.Test)3 CloudSubnet (com.sequenceiq.cloudbreak.cloud.model.CloudSubnet)2 BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)2 CloudbreakServiceException (com.sequenceiq.cloudbreak.common.exception.CloudbreakServiceException)2 ValidationResultBuilder (com.sequenceiq.cloudbreak.validation.ValidationResult.ValidationResultBuilder)1 NetworkTest (com.sequenceiq.environment.environment.service.network.NetworkTest)1 FreeIpaOperationFailedException (com.sequenceiq.environment.exception.FreeIpaOperationFailedException)1 AwsNetwork (com.sequenceiq.environment.network.dao.domain.AwsNetwork)1 AzureParametersDto (com.sequenceiq.environment.parameter.dto.AzureParametersDto)1 BadRequestException (javax.ws.rs.BadRequestException)1