Add a new section in config "apier"

This commit is contained in:
TeoV
2019-03-18 16:13:58 +02:00
committed by Dan Christian Bogos
parent a6363e0a33
commit 0875a7c753
7 changed files with 109 additions and 3 deletions

42
config/apiercfg.go Normal file
View File

@@ -0,0 +1,42 @@
/*
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
// ApierCfg is the configuration of Apier service
type ApierCfg struct {
CachesConns []*HaPoolConfig // connections towards Cache
DefaultCache string
}
func (aCfg *ApierCfg) loadFromJsonCfg(jsnCfg *ApierJsonCfg) (err error) {
if jsnCfg == nil {
return
}
if jsnCfg.Caches_conns != nil {
aCfg.CachesConns = make([]*HaPoolConfig, len(*jsnCfg.Caches_conns))
for idx, jsnHaCfg := range *jsnCfg.Caches_conns {
aCfg.CachesConns[idx] = NewDfltHaPoolConfig()
aCfg.CachesConns[idx].loadFromJsonCfg(jsnHaCfg)
}
}
if jsnCfg.Default_cache != nil {
aCfg.DefaultCache = *jsnCfg.Default_cache
}
return nil
}

View File

@@ -165,6 +165,7 @@ func NewDefaultCGRConfig() (*CGRConfig, error) {
cfg.mailerCfg = new(MailerCfg)
cfg.loaderCfg = make([]*LoaderSCfg, 0)
cfg.SmOsipsConfig = new(SmOsipsConfig)
cfg.apier = new(ApierCfg)
cfg.ConfigReloads = make(map[string]chan struct{})
cfg.ConfigReloads[utils.CDRC] = make(chan struct{}, 1)
@@ -336,6 +337,7 @@ type CGRConfig struct {
mailerCfg *MailerCfg // Mailer config
analyzerSCfg *AnalyzerSCfg // AnalyzerS config
SmOsipsConfig *SmOsipsConfig // SMOpenSIPS Configuration
apier *ApierCfg
}
func (self *CGRConfig) checkConfigSanity() error {
@@ -967,6 +969,14 @@ func (self *CGRConfig) loadFromJsonCfg(jsnCfg *CgrJsonCfg) (err error) {
return err
}
jsnApierCfg, err := jsnCfg.ApierCfgJson()
if err != nil {
return nil
}
if err := self.apier.loadFromJsonCfg(jsnApierCfg); err != nil {
return err
}
if jsnCdreCfg != nil {
for profileName, jsnCdre1Cfg := range jsnCdreCfg {
if _, hasProfile := self.CdreProfiles[profileName]; !hasProfile { // New profile, create before loading from json

View File

@@ -750,4 +750,11 @@ const CGRATES_CFG_JSON = `
},
"apier": {
"caches_conns":[ // connections to CacheS for reloads
{"address": "127.0.0.1:2012", "transport": "*json"}
],
"default_cache":"", // default actions to do when caching items(""-default caching by data manager, "*none"-disable caching)
},
}`

View File

@@ -38,20 +38,17 @@ const (
RALS_JSN = "rals"
SCHEDULER_JSN = "scheduler"
CDRS_JSN = "cdrs"
MEDIATOR_JSN = "mediator"
CDRE_JSN = "cdre"
CDRC_JSN = "cdrc"
SessionSJson = "sessions"
FreeSWITCHAgentJSN = "freeswitch_agent"
KamailioAgentJSN = "kamailio_agent"
AsteriskAgentJSN = "asterisk_agent"
SM_JSN = "session_manager"
FS_JSN = "freeswitch"
OSIPS_JSN = "opensips"
DA_JSN = "diameter_agent"
RA_JSN = "radius_agent"
HttpAgentJson = "http_agent"
HISTSERV_JSN = "historys"
ATTRIBUTE_JSN = "attributes"
RESOURCES_JSON = "resources"
STATS_JSON = "stats"
@@ -68,6 +65,7 @@ const (
ChargerSCfgJson = "chargers"
TlsCfgJson = "tls"
AnalyzerCfgJson = "analyzers"
Apier = "apier"
)
// Loads the json config out of io.Reader, eg other sources than file, maybe over http
@@ -489,3 +487,15 @@ func (self CgrJsonCfg) AnalyzerCfgJson() (*AnalyzerSJsonCfg, error) {
}
return cfg, nil
}
func (self CgrJsonCfg) ApierCfgJson() (*ApierJsonCfg, error) {
rawCfg, hasKey := self[Apier]
if !hasKey {
return nil, nil
}
cfg := new(ApierJsonCfg)
if err := json.Unmarshal(*rawCfg, cfg); err != nil {
return nil, err
}
return cfg, nil
}

View File

@@ -1492,3 +1492,20 @@ func TestDfAnalyzerCfg(t *testing.T) {
t.Errorf("Expected: %+v, received: %+v", utils.ToJSON(eCfg), utils.ToJSON(cfg))
}
}
func TestDfApierCfg(t *testing.T) {
eCfg := &ApierJsonCfg{
Caches_conns: &[]*HaPoolJsonCfg{
{
Address: utils.StringPointer("127.0.0.1:2012"),
Transport: utils.StringPointer(utils.MetaJSONrpc),
},
},
Default_cache: utils.StringPointer(utils.EmptyString),
}
if cfg, err := dfCgrJsonCfg.ApierCfgJson(); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eCfg, cfg) {
t.Errorf("Expected: %+v, received: %+v", utils.ToJSON(eCfg), utils.ToJSON(cfg))
}
}

View File

@@ -1700,3 +1700,18 @@ func TestNewCGRConfigFromPathNotFound(t *testing.T) {
t.Fatalf("Expected error,received %v", cfg)
}
}
func TestCgrCfgJSONDefaultApierCfg(t *testing.T) {
aCfg := &ApierCfg{
CachesConns: []*HaPoolConfig{
{
Address: "127.0.0.1:2012",
Transport: utils.MetaJSONrpc,
},
},
DefaultCache: utils.EmptyString,
}
if !reflect.DeepEqual(cgrCfg.apier, aCfg) {
t.Errorf("received: %+v, expecting: %+v", cgrCfg.apier, aCfg)
}
}

View File

@@ -583,3 +583,8 @@ type FcTemplateJsonCfg struct {
type AnalyzerSJsonCfg struct {
Enabled *bool
}
type ApierJsonCfg struct {
Caches_conns *[]*HaPoolJsonCfg
Default_cache *string
}