Search in sources :

Example 26 with DataSource

use of com.thinkbiganalytics.kylo.catalog.rest.model.DataSource in project kylo by Teradata.

the class DataSourceProvider method createOrUpdateNiFiControllerService.

/**
 * Creates or updates the NiFi controller service linked to the specified data source.
 */
private void createOrUpdateNiFiControllerService(@Nonnull final DataSource dataSource, @Nonnull final ConnectorPluginDescriptor connectorPluginDescriptor, boolean checkExisting) throws PotentialControllerServiceConflictException {
    ConnectorPluginNiFiControllerService plugin = connectorPluginDescriptor.getNifiControllerService();
    // Resolve properties
    final PropertyPlaceholderHelper.PlaceholderResolver resolver = new DataSourcePlaceholderResolver(dataSource);
    final Map<String, String> properties = new HashMap<>(plugin.getProperties().size());
    final Map<String, ConnectorPluginNiFiControllerServicePropertyDescriptor> propertyDescriptors = plugin.getPropertyDescriptors();
    plugin.getProperties().forEach((key, value) -> {
        final String resolvedValue = placeholderHelper.replacePlaceholders(value, resolver);
        // set empty string to null
        ConnectorPluginNiFiControllerServicePropertyDescriptor descriptor = propertyDescriptors != null ? propertyDescriptors.get(key) : null;
        if (StringUtils.isBlank(resolvedValue) && (descriptor == null || (descriptor != null && !descriptor.isEmptyStringIfNull()))) {
            // set the value to null if its not explicitly configured to be set to an empty string
            properties.put(key, null);
        } else if (resolvedValue != null && !resolvedValue.startsWith("{cipher}")) {
            properties.put(key, resolvedValue);
        }
    });
    // Update or create the controller service
    ControllerServiceDTO controllerService = null;
    if (dataSource.getNifiControllerServiceId() != null && dataSource.getId() != null) {
        controllerService = new ControllerServiceDTO();
        controllerService.setId(dataSource.getNifiControllerServiceId());
        controllerService.setName(dataSource.getTitle());
        controllerService.setProperties(properties);
        try {
            controllerService = nifiRestClient.controllerServices().updateServiceAndReferencingComponents(controllerService);
            dataSource.setNifiControllerServiceId(controllerService.getId());
        } catch (final NifiComponentNotFoundException e) {
            log.warn("Controller service is missing for data source: {}", dataSource.getId(), e);
            controllerService = null;
        }
    }
    if (controllerService == null && StringUtils.isBlank(dataSource.getNifiControllerServiceId())) {
        if (checkExisting) {
            List<ControllerServiceDTO> matchingServices = findMatchingControllerService(dataSource, properties, connectorPluginDescriptor);
            if (matchingServices != null && !matchingServices.isEmpty()) {
                Map<String, String> identityProperties = plugin.getIdentityProperties().stream().collect(Collectors.toMap(propertyKey -> propertyKey, propertyKey -> properties.get(propertyKey)));
                throw new PotentialControllerServiceConflictException(new ControllerServiceConflictEntity(dataSource.getTitle(), identityProperties, matchingServices));
            }
        }
        controllerService = new ControllerServiceDTO();
        controllerService.setType(plugin.getType());
        controllerService.setName(dataSource.getTitle());
        controllerService.setProperties(properties);
        controllerService = nifiRestClient.controllerServices().create(controllerService);
        try {
            nifiRestClient.controllerServices().updateStateById(controllerService.getId(), NiFiControllerServicesRestClient.State.ENABLED);
        } catch (final NifiClientRuntimeException e) {
            log.error("Failed to enable controller service for data source: {}", dataSource.getId(), e);
            nifiRestClient.controllerServices().disableAndDeleteAsync(controllerService.getId());
            throw e;
        }
        dataSource.setNifiControllerServiceId(controllerService.getId());
    }
}
Also used : NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) DatasourceModelTransform(com.thinkbiganalytics.feedmgr.service.datasource.DatasourceModelTransform) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) DefaultDataSetTemplate(com.thinkbiganalytics.kylo.catalog.rest.model.DefaultDataSetTemplate) NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) DatasourceCriteria(com.thinkbiganalytics.metadata.api.datasource.DatasourceCriteria) DataSource(com.thinkbiganalytics.kylo.catalog.rest.model.DataSource) Map(java.util.Map) NiFiRestClient(com.thinkbiganalytics.nifi.rest.client.NiFiRestClient) Pageable(org.springframework.data.domain.Pageable) MetadataAccess(com.thinkbiganalytics.metadata.api.MetadataAccess) NiFiControllerServicesRestClient(com.thinkbiganalytics.nifi.rest.client.NiFiControllerServicesRestClient) CatalogModelTransform(com.thinkbiganalytics.kylo.catalog.rest.model.CatalogModelTransform) PropertyPlaceholderHelper(org.springframework.util.PropertyPlaceholderHelper) ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) Predicate(java.util.function.Predicate) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) ConnectorPluginNiFiControllerService(com.thinkbiganalytics.kylo.catalog.rest.model.ConnectorPluginNiFiControllerService) Optional(java.util.Optional) PageImpl(org.springframework.data.domain.PageImpl) ConnectorPlugin(com.thinkbiganalytics.kylo.catalog.spi.ConnectorPlugin) ConnectorPluginManager(com.thinkbiganalytics.kylo.catalog.ConnectorPluginManager) HashMap(java.util.HashMap) Function(java.util.function.Function) Supplier(java.util.function.Supplier) DataSetTemplate(com.thinkbiganalytics.kylo.catalog.rest.model.DataSetTemplate) ConnectorPluginNiFiControllerServicePropertyDescriptor(com.thinkbiganalytics.kylo.catalog.rest.model.ConnectorPluginNiFiControllerServicePropertyDescriptor) JdbcDatasource(com.thinkbiganalytics.metadata.rest.model.data.JdbcDatasource) CatalogException(com.thinkbiganalytics.kylo.catalog.CatalogException) DataSourceCredentialManager(com.thinkbiganalytics.kylo.catalog.credential.api.DataSourceCredentialManager) NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ConnectorProvider(com.thinkbiganalytics.metadata.api.catalog.ConnectorProvider) UserDatasource(com.thinkbiganalytics.metadata.api.datasource.UserDatasource) Datasource(com.thinkbiganalytics.metadata.api.datasource.Datasource) SecurityContextUtil(com.thinkbiganalytics.security.context.SecurityContextUtil) Logger(org.slf4j.Logger) Consumer(java.util.function.Consumer) Component(org.springframework.stereotype.Component) Connector(com.thinkbiganalytics.kylo.catalog.rest.model.Connector) ConnectorPluginDescriptor(com.thinkbiganalytics.kylo.catalog.rest.model.ConnectorPluginDescriptor) Comparator(java.util.Comparator) DatasourceProvider(com.thinkbiganalytics.metadata.api.datasource.DatasourceProvider) ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) HashMap(java.util.HashMap) ConnectorPluginNiFiControllerServicePropertyDescriptor(com.thinkbiganalytics.kylo.catalog.rest.model.ConnectorPluginNiFiControllerServicePropertyDescriptor) NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) ConnectorPluginNiFiControllerService(com.thinkbiganalytics.kylo.catalog.rest.model.ConnectorPluginNiFiControllerService) PropertyPlaceholderHelper(org.springframework.util.PropertyPlaceholderHelper)

Example 27 with DataSource

use of com.thinkbiganalytics.kylo.catalog.rest.model.DataSource in project kylo by Teradata.

the class DataSourceProvider method findDataSource.

/**
 * Gets the connector with the specified id.
 */
@Nonnull
@SuppressWarnings("squid:S2259")
public Optional<DataSource> findDataSource(@Nonnull final String id, final boolean includeCredentials) {
    return metadataService.read(() -> {
        // Find the data source
        DataSource dataSource = metadataProvider.find(metadataProvider.resolveId(id)).map(modelTransform.dataSourceToRestModel()).orElse(null);
        if (dataSource != null) {
            dataSource = new DataSource(dataSource);
        } else {
            try {
                final Datasource feedDataSource = feedDataSourceProvider.getDatasource(feedDataSourceProvider.resolve(id));
                DatasourceModelTransform.Level level = DatasourceModelTransform.Level.FULL;
                if (includeCredentials) {
                    level = DatasourceModelTransform.Level.ADMIN;
                }
                dataSource = toDataSource(feedDataSource, level);
            } catch (final IllegalArgumentException e) {
                log.debug("Failed to resolve data source {}: {}", id, e, e);
            }
        }
        // Set connector
        final Optional<Connector> connector = Optional.ofNullable(dataSource).map(DataSource::getConnector).map(Connector::getId).map(connectorProvider::resolveId).flatMap(connectorProvider::find).map(modelTransform.connectorToRestModel());
        if (connector.isPresent()) {
            dataSource.setConnector(connector.get());
            return Optional.of(dataSource);
        } else {
            if (dataSource != null) {
                log.error("Unable to find connector for data source: {}", dataSource);
            }
            return Optional.empty();
        }
    });
}
Also used : JdbcDatasource(com.thinkbiganalytics.metadata.rest.model.data.JdbcDatasource) UserDatasource(com.thinkbiganalytics.metadata.api.datasource.UserDatasource) Datasource(com.thinkbiganalytics.metadata.api.datasource.Datasource) Connector(com.thinkbiganalytics.kylo.catalog.rest.model.Connector) DatasourceModelTransform(com.thinkbiganalytics.feedmgr.service.datasource.DatasourceModelTransform) DataSource(com.thinkbiganalytics.kylo.catalog.rest.model.DataSource) Nonnull(javax.annotation.Nonnull)

Example 28 with DataSource

use of com.thinkbiganalytics.kylo.catalog.rest.model.DataSource in project kylo by Teradata.

the class DataSourceProvider method createDataSource.

/**
 * Creates a new data source using the specified template.
 *
 * @throws CatalogException if the data source is not valid
 */
@Nonnull
public DataSource createDataSource(@Nonnull final DataSource source, boolean checkExisting) throws PotentialControllerServiceConflictException {
    return metadataService.commit(() -> {
        // Find connector
        final com.thinkbiganalytics.metadata.api.catalog.Connector connector = Optional.ofNullable(source.getConnector()).map(Connector::getId).map(connectorProvider::resolveId).flatMap(connectorProvider::find).orElseThrow(() -> new CatalogException("catalog.datasource.connector.invalid"));
        // Create data source
        final com.thinkbiganalytics.metadata.api.catalog.DataSource domain = metadataProvider.create(connector.getId(), source.getTitle());
        // Create or update controller service
        final ConnectorPluginDescriptor plugin = pluginManager.getPlugin(connector.getPluginId()).map(ConnectorPlugin::getDescriptor).orElse(null);
        if (plugin != null && plugin.getNifiControllerService() != null) {
            createOrUpdateNiFiControllerService(source, plugin, checkExisting);
        }
        // Update catalog
        final DataSource updatedDataSource = this.credentialManager.applyPlaceholders(source, SecurityContextUtil.getCurrentPrincipals());
        modelTransform.updateDataSource(updatedDataSource, domain);
        // Return a copy with the connector
        return modelTransform.dataSourceToRestModel().apply(domain);
    });
}
Also used : CatalogException(com.thinkbiganalytics.kylo.catalog.CatalogException) ConnectorPluginDescriptor(com.thinkbiganalytics.kylo.catalog.rest.model.ConnectorPluginDescriptor) DataSource(com.thinkbiganalytics.kylo.catalog.rest.model.DataSource) Nonnull(javax.annotation.Nonnull)

Example 29 with DataSource

use of com.thinkbiganalytics.kylo.catalog.rest.model.DataSource in project kylo by Teradata.

the class DataSourceProvider method findMatchingControllerService.

private List<ControllerServiceDTO> findMatchingControllerService(@Nonnull final DataSource dataSource, @Nonnull final Map<String, String> properties, @Nonnull final ConnectorPluginDescriptor connectorPluginDescriptor) {
    ConnectorPluginNiFiControllerService plugin = connectorPluginDescriptor.getNifiControllerService();
    String type = plugin.getType();
    Map<String, String> identityProperties = plugin.getIdentityProperties().stream().collect(Collectors.toMap(propertyKey -> propertyKey, propertyKey -> properties.get(propertyKey)));
    // TODO hit cache first
    String rootProcessGroupId = nifiRestClient.processGroups().findRoot().getId();
    return nifiRestClient.processGroups().getControllerServices(rootProcessGroupId).stream().filter(controllerServiceDTO -> isMatch(controllerServiceDTO, type, dataSource.getTitle(), identityProperties)).collect(Collectors.toList());
}
Also used : Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) DatasourceModelTransform(com.thinkbiganalytics.feedmgr.service.datasource.DatasourceModelTransform) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) DefaultDataSetTemplate(com.thinkbiganalytics.kylo.catalog.rest.model.DefaultDataSetTemplate) NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) DatasourceCriteria(com.thinkbiganalytics.metadata.api.datasource.DatasourceCriteria) DataSource(com.thinkbiganalytics.kylo.catalog.rest.model.DataSource) Map(java.util.Map) NiFiRestClient(com.thinkbiganalytics.nifi.rest.client.NiFiRestClient) Pageable(org.springframework.data.domain.Pageable) MetadataAccess(com.thinkbiganalytics.metadata.api.MetadataAccess) NiFiControllerServicesRestClient(com.thinkbiganalytics.nifi.rest.client.NiFiControllerServicesRestClient) CatalogModelTransform(com.thinkbiganalytics.kylo.catalog.rest.model.CatalogModelTransform) PropertyPlaceholderHelper(org.springframework.util.PropertyPlaceholderHelper) ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) Predicate(java.util.function.Predicate) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) ConnectorPluginNiFiControllerService(com.thinkbiganalytics.kylo.catalog.rest.model.ConnectorPluginNiFiControllerService) Optional(java.util.Optional) PageImpl(org.springframework.data.domain.PageImpl) ConnectorPlugin(com.thinkbiganalytics.kylo.catalog.spi.ConnectorPlugin) ConnectorPluginManager(com.thinkbiganalytics.kylo.catalog.ConnectorPluginManager) HashMap(java.util.HashMap) Function(java.util.function.Function) Supplier(java.util.function.Supplier) DataSetTemplate(com.thinkbiganalytics.kylo.catalog.rest.model.DataSetTemplate) ConnectorPluginNiFiControllerServicePropertyDescriptor(com.thinkbiganalytics.kylo.catalog.rest.model.ConnectorPluginNiFiControllerServicePropertyDescriptor) JdbcDatasource(com.thinkbiganalytics.metadata.rest.model.data.JdbcDatasource) CatalogException(com.thinkbiganalytics.kylo.catalog.CatalogException) DataSourceCredentialManager(com.thinkbiganalytics.kylo.catalog.credential.api.DataSourceCredentialManager) NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ConnectorProvider(com.thinkbiganalytics.metadata.api.catalog.ConnectorProvider) UserDatasource(com.thinkbiganalytics.metadata.api.datasource.UserDatasource) Datasource(com.thinkbiganalytics.metadata.api.datasource.Datasource) SecurityContextUtil(com.thinkbiganalytics.security.context.SecurityContextUtil) Logger(org.slf4j.Logger) Consumer(java.util.function.Consumer) Component(org.springframework.stereotype.Component) Connector(com.thinkbiganalytics.kylo.catalog.rest.model.Connector) ConnectorPluginDescriptor(com.thinkbiganalytics.kylo.catalog.rest.model.ConnectorPluginDescriptor) Comparator(java.util.Comparator) DatasourceProvider(com.thinkbiganalytics.metadata.api.datasource.DatasourceProvider) ConnectorPluginNiFiControllerService(com.thinkbiganalytics.kylo.catalog.rest.model.ConnectorPluginNiFiControllerService)

Example 30 with DataSource

use of com.thinkbiganalytics.kylo.catalog.rest.model.DataSource in project kylo by Teradata.

the class DataSourceController method updateDataSource.

@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) })
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateDataSource(@Nonnull final DataSource source) {
    log.entry(source);
    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();
}
Also used : CatalogException(com.thinkbiganalytics.kylo.catalog.CatalogException) BadRequestException(javax.ws.rs.BadRequestException) DataSource(com.thinkbiganalytics.kylo.catalog.rest.model.DataSource) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

DataSource (com.thinkbiganalytics.kylo.catalog.rest.model.DataSource)41 ApiOperation (io.swagger.annotations.ApiOperation)15 DefaultDataSetTemplate (com.thinkbiganalytics.kylo.catalog.rest.model.DefaultDataSetTemplate)14 ApiResponses (io.swagger.annotations.ApiResponses)14 Nonnull (javax.annotation.Nonnull)14 Path (javax.ws.rs.Path)13 Connector (com.thinkbiganalytics.kylo.catalog.rest.model.Connector)11 CatalogException (com.thinkbiganalytics.kylo.catalog.CatalogException)10 GET (javax.ws.rs.GET)9 BadRequestException (javax.ws.rs.BadRequestException)8 DataSet (com.thinkbiganalytics.kylo.catalog.rest.model.DataSet)7 DataSetTemplate (com.thinkbiganalytics.kylo.catalog.rest.model.DataSetTemplate)7 List (java.util.List)7 Collectors (java.util.stream.Collectors)7 Nullable (javax.annotation.Nullable)7 StringUtils (org.apache.commons.lang3.StringUtils)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 CatalogModelTransform (com.thinkbiganalytics.kylo.catalog.rest.model.CatalogModelTransform)5 ConnectorPluginDescriptor (com.thinkbiganalytics.kylo.catalog.rest.model.ConnectorPluginDescriptor)5