Search in sources :

Example 6 with DsInfo

use of com.dtstack.taier.dao.domain.DsInfo in project Taier by DTStack.

the class DatasourceService method addOrUpdateSourceWithKerberos.

/**
 * 上传Kerberos添加和修改数据源
 * @param dataSourceVO
 * @param resource
 * @param userId
 * @param tenantId
 * @return
 */
@Transactional(rollbackFor = Exception.class)
public Long addOrUpdateSourceWithKerberos(DataSourceVO dataSourceVO, Pair<String, String> resource, Long userId, Long tenantId) {
    Map<String, Object> confMap;
    JSONObject dataJson = DataSourceUtils.getDataSourceJson(dataSourceVO.getDataJsonString());
    dataSourceVO.setDataJson(dataJson);
    List<Integer> list = JSON.parseObject(dataSourceVO.getAppTypeListString(), List.class);
    dataSourceVO.setAppTypeList(list);
    String localKerberosConf;
    DataSourceTypeEnum typeEnum = DataSourceTypeEnum.typeVersionOf(dataSourceVO.getDataType(), dataSourceVO.getDataVersion());
    if (Objects.nonNull(resource)) {
        // resource不为空表示本地上传文件
        localKerberosConf = kerberosService.getTempLocalKerberosConf(userId);
        try {
            confMap = ClientCache.getKerberos(typeEnum.getVal()).parseKerberosFromUpload(resource.getRight(), localKerberosConf);
        } catch (IOException e) {
            LOGGER.error("添加数据源, kerberos config 解析异常,{}", e.getMessage(), e);
            throw new PubSvcDefineException(String.format("kerberos config 解析异常,Caused by: %s", e.getMessage()), e);
        }
        // 设置openKerberos变量表示开启kerberos认证
        DataSourceUtils.setOpenKerberos(dataJson, true);
        DataSourceUtils.setKerberosFile(dataJson, resource.getRight());
    } else {
        DsInfo originSource = dsInfoService.getOneById(dataSourceVO.getId());
        DataSourceUtils.getOriginKerberosConfig(dataJson, originSource.getDataJson());
        localKerberosConf = kerberosService.getLocalKerberosPath(dataSourceVO.getId());
        confMap = fillKerberosConfig(dataSourceVO.getId());
    }
    // 设置前台传入的principals
    setPrincipals(dataJson, confMap);
    // 检查链接
    Boolean connValue = checkConnectionWithConf(dataSourceVO, confMap, localKerberosConf);
    if (!connValue) {
        throw new PubSvcDefineException("不能添加连接失败的数据源", ErrorCode.CONFIG_ERROR);
    }
    Long dataSourceId = dataSourceVO.getId();
    // 有认证文件上传 进行上传至sftp操作
    Long id = null;
    try {
        if (Objects.nonNull(resource)) {
            if (dataSourceVO.getId() == 0L) {
                // 没有保存过的数据源需要先保存, 获取自增id
                dataSourceVO.setKerberosConfig(confMap);
                dataSourceVO.setDataJson((JSONObject) dataJson.clone());
                dataSourceVO.setLocalKerberosConf(localKerberosConf);
                dataSourceId = addOrUpdate(dataSourceVO, userId);
            }
            Map<String, String> sftpMap = kerberosService.getSftpMap(tenantId);
            // 目录转换 - 将临时目录根据数据源ID转移到新的kerberos文件目录
            File localKerberosConfDir = new File(localKerberosConf);
            File newConfDir = new File(kerberosService.getLocalKerberosPath(dataSourceId));
            // 如果原来的目录存在 删除原来的文件
            try {
                FileUtils.deleteDirectory(newConfDir);
            } catch (IOException e) {
                LOGGER.error("删除历史的kerberos文件失败", e);
            }
            // 目录转换, temp路径转换成新路径
            localKerberosConfDir.renameTo(newConfDir);
            // 上传配置文件到sftp
            String dataSourceKey = kerberosService.getSourceKey(dataSourceId, null);
            KerberosService.uploadDirFinal(sftpMap, newConfDir.getPath(), dataSourceKey);
            confMap.put("kerberosDir", dataSourceKey);
        }
        dataSourceVO.setKerberosConfig(confMap);
        dataSourceVO.setLocalKerberosConf(localKerberosConf);
        dataSourceVO.setDataJson(dataJson);
        dataSourceVO.setId(dataSourceId);
        id = addOrUpdate(dataSourceVO, userId);
    } catch (Exception e) {
        LOGGER.error("addOrUpdateSourceWithKerberos error", e);
        throw new PubSvcDefineException(e.getMessage());
    }
    return id;
}
Also used : DataSourceTypeEnum(com.dtstack.taier.common.enums.DataSourceTypeEnum) PubSvcDefineException(com.dtstack.taier.common.exception.PubSvcDefineException) IOException(java.io.IOException) SftpException(com.jcraft.jsch.SftpException) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) IOException(java.io.IOException) PubSvcDefineException(com.dtstack.taier.common.exception.PubSvcDefineException) DtCenterDefException(com.dtstack.taier.common.exception.DtCenterDefException) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) DsInfo(com.dtstack.taier.dao.domain.DsInfo) File(java.io.File) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with DsInfo

use of com.dtstack.taier.dao.domain.DsInfo in project Taier by DTStack.

the class DatasourceService method addOrUpdate.

/**
 * 添加和修改数据源信息
 * @param dataSourceVO
 * @param userId
 * @return
 */
@Transactional(rollbackFor = Exception.class)
public Long addOrUpdate(DataSourceVO dataSourceVO, Long userId) {
    DataSourceTypeEnum typeEnum = DataSourceTypeEnum.typeVersionOf(dataSourceVO.getDataType(), dataSourceVO.getDataVersion());
    JSONObject json = dataSourceVO.getDataJson();
    // 字段转换
    colMap(json, typeEnum.getVal(), dataSourceVO.getKerberosConfig());
    dataSourceVO.setDataJson(json);
    dataSourceVO.setModifyUserId(userId);
    // 构造数据源元数据
    DsInfo dsInfo = buildDsInfo(dataSourceVO);
    if (dataSourceVO.getId() > 0) {
        // edit 不存在授权操作
        dsInfoService.getOneById(dataSourceVO.getId());
        dsInfo.setId(dataSourceVO.getId());
        dsInfo.setModifyUserId(dataSourceVO.getUserId());
        if (dsInfoService.checkDataNameDup(dsInfo)) {
            throw new PubSvcDefineException(ErrorCode.DATASOURCE_DUP_NAME);
        }
        dsInfoService.updateById(dsInfo);
    } else {
        // add 存在授权产品操作
        dsInfo.setCreateUserId(dataSourceVO.getUserId());
        dsInfo.setModifyUserId(dataSourceVO.getUserId());
        if (dsInfoService.checkDataNameDup(dsInfo)) {
            throw new PubSvcDefineException(ErrorCode.DATASOURCE_DUP_NAME);
        }
        dsInfoService.save(dsInfo);
        // 保存数据源类型权重
        typeService.plusDataTypeWeight(dsInfo.getDataType(), 1);
    }
    return dsInfo.getId();
}
Also used : DataSourceTypeEnum(com.dtstack.taier.common.enums.DataSourceTypeEnum) JSONObject(com.alibaba.fastjson.JSONObject) PubSvcDefineException(com.dtstack.taier.common.exception.PubSvcDefineException) DsInfo(com.dtstack.taier.dao.domain.DsInfo) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with DsInfo

use of com.dtstack.taier.dao.domain.DsInfo in project Taier by DTStack.

the class DsInfoService method getOneById.

/**
 * 通过数据源主键id获取特定数据源
 * @param dsInfoId
 * @return
 */
public DsInfo getOneById(Long dsInfoId) {
    DsInfo dataSource = this.getById(dsInfoId);
    if (Objects.isNull(dataSource) || Objects.isNull(dataSource.getId())) {
        throw new PubSvcDefineException(ErrorCode.CAN_NOT_FIND_DATA_SOURCE);
    }
    // 查询引入表判断是否是迁移的数据源
    JSONObject dataSourceJson = DataSourceUtils.getDataSourceJson(dataSource.getDataJson());
    dataSource.setDataJson(dataSourceJson.toJSONString());
    return dataSource;
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) PubSvcDefineException(com.dtstack.taier.common.exception.PubSvcDefineException) DsInfo(com.dtstack.taier.dao.domain.DsInfo)

Aggregations

DsInfo (com.dtstack.taier.dao.domain.DsInfo)8 JSONObject (com.alibaba.fastjson.JSONObject)7 PubSvcDefineException (com.dtstack.taier.common.exception.PubSvcDefineException)5 DataSourceTypeEnum (com.dtstack.taier.common.enums.DataSourceTypeEnum)4 DtCenterDefException (com.dtstack.taier.common.exception.DtCenterDefException)3 SftpException (com.jcraft.jsch.SftpException)3 RdosDefineException (com.dtstack.taier.common.exception.RdosDefineException)2 IOException (java.io.IOException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 IKerberos (com.dtstack.dtcenter.loader.client.IKerberos)1 BatchDataSource (com.dtstack.taier.dao.domain.BatchDataSource)1 DsDetailVO (com.dtstack.taier.develop.vo.datasource.DsDetailVO)1 File (java.io.File)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1