Skip to content

Commit 9daab0f

Browse files
committed
fix(sql_workbench): fix MongoDB datasource sync for ODC
Remove MongoDB from SupportDBType so datasources are retained without provision accounts. Map connection params to jdbcUrlParameters that ODC actually reads instead of unused properties, and add TLS-only test.
1 parent 0973eb1 commit 9daab0f

2 files changed

Lines changed: 42 additions & 24 deletions

File tree

internal/sql_workbench/service/sql_workbench_service.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -985,8 +985,7 @@ func (sqlWorkbenchService *SqlWorkbenchService) SupportDBType(dbType pkgConst.DB
985985
dbType == pkgConst.DBTypeTiDB ||
986986
dbType == pkgConst.DBTypeTDSQLForInnoDB ||
987987
dbType == pkgConst.DBTypeGoldenDB ||
988-
dbType == pkgConst.DBTypePolarDBForMySQL ||
989-
dbType == pkgConst.DBTypeMongoDB
988+
dbType == pkgConst.DBTypePolarDBForMySQL
990989
}
991990

992991
func buildMongoDatasourceOptions(dbService *biz.DBService) (*string, interface{}, map[string]interface{}) {
@@ -996,35 +995,34 @@ func buildMongoDatasourceOptions(dbService *biz.DBService) (*string, interface{}
996995
defaultSchema = &defaultDatabase
997996
}
998997

999-
properties := map[string]interface{}{}
1000-
if defaultDatabase != "" {
1001-
properties["defaultDatabase"] = defaultDatabase
1002-
}
998+
jdbcParams := map[string]interface{}{}
1003999
if authDB := dbService.AdditionalParams.GetParam(mongoAuthDatabaseParam).String(); authDB != "" {
1004-
properties["authenticationDatabase"] = authDB
1000+
jdbcParams["authSource"] = authDB
10051001
}
10061002
if authMechanism := dbService.AdditionalParams.GetParam(mongoAuthMechanismParam).String(); authMechanism != "" {
1007-
properties["authMechanism"] = authMechanism
1003+
jdbcParams["authMechanism"] = authMechanism
10081004
}
10091005
if replicaSet := dbService.AdditionalParams.GetParam(mongoReplicaSetParam).String(); replicaSet != "" {
1010-
properties["replicaSet"] = replicaSet
1006+
jdbcParams["replicaSet"] = replicaSet
10111007
}
1012-
if len(properties) > 0 {
1013-
properties["tlsEnabled"] = dbService.AdditionalParams.GetParam(mongoTLSEnabledParam).Bool()
1008+
if tlsParam := dbService.AdditionalParams.GetParam(mongoTLSEnabledParam); tlsParam != nil && tlsParam.String() != "" {
1009+
if tlsParam.Bool() {
1010+
jdbcParams["tls"] = "true"
1011+
} else {
1012+
jdbcParams["tls"] = "false"
1013+
}
10141014
}
1015-
1016-
jdbcParams := map[string]interface{}{}
10171015
if dbService.AdditionalParams.GetParam(mongoDirectConnectionParam).Bool() {
10181016
jdbcParams["directConnection"] = true
10191017
}
10201018
if dbService.AdditionalParams.GetParam(mongoTLSSkipVerifyParam).Bool() {
10211019
jdbcParams["tlsInsecure"] = true
10221020
}
10231021

1024-
if len(properties) == 0 {
1025-
return defaultSchema, nil, jdbcParams
1022+
if len(jdbcParams) == 0 {
1023+
return defaultSchema, nil, nil
10261024
}
1027-
return defaultSchema, properties, jdbcParams
1025+
return defaultSchema, nil, jdbcParams
10281026
}
10291027

10301028
func interfacePtr(v interface{}) *interface{} {

internal/sql_workbench/service/sql_workbench_service_test.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func Test_SupportDBType(t *testing.T) {
5151
"TiDB supported": {input: pkgConst.DBTypeTiDB, expected: true},
5252
"TDSQL supported": {input: pkgConst.DBTypeTDSQLForInnoDB, expected: true},
5353
"GoldenDB supported": {input: pkgConst.DBTypeGoldenDB, expected: true},
54-
"MongoDB supported": {input: pkgConst.DBTypeMongoDB, expected: true},
54+
"MongoDB unsupported": {input: pkgConst.DBTypeMongoDB, expected: false},
5555
"PostgreSQL unsupported": {input: pkgConst.DBTypePostgreSQL, expected: false},
5656
"SQL Server unsupported": {input: pkgConst.DBTypeSQLServer, expected: false},
5757
"PolarDB For MySQL supported": {input: pkgConst.DBTypePolarDBForMySQL, expected: true},
@@ -85,18 +85,38 @@ func Test_buildMongoDatasourceOptions(t *testing.T) {
8585
if defaultSchema == nil || *defaultSchema != defaultDB {
8686
t.Fatalf("unexpected default schema: %#v", defaultSchema)
8787
}
88-
properties, ok := propertiesValue.(map[string]interface{})
89-
if !ok {
90-
t.Fatalf("unexpected properties type: %T", propertiesValue)
88+
if propertiesValue != nil {
89+
t.Fatalf("expected nil properties, got %#v", propertiesValue)
9190
}
92-
if properties["authenticationDatabase"] != "admin" {
93-
t.Fatalf("unexpected auth db: %#v", properties)
91+
if jdbcParams["authSource"] != "admin" {
92+
t.Fatalf("unexpected authSource: %#v", jdbcParams["authSource"])
9493
}
95-
if properties["tlsEnabled"] != true {
96-
t.Fatalf("unexpected tlsEnabled: %#v", properties)
94+
if jdbcParams["authMechanism"] != "SCRAM-SHA-256" {
95+
t.Fatalf("unexpected authMechanism: %#v", jdbcParams["authMechanism"])
96+
}
97+
if jdbcParams["replicaSet"] != "rs0" {
98+
t.Fatalf("unexpected replicaSet: %#v", jdbcParams["replicaSet"])
99+
}
100+
if jdbcParams["tls"] != "true" {
101+
t.Fatalf("unexpected tls: %#v", jdbcParams["tls"])
97102
}
98103
if jdbcParams["directConnection"] != true || jdbcParams["tlsInsecure"] != true {
99104
t.Fatalf("unexpected jdbc params: %#v", jdbcParams)
100105
}
101106
}
102107

108+
func Test_buildMongoDatasourceOptions_tlsOnly(t *testing.T) {
109+
_, propertiesValue, jdbcParams := buildMongoDatasourceOptions(&biz.DBService{
110+
DBType: string(pkgConst.DBTypeMongoDB),
111+
AdditionalParams: pkgParams.Params{
112+
&pkgParams.Param{Key: mongoTLSEnabledParam, Value: "true", Type: pkgParams.ParamTypeBool},
113+
},
114+
})
115+
if propertiesValue != nil {
116+
t.Fatalf("expected nil properties, got %#v", propertiesValue)
117+
}
118+
if jdbcParams["tls"] != "true" {
119+
t.Fatalf("expected tls in jdbcUrlParameters when only tls is configured, got %#v", jdbcParams)
120+
}
121+
}
122+

0 commit comments

Comments
 (0)