@Test public void testCreateAlreadyExists() { thrown.expect(BadRequestException.class); when(repository.findByName(anyString())).thenReturn(Optional.of(server)); underTest.create(server, 0L, false); }
public DatabaseServerConfig create(DatabaseServerConfig resource, Long workspaceId, boolean test) { if (repository.findByName(resource.getName()).isPresent()) { throw new BadRequestException(String.format("%s already exists with name '%s' in workspace %d", DatabaseServerConfig.class.getSimpleName(), resource.getName(), resource.getWorkspaceId())); } if (resource.getConnectionDriver() == null) { resource.setConnectionDriver(resource.getDatabaseVendor().connectionDriver()); LOGGER.info("Database server configuration lacked a connection driver; defaulting to {}", resource.getConnectionDriver()); } if (test) { String testResults = testConnection(resource); if (!testResults.equals(DATABASE_TEST_RESULT_SUCCESS)) { throw new IllegalArgumentException(testResults); } } try { MDCBuilder.buildMdcContext(resource); resource.setCreationDate(clock.getCurrentTimeMillis()); if (resource.getResourceCrn() == null) { Crn crn = crnService.createCrn(resource); resource.setResourceCrn(crn); resource.setAccountId(crn.getAccountId()); } resource.setWorkspaceId(workspaceId); return transactionService.required(() -> { DatabaseServerConfig saved = repository.save(resource); grpcUmsClient.assignResourceOwnerRoleIfEntitled(ThreadBasedUserCrnProvider.getUserCrn(), saved.getResourceCrn().toString(), ThreadBasedUserCrnProvider.getAccountId()); return saved; }); } catch (TransactionService.TransactionExecutionException e) { LOGGER.error("Error happened during database server creation: ", e); throw new InternalServerErrorException(e); } }
DatabaseServerConfigService extends AbstractArchivistService<DatabaseServerConfig> implements ResourceBasedCrnProvider { public DatabaseServerConfig create(DatabaseServerConfig resource, Long workspaceId, boolean test) { if (repository.findByName(resource.getName()).isPresent()) { throw new BadRequestException(String.format("%s already exists with name '%s' in workspace %d", DatabaseServerConfig.class.getSimpleName(), resource.getName(), resource.getWorkspaceId())); } if (resource.getConnectionDriver() == null) { resource.setConnectionDriver(resource.getDatabaseVendor().connectionDriver()); LOGGER.info("Database server configuration lacked a connection driver; defaulting to {}", resource.getConnectionDriver()); } if (test) { String testResults = testConnection(resource); if (!testResults.equals(DATABASE_TEST_RESULT_SUCCESS)) { throw new IllegalArgumentException(testResults); } } try { MDCBuilder.buildMdcContext(resource); resource.setCreationDate(clock.getCurrentTimeMillis()); if (resource.getResourceCrn() == null) { Crn crn = crnService.createCrn(resource); resource.setResourceCrn(crn); resource.setAccountId(crn.getAccountId()); } resource.setWorkspaceId(workspaceId); return transactionService.required(() -> { DatabaseServerConfig saved = repository.save(resource); grpcUmsClient.assignResourceOwnerRoleIfEntitled(ThreadBasedUserCrnProvider.getUserCrn(), saved.getResourceCrn().toString(), ThreadBasedUserCrnProvider.getAccountId()); return saved; }); } catch (TransactionService.TransactionExecutionException e) { LOGGER.error("Error happened during database server creation: ", e); throw new InternalServerErrorException(e); } } }
DatabaseServerConfigService extends AbstractArchivistService<DatabaseServerConfig> implements ResourceBasedCrnProvider { public DatabaseServerConfig create(DatabaseServerConfig resource, Long workspaceId, boolean test) { if (repository.findByName(resource.getName()).isPresent()) { throw new BadRequestException(String.format("%s already exists with name '%s' in workspace %d", DatabaseServerConfig.class.getSimpleName(), resource.getName(), resource.getWorkspaceId())); } if (resource.getConnectionDriver() == null) { resource.setConnectionDriver(resource.getDatabaseVendor().connectionDriver()); LOGGER.info("Database server configuration lacked a connection driver; defaulting to {}", resource.getConnectionDriver()); } if (test) { String testResults = testConnection(resource); if (!testResults.equals(DATABASE_TEST_RESULT_SUCCESS)) { throw new IllegalArgumentException(testResults); } } try { MDCBuilder.buildMdcContext(resource); resource.setCreationDate(clock.getCurrentTimeMillis()); if (resource.getResourceCrn() == null) { Crn crn = crnService.createCrn(resource); resource.setResourceCrn(crn); resource.setAccountId(crn.getAccountId()); } resource.setWorkspaceId(workspaceId); return transactionService.required(() -> { DatabaseServerConfig saved = repository.save(resource); grpcUmsClient.assignResourceOwnerRoleIfEntitled(ThreadBasedUserCrnProvider.getUserCrn(), saved.getResourceCrn().toString(), ThreadBasedUserCrnProvider.getAccountId()); return saved; }); } catch (TransactionService.TransactionExecutionException e) { LOGGER.error("Error happened during database server creation: ", e); throw new InternalServerErrorException(e); } }  }
DatabaseServerConfigService extends AbstractArchivistService<DatabaseServerConfig> implements ResourceBasedCrnProvider { public DatabaseServerConfig create(DatabaseServerConfig resource, Long workspaceId, boolean test) { if (repository.findByName(resource.getName()).isPresent()) { throw new BadRequestException(String.format("%s already exists with name '%s' in workspace %d", DatabaseServerConfig.class.getSimpleName(), resource.getName(), resource.getWorkspaceId())); } if (resource.getConnectionDriver() == null) { resource.setConnectionDriver(resource.getDatabaseVendor().connectionDriver()); LOGGER.info("Database server configuration lacked a connection driver; defaulting to {}", resource.getConnectionDriver()); } if (test) { String testResults = testConnection(resource); if (!testResults.equals(DATABASE_TEST_RESULT_SUCCESS)) { throw new IllegalArgumentException(testResults); } } try { MDCBuilder.buildMdcContext(resource); resource.setCreationDate(clock.getCurrentTimeMillis()); if (resource.getResourceCrn() == null) { Crn crn = crnService.createCrn(resource); resource.setResourceCrn(crn); resource.setAccountId(crn.getAccountId()); } resource.setWorkspaceId(workspaceId); return transactionService.required(() -> { DatabaseServerConfig saved = repository.save(resource); grpcUmsClient.assignResourceOwnerRoleIfEntitled(ThreadBasedUserCrnProvider.getUserCrn(), saved.getResourceCrn().toString(), ThreadBasedUserCrnProvider.getAccountId()); return saved; }); } catch (TransactionService.TransactionExecutionException e) { LOGGER.error("Error happened during database server creation: ", e); throw new InternalServerErrorException(e); } }  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 DatabaseServerConfig create(DatabaseServerConfig resource, Long workspaceId, boolean test) { if (repository.findByName(resource.getName()).isPresent()) { throw new BadRequestException(String.format("%s already exists with name '%s' in workspace %d", DatabaseServerConfig.class.getSimpleName(), resource.getName(), resource.getWorkspaceId())); } if (resource.getConnectionDriver() == null) { resource.setConnectionDriver(resource.getDatabaseVendor().connectionDriver()); LOGGER.info("Database server configuration lacked a connection driver; defaulting to {}", resource.getConnectionDriver()); } if (test) { String testResults = testConnection(resource); if (!testResults.equals(DATABASE_TEST_RESULT_SUCCESS)) { throw new IllegalArgumentException(testResults); } } try { MDCBuilder.buildMdcContext(resource); resource.setCreationDate(clock.getCurrentTimeMillis()); if (resource.getResourceCrn() == null) { Crn crn = crnService.createCrn(resource); resource.setResourceCrn(crn); resource.setAccountId(crn.getAccountId()); } resource.setWorkspaceId(workspaceId); return transactionService.required(() -> { DatabaseServerConfig saved = repository.save(resource); grpcUmsClient.assignResourceOwnerRoleIfEntitled(ThreadBasedUserCrnProvider.getUserCrn(), saved.getResourceCrn().toString(), ThreadBasedUserCrnProvider.getAccountId()); return saved; }); } catch (TransactionService.TransactionExecutionException e) { LOGGER.error("Error happened during database server creation: ", e); throw new InternalServerErrorException(e); } }  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();  }