Files
cgrates/engine/storage_utils.go
2022-03-09 14:14:30 +01:00

59 lines
1.9 KiB
Go

/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package engine
import (
"fmt"
"strconv"
"strings"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
)
// Various helpers to deal with database
// NewDataDBConn creates a DataDB connection
func NewDataDBConn(dbType, host, port, name, user,
pass, marshaler string, opts *config.DataDBOpts,
itmsCfg map[string]*config.ItemOpts) (d DataDBDriver, err error) {
switch dbType {
case utils.Redis:
var dbNo int
dbNo, err = strconv.Atoi(name)
if err != nil {
utils.Logger.Crit("Redis db name must be an integer!")
return
}
if port != "" && !strings.Contains(host, ":") {
host += ":" + port
}
d, err = NewRedisStorage(host, dbNo, user, pass, marshaler, utils.RedisMaxConns, utils.RedisMaxAttempts,
opts.RedisSentinel, opts.RedisCluster, opts.RedisClusterSync, opts.RedisClusterOndownDelay,
opts.RedisTLS, opts.RedisClientCertificate, opts.RedisClientKey, opts.RedisCACertificate)
case utils.Mongo:
d, err = NewMongoStorage(host, port, name, user, pass, marshaler, utils.DataDB, nil, opts.MongoQueryTimeout)
case utils.Internal:
d = NewInternalDB(nil, nil, itmsCfg)
default:
err = fmt.Errorf("unsupported db_type <%s>", dbType)
}
return
}