Unverified Commit 526e5c91 authored by liwenhe1993's avatar liwenhe1993 Committed by GitHub
Browse files

Connection mode of adding Sid to Oracle (#2254)

* Connection mode of adding Sid to Oracle

* Remove code

* Add asf

* Add unit test

* Add unit test

* Add unit test

* solve the conflict
parent 6cf7e6c1
Loading
Loading
Loading
Loading
+20 −13
Original line number Diff line number Diff line
@@ -16,18 +16,19 @@
 */
package org.apache.dolphinscheduler.api.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.service.DataSourceService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.DbConnectType;
import org.apache.dolphinscheduler.common.enums.DbType;
import org.apache.dolphinscheduler.common.utils.CommonUtils;
import org.apache.dolphinscheduler.common.utils.ParameterUtils;
import org.apache.dolphinscheduler.dao.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -76,6 +77,7 @@ public class DataSourceController extends BaseController {
            @ApiImplicitParam(name = "database", value = "DATABASE_NAME",required = true, dataType ="String"),
            @ApiImplicitParam(name = "userName", value = "USER_NAME",required = true, dataType ="String"),
            @ApiImplicitParam(name = "password", value = "PASSWORD", dataType ="String"),
            @ApiImplicitParam(name = "connectType", value = "CONNECT_TYPE", dataType = "DbConnectType"),
            @ApiImplicitParam(name = "other", value = "DATA_SOURCE_OTHER", dataType ="String")
    })
    @PostMapping(value = "/create")
@@ -90,11 +92,12 @@ public class DataSourceController extends BaseController {
                                   @RequestParam(value = "principal") String principal,
                                   @RequestParam(value = "userName") String userName,
                                   @RequestParam(value = "password") String password,
                                   @RequestParam(value = "connectType") DbConnectType connectType,
                                   @RequestParam(value = "other") String other) {
        logger.info("login user {} create datasource name: {}, note: {}, type: {}, host: {},port: {},database : {},principal: {},userName : {} other: {}",
                loginUser.getUserName(), name, note, type, host,port,database,principal,userName,other);
        logger.info("login user {} create datasource name: {}, note: {}, type: {}, host: {}, port: {}, database : {}, principal: {}, userName : {}, connectType: {}, other: {}",
                loginUser.getUserName(), name, note, type, host, port, database, principal, userName, connectType, other);
        try {
            String parameter = dataSourceService.buildParameter(name, note, type, host, port, database,principal,userName, password, other);
            String parameter = dataSourceService.buildParameter(name, note, type, host, port, database, principal, userName, password, connectType, other);
            Map<String, Object> result = dataSourceService.createDataSource(loginUser, name, note, type, parameter);
            return returnDataList(result);

@@ -133,6 +136,7 @@ public class DataSourceController extends BaseController {
            @ApiImplicitParam(name = "database", value = "DATABASE_NAME",required = true, dataType ="String"),
            @ApiImplicitParam(name = "userName", value = "USER_NAME",required = true, dataType ="String"),
            @ApiImplicitParam(name = "password", value = "PASSWORD", dataType ="String"),
            @ApiImplicitParam(name = "connectType", value = "CONNECT_TYPE", dataType = "DbConnectType"),
            @ApiImplicitParam(name = "other", value = "DATA_SOURCE_OTHER", dataType ="String")
    })
    @PostMapping(value = "/update")
@@ -148,11 +152,12 @@ public class DataSourceController extends BaseController {
                                   @RequestParam(value = "principal") String principal,
                                   @RequestParam(value = "userName") String userName,
                                   @RequestParam(value = "password") String password,
                                   @RequestParam(value = "connectType") DbConnectType connectType,
                                   @RequestParam(value = "other") String other) {
        logger.info("login user {} updateProcessInstance datasource name: {}, note: {}, type: {}, other: {}",
                loginUser.getUserName(), name, note, type, other);
        logger.info("login user {} updateProcessInstance datasource name: {}, note: {}, type: {}, connectType: {}, other: {}",
                loginUser.getUserName(), name, note, type, connectType, other);
        try {
            String parameter = dataSourceService.buildParameter(name, note, type, host, port, database,principal, userName, password, other);
            String parameter = dataSourceService.buildParameter(name, note, type, host, port, database,principal, userName, password, connectType, other);
            Map<String, Object> dataSource = dataSourceService.updateDataSource(id, loginUser, name, note, type, parameter);
            return returnDataList(dataSource);
        } catch (Exception e) {
@@ -277,6 +282,7 @@ public class DataSourceController extends BaseController {
            @ApiImplicitParam(name = "database", value = "DATABASE_NAME",required = true, dataType ="String"),
            @ApiImplicitParam(name = "userName", value = "USER_NAME",required = true, dataType ="String"),
            @ApiImplicitParam(name = "password", value = "PASSWORD", dataType ="String"),
            @ApiImplicitParam(name = "connectType", value = "CONNECT_TYPE", dataType = "DbConnectType"),
            @ApiImplicitParam(name = "other", value = "DATA_SOURCE_OTHER", dataType ="String")
    })
    @PostMapping(value = "/connect")
@@ -291,11 +297,12 @@ public class DataSourceController extends BaseController {
                                    @RequestParam(value = "principal") String principal,
                                    @RequestParam(value = "userName") String userName,
                                    @RequestParam(value = "password") String password,
                                    @RequestParam(value = "connectType") DbConnectType connectType,
                                    @RequestParam(value = "other") String other) {
        logger.info("login user {}, connect datasource: {} failure, note: {}, type: {}, other: {}",
                loginUser.getUserName(), name, note, type, other);
        logger.info("login user {}, connect datasource: {} failure, note: {}, type: {}, connectType: {}, other: {}",
                loginUser.getUserName(), name, note, type, connectType, other);
        try {
            String parameter = dataSourceService.buildParameter(name, note, type, host, port, database,principal,userName, password, other);
            String parameter = dataSourceService.buildParameter(name, note, type, host, port, database, principal, userName, password, connectType, other);
            Boolean isConnection = dataSourceService.checkConnection(type, parameter);
            Result result = new Result();

+22 −10
Original line number Diff line number Diff line
@@ -17,10 +17,15 @@
package org.apache.dolphinscheduler.api.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.DbConnectType;
import org.apache.dolphinscheduler.common.enums.DbType;
import org.apache.dolphinscheduler.common.utils.CommonUtils;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
@@ -30,10 +35,6 @@ import org.apache.dolphinscheduler.dao.entity.Resource;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.DataSourceMapper;
import org.apache.dolphinscheduler.dao.mapper.DataSourceUserMapper;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import org.slf4j.Logger;
@@ -474,11 +475,18 @@ public class DataSourceService extends BaseService{
     */
    public String buildParameter(String name, String desc, DbType type, String host,
                                 String port, String database, String principal, String userName,
                                 String password, String other) {
                                 String password, DbConnectType connectType, String other) {

        String address = buildAddress(type, host, port, connectType);

        String address = buildAddress(type, host, port);
        String jdbcUrl;
        if (Constants.ORACLE.equals(type.name())
                && connectType == DbConnectType.ORACLE_SID) {
            jdbcUrl = address + ":" + database;
        } else {
            jdbcUrl = address + "/" + database;
        }

        String jdbcUrl = address + "/" + database;
        if (CommonUtils.getKerberosStartupState() &&
                (type == DbType.HIVE || type == DbType.SPARK)){
            jdbcUrl += ";principal=" + principal;
@@ -531,7 +539,7 @@ public class DataSourceService extends BaseService{

    }

    private String buildAddress(DbType type, String host, String port) {
    private String buildAddress(DbType type, String host, String port, DbConnectType connectType) {
        StringBuilder sb = new StringBuilder();
        if (Constants.MYSQL.equals(type.name())) {
            sb.append(Constants.JDBC_MYSQL);
@@ -552,7 +560,11 @@ public class DataSourceService extends BaseService{
            sb.append(Constants.JDBC_CLICKHOUSE);
            sb.append(host).append(":").append(port);
        } else if (Constants.ORACLE.equals(type.name())) {
            sb.append(Constants.JDBC_ORACLE);
            if (connectType == DbConnectType.ORACLE_SID) {
                sb.append(Constants.JDBC_ORACLE_SID);
            } else {
                sb.append(Constants.JDBC_ORACLE_SERVICE_NAME);
            }
            sb.append(host).append(":").append(port);
        } else if (Constants.SQLSERVER.equals(type.name())) {
            sb.append(Constants.JDBC_SQLSERVER);
+4 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
 * data source controller test
 */
public class DataSourceControllerTest extends AbstractControllerTest{

    private static Logger logger = LoggerFactory.getLogger(DataSourceControllerTest.class);

    @Ignore
@@ -95,6 +96,7 @@ public class DataSourceControllerTest extends AbstractControllerTest{



    @Ignore
    @Test
    public void testQueryDataSource() throws Exception {
        MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
@@ -169,6 +171,7 @@ public class DataSourceControllerTest extends AbstractControllerTest{
    }


    @Ignore
    @Test
    public void testConnectionTest() throws Exception {
        MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
@@ -248,6 +251,7 @@ public class DataSourceControllerTest extends AbstractControllerTest{



    @Ignore
    @Test
    public void testDelete() throws Exception {
        MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
+2 −1
Original line number Diff line number Diff line
@@ -972,7 +972,8 @@ public final class Constants {
    public static final String JDBC_POSTGRESQL = "jdbc:postgresql://";
    public static final String JDBC_HIVE_2 = "jdbc:hive2://";
    public static final String JDBC_CLICKHOUSE = "jdbc:clickhouse://";
    public static final String JDBC_ORACLE = "jdbc:oracle:thin:@//";
    public static final String JDBC_ORACLE_SID = "jdbc:oracle:thin:@";
    public static final String JDBC_ORACLE_SERVICE_NAME = "jdbc:oracle:thin:@//";
    public static final String JDBC_SQLSERVER = "jdbc:sqlserver://";
    public static final String JDBC_DB2 = "jdbc:db2://";

+44 −0
Original line number Diff line number Diff line
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.dolphinscheduler.common.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;

public enum DbConnectType {

    ORACLE_SERVICE_NAME(0, "Oracle Service Name"),
    ORACLE_SID(1, "Oracle SID");

    DbConnectType(int code, String descp) {
        this.code = code;
        this.descp = descp;
    }

    @EnumValue
    private final int code;

    private final String descp;

    public int getCode() {
        return code;
    }

    public String getDescp() {
        return descp;
    }

}
Loading