github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/mongodb/client.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package mongodb 21 22 import ( 23 "context" 24 25 "github.com/pkg/errors" 26 "go.mongodb.org/mongo-driver/mongo" 27 "go.mongodb.org/mongo-driver/mongo/options" 28 "go.mongodb.org/mongo-driver/mongo/readpref" 29 "go.mongodb.org/mongo-driver/mongo/writeconcern" 30 ) 31 32 func NewMongodbClient(ctx context.Context, config *Config) (*mongo.Client, error) { 33 if len(config.hosts) == 0 { 34 return nil, errors.New("Get replset client whitout hosts") 35 } 36 37 opts := options.Client(). 38 SetHosts(config.hosts). 39 SetReplicaSet(config.replSetName). 40 SetAuth(options.Credential{ 41 Password: config.password, 42 Username: config.username, 43 }). 44 SetWriteConcern(writeconcern.New(writeconcern.WMajority(), writeconcern.J(true))). 45 SetReadPreference(readpref.Primary()). 46 SetDirect(config.direct) 47 48 client, err := mongo.Connect(ctx, opts) 49 if err != nil { 50 return nil, errors.Wrap(err, "connect to mongodb") 51 } 52 return client, nil 53 } 54 55 func NewReplSetClient(ctx context.Context, hosts []string) (*mongo.Client, error) { 56 config := GetConfig().DeepCopy() 57 config.hosts = hosts 58 config.direct = false 59 return NewMongodbClient(ctx, config) 60 61 } 62 63 func NewMongosClient(ctx context.Context, hosts []string) (*mongo.Client, error) { 64 config := GetConfig().DeepCopy() 65 config.hosts = hosts 66 config.direct = false 67 config.replSetName = "" 68 69 return NewMongodbClient(ctx, config) 70 } 71 72 func NewStandaloneClient(ctx context.Context, host string) (*mongo.Client, error) { 73 config := GetConfig().DeepCopy() 74 config.hosts = []string{host} 75 config.direct = true 76 config.replSetName = "" 77 78 return NewMongodbClient(ctx, config) 79 } 80 81 func NewLocalUnauthClient(ctx context.Context) (*mongo.Client, error) { 82 config := GetConfig().DeepCopy() 83 config.direct = true 84 config.replSetName = "" 85 86 opts := options.Client(). 87 SetHosts(config.hosts). 88 SetWriteConcern(writeconcern.New(writeconcern.WMajority(), writeconcern.J(true))). 89 SetReadPreference(readpref.Primary()). 90 SetDirect(config.direct) 91 92 client, err := mongo.Connect(ctx, opts) 93 if err != nil { 94 return nil, errors.Wrap(err, "connect to mongodb") 95 } 96 97 return client, nil 98 }