Search in sources :

Example 31 with DBStack

use of com.sequenceiq.redbeams.domain.stack.DBStack 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 32 with DBStack

use of com.sequenceiq.redbeams.domain.stack.DBStack in project cloudbreak by hortonworks.

the class DatabaseServerConfigToDatabaseServerV4ResponseConverter method convert.

public DatabaseServerV4Response convert(DatabaseServerConfig source) {
    DatabaseServerV4Response response = new DatabaseServerV4Response();
    response.setId(source.getId());
    response.setCrn(source.getResourceCrn().toString());
    response.setName(source.getName());
    response.setDescription(source.getDescription());
    response.setHost(source.getHost());
    response.setPort(source.getPort());
    response.setDatabaseVendor(source.getDatabaseVendor().databaseType());
    response.setDatabaseVendorDisplayName(source.getDatabaseVendor().displayName());
    response.setConnectionDriver(source.getConnectionDriver());
    response.setConnectionUserName(stringToSecretResponseConverter.convert(source.getConnectionUserNameSecret()));
    response.setConnectionPassword(stringToSecretResponseConverter.convert(source.getConnectionPasswordSecret()));
    response.setCreationDate(source.getCreationDate());
    response.setEnvironmentCrn(source.getEnvironmentId());
    response.setClusterCrn(source.getClusterCrn());
    response.setResourceStatus(source.getResourceStatus());
    if (source.getDbStack().isPresent()) {
        DBStack dbStack = source.getDbStack().get();
        response.setStatus(dbStack.getStatus());
        response.setStatusReason(dbStack.getStatusReason());
        if (dbStack.getSslConfig() != null) {
            SslConfig sslConfig = dbStack.getSslConfig();
            SslConfigV4Response sslConfigV4Response = new SslConfigV4Response();
            sslConfigV4Response.setSslCertificates(sslConfig.getSslCertificates());
            sslConfigV4Response.setSslCertificateType(sslConfig.getSslCertificateType());
            sslConfigV4Response.setSslMode(NONE.equals(sslConfig.getSslCertificateType()) ? DISABLED : ENABLED);
            String cloudPlatform = dbStack.getCloudPlatform();
            String region = dbStack.getRegion();
            // TODO Add SslConfig.sslCertificateMaxVersion that is kept up-to-date (mostly for GCP), use getMaxVersionByPlatform() as fallback
            sslConfigV4Response.setSslCertificateHighestAvailableVersion(databaseServerSslCertificateConfig.getMaxVersionByCloudPlatformAndRegion(cloudPlatform, region));
            sslConfigV4Response.setSslCertificateActiveVersion(Optional.ofNullable(sslConfig.getSslCertificateActiveVersion()).orElse(databaseServerSslCertificateConfig.getLegacyMaxVersionByCloudPlatformAndRegion(cloudPlatform, region)));
            sslConfigV4Response.setSslCertificateActiveCloudProviderIdentifier(Optional.ofNullable(sslConfig.getSslCertificateActiveCloudProviderIdentifier()).orElse(databaseServerSslCertificateConfig.getLegacyCloudProviderIdentifierByCloudPlatformAndRegion(cloudPlatform, region)));
            response.setSslConfig(sslConfigV4Response);
        }
    } else if (source.getHost() != null && source.getPort() != null) {
        response.setStatus(Status.AVAILABLE);
    } else {
        response.setStatus(Status.UNKNOWN);
    }
    if (response.getSslConfig() == null) {
        response.setSslConfig(new SslConfigV4Response());
    }
    return response;
}
Also used : SslConfig(com.sequenceiq.redbeams.domain.stack.SslConfig) DatabaseServerV4Response(com.sequenceiq.redbeams.api.endpoint.v4.databaseserver.responses.DatabaseServerV4Response) DBStack(com.sequenceiq.redbeams.domain.stack.DBStack) SslConfigV4Response(com.sequenceiq.redbeams.api.endpoint.v4.databaseserver.responses.SslConfigV4Response)

Example 33 with DBStack

use of com.sequenceiq.redbeams.domain.stack.DBStack in project cloudbreak by hortonworks.

the class AbstractRedbeamsStartAction method createFlowContext.

@Override
protected RedbeamsStartContext createFlowContext(FlowParameters flowParameters, StateContext<RedbeamsStartState, RedbeamsStartEvent> stateContext, P payload) {
    DBStack dbStack = dbStackService.getById(payload.getResourceId());
    MDCBuilder.buildMdcContext(dbStack);
    Location location = location(region(dbStack.getRegion()), availabilityZone(dbStack.getAvailabilityZone()));
    String userName = dbStack.getOwnerCrn().getUserId();
    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(userName).withAccountId(accountId).build();
    Credential credential = credentialService.getCredentialByEnvCrn(dbStack.getEnvironmentId());
    CloudCredential cloudCredential = credentialConverter.convert(credential);
    DatabaseStack databaseStack = databaseStackConverter.convert(dbStack);
    return new RedbeamsStartContext(flowParameters, cloudContext, cloudCredential, databaseStack);
}
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) RedbeamsStartContext(com.sequenceiq.redbeams.flow.redbeams.start.RedbeamsStartContext) Location(com.sequenceiq.cloudbreak.cloud.model.Location)

Example 34 with DBStack

use of com.sequenceiq.redbeams.domain.stack.DBStack 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 35 with DBStack

use of com.sequenceiq.redbeams.domain.stack.DBStack in project cloudbreak by hortonworks.

the class AllocateDatabaseServerV4RequestToDBStackConverterTest method conversionTestWhenOptionalElementsGenerated.

@Test
void conversionTestWhenOptionalElementsGenerated() throws IOException {
    setupAllocateRequest(false);
    when(databaseServerSslCertificateConfig.getNumberOfCertsByCloudPlatformAndRegion(AWS_CLOUD_PLATFORM.name(), REGION)).thenReturn(SINGLE_CERT);
    when(databaseServerSslCertificateConfig.getCertByCloudPlatformAndRegionAndVersion(AWS_CLOUD_PLATFORM.name(), REGION, VERSION_3)).thenReturn(sslCertificateEntryV3);
    List<CloudSubnet> cloudSubnets = List.of(new CloudSubnet("subnet-1", "", "az-a", ""), new CloudSubnet("subnet-2", "", "az-b", ""));
    DetailedEnvironmentResponse environment = DetailedEnvironmentResponse.builder().withCloudPlatform(AWS_CLOUD_PLATFORM.name()).withName(ENVIRONMENT_NAME).withCrn(ENVIRONMENT_CRN).withTag(new TagResponse()).withLocation(LocationResponse.LocationResponseBuilder.aLocationResponse().withName(REGION).build()).withSecurityAccess(SecurityAccessResponse.builder().withDefaultSecurityGroupId(DEFAULT_SECURITY_GROUP_ID).build()).withNetwork(EnvironmentNetworkResponse.builder().withSubnetMetas(Map.of("subnet-1", cloudSubnets.get(0), "subnet-2", cloudSubnets.get(1))).build()).build();
    when(environmentService.getByCrn(ENVIRONMENT_CRN)).thenReturn(environment);
    when(subnetListerService.listSubnets(any(), any())).thenReturn(cloudSubnets);
    when(subnetChooserService.chooseSubnets(any(), any(), any())).thenReturn(cloudSubnets);
    when(networkParameterAdder.addSubnetIds(any(), any(), any(), any())).thenReturn(SUBNET_ID_REQUEST_PARAMETERS);
    when(userGeneratorService.generateUserName()).thenReturn(USERNAME);
    when(passwordGeneratorService.generatePassword(any())).thenReturn(PASSWORD);
    DBStack dbStack = underTest.convert(allocateRequest, OWNER_CRN);
    assertEquals(ENVIRONMENT_NAME + "-dbstck-parts", dbStack.getName());
    assertEquals(PASSWORD, dbStack.getDatabaseServer().getRootPassword());
    assertEquals(USERNAME, dbStack.getDatabaseServer().getRootUserName());
    assertEquals("n-uuid", dbStack.getNetwork().getName());
    assertEquals(1, dbStack.getNetwork().getAttributes().getMap().size());
    assertEquals("netvalue", dbStack.getNetwork().getAttributes().getMap().get("netkey"));
    assertThat(dbStack.getDatabaseServer().getSecurityGroup().getSecurityGroupIds()).hasSize(1);
    assertEquals(dbStack.getDatabaseServer().getSecurityGroup().getSecurityGroupIds().iterator().next(), DEFAULT_SECURITY_GROUP_ID);
    assertEquals(dbStack.getTags().get(StackTags.class).getUserDefinedTags().get("DistroXKey1"), "DistroXValue1");
    verifySsl(dbStack, Set.of(CERT_PEM_V3), CLOUD_PROVIDER_IDENTIFIER_V3);
    verify(databaseServerSslCertificateConfig, never()).getCertsByCloudPlatformAndRegionAndVersions(anyString(), anyString(), any());
    verify(providerParameterCalculator).get(allocateRequest);
    verify(providerParameterCalculator, never()).get(networkRequest);
    verify(subnetListerService).listSubnets(any(), any());
    verify(subnetChooserService).chooseSubnets(anyList(), any(), any());
    verify(networkParameterAdder).addSubnetIds(any(), any(), any(), any());
    verify(userGeneratorService).generateUserName();
    verify(passwordGeneratorService).generatePassword(any());
}
Also used : StackTags(com.sequenceiq.cloudbreak.cloud.model.StackTags) TagResponse(com.sequenceiq.environment.api.v1.environment.model.response.TagResponse) DBStack(com.sequenceiq.redbeams.domain.stack.DBStack) DetailedEnvironmentResponse(com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse) CloudSubnet(com.sequenceiq.cloudbreak.cloud.model.CloudSubnet) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

DBStack (com.sequenceiq.redbeams.domain.stack.DBStack)83 Test (org.junit.jupiter.api.Test)28 DatabaseServerConfig (com.sequenceiq.redbeams.domain.DatabaseServerConfig)17 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)17 Test (org.junit.Test)13 Selectable (com.sequenceiq.cloudbreak.common.event.Selectable)8 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)7 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)7 CloudSubnet (com.sequenceiq.cloudbreak.cloud.model.CloudSubnet)7 DatabaseStack (com.sequenceiq.cloudbreak.cloud.model.DatabaseStack)7 DatabaseServerV4Response (com.sequenceiq.redbeams.api.endpoint.v4.databaseserver.responses.DatabaseServerV4Response)7 DetailedEnvironmentResponse (com.sequenceiq.environment.api.v1.environment.model.response.DetailedEnvironmentResponse)6 SslConfig (com.sequenceiq.redbeams.domain.stack.SslConfig)6 Credential (com.sequenceiq.redbeams.dto.Credential)6 RedbeamsFailureEvent (com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsFailureEvent)6 Crn (com.sequenceiq.cloudbreak.auth.crn.Crn)5 CloudDatabaseServerSslCertificate (com.sequenceiq.cloudbreak.cloud.model.database.CloudDatabaseServerSslCertificate)5 RedbeamsEvent (com.sequenceiq.redbeams.flow.redbeams.common.RedbeamsEvent)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Location (com.sequenceiq.cloudbreak.cloud.model.Location)4