mirror of
https://github.com/dbeaver/cloudbeaver.git
synced 2026-09-24 16:04:36 +08:00
CB-219 Connection create/update/delete API implementation
This commit is contained in:
@@ -16,22 +16,27 @@
|
||||
*/
|
||||
package io.cloudbeaver;
|
||||
|
||||
import graphql.schema.idl.SchemaParser;
|
||||
import graphql.schema.idl.TypeDefinitionRegistry;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import io.cloudbeaver.model.WebConnectionConfig;
|
||||
import org.jkiss.code.NotNull;
|
||||
import org.jkiss.dbeaver.Log;
|
||||
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
|
||||
import org.jkiss.dbeaver.model.DBPImage;
|
||||
import org.jkiss.dbeaver.model.app.DBPDataSourceRegistry;
|
||||
import org.jkiss.dbeaver.model.auth.DBAAuthCredentials;
|
||||
import org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration;
|
||||
import org.jkiss.dbeaver.model.connection.DBPDriver;
|
||||
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
|
||||
import org.jkiss.dbeaver.registry.DataSourceNavigatorSettings;
|
||||
import org.jkiss.dbeaver.registry.DataSourceProviderDescriptor;
|
||||
import org.jkiss.dbeaver.registry.DataSourceProviderRegistry;
|
||||
import org.jkiss.dbeaver.registry.driver.DriverDescriptor;
|
||||
import org.jkiss.dbeaver.runtime.DBWorkbench;
|
||||
import org.jkiss.utils.CommonUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Various constants
|
||||
@@ -40,6 +45,8 @@ public class WebServiceUtils {
|
||||
|
||||
private static final Log log = Log.getLog(WebServiceUtils.class);
|
||||
|
||||
private static final Gson gson = new GsonBuilder().create();
|
||||
|
||||
public static String makeIconId(DBPImage icon) {
|
||||
return icon.getLocation();
|
||||
}
|
||||
@@ -80,4 +87,80 @@ public class WebServiceUtils {
|
||||
return WebServiceUtils.class.getClassLoader().getResourceAsStream(path);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static DBPDataSourceContainer createConnectionFromConfig(WebConnectionConfig config, DBPDataSourceRegistry registry) throws DBWebException {
|
||||
DBPDataSourceContainer newDataSource;
|
||||
if (!CommonUtils.isEmpty(config.getTemplateId())) {
|
||||
DBPDataSourceContainer tpl = registry.getDataSource(config.getTemplateId());
|
||||
if (tpl == null) {
|
||||
throw new DBWebException("Template connection '" + config.getTemplateId() + "' not found");
|
||||
}
|
||||
newDataSource = registry.createDataSource(tpl);
|
||||
} else if (!CommonUtils.isEmpty(config.getDriverId())) {
|
||||
String driverId = config.getDriverId();
|
||||
if (CommonUtils.isEmpty(driverId)) {
|
||||
throw new DBWebException("Driver not specified");
|
||||
}
|
||||
DBPDriver driver = getDriverById(driverId);
|
||||
|
||||
DBPConnectionConfiguration dsConfig = new DBPConnectionConfiguration();
|
||||
|
||||
setConnectionConfiguration(driver, dsConfig, config);
|
||||
|
||||
newDataSource = registry.createDataSource(driver, dsConfig);
|
||||
} else {
|
||||
throw new DBWebException("Template connection or driver must be specified");
|
||||
}
|
||||
|
||||
newDataSource.setSavePassword(true);
|
||||
newDataSource.setName(config.getName());
|
||||
newDataSource.setDescription(config.getDescription());
|
||||
|
||||
// Set default navigator settings
|
||||
DataSourceNavigatorSettings navSettings = new DataSourceNavigatorSettings(newDataSource.getNavigatorSettings());
|
||||
navSettings.setShowSystemObjects(false);
|
||||
((DataSourceDescriptor)newDataSource).setNavigatorSettings(navSettings);
|
||||
|
||||
initAuthProperties(newDataSource, config.getCredentials());
|
||||
|
||||
return newDataSource;
|
||||
}
|
||||
|
||||
private static void setConnectionConfiguration(DBPDriver driver, DBPConnectionConfiguration dsConfig, WebConnectionConfig config) {
|
||||
if (!CommonUtils.isEmpty(config.getUrl())) {
|
||||
dsConfig.setUrl(config.getUrl());
|
||||
} else {
|
||||
dsConfig.setHostName(config.getHost());
|
||||
dsConfig.setHostPort(config.getPort());
|
||||
dsConfig.setDatabaseName(config.getDatabaseName());
|
||||
dsConfig.setServerName(config.getServerName());
|
||||
dsConfig.setUrl(driver.getDataSourceProvider().getConnectionURL(driver, dsConfig));
|
||||
}
|
||||
if (config.getProperties() != null) {
|
||||
for (Map.Entry<String, Object> pe : config.getProperties().entrySet()) {
|
||||
dsConfig.setProperty(pe.getKey(), CommonUtils.toString(pe.getValue()));
|
||||
}
|
||||
}
|
||||
dsConfig.setUserName(config.getUserName());
|
||||
dsConfig.setUserPassword(config.getUserPassword());
|
||||
}
|
||||
|
||||
public static void initAuthProperties(DBPDataSourceContainer dataSourceContainer, Map<String, Object> authProperties) {
|
||||
if (!CommonUtils.isEmpty(authProperties)) {
|
||||
DBPConnectionConfiguration configuration = dataSourceContainer.getConnectionConfiguration();
|
||||
//DBPAuthModelDescriptor authModelDescriptor = configuration.getAuthModelDescriptor();
|
||||
DBAAuthCredentials credentials = configuration.getAuthModel().loadCredentials(dataSourceContainer, configuration);
|
||||
|
||||
credentials = gson.fromJson(gson.toJsonTree(authProperties), credentials.getClass());
|
||||
|
||||
configuration.getAuthModel().saveCredentials(dataSourceContainer, configuration, credentials);
|
||||
}
|
||||
}
|
||||
|
||||
public static void updateConnectionFromConfig(DBPDataSourceContainer dataSource, WebConnectionConfig config) {
|
||||
setConnectionConfiguration(dataSource.getDriver(), dataSource.getConnectionConfiguration(), config);
|
||||
dataSource.setName(config.getName());
|
||||
dataSource.setDescription(config.getDescription());
|
||||
initAuthProperties(dataSource, config.getCredentials());
|
||||
}
|
||||
}
|
||||
|
||||
+3
-63
@@ -17,8 +17,6 @@
|
||||
package io.cloudbeaver.service.core.impl;
|
||||
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import io.cloudbeaver.DBWebException;
|
||||
import io.cloudbeaver.WebServiceUtils;
|
||||
import io.cloudbeaver.model.*;
|
||||
@@ -31,7 +29,6 @@ import org.jkiss.dbeaver.DBException;
|
||||
import org.jkiss.dbeaver.Log;
|
||||
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
|
||||
import org.jkiss.dbeaver.model.app.DBPDataSourceRegistry;
|
||||
import org.jkiss.dbeaver.model.auth.DBAAuthCredentials;
|
||||
import org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration;
|
||||
import org.jkiss.dbeaver.model.connection.DBPDriver;
|
||||
import org.jkiss.dbeaver.model.data.json.JSONUtils;
|
||||
@@ -39,7 +36,6 @@ import org.jkiss.dbeaver.model.exec.DBCException;
|
||||
import org.jkiss.dbeaver.model.impl.auth.AuthModelDatabaseNative;
|
||||
import org.jkiss.dbeaver.model.navigator.DBNBrowseSettings;
|
||||
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
|
||||
import org.jkiss.dbeaver.registry.DataSourceNavigatorSettings;
|
||||
import org.jkiss.dbeaver.registry.DataSourceProviderRegistry;
|
||||
import org.jkiss.dbeaver.runtime.jobs.ConnectionTestJob;
|
||||
import org.jkiss.dbeaver.utils.RuntimeUtils;
|
||||
@@ -58,8 +54,6 @@ public class WebServiceCore implements DBWServiceCore {
|
||||
|
||||
private static final Log log = Log.getLog(WebServiceCore.class);
|
||||
|
||||
private static final Gson gson = new GsonBuilder().create();
|
||||
|
||||
@Override
|
||||
public WebServerConfig getServerConfig() {
|
||||
return new WebServerConfig(CBApplication.getInstance());
|
||||
@@ -214,7 +208,7 @@ public class WebServiceCore implements DBWServiceCore {
|
||||
throw new DBWebException("Datasource '" + dataSourceContainer.getName() + "' is already connected");
|
||||
}
|
||||
|
||||
initAuthProperties(dataSourceContainer, authProperties);
|
||||
WebServiceUtils.initAuthProperties(dataSourceContainer, authProperties);
|
||||
|
||||
boolean oldSavePassword = dataSourceContainer.isSavePassword();
|
||||
try {
|
||||
@@ -234,7 +228,7 @@ public class WebServiceCore implements DBWServiceCore {
|
||||
public WebConnectionInfo createConnection(WebSession webSession, WebConnectionConfig connectionConfig) throws DBWebException {
|
||||
DBPDataSourceRegistry sessionRegistry = webSession.getDatabasesNode().getDataSourceRegistry();
|
||||
|
||||
DBPDataSourceContainer newDataSource = makeConnectionInstance(connectionConfig, sessionRegistry);
|
||||
DBPDataSourceContainer newDataSource = WebServiceUtils.createConnectionFromConfig(connectionConfig, sessionRegistry);
|
||||
if (CommonUtils.isEmpty(newDataSource.getName())) {
|
||||
newDataSource.setName(CommonUtils.notNull(connectionConfig.getName(), "NewConnection"));
|
||||
}
|
||||
@@ -251,7 +245,7 @@ public class WebServiceCore implements DBWServiceCore {
|
||||
public WebConnectionInfo testConnection(WebSession webSession, WebConnectionConfig connectionConfig) throws DBWebException {
|
||||
DBPDataSourceRegistry sessionRegistry = webSession.getDatabasesNode().getDataSourceRegistry();
|
||||
|
||||
DBPDataSourceContainer newDataSource = makeConnectionInstance(connectionConfig, sessionRegistry);
|
||||
DBPDataSourceContainer newDataSource = WebServiceUtils.createConnectionFromConfig(connectionConfig, sessionRegistry);
|
||||
try {
|
||||
ConnectionTestJob ct = new ConnectionTestJob(newDataSource, param -> {});
|
||||
ct.run(webSession.getProgressMonitor());
|
||||
@@ -280,18 +274,6 @@ public class WebServiceCore implements DBWServiceCore {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void initAuthProperties(DBPDataSourceContainer dataSourceContainer, Map<String, Object> authProperties) {
|
||||
if (!CommonUtils.isEmpty(authProperties)) {
|
||||
DBPConnectionConfiguration configuration = dataSourceContainer.getConnectionConfiguration();
|
||||
//DBPAuthModelDescriptor authModelDescriptor = configuration.getAuthModelDescriptor();
|
||||
DBAAuthCredentials credentials = configuration.getAuthModel().loadCredentials(dataSourceContainer, configuration);
|
||||
|
||||
credentials = gson.fromJson(gson.toJsonTree(authProperties), credentials.getClass());
|
||||
|
||||
configuration.getAuthModel().saveCredentials(dataSourceContainer, configuration, credentials);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private WebConnectionInfo closeAndDeleteConnection(WebSession webSession, String connectionId, boolean forceDelete) throws DBWebException {
|
||||
WebConnectionInfo connectionInfo = webSession.getWebConnectionInfo(connectionId);
|
||||
@@ -329,48 +311,6 @@ public class WebServiceCore implements DBWServiceCore {
|
||||
return true;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private DBPDataSourceContainer makeConnectionInstance(WebConnectionConfig config, DBPDataSourceRegistry sessionRegistry) throws DBWebException {
|
||||
String driverId = config.getDriverId();
|
||||
if (CommonUtils.isEmpty(driverId)) {
|
||||
throw new DBWebException("Driver not specified");
|
||||
}
|
||||
DBPDriver driver = WebServiceUtils.getDriverById(driverId);
|
||||
|
||||
DBPConnectionConfiguration dsConfig = new DBPConnectionConfiguration();
|
||||
|
||||
DBPDataSourceContainer newDataSource = sessionRegistry.createDataSource(driver, dsConfig);
|
||||
newDataSource.setSavePassword(true);
|
||||
|
||||
if (!CommonUtils.isEmpty(config.getUrl())) {
|
||||
dsConfig.setUrl(config.getUrl());
|
||||
} else {
|
||||
dsConfig.setHostName(config.getHost());
|
||||
dsConfig.setHostPort(config.getPort());
|
||||
dsConfig.setDatabaseName(config.getDatabaseName());
|
||||
dsConfig.setServerName(config.getServerName());
|
||||
dsConfig.setUrl(driver.getDataSourceProvider().getConnectionURL(driver, dsConfig));
|
||||
}
|
||||
if (config.getProperties() != null) {
|
||||
for (Map.Entry<String, Object> pe : config.getProperties().entrySet()) {
|
||||
dsConfig.setProperty(pe.getKey(), CommonUtils.toString(pe.getValue()));
|
||||
}
|
||||
}
|
||||
dsConfig.setUserName(config.getUserName());
|
||||
dsConfig.setUserPassword(config.getUserPassword());
|
||||
newDataSource.setName(config.getName());
|
||||
newDataSource.setDescription(config.getDescription());
|
||||
|
||||
// Set default navigator settings
|
||||
DataSourceNavigatorSettings navSettings = new DataSourceNavigatorSettings(newDataSource.getNavigatorSettings());
|
||||
navSettings.setShowSystemObjects(false);
|
||||
((DataSourceDescriptor)newDataSource).setNavigatorSettings(navSettings);
|
||||
|
||||
initAuthProperties(newDataSource, config.getCredentials());
|
||||
|
||||
return newDataSource;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public WebAsyncTaskInfo getAsyncTaskStatus(WebSession webSession, String taskId) throws DBWebException {
|
||||
|
||||
+30
-5
@@ -17,6 +17,7 @@
|
||||
package io.cloudbeaver.service.admin.impl;
|
||||
|
||||
import io.cloudbeaver.DBWebException;
|
||||
import io.cloudbeaver.WebServiceUtils;
|
||||
import io.cloudbeaver.auth.provider.local.LocalAuthProvider;
|
||||
import io.cloudbeaver.model.WebConnectionConfig;
|
||||
import io.cloudbeaver.model.WebConnectionInfo;
|
||||
@@ -33,6 +34,7 @@ import io.cloudbeaver.service.admin.AdminRoleInfo;
|
||||
import io.cloudbeaver.service.admin.AdminUserInfo;
|
||||
import io.cloudbeaver.service.admin.DBWServiceAdmin;
|
||||
import org.jkiss.code.NotNull;
|
||||
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
|
||||
import org.jkiss.utils.ArrayUtils;
|
||||
import org.jkiss.utils.CommonUtils;
|
||||
|
||||
@@ -226,25 +228,48 @@ public class WebServiceAdmin implements DBWServiceAdmin {
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Connection management
|
||||
|
||||
|
||||
@Override
|
||||
public List<WebConnectionInfo> getAllConnections(@NotNull WebSession webSession) throws DBWebException {
|
||||
throw new DBWebException("Not supported yet");
|
||||
// Get all connections from global configuration
|
||||
List<WebConnectionInfo> result = new ArrayList<>();
|
||||
for (DBPDataSourceContainer ds : WebServiceUtils.getDataSourceRegistry().getDataSources()) {
|
||||
if (CBPlatform.getInstance().getApplicableDrivers().contains(ds.getDriver())) {
|
||||
result.add(new WebConnectionInfo(webSession, ds));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebConnectionInfo createConnectionConfiguration(@NotNull WebSession webSession, @NotNull WebConnectionConfig config) throws DBWebException {
|
||||
throw new DBWebException("Not supported yet");
|
||||
DBPDataSourceContainer dataSource = WebServiceUtils.createConnectionFromConfig(config, WebServiceUtils.getDataSourceRegistry());
|
||||
|
||||
WebServiceUtils.getDataSourceRegistry().flushConfig();
|
||||
|
||||
return new WebConnectionInfo(webSession, dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebConnectionInfo updateConnectionConfiguration(@NotNull WebSession webSession, @NotNull String id, @NotNull WebConnectionConfig config) throws DBWebException {
|
||||
throw new DBWebException("Not supported yet");
|
||||
DBPDataSourceContainer dataSource = WebServiceUtils.getDataSourceRegistry().getDataSource(id);
|
||||
if (dataSource == null) {
|
||||
throw new DBWebException("Connection '" + id + "' not found");
|
||||
}
|
||||
WebServiceUtils.updateConnectionFromConfig(dataSource, config);
|
||||
dataSource.persistConfiguration();
|
||||
return new WebConnectionInfo(webSession, dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteConnectionConfiguration(@NotNull WebSession webSession, @NotNull String id) throws DBWebException {
|
||||
throw new DBWebException("Not supported yet");
|
||||
DBPDataSourceContainer dataSource = WebServiceUtils.getDataSourceRegistry().getDataSource(id);
|
||||
if (dataSource == null) {
|
||||
throw new DBWebException("Connection '" + id + "' not found");
|
||||
}
|
||||
WebServiceUtils.getDataSourceRegistry().removeDataSource(dataSource);
|
||||
WebServiceUtils.getDataSourceRegistry().flushConfig();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user