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