mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Use simple string concatenation to build URI
No need to keep the Parse call as a validation step since any issue would be caught when establishing a connection. Renamed buildURL to composeURI.
This commit is contained in:
committed by
Dan Christian Bogos
parent
b502920b0b
commit
a057b34505
@@ -151,10 +151,6 @@ func decimalDecoder(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val refle
|
||||
// Returns an error if the setup fails.
|
||||
func NewMongoStorage(host, port, db, user, pass, mrshlerStr string, storageType string,
|
||||
cdrsIndexes []string, ttl time.Duration) (*MongoStorage, error) {
|
||||
url, err := buildURL("mongodb", host, port, db, user, pass)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mongoStorage := &MongoStorage{
|
||||
ctx: context.TODO(),
|
||||
ctxTTL: ttl,
|
||||
@@ -162,19 +158,21 @@ func NewMongoStorage(host, port, db, user, pass, mrshlerStr string, storageType
|
||||
storageType: storageType,
|
||||
counter: utils.NewCounter(time.Now().UnixNano(), 0),
|
||||
}
|
||||
uri := composeURI("mongodb", host, port, db, user, pass)
|
||||
reg := bson.NewRegistry()
|
||||
decimalType := reflect.TypeOf(utils.Decimal{})
|
||||
reg.RegisterTypeEncoder(decimalType, bsoncodec.ValueEncoderFunc(decimalEncoder))
|
||||
reg.RegisterTypeDecoder(decimalType, bsoncodec.ValueDecoderFunc(decimalDecoder))
|
||||
// serverAPI := options.ServerAPI(options.ServerAPIVersion1).SetStrict(true).SetDeprecationErrors(true)
|
||||
opts := options.Client().
|
||||
ApplyURI(url.String()).
|
||||
ApplyURI(uri).
|
||||
SetRegistry(reg).
|
||||
SetServerSelectionTimeout(mongoStorage.ctxTTL).
|
||||
SetRetryWrites(false) // default is true
|
||||
// SetServerAPIOptions(serverAPI)
|
||||
|
||||
// Create a new client and connect to the server
|
||||
var err error
|
||||
mongoStorage.client, err = mongo.Connect(mongoStorage.ctx, opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -1829,3 +1829,87 @@ func TestIDBGetAllActionPlanDrv(t *testing.T) {
|
||||
t.Errorf("Expected : 2,Received %v", len(apl))
|
||||
}
|
||||
}
|
||||
|
||||
func TestComposeURI(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
scheme string
|
||||
host string
|
||||
port string
|
||||
db string
|
||||
user string
|
||||
pass string
|
||||
expected string
|
||||
parseErr bool
|
||||
}{
|
||||
{
|
||||
name: "multiple nodes",
|
||||
scheme: "mongodb",
|
||||
host: "clusternode1:1230,clusternode2:1231,clusternode3",
|
||||
port: "1232",
|
||||
db: "cgrates",
|
||||
user: "user",
|
||||
pass: "pass",
|
||||
expected: "mongodb://user:pass@clusternode1:1230,clusternode2:1231,clusternode3:1232/cgrates",
|
||||
},
|
||||
{
|
||||
name: "no port",
|
||||
scheme: "mongodb",
|
||||
host: "localhost:1234",
|
||||
port: "0",
|
||||
db: "cgrates",
|
||||
user: "user",
|
||||
pass: "pass",
|
||||
expected: "mongodb://user:pass@localhost:1234/cgrates",
|
||||
},
|
||||
{
|
||||
name: "with port",
|
||||
scheme: "mongodb",
|
||||
host: "localhost",
|
||||
port: "1234",
|
||||
db: "cgrates",
|
||||
user: "user",
|
||||
pass: "pass",
|
||||
expected: "mongodb://user:pass@localhost:1234/cgrates",
|
||||
},
|
||||
{
|
||||
name: "no password",
|
||||
scheme: "mongodb",
|
||||
host: "localhost",
|
||||
port: "1234",
|
||||
db: "cgrates",
|
||||
user: "user",
|
||||
pass: "",
|
||||
expected: "mongodb://localhost:1234/cgrates",
|
||||
},
|
||||
{
|
||||
name: "no db",
|
||||
scheme: "mongodb",
|
||||
host: "localhost",
|
||||
port: "1234",
|
||||
db: "",
|
||||
user: "user",
|
||||
pass: "pass",
|
||||
expected: "mongodb://user:pass@localhost:1234",
|
||||
},
|
||||
{
|
||||
name: "different scheme",
|
||||
scheme: "mongodb+srv",
|
||||
host: "cgr.abcdef.mongodb.net",
|
||||
port: "0",
|
||||
db: "?retryWrites=true&w=majority",
|
||||
user: "user",
|
||||
pass: "pass",
|
||||
expected: "mongodb+srv://user:pass@cgr.abcdef.mongodb.net/?retryWrites=true&w=majority",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
url := composeURI(tt.scheme, tt.host, tt.port, tt.db, tt.user, tt.pass)
|
||||
if url != tt.expected {
|
||||
t.Errorf("expected %v,\nreceived %v", tt.expected, url)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,8 +20,6 @@ package engine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -83,20 +81,19 @@ func NewStorDBConn(dbType, host, port, name, user, pass, marshaler string,
|
||||
return
|
||||
}
|
||||
|
||||
func buildURL(scheme, host, port, db, user, pass string) (*url.URL, error) {
|
||||
rawURL := scheme + "://"
|
||||
func composeURI(scheme, host, port, db, user, pass string) string {
|
||||
uri := scheme + "://"
|
||||
if user != "" && pass != "" {
|
||||
rawURL += url.UserPassword(user, pass).String() + "@"
|
||||
uri += user + ":" + pass + "@"
|
||||
}
|
||||
uri += host
|
||||
if port != "0" {
|
||||
rawURL += net.JoinHostPort(host, port)
|
||||
} else {
|
||||
rawURL += host
|
||||
uri += ":" + port
|
||||
}
|
||||
if db != "" {
|
||||
rawURL += "/" + db
|
||||
uri += "/" + db
|
||||
}
|
||||
return url.Parse(rawURL)
|
||||
return uri
|
||||
}
|
||||
|
||||
// SMCost stores one Cost coming from SM
|
||||
|
||||
Reference in New Issue
Block a user