Search in sources :

Example 1 with RedbeamsException

use of com.sequenceiq.redbeams.exception.RedbeamsException in project cloudbreak by hortonworks.

the class DatabaseServerConfigService method release.

public DatabaseServerConfig release(String resourceCrn) {
    try {
        return transactionService.required(() -> {
            DatabaseServerConfig resource = getByCrn(resourceCrn);
            if (!resource.getResourceStatus().isReleasable()) {
                throw new ConflictException(String.format("Database server configuration has unreleasable resource " + "status %s: releasable statuses are %s", resource.getResourceStatus(), ResourceStatus.getReleasableValues()));
            }
            Optional<DBStack> dbStack = resource.getDbStack();
            if (dbStack.isPresent()) {
                dbStackService.delete(dbStack.get());
                resource.setDbStack(null);
            } else {
                LOGGER.info("Database stack missing for crn: '{}', continuing anyway", resourceCrn);
            }
            resource.setResourceStatus(ResourceStatus.USER_MANAGED);
            return repository.save(resource);
        });
    } catch (TransactionService.TransactionExecutionException e) {
        throw e.getCause() != null ? e.getCause() : new RedbeamsException("Failed to release management of " + resourceCrn);
    }
}
Also used : TransactionService(com.sequenceiq.cloudbreak.common.service.TransactionService) DatabaseServerConfig(com.sequenceiq.redbeams.domain.DatabaseServerConfig) ConflictException(com.sequenceiq.redbeams.exception.ConflictException) DBStack(com.sequenceiq.redbeams.domain.stack.DBStack) RedbeamsException(com.sequenceiq.redbeams.exception.RedbeamsException)

Example 2 with RedbeamsException

use of com.sequenceiq.redbeams.exception.RedbeamsException in project cloudbreak by hortonworks.

the class RedbeamsCreationService method saveDbStack.

private DBStack saveDbStack(DBStack dbStack) {
    // possible future change is to use a flow here (GetPlatformTemplateRequest, modified for database server)
    // for now, just get it synchronously / within this thread, it ought to be quick
    CloudPlatformVariant platformVariant = new CloudPlatformVariant(dbStack.getCloudPlatform(), dbStack.getPlatformVariant());
    try {
        CloudConnector<Object> connector = cloudPlatformConnectors.get(platformVariant);
        if (connector == null) {
            throw new RedbeamsException("Failed to find cloud connector for platform variant " + platformVariant);
        }
        String template = connector.resources().getDBStackTemplate();
        if (template == null) {
            throw new RedbeamsException("No database stack template is available for platform variant " + platformVariant);
        }
        dbStack.setTemplate(template);
    } catch (TemplatingNotSupportedException e) {
        throw new RedbeamsException("Failed to retrieve database stack template for cloud platform", e);
    }
    DatabaseServer databaseServer = dbStack.getDatabaseServer();
    if (databaseServer.getConnectionDriver() == null) {
        String connectionDriver = databaseServer.getDatabaseVendor().connectionDriver();
        databaseServer.setConnectionDriver(connectionDriver);
        LOGGER.info("Database server allocation request lacked a connection driver; defaulting to {}", connectionDriver);
    }
    return dbStackService.save(dbStack);
}
Also used : CloudPlatformVariant(com.sequenceiq.cloudbreak.cloud.model.CloudPlatformVariant) DatabaseServer(com.sequenceiq.redbeams.domain.stack.DatabaseServer) TemplatingNotSupportedException(com.sequenceiq.cloudbreak.cloud.exception.TemplatingNotSupportedException) RedbeamsException(com.sequenceiq.redbeams.exception.RedbeamsException)

Example 3 with RedbeamsException

use of com.sequenceiq.redbeams.exception.RedbeamsException in project cloudbreak by hortonworks.

the class SubnetListerService method getAzureSubscriptionId.

String getAzureSubscriptionId(String environmentCrn) {
    Credential credential = credentialService.getCredentialByEnvCrn(environmentCrn);
    LOGGER.info("Found credential {} for environment {}", credential.getName(), environmentCrn);
    if (credential.getAzure().isPresent()) {
        return credential.getAzure().get().getSubscriptionId();
    } else {
        throw new RedbeamsException(String.format("Retrieved credential %s for Azure environment %s which lacks subscription ID", credential.getName(), environmentCrn));
    }
}
Also used : Credential(com.sequenceiq.redbeams.dto.Credential) RedbeamsException(com.sequenceiq.redbeams.exception.RedbeamsException)

Example 4 with RedbeamsException

use of com.sequenceiq.redbeams.exception.RedbeamsException in project cloudbreak by hortonworks.

the class DatabaseConfigService method deleteServiceManagedDatabase.

private void deleteServiceManagedDatabase(DatabaseConfig databaseConfig) {
    String databaseName = databaseConfig.getName();
    String databaseUserName = databaseConfig.getConnectionUserName().getRaw();
    DatabaseServerConfig databaseServerConfig = databaseConfig.getServer();
    if (databaseServerConfig == null) {
        LOGGER.error("Cannot delete database " + databaseName + " from server, server not known");
        return;
    }
    List<String> sqlStrings = new ArrayList<>();
    boolean distinctDbUser = !databaseUserName.equals(databaseServerConfig.getConnectionUserName());
    if (distinctDbUser) {
        sqlStrings.add("REVOKE ALL PRIVILEGES ON DATABASE " + databaseName + " FROM " + databaseUserName);
    }
    sqlStrings.add("DROP DATABASE " + databaseName);
    if (distinctDbUser) {
        sqlStrings.add("DROP USER " + databaseUserName);
    }
    // For now, do not use a transaction (PostgreSQL forbids it).
    boolean dropDatabaseInsideTransaction = false;
    driverFunctions.execWithDatabaseDriver(databaseServerConfig, driver -> {
        try (Connection conn = driver.connect(databaseServerConfig)) {
            databaseCommon.executeUpdates(conn, sqlStrings, dropDatabaseInsideTransaction);
        } catch (SQLException e) {
            throw new RedbeamsException("Failed to drop database " + databaseName, e);
        }
    });
}
Also used : DatabaseServerConfig(com.sequenceiq.redbeams.domain.DatabaseServerConfig) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) RedbeamsException(com.sequenceiq.redbeams.exception.RedbeamsException)

Example 5 with RedbeamsException

use of com.sequenceiq.redbeams.exception.RedbeamsException in project cloudbreak by hortonworks.

the class DatabaseServerConfigService method createDatabaseOnServer.

public String createDatabaseOnServer(String serverCrn, String databaseName, String databaseType, Optional<String> databaseDescription) {
    // identifiers that protects us from SQL injections
    if (!validateDatabaseName(databaseName)) {
        throw new IllegalArgumentException("The database must contain only alphanumeric characters or underscores");
    }
    LOGGER.info("Creating database with name: {}", databaseName);
    DatabaseServerConfig databaseServerConfig = getByCrn(serverCrn);
    // A database password does not necessarily need to follow cloud provider rules for the root
    // password of a database server, but we can try to follow them anyway. A user-managed
    // database server will not have a known cloud platform, however.
    Optional<CloudPlatform> cloudPlatform = databaseServerConfig.getDbStack().map(DBStack::getCloudPlatform).map(CloudPlatform::valueOf);
    String databaseUserName = userGeneratorService.generateUserName();
    String databasePassword = passwordGeneratorService.generatePassword(cloudPlatform);
    List<String> sqlStrings = List.of("CREATE DATABASE " + databaseName, "CREATE USER " + databaseUserName + " WITH ENCRYPTED PASSWORD '" + databasePassword + "'", "GRANT ALL PRIVILEGES ON DATABASE " + databaseName + " TO " + databaseUserName);
    // For now, do not use a transaction (PostgreSQL forbids it).
    boolean createDatabaseInsideTransaction = false;
    driverFunctions.execWithDatabaseDriver(databaseServerConfig, driver -> {
        try (Connection conn = driver.connect(databaseServerConfig)) {
            databaseCommon.executeUpdates(conn, sqlStrings, createDatabaseInsideTransaction);
        } catch (SQLException e) {
            throw new RedbeamsException("Failed to create database " + databaseName, e);
        }
    });
    // Only record database on server if successfully created on server
    DatabaseConfig newDatabaseConfig = databaseServerConfig.createDatabaseConfig(databaseName, databaseType, databaseDescription, ResourceStatus.SERVICE_MANAGED, databaseUserName, databasePassword);
    databaseConfigService.register(newDatabaseConfig, false);
    return "created";
}
Also used : CloudPlatform(com.sequenceiq.cloudbreak.common.mappable.CloudPlatform) DatabaseServerConfig(com.sequenceiq.redbeams.domain.DatabaseServerConfig) SQLException(java.sql.SQLException) Connection(java.sql.Connection) RedbeamsException(com.sequenceiq.redbeams.exception.RedbeamsException) DatabaseConfig(com.sequenceiq.redbeams.domain.DatabaseConfig)

Aggregations

RedbeamsException (com.sequenceiq.redbeams.exception.RedbeamsException)6 DatabaseServerConfig (com.sequenceiq.redbeams.domain.DatabaseServerConfig)3 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 TemplatingNotSupportedException (com.sequenceiq.cloudbreak.cloud.exception.TemplatingNotSupportedException)1 CloudPlatformVariant (com.sequenceiq.cloudbreak.cloud.model.CloudPlatformVariant)1 CloudPlatform (com.sequenceiq.cloudbreak.common.mappable.CloudPlatform)1 TransactionService (com.sequenceiq.cloudbreak.common.service.TransactionService)1 PrivateEndpointType (com.sequenceiq.common.model.PrivateEndpointType)1 DatabaseConfig (com.sequenceiq.redbeams.domain.DatabaseConfig)1 DBStack (com.sequenceiq.redbeams.domain.stack.DBStack)1 DatabaseServer (com.sequenceiq.redbeams.domain.stack.DatabaseServer)1 Credential (com.sequenceiq.redbeams.dto.Credential)1 ConflictException (com.sequenceiq.redbeams.exception.ConflictException)1 ArrayList (java.util.ArrayList)1