From 0875a7c753096cc967202ccc8ec219656f74af82 Mon Sep 17 00:00:00 2001 From: TeoV Date: Mon, 18 Mar 2019 16:13:58 +0200 Subject: [PATCH] Add a new section in config "apier" --- config/apiercfg.go | 42 ++++++++++++++++++++++++++++++++++++++ config/config.go | 10 +++++++++ config/config_defaults.go | 7 +++++++ config/config_json.go | 16 ++++++++++++--- config/config_json_test.go | 17 +++++++++++++++ config/config_test.go | 15 ++++++++++++++ config/libconfig_json.go | 5 +++++ 7 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 config/apiercfg.go diff --git a/config/apiercfg.go b/config/apiercfg.go new file mode 100644 index 000000000..8fad6a6af --- /dev/null +++ b/config/apiercfg.go @@ -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 +*/ + +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 +} diff --git a/config/config.go b/config/config.go index 57a94414a..3f0c37a02 100755 --- a/config/config.go +++ b/config/config.go @@ -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 diff --git a/config/config_defaults.go b/config/config_defaults.go index 438bb8010..f79cf1e4a 100755 --- a/config/config_defaults.go +++ b/config/config_defaults.go @@ -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) +}, + }` diff --git a/config/config_json.go b/config/config_json.go index 29b6f22ae..f4039e71a 100644 --- a/config/config_json.go +++ b/config/config_json.go @@ -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 +} diff --git a/config/config_json_test.go b/config/config_json_test.go index 00caab5ca..eb0792d35 100755 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -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)) + } +} diff --git a/config/config_test.go b/config/config_test.go index 35ceadd45..e22c5c829 100755 --- a/config/config_test.go +++ b/config/config_test.go @@ -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) + } +} diff --git a/config/libconfig_json.go b/config/libconfig_json.go index 529ad79fb..f32a726fc 100755 --- a/config/libconfig_json.go +++ b/config/libconfig_json.go @@ -583,3 +583,8 @@ type FcTemplateJsonCfg struct { type AnalyzerSJsonCfg struct { Enabled *bool } + +type ApierJsonCfg struct { + Caches_conns *[]*HaPoolJsonCfg + Default_cache *string +}