@Test public void testCreateDatabaseOnServer() { when(repository.findByResourceCrn(SERVER_CRN)).thenReturn(Optional.of(server)); when(databaseConfigService.register(any(DatabaseConfig.class), eq(false))) .thenAnswer((Answer<DatabaseConfig>) invocation -> { return invocation.getArgument(0, DatabaseConfig.class); }); when(userGeneratorService.generateUserName()).thenReturn(USERNAME); when(passwordGeneratorService.generatePassword(any())).thenReturn(PASSWORD); server.setDatabaseVendor(DatabaseVendor.POSTGRES); server.setHost("myhost"); server.setPort(5432); server.setConnectionUserName("root"); server.setConnectionPassword("rootpassword"); String databaseName = "mydb"; String databaseType = "hive"; Optional<String> databaseDescription = Optional.of("mine not yours"); String result = underTest.createDatabaseOnServer(SERVER_CRN.toString(), databaseName, databaseType, databaseDescription); assertEquals("created", result); verify(driverFunctions).execWithDatabaseDriver(eq(server), any()); ArgumentCaptor<DatabaseConfig> captor = ArgumentCaptor.forClass(DatabaseConfig.class); verify(databaseConfigService).register(captor.capture(), eq(false)); DatabaseConfig db = captor.getValue(); assertEquals(databaseName, db.getName()); assertEquals(databaseType, db.getType()); assertEquals(databaseDescription.get(), db.getDescription()); String databaseUserName = db.getConnectionUserName().getRaw(); assertEquals(USERNAME, databaseUserName); assertNotEquals(server.getConnectionUserName(), databaseUserName); assertEquals(PASSWORD, db.getConnectionPassword().getRaw()); }
public String createDatabaseOnServer(String serverCrn, String databaseName, String databaseType, Optional<String> databaseDescription) { 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); 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 ); 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); } }); DatabaseConfig newDatabaseConfig = databaseServerConfig.createDatabaseConfig(databaseName, databaseType, databaseDescription, ResourceStatus.SERVICE_MANAGED, databaseUserName, databasePassword); databaseConfigService.register(newDatabaseConfig, false); return "created"; }
DatabaseServerConfigService extends AbstractArchivistService<DatabaseServerConfig> implements ResourceBasedCrnProvider { public String createDatabaseOnServer(String serverCrn, String databaseName, String databaseType, Optional<String> databaseDescription) { 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); 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 ); 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); } }); DatabaseConfig newDatabaseConfig = databaseServerConfig.createDatabaseConfig(databaseName, databaseType, databaseDescription, ResourceStatus.SERVICE_MANAGED, databaseUserName, databasePassword); databaseConfigService.register(newDatabaseConfig, false); return "created"; } }
DatabaseServerConfigService extends AbstractArchivistService<DatabaseServerConfig> implements ResourceBasedCrnProvider { public String createDatabaseOnServer(String serverCrn, String databaseName, String databaseType, Optional<String> databaseDescription) { 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); 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 ); 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); } }); DatabaseConfig newDatabaseConfig = databaseServerConfig.createDatabaseConfig(databaseName, databaseType, databaseDescription, ResourceStatus.SERVICE_MANAGED, databaseUserName, databasePassword); databaseConfigService.register(newDatabaseConfig, false); return "created"; }  }
DatabaseServerConfigService extends AbstractArchivistService<DatabaseServerConfig> implements ResourceBasedCrnProvider { public String createDatabaseOnServer(String serverCrn, String databaseName, String databaseType, Optional<String> databaseDescription) { 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); 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 ); 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); } }); DatabaseConfig newDatabaseConfig = databaseServerConfig.createDatabaseConfig(databaseName, databaseType, databaseDescription, ResourceStatus.SERVICE_MANAGED, databaseUserName, databasePassword); databaseConfigService.register(newDatabaseConfig, false); return "created"; }  Set<DatabaseServerConfig> findAll(Long workspaceId, String environmentCrn); DatabaseServerConfig create(DatabaseServerConfig resource, Long workspaceId, boolean test); DatabaseServerConfig update(DatabaseServerConfig resource); DatabaseServerConfig release(String resourceCrn); @Override DatabaseServerConfig delete(DatabaseServerConfig resource); DatabaseServerConfig getByName(Long workspaceId, String environmentCrn, String name); DatabaseServerConfig getByCrn(String resourceCrn); Optional<DatabaseServerConfig> getByCrn(Crn resourceCrn); Optional<DatabaseServerConfig> getByClusterCrn(String clusterCrn); DatabaseServerConfig deleteByCrn(String crn); @Override JpaRepository repository(); Set<DatabaseServerConfig> getByCrns(Set<String> crns); String testConnection(String crn); String testConnection(DatabaseServerConfig resource); String createDatabaseOnServer(String serverCrn, String databaseName, String databaseType, Optional<String> databaseDescription); @Override AuthorizationResourceType getResourceType(); @Override String getResourceCrnByResourceName(String resourceName); @Override List<String> getResourceCrnListByResourceNameList(List<String> resourceNames); @Override List<String> getResourceCrnsInAccount(); }
DatabaseServerConfigService extends AbstractArchivistService<DatabaseServerConfig> implements ResourceBasedCrnProvider { public String createDatabaseOnServer(String serverCrn, String databaseName, String databaseType, Optional<String> databaseDescription) { 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); 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 ); 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); } }); DatabaseConfig newDatabaseConfig = databaseServerConfig.createDatabaseConfig(databaseName, databaseType, databaseDescription, ResourceStatus.SERVICE_MANAGED, databaseUserName, databasePassword); databaseConfigService.register(newDatabaseConfig, false); return "created"; }  Set<DatabaseServerConfig> findAll(Long workspaceId, String environmentCrn); DatabaseServerConfig create(DatabaseServerConfig resource, Long workspaceId, boolean test); DatabaseServerConfig update(DatabaseServerConfig resource); DatabaseServerConfig release(String resourceCrn); @Override DatabaseServerConfig delete(DatabaseServerConfig resource); DatabaseServerConfig getByName(Long workspaceId, String environmentCrn, String name); DatabaseServerConfig getByCrn(String resourceCrn); Optional<DatabaseServerConfig> getByCrn(Crn resourceCrn); Optional<DatabaseServerConfig> getByClusterCrn(String clusterCrn); DatabaseServerConfig deleteByCrn(String crn); @Override JpaRepository repository(); Set<DatabaseServerConfig> getByCrns(Set<String> crns); String testConnection(String crn); String testConnection(DatabaseServerConfig resource); String createDatabaseOnServer(String serverCrn, String databaseName, String databaseType, Optional<String> databaseDescription); @Override AuthorizationResourceType getResourceType(); @Override String getResourceCrnByResourceName(String resourceName); @Override List<String> getResourceCrnListByResourceNameList(List<String> resourceNames); @Override List<String> getResourceCrnsInAccount();  }