Repository: metamodel-membrane Updated Branches: refs/heads/master 364686d8f -> 516a05397
METAMODEL-1153: Added endpoint for DELETE data source Project: http://git-wip-us.apache.org/repos/asf/metamodel-membrane/repo Commit: http://git-wip-us.apache.org/repos/asf/metamodel-membrane/commit/8e8a90c6 Tree: http://git-wip-us.apache.org/repos/asf/metamodel-membrane/tree/8e8a90c6 Diff: http://git-wip-us.apache.org/repos/asf/metamodel-membrane/diff/8e8a90c6 Branch: refs/heads/master Commit: 8e8a90c651a6307cd3480f953ac793e766677556 Parents: bb7a124 Author: Kasper Sørensen <i.am.kasper.soren...@gmail.com> Authored: Mon Aug 21 22:20:47 2017 -0700 Committer: Kasper Sørensen <i.am.kasper.soren...@gmail.com> Committed: Mon Aug 21 22:20:47 2017 -0700 ---------------------------------------------------------------------- CHANGES.md | 1 + .../app/CachedDataSourceRegistryWrapper.java | 5 ++++ .../membrane/app/DataSourceRegistry.java | 4 ++- .../app/InMemoryDataSourceRegistry.java | 8 ++++++ .../file/FileBasedDataSourceRegistry.java | 20 ++++++++++++-- .../controllers/DataSourceController.java | 29 +++++++++++++++++--- core/src/main/resources/swagger.yaml | 22 +++++++++++++++ 7 files changed, 82 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/metamodel-membrane/blob/8e8a90c6/CHANGES.md ---------------------------------------------------------------------- diff --git a/CHANGES.md b/CHANGES.md index 87234d5..78c1e34 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,6 @@ ### Apache MetaModel Membrane 0.1 [work in progress] +* [METAMODEL-1153] - Added endpoints for deleting data sources. * [METAMODEL-1147] - Implemented Swagger codegen to build model classes from swagger file. * [METAMODEL-1149] - File based registry for tenant and data source information. * [METAMODEL-1154] - Added update and delete capabilities in POST call to table data endpoint. http://git-wip-us.apache.org/repos/asf/metamodel-membrane/blob/8e8a90c6/core/src/main/java/org/apache/metamodel/membrane/app/CachedDataSourceRegistryWrapper.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/metamodel/membrane/app/CachedDataSourceRegistryWrapper.java b/core/src/main/java/org/apache/metamodel/membrane/app/CachedDataSourceRegistryWrapper.java index 77fbb77..96c9e80 100644 --- a/core/src/main/java/org/apache/metamodel/membrane/app/CachedDataSourceRegistryWrapper.java +++ b/core/src/main/java/org/apache/metamodel/membrane/app/CachedDataSourceRegistryWrapper.java @@ -107,4 +107,9 @@ public class CachedDataSourceRegistryWrapper implements DataSourceRegistry { } } + @Override + public void removeDataSource(String dataSourceName) throws NoSuchDataSourceException { + delegate.removeDataSource(dataSourceName); + loadingCache.invalidate(dataSourceName); + } } http://git-wip-us.apache.org/repos/asf/metamodel-membrane/blob/8e8a90c6/core/src/main/java/org/apache/metamodel/membrane/app/DataSourceRegistry.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/metamodel/membrane/app/DataSourceRegistry.java b/core/src/main/java/org/apache/metamodel/membrane/app/DataSourceRegistry.java index b4678c0..53ba51d 100644 --- a/core/src/main/java/org/apache/metamodel/membrane/app/DataSourceRegistry.java +++ b/core/src/main/java/org/apache/metamodel/membrane/app/DataSourceRegistry.java @@ -36,6 +36,8 @@ public interface DataSourceRegistry { public String registerDataSource(String dataSourceName, DataContextProperties dataContextProperties) throws DataSourceAlreadyExistException; + public void removeDataSource(String dataSourceName) throws NoSuchDataSourceException; + public DataContext openDataContext(String dataSourceName) throws NoSuchDataSourceException; public default UpdateableDataContext openDataContextForUpdate(String dataSourceName) { @@ -44,5 +46,5 @@ public interface DataSourceRegistry { return (UpdateableDataContext) dataContext; } throw new DataSourceNotUpdateableException(dataSourceName); - }; + } } http://git-wip-us.apache.org/repos/asf/metamodel-membrane/blob/8e8a90c6/core/src/main/java/org/apache/metamodel/membrane/app/InMemoryDataSourceRegistry.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/metamodel/membrane/app/InMemoryDataSourceRegistry.java b/core/src/main/java/org/apache/metamodel/membrane/app/InMemoryDataSourceRegistry.java index 1272cee..573dfd7 100644 --- a/core/src/main/java/org/apache/metamodel/membrane/app/InMemoryDataSourceRegistry.java +++ b/core/src/main/java/org/apache/metamodel/membrane/app/InMemoryDataSourceRegistry.java @@ -62,4 +62,12 @@ public class InMemoryDataSourceRegistry implements DataSourceRegistry { return supplier.get(); } + @Override + public void removeDataSource(String dataSourceName) throws NoSuchDataSourceException { + if (!dataSources.containsKey(dataSourceName)) { + throw new NoSuchDataSourceException(dataSourceName); + } + dataSources.remove(dataSourceName); + } + } http://git-wip-us.apache.org/repos/asf/metamodel-membrane/blob/8e8a90c6/core/src/main/java/org/apache/metamodel/membrane/app/registry/file/FileBasedDataSourceRegistry.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/metamodel/membrane/app/registry/file/FileBasedDataSourceRegistry.java b/core/src/main/java/org/apache/metamodel/membrane/app/registry/file/FileBasedDataSourceRegistry.java index 74d1d3a..ba1506c 100644 --- a/core/src/main/java/org/apache/metamodel/membrane/app/registry/file/FileBasedDataSourceRegistry.java +++ b/core/src/main/java/org/apache/metamodel/membrane/app/registry/file/FileBasedDataSourceRegistry.java @@ -114,9 +114,25 @@ public class FileBasedDataSourceRegistry implements DataSourceRegistry { throw new UncheckedIOException(e); } - final DataContextSupplier supplier = new DataContextSupplier(dataSourceName, dataSource - .toDataContextProperties()); + final DataContextSupplier supplier = + new DataContextSupplier(dataSourceName, dataSource.toDataContextProperties()); return supplier.get(); } + @Override + public void removeDataSource(String dataSourceName) throws NoSuchDataSourceException { + if (Strings.isNullOrEmpty(dataSourceName)) { + throw new IllegalArgumentException("DataSource name cannot be null or empty"); + } + final File file = getDataSourceFile(dataSourceName); + if (!file.exists()) { + throw new NoSuchDataSourceException(dataSourceName); + } + + final boolean deleted = file.delete(); + if (!deleted) { + throw new UncheckedIOException(new IOException("Unable to delete file: " + file)); + } + } + } http://git-wip-us.apache.org/repos/asf/metamodel-membrane/blob/8e8a90c6/core/src/main/java/org/apache/metamodel/membrane/controllers/DataSourceController.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/metamodel/membrane/controllers/DataSourceController.java b/core/src/main/java/org/apache/metamodel/membrane/controllers/DataSourceController.java index 194bbe4..ef1fe67 100644 --- a/core/src/main/java/org/apache/metamodel/membrane/controllers/DataSourceController.java +++ b/core/src/main/java/org/apache/metamodel/membrane/controllers/DataSourceController.java @@ -30,11 +30,15 @@ import org.apache.metamodel.DataContext; import org.apache.metamodel.UpdateableDataContext; import org.apache.metamodel.factory.DataContextProperties; import org.apache.metamodel.factory.DataContextPropertiesImpl; +import org.apache.metamodel.membrane.app.DataSourceRegistry; import org.apache.metamodel.membrane.app.TenantContext; import org.apache.metamodel.membrane.app.TenantRegistry; import org.apache.metamodel.membrane.controllers.model.RestDataSourceDefinition; +import org.apache.metamodel.membrane.swagger.model.DeleteDatasourceResponse; import org.apache.metamodel.membrane.swagger.model.GetDatasourceResponse; import org.apache.metamodel.membrane.swagger.model.GetDatasourceResponseSchemas; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; @@ -48,6 +52,8 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping(value = "/{tenant}/{datasource}", produces = MediaType.APPLICATION_JSON_VALUE) public class DataSourceController { + private static final Logger logger = LoggerFactory.getLogger(DataSourceController.class); + private final TenantRegistry tenantRegistry; @Autowired @@ -72,10 +78,12 @@ public class DataSourceController { final DataContextProperties properties = new DataContextPropertiesImpl(map); - final String dataContextIdentifier = tenantRegistry.getTenantContext(tenantId).getDataSourceRegistry() + final String dataSourceIdentifier = tenantRegistry.getTenantContext(tenantId).getDataSourceRegistry() .registerDataSource(dataSourceId, properties); - return get(tenantId, dataContextIdentifier); + logger.info("Created data source: {}/{}", tenantId, dataSourceIdentifier); + + return get(tenantId, dataSourceIdentifier); } @RequestMapping(method = RequestMethod.GET) @@ -98,9 +106,22 @@ public class DataSourceController { resp.name(dataSourceName); resp.tenant(tenantName); resp.updateable(dataContext instanceof UpdateableDataContext); - resp.queryUri(UriBuilder.fromPath("/{tenant}/{dataContext}/query").build(tenantName, dataSourceName) - .toString()); + resp.queryUri( + UriBuilder.fromPath("/{tenant}/{dataContext}/query").build(tenantName, dataSourceName).toString()); resp.schemas(schemaLinks); return resp; } + + @RequestMapping(method = RequestMethod.DELETE) + @ResponseBody + public DeleteDatasourceResponse delete(@PathVariable("tenant") String tenantId, + @PathVariable("datasource") String dataSourceName) { + final TenantContext tenantContext = tenantRegistry.getTenantContext(tenantId); + final DataSourceRegistry dataSourceRegistry = tenantContext.getDataSourceRegistry(); + dataSourceRegistry.removeDataSource(dataSourceName); + + logger.info("Deleted data source: {}/{}", tenantId, dataSourceName); + + return new DeleteDatasourceResponse().deleted(true).type("datasource").name(dataSourceName); + } } http://git-wip-us.apache.org/repos/asf/metamodel-membrane/blob/8e8a90c6/core/src/main/resources/swagger.yaml ---------------------------------------------------------------------- diff --git a/core/src/main/resources/swagger.yaml b/core/src/main/resources/swagger.yaml index 1423ee7..e15b60e 100644 --- a/core/src/main/resources/swagger.yaml +++ b/core/src/main/resources/swagger.yaml @@ -116,6 +116,16 @@ paths: description: Datasource created schema: $ref: "#/definitions/getDatasourceResponse" + delete: + responses: + 200: + description: Datasource deleted + schema: + $ref: "#/definitions/deleteDatasourceResponse" + 404: + description: Datasource not found + schema: + $ref: "#/definitions/error" /{tenant}/{datasource}/q: parameters: - name: tenant @@ -539,6 +549,18 @@ definitions: type: string description: A link to the schema information format: uri + deleteDatasourceResponse: + type: object + properties: + type: + type: string + description: The type of entity (datasource) + name: + type: string + description: The datasource name/identifier + deleted: + type: boolean + description: A confirmation boolean to indicate that the deletion is effectuated. getSchemaResponse: type: object properties: