Skip to content

Commit

Permalink
Ensure controller services are created for data sources
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Hart committed Oct 11, 2018
1 parent d0f2c16 commit 8fd53be
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.thinkbiganalytics.kylo.catalog.rest.controller;

import com.thinkbiganalytics.feedmgr.service.security.SecurityService;

/*-
* #%L
* kylo-catalog-controller
Expand All @@ -22,6 +20,7 @@
* #L%
*/

import com.thinkbiganalytics.feedmgr.service.security.SecurityService;
import com.thinkbiganalytics.kylo.catalog.CatalogException;
import com.thinkbiganalytics.kylo.catalog.ConnectorPluginManager;
import com.thinkbiganalytics.kylo.catalog.credential.api.DataSourceCredentialManager;
Expand All @@ -37,8 +36,6 @@
import com.thinkbiganalytics.kylo.catalog.spi.ConnectorPlugin;
import com.thinkbiganalytics.kylo.catalog.table.CatalogTableManager;
import com.thinkbiganalytics.metadata.api.MetadataAccess;
import com.thinkbiganalytics.metadata.api.catalog.Connector;
import com.thinkbiganalytics.metadata.api.catalog.ConnectorProvider;
import com.thinkbiganalytics.metadata.api.catalog.DataSourceProvider;
import com.thinkbiganalytics.rest.model.RestResponseStatus;
import com.thinkbiganalytics.rest.model.search.SearchResult;
Expand Down Expand Up @@ -104,20 +101,17 @@ public class DataSourceController extends AbstractCatalogController {

public static final String BASE = "/v1/catalog/datasource";

public enum CredentialMode {NONE, EMBED, ATTACH};

@Inject
private ConnectorProvider connectorProvider;
public enum CredentialMode {NONE, EMBED, ATTACH}

@Inject
private DataSourceProvider dataSourceProvider;

@Inject
private SecurityService securityService;

@Inject
private SecurityModelTransform securityTransform;

@Inject
private CatalogModelTransform modelTransform;

Expand All @@ -138,10 +132,13 @@ public enum CredentialMode {NONE, EMBED, ATTACH};

@Inject
private ConnectorPluginManager pluginManager;

@Inject
private DataSetController dataSetController;

@Inject
private com.thinkbiganalytics.kylo.catalog.datasource.DataSourceProvider dataSourceService;


@POST
@ApiOperation("Create a new data source")
Expand All @@ -153,28 +150,23 @@ public enum CredentialMode {NONE, EMBED, ATTACH};
@Consumes(MediaType.APPLICATION_JSON)
public Response createDataSource(@Nonnull final DataSource source) {
log.entry(source);

// TODO: Remove this check for the ID and force updates to use the PUT to updateDataSource() for a more typical REST API
if (! StringUtils.isEmpty(source.getId())) {
final DataSource dataSource;
if (StringUtils.isNotEmpty(source.getId())) {
return updateDataSource(source);
} else {
return metadataService.commit(() -> {
Connector.ID connId = connectorProvider.resolveId(source.getConnector().getId());

return connectorProvider.find(connId)
.map(conn -> dataSourceProvider.create(connId, source.getTitle()))
.map(domain -> {
modelTransform.updateDataSource(source, domain);
return domain;
})
.map(modelTransform.dataSourceToRestModel())
.map(dataSource -> Response.ok(log.exit(dataSource)).build())
.orElseThrow(() -> {
log.debug("Connector not found with ID: {}", connId);
return new BadRequestException(getMessage("catalog.connector.notFound"));
});
});
try {
dataSource = dataSourceService.createDataSource(source);
} catch (final CatalogException e) {
if (log.isDebugEnabled()) {
log.debug("Cannot create data source from request: " + source, e);
}
throw new BadRequestException(getMessage(e));
}
}

return Response.ok(log.exit(dataSource)).build();
}

@POST
Expand Down Expand Up @@ -251,31 +243,30 @@ public Response getDataSource(@PathParam("id") final String dataSourceId,

return Response.ok(log.exit(dataSource)).build();
}

@PUT
@ApiOperation("Updates an existing data source")
@ApiResponses({
@ApiResponse(code = 200, message = "Data source updated", response = DataSource.class),
@ApiResponse(code = 400, message = "Invalid connector", response = RestResponseStatus.class),
@ApiResponse(code = 500, message = "Internal server error", response = RestResponseStatus.class)
})
@ApiResponse(code = 200, message = "Data source updated", response = DataSource.class),
@ApiResponse(code = 400, message = "Invalid connector", response = RestResponseStatus.class),
@ApiResponse(code = 500, message = "Internal server error", response = RestResponseStatus.class)
})
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateDataSource(@Nonnull final DataSource source) {
log.entry(source);

return metadataService.commit(() -> {
com.thinkbiganalytics.metadata.api.catalog.DataSource.ID dsId = dataSourceProvider.resolveId(source.getId());

return dataSourceProvider.find(dsId)
.map(domain -> modelTransform.updateDataSource(source, domain))
.map(modelTransform.dataSourceToRestModel())
.map(dataSource -> Response.ok(log.exit(dataSource)).build())
.orElseThrow(() -> {
log.debug("Data source not found with ID: {}", dsId);
return new BadRequestException(getMessage("catalog.datasource.notFound.id", dsId));
});
});

final DataSource dataSource;
try {
dataSource = dataSourceService.updateDataSource(source);
} catch (final CatalogException e) {
if (log.isDebugEnabled()) {
log.debug("Cannot create data source from request: " + source, e);
}
throw new BadRequestException(getMessage(e));
}

return Response.ok(log.exit(dataSource)).build();
}

@DELETE
Expand Down Expand Up @@ -319,8 +310,8 @@ public Response getDataSources(@QueryParam("connector") final String connectorId

// Fetch page
final PageRequest pageRequest = new PageRequest((start != null) ? start : 0, (limit != null) ? limit : Integer.MAX_VALUE);
return metadataService.read(() -> {

return metadataService.read(() -> {
Page<com.thinkbiganalytics.metadata.api.catalog.DataSource> domainPage = dataSourceProvider.findPage(pageRequest, filter);
final Page<DataSource> page = domainPage.map(modelTransform.convertDataSourceToRestModel());

Expand Down Expand Up @@ -396,7 +387,7 @@ public Response listTables(@PathParam("id") final String dataSourceId, @QueryPar
// List tables
final DataSource dataSource = findDataSource(dataSourceId);
final List<DataSetTable> tables = doListTables(catalogName, schemaName, dataSource);

return Response.ok(log.exit(tables)).build();
});
}
Expand Down Expand Up @@ -470,7 +461,6 @@ public Response getConnectorPlugin(@PathParam("id") final String dataSourceId) {
}



@POST
@Path("{id}/dataset")
@ApiOperation("creates a new dataset for a datasource")
Expand All @@ -479,7 +469,7 @@ public Response createDataSet(@PathParam("id") final String datasourceId) {
final DataSource dataSource = findDataSource(datasourceId);
final DataSet dataSet = new DataSet();
dataSet.setDataSource(dataSource);

return dataSetController.createDataSet(dataSet);
}

Expand All @@ -500,7 +490,7 @@ private DataSource findDataSource(@Nonnull final String id) {
});
});
}


@GET
@Path("{id}/actions/available")
Expand Down
Loading

0 comments on commit 8fd53be

Please sign in to comment.