From 21e08d5a927cf9711ba243332b510dbcf12f861d Mon Sep 17 00:00:00 2001 From: TeoV Date: Fri, 4 Sep 2020 17:54:05 +0300 Subject: [PATCH] Add new section "configs" --- agents/diamagent.go | 8 ++++---- cmd/cgr-engine/cgr-engine.go | 4 ++-- config/config.go | 29 ++++++++++++++++++++++---- config/config_defaults.go | 14 +++++++++++-- config/config_json.go | 15 +++++++++++++- config/config_json_test.go | 1 - config/config_test.go | 11 ++++++++++ config/configs.go | 40 ++++++++++++++++++++++++++++++++++++ config/httpcfg.go | 6 ------ config/httpcfg_test.go | 2 -- config/libconfig_json.go | 6 +++--- services/ees_it_test.go | 2 +- 12 files changed, 112 insertions(+), 26 deletions(-) create mode 100644 config/configs.go diff --git a/agents/diamagent.go b/agents/diamagent.go index c005934e2..56299d7c3 100644 --- a/agents/diamagent.go +++ b/agents/diamagent.go @@ -60,7 +60,7 @@ func NewDiameterAgent(cgrCfg *config.CGRConfig, filterS *engine.FilterS, return nil, err } } - msgTemplates := da.cgrCfg.TemplateCfg() + msgTemplates := da.cgrCfg.TemplatesCfg() // Inflate *template field types for _, procsr := range da.cgrCfg.DiameterAgentCfg().RequestProcessors { if tpls, err := config.InflateTemplates(procsr.RequestFields, msgTemplates); err != nil { @@ -185,7 +185,7 @@ func (da *DiameterAgent) handleMessage(c diam.Conn, m *diam.Message) { // build the negative error answer diamErr, err := diamErr( m, diam.UnableToComply, reqVars, - da.cgrCfg.TemplateCfg()[utils.MetaErr], + da.cgrCfg.TemplatesCfg()[utils.MetaErr], da.cgrCfg.GeneralCfg().DefaultTenant, da.cgrCfg.GeneralCfg().DefaultTimezone, da.filterS) @@ -518,7 +518,7 @@ func (da *DiameterAgent) sendASR(originID string, reply *string) (err error) { dmd.vars, nil, nil, nil, nil, da.cgrCfg.GeneralCfg().DefaultTenant, da.cgrCfg.GeneralCfg().DefaultTimezone, da.filterS, nil, nil) - if err = aReq.SetFields(da.cgrCfg.TemplateCfg()[da.cgrCfg.DiameterAgentCfg().ASRTemplate]); err != nil { + if err = aReq.SetFields(da.cgrCfg.TemplatesCfg()[da.cgrCfg.DiameterAgentCfg().ASRTemplate]); err != nil { utils.Logger.Warning( fmt.Sprintf("<%s> cannot disconnect session with OriginID: <%s>, err: %s", utils.DiameterAgent, originID, err.Error())) @@ -561,7 +561,7 @@ func (da *DiameterAgent) V1ReAuthorize(originID string, reply *string) (err erro dmd.vars, nil, nil, nil, nil, da.cgrCfg.GeneralCfg().DefaultTenant, da.cgrCfg.GeneralCfg().DefaultTimezone, da.filterS, nil, nil) - if err = aReq.SetFields(da.cgrCfg.TemplateCfg()[da.cgrCfg.DiameterAgentCfg().RARTemplate]); err != nil { + if err = aReq.SetFields(da.cgrCfg.TemplatesCfg()[da.cgrCfg.DiameterAgentCfg().RARTemplate]); err != nil { utils.Logger.Warning( fmt.Sprintf("<%s> cannot send RAR with OriginID: <%s>, err: %s", utils.DiameterAgent, originID, err.Error())) diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index 1420ad9c7..c98688add 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -508,8 +508,8 @@ func main() { if len(cfg.HTTPCfg().DispatchersRegistrarURL) != 0 { server.RegisterHttpFunc(cfg.HTTPCfg().DispatchersRegistrarURL, dispatcherh.Registar) } - if len(cfg.HTTPCfg().ConfigsURL) != 0 { - server.RegisterHttpFunc(cfg.HTTPCfg().ConfigsURL, config.HandlerConfigS) + if cfg.ConfigSCfg().Enabled { + server.RegisterHttpFunc(cfg.ConfigSCfg().Url, config.HandlerConfigS) } if *httpPprofPath != "" { go server.RegisterProfiler(*httpPprofPath) diff --git a/config/config.go b/config/config.go index bc2c1a0b9..50a56ccd6 100755 --- a/config/config.go +++ b/config/config.go @@ -190,6 +190,7 @@ func NewDefaultCGRConfig() (cfg *CGRConfig, err error) { cfg.eesCfg.Cache = make(map[string]*CacheParamCfg) cfg.rateSCfg = new(RateSCfg) cfg.sipAgentCfg = new(SIPAgentCfg) + cfg.configSCfg = new(ConfigSCfg) cfg.ConfigReloads = make(map[string]chan struct{}) @@ -312,6 +313,7 @@ type CGRConfig struct { eesCfg *EEsCfg // EventExporter config rateSCfg *RateSCfg // RateS config sipAgentCfg *SIPAgentCfg // SIPAgent config + configSCfg *ConfigSCfg //ConfigS config } var posibleLoaderTypes = utils.NewStringSet([]string{utils.MetaAttributes, @@ -377,7 +379,8 @@ func (cfg *CGRConfig) loadFromJsonCfg(jsnCfg *CgrJsonCfg) (err error) { cfg.loadMailerCfg, cfg.loadSureTaxCfg, cfg.loadDispatcherSCfg, cfg.loadLoaderCgrCfg, cfg.loadMigratorCgrCfg, cfg.loadTlsCgrCfg, cfg.loadAnalyzerCgrCfg, cfg.loadApierCfg, cfg.loadErsCfg, cfg.loadEesCfg, - cfg.loadRateSCfg, cfg.loadSIPAgentCfg, cfg.loadDispatcherHCfg} { + cfg.loadRateSCfg, cfg.loadSIPAgentCfg, cfg.loadDispatcherHCfg, + cfg.loadConfigSCfg} { if err = loadFunc(jsnCfg); err != nil { return } @@ -778,6 +781,14 @@ func (cfg *CGRConfig) loadTemplateSCfg(jsnCfg *CgrJsonCfg) (err error) { return } +func (cfg *CGRConfig) loadConfigSCfg(jsnCfg *CgrJsonCfg) (err error) { + var jsnConfigSCfg *ConfigSCfgJson + if jsnConfigSCfg, err = jsnCfg.ConfigSJsonCfg(); err != nil { + return + } + return cfg.configSCfg.loadFromJsonCfg(jsnConfigSCfg) +} + // SureTaxCfg use locking to retrieve the configuration, possibility later for runtime reload func (cfg *CGRConfig) SureTaxCfg() *SureTaxCfg { cfg.lks[SURETAX_JSON].Lock() @@ -1055,13 +1066,20 @@ func (cfg *CGRConfig) RPCConns() map[string]*RPCConn { return cfg.rpcConns } -// DiameterAgentCfg returns the config for Diameter Agent -func (cfg *CGRConfig) TemplateCfg() map[string][]*FCTemplate { +// TemplatesCfg returns the config for templates +func (cfg *CGRConfig) TemplatesCfg() map[string][]*FCTemplate { cfg.lks[TemplatesJson].Lock() defer cfg.lks[TemplatesJson].Unlock() return cfg.templates } +// ConfigSCfg returns the configs configuration +func (cfg *CGRConfig) ConfigSCfg() *ConfigSCfg { + cfg.lks[ConfigSJson].RLock() + defer cfg.lks[ConfigSJson].RUnlock() + return cfg.configSCfg +} + // GetReloadChan returns the reload chanel for the given section func (cfg *CGRConfig) GetReloadChan(sectID string) chan struct{} { return cfg.rldChans[sectID] @@ -1155,7 +1173,9 @@ func (cfg *CGRConfig) V1GetConfigSection(args *SectionWithOpts, reply *map[strin case SIPAgentJson: jsonString = utils.ToJSON(cfg.SIPAgentCfg()) case TemplatesJson: - jsonString = utils.ToJSON(cfg.TemplateCfg()) + jsonString = utils.ToJSON(cfg.TemplatesCfg()) + case ConfigSJson: + jsonString = utils.ToJSON(cfg.ConfigSCfg()) default: return errors.New("Invalid section") } @@ -1283,6 +1303,7 @@ func (cfg *CGRConfig) getLoadFunctions() map[string]func(*CgrJsonCfg) error { RateSJson: cfg.loadRateSCfg, SIPAgentJson: cfg.loadSIPAgentCfg, TemplatesJson: cfg.loadTemplateSCfg, + ConfigSJson: cfg.loadConfigSCfg, } } diff --git a/config/config_defaults.go b/config/config_defaults.go index ce3d70022..18b6841e5 100755 --- a/config/config_defaults.go +++ b/config/config_defaults.go @@ -185,7 +185,6 @@ const CGRATES_CFG_JSON = ` "http_cdrs": "/cdr_http", // CDRS relative URL ("" to disable) "use_basic_auth": false, // use basic authentication "auth_users": {}, // basic authentication usernames and base64-encoded passwords (eg: { "username1": "cGFzc3dvcmQ=", "username2": "cGFzc3dvcmQy "}) - "configs_url": "/configs/" // configs }, @@ -918,10 +917,11 @@ const CGRATES_CFG_JSON = ` "rate_indexed_selects": true, // enable profile matching exclusively on indexes //"rate_string_indexed_fields": [], // query indexes based on these fields for faster processing "rate_prefix_indexed_fields": [], // query indexes based on these fields for faster processing - "rate_suffix_indexed_fields": [], // query indexes based on these fields for faster processing + "rate_suffix_indexed_fields": [], // query indexes based on these fields for faster processing "rate_nested_fields": false, // determines which field is checked when matching indexed filters(true: all; false: only the one on the first level) }, + "sip_agent": { // SIP Agents, only used for redirections "enabled": false, // enables the SIP agent: "listen": "127.0.0.1:5060", // address where to listen for SIP requests @@ -933,6 +933,7 @@ const CGRATES_CFG_JSON = ` ], }, + "templates": { "*err": [ {"tag": "SessionId", "path": "*rep.Session-Id", "type": "*variable", @@ -993,4 +994,13 @@ const CGRATES_CFG_JSON = ` "value": "SIP/2.0 500 Internal Server Error", "mandatory": true}, ], }, + + +"configs": { + "enabled": false, + "url": "/configs/", // configs url + "root_dir": "/var/spool/cgrates/configs", // root directory in case of calling /configs request +}, + + }` diff --git a/config/config_json.go b/config/config_json.go index 59e9bac68..6e7264748 100644 --- a/config/config_json.go +++ b/config/config_json.go @@ -63,6 +63,7 @@ const ( RPCConnsJsonName = "rpc_conns" SIPAgentJson = "sip_agent" TemplatesJson = "templates" + ConfigSJson = "configs" ) var ( @@ -70,7 +71,7 @@ var ( CACHE_JSN, FilterSjsn, RALS_JSN, CDRS_JSN, ERsJson, SessionSJson, AsteriskAgentJSN, FreeSWITCHAgentJSN, KamailioAgentJSN, DA_JSN, RA_JSN, HttpAgentJson, DNSAgentJson, ATTRIBUTE_JSN, ChargerSCfgJson, RESOURCES_JSON, STATS_JSON, THRESHOLDS_JSON, RouteSJson, LoaderJson, MAILER_JSN, SURETAX_JSON, CgrLoaderCfgJson, CgrMigratorCfgJson, DispatcherSJson, - AnalyzerCfgJson, ApierS, EEsJson, RateSJson, SIPAgentJson, DispatcherHJson, TemplatesJson} + AnalyzerCfgJson, ApierS, EEsJson, RateSJson, SIPAgentJson, DispatcherHJson, TemplatesJson, ConfigSJson} ) // Loads the json config out of io.Reader, eg other sources than file, maybe over http @@ -544,3 +545,15 @@ func (self CgrJsonCfg) TemplateSJsonCfg() (map[string][]*FcTemplateJsonCfg, erro } return cfg, nil } + +func (self CgrJsonCfg) ConfigSJsonCfg() (*ConfigSCfgJson, error) { + rawCfg, hasKey := self[ConfigSJson] + if !hasKey { + return nil, nil + } + cfg := new(ConfigSCfgJson) + 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 5d5a2e69f..2e76c34a2 100755 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -1557,7 +1557,6 @@ func TestDfHttpJsonCfg(t *testing.T) { Http_Cdrs: utils.StringPointer("/cdr_http"), Use_basic_auth: utils.BoolPointer(false), Auth_users: utils.MapStringStringPointer(map[string]string{}), - Configs_url: utils.StringPointer("/configs/"), } if cfg, err := dfCgrJSONCfg.HttpJsonCfg(); err != nil { t.Error(err) diff --git a/config/config_test.go b/config/config_test.go index 9c418f1c3..c6bf9bd17 100755 --- a/config/config_test.go +++ b/config/config_test.go @@ -2496,3 +2496,14 @@ func TestRpcConnsDefaults(t *testing.T) { t.Errorf("received: %+v,\n expecting: %+v", utils.ToJSON(cgrCfg.rpcConns), utils.ToJSON(eCfg)) } } + +func TestCgrCfgJSONDefaultsConfigS(t *testing.T) { + eCfg := &ConfigSCfg{ + Enabled: false, + Url: "/configs/", + RootDir: "/var/spool/cgrates/configs", + } + if !reflect.DeepEqual(cgrCfg.configSCfg, eCfg) { + t.Errorf("received: %+v, expecting: %+v", utils.ToJSON(cgrCfg.configSCfg), utils.ToJSON(eCfg)) + } +} diff --git a/config/configs.go b/config/configs.go new file mode 100644 index 000000000..f19ce6959 --- /dev/null +++ b/config/configs.go @@ -0,0 +1,40 @@ +/* +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 + +// ConfigSCfg config for listening over http +type ConfigSCfg struct { + Enabled bool + Url string + RootDir string +} + +//loadFromJsonCfg loads Database config from JsonCfg +func (cScfg *ConfigSCfg) loadFromJsonCfg(jsnCfg *ConfigSCfgJson) (err error) { + if jsnCfg == nil { + return nil + } + if jsnCfg.Enabled != nil { + cScfg.Enabled = *jsnCfg.Enabled + } + if jsnCfg.Url != nil { + cScfg.Url = *jsnCfg.Url + } + if jsnCfg.Root_dir != nil { + cScfg.RootDir = *jsnCfg.Root_dir + } + return +} diff --git a/config/httpcfg.go b/config/httpcfg.go index 331c99fca..c2faaddeb 100644 --- a/config/httpcfg.go +++ b/config/httpcfg.go @@ -29,7 +29,6 @@ type HTTPCfg struct { HTTPCDRsURL string // CDRS relative URL ("" to disable) HTTPUseBasicAuth bool // Use basic auth for HTTP API HTTPAuthUsers map[string]string // Basic auth user:password map (base64 passwords) - ConfigsURL string } //loadFromJsonCfg loads Database config from JsonCfg @@ -58,10 +57,6 @@ func (httpcfg *HTTPCfg) loadFromJsonCfg(jsnHttpCfg *HTTPJsonCfg) (err error) { if jsnHttpCfg.Auth_users != nil { httpcfg.HTTPAuthUsers = *jsnHttpCfg.Auth_users } - if jsnHttpCfg.Configs_url != nil { - httpcfg.ConfigsURL = *jsnHttpCfg.Configs_url - } - return nil } @@ -79,6 +74,5 @@ func (httpcfg *HTTPCfg) AsMapInterface() map[string]interface{} { utils.HTTPCDRsURLCfg: httpcfg.HTTPCDRsURL, utils.HTTPUseBasicAuthCfg: httpcfg.HTTPUseBasicAuth, utils.HTTPAuthUsersCfg: httpUsers, - utils.ConfigsURL: httpcfg.ConfigsURL, } } diff --git a/config/httpcfg_test.go b/config/httpcfg_test.go index 85df0925c..e1e6757d0 100644 --- a/config/httpcfg_test.go +++ b/config/httpcfg_test.go @@ -76,7 +76,6 @@ func TestHTTPCfgAsMapInterface(t *testing.T) { "http_cdrs": "/cdr_http", "use_basic_auth": false, "auth_users": {}, - "configs_url": "/configs/" }, }` @@ -88,7 +87,6 @@ func TestHTTPCfgAsMapInterface(t *testing.T) { "http_cdrs": "/cdr_http", "use_basic_auth": false, "auth_users": map[string]interface{}{}, - "configs_url": "/configs/", } if jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { diff --git a/config/libconfig_json.go b/config/libconfig_json.go index c058059e8..b331cd290 100755 --- a/config/libconfig_json.go +++ b/config/libconfig_json.go @@ -71,7 +71,6 @@ type HTTPJsonCfg struct { Http_Cdrs *string Use_basic_auth *bool Auth_users *map[string]string - Configs_url *string } type TlsJsonCfg struct { @@ -648,6 +647,7 @@ type SIPAgentJsonCfg struct { } type ConfigSCfgJson struct { - Enabled *bool - Listen *string + Enabled *bool + Url *string + Root_dir *string } diff --git a/services/ees_it_test.go b/services/ees_it_test.go index b51d34368..db7346aa8 100644 --- a/services/ees_it_test.go +++ b/services/ees_it_test.go @@ -79,7 +79,7 @@ func TestEventExporterSReload(t *testing.T) { Layout: time.RFC3339, } fcTmp.ComputePath() - cfg.TemplateCfg()["requiredFields"] = []*config.FCTemplate{fcTmp} + cfg.TemplatesCfg()["requiredFields"] = []*config.FCTemplate{fcTmp} var reply string if err := cfg.V1ReloadConfigFromPath(&config.ConfigReloadWithOpts{ Path: path.Join("/usr", "share", "cgrates", "conf", "samples", "ees"),