Files
cgrates/config/stordbcfg.go
2020-09-30 13:28:16 +02:00

161 lines
4.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 config
import (
"fmt"
"strconv"
"strings"
"github.com/cgrates/cgrates/utils"
)
// StorDbCfg StroreDb config
type StorDbCfg struct {
Type string // Should reflect the database type used to store logs
Host string // The host to connect to. Values that start with / are for UNIX domain sockets.
Port string // Th e port to bind to.
Name string // The name of the database to connect to.
User string // The user to sign in as.
Password string // The user's password.
StringIndexedFields []string
PrefixIndexedFields []string
RmtConns []string // Remote DataDB connIDs
RplConns []string // Replication connIDs
Items map[string]*ItemOpt
Opts map[string]interface{}
}
// loadFromJsonCfg loads StoreDb config from JsonCfg
func (dbcfg *StorDbCfg) loadFromJsonCfg(jsnDbCfg *DbJsonCfg) (err error) {
if jsnDbCfg == nil {
return nil
}
if jsnDbCfg.Db_type != nil {
dbcfg.Type = strings.TrimPrefix(*jsnDbCfg.Db_type, "*")
}
if jsnDbCfg.Db_host != nil {
dbcfg.Host = *jsnDbCfg.Db_host
}
if jsnDbCfg.Db_port != nil {
port := strconv.Itoa(*jsnDbCfg.Db_port)
if port == "-1" {
port = utils.MetaDynamic
}
dbcfg.Port = dbDefaultsCfg.dbPort(dbcfg.Type, port)
}
if jsnDbCfg.Db_name != nil {
dbcfg.Name = *jsnDbCfg.Db_name
}
if jsnDbCfg.Db_user != nil {
dbcfg.User = *jsnDbCfg.Db_user
}
if jsnDbCfg.Db_password != nil {
dbcfg.Password = *jsnDbCfg.Db_password
}
if jsnDbCfg.String_indexed_fields != nil {
dbcfg.StringIndexedFields = *jsnDbCfg.String_indexed_fields
}
if jsnDbCfg.Prefix_indexed_fields != nil {
dbcfg.PrefixIndexedFields = *jsnDbCfg.Prefix_indexed_fields
}
if jsnDbCfg.Remote_conns != nil {
dbcfg.RmtConns = make([]string, len(*jsnDbCfg.Remote_conns))
for i, item := range *jsnDbCfg.Remote_conns {
if item == utils.MetaInternal {
return fmt.Errorf("Remote connection ID needs to be different than *internal")
}
dbcfg.RmtConns[i] = item
}
}
if jsnDbCfg.Replication_conns != nil {
dbcfg.RplConns = make([]string, len(*jsnDbCfg.Replication_conns))
for i, item := range *jsnDbCfg.Replication_conns {
if item == utils.MetaInternal {
return fmt.Errorf("Replication connection ID needs to be different than *internal")
}
dbcfg.RplConns[i] = item
}
}
if jsnDbCfg.Opts != nil {
for k, v := range jsnDbCfg.Opts {
dbcfg.Opts[k] = v
}
}
if jsnDbCfg.Items != nil {
for kJsn, vJsn := range *jsnDbCfg.Items {
val := new(ItemOpt)
if err := val.loadFromJsonCfg(vJsn); err != nil {
return err
}
dbcfg.Items[kJsn] = val
}
}
return nil
}
// Clone returns the cloned object
func (dbcfg *StorDbCfg) Clone() *StorDbCfg {
items := make(map[string]*ItemOpt)
for key, item := range dbcfg.Items {
items[key] = item.Clone()
}
return &StorDbCfg{
Type: dbcfg.Type,
Host: dbcfg.Host,
Port: dbcfg.Port,
Name: dbcfg.Name,
User: dbcfg.User,
Password: dbcfg.Password,
StringIndexedFields: dbcfg.StringIndexedFields,
PrefixIndexedFields: dbcfg.PrefixIndexedFields,
RmtConns: dbcfg.RmtConns,
RplConns: dbcfg.RplConns,
Items: items,
Opts: dbcfg.Opts,
}
}
func (dbcfg *StorDbCfg) AsMapInterface() (initialMP map[string]interface{}) {
initialMP = map[string]interface{}{
utils.DataDbTypeCfg: utils.Meta + dbcfg.Type,
utils.DataDbHostCfg: dbcfg.Host,
utils.DataDbNameCfg: dbcfg.Name,
utils.DataDbUserCfg: dbcfg.User,
utils.DataDbPassCfg: dbcfg.Password,
utils.StringIndexedFieldsCfg: dbcfg.StringIndexedFields,
utils.PrefixIndexedFieldsCfg: dbcfg.PrefixIndexedFields,
utils.RmtConnsCfg: dbcfg.RmtConns,
utils.RpcConns: dbcfg.RplConns,
utils.OptsCfg: dbcfg.Opts,
}
if dbcfg.Items != nil {
items := make(map[string]interface{})
for key, item := range dbcfg.Items {
items[key] = item.AsMapInterface()
}
initialMP[utils.ItemsCfg] = items
}
if dbcfg.Port != utils.EmptyString {
dbPort, _ := strconv.Atoi(dbcfg.Port)
initialMP[utils.DataDbPortCfg] = dbPort
}
return
}