Added MailerCfg

This commit is contained in:
Trial97
2018-10-30 11:58:15 +02:00
committed by Dan Christian Bogos
parent 917710642b
commit 25cdc23352
5 changed files with 128 additions and 30 deletions

View File

@@ -161,6 +161,7 @@ func NewDefaultCGRConfig() (*CGRConfig, error) {
cfg.dispatcherSCfg = new(DispatcherSCfg)
cfg.loaderCgrCfg = new(LoaderCgrCfg)
cfg.migratorCgrCfg = new(MigratorCgrCfg)
cfg.mailerCfg = new(MailerCfg)
//Depricated
cfg.cdrStatsCfg = new(CdrStatsCfg)
@@ -277,11 +278,6 @@ type CGRConfig struct {
httpAgentCfg []*HttpAgentCfg // HttpAgent configuration
MailerServer string // The server to use when sending emails out
MailerAuthUser string // Authenticate to email server using this user
MailerAuthPass string // Authenticate to email server with this password
MailerFromAddr string // From address used when sending emails out
ConfigReloads map[string]chan struct{} // Signals to specific entities that a config reload should occur
generalCfg *GeneralCfg // General config
@@ -311,6 +307,7 @@ type CGRConfig struct {
dispatcherSCfg *DispatcherSCfg // DispatcherS config
loaderCgrCfg *LoaderCgrCfg // LoaderCgr config
migratorCgrCfg *MigratorCgrCfg // MigratorCgr config
mailerCfg *MailerCfg // Mailer config
analyzerSCfg *AnalyzerSCfg
// Deprecated
@@ -931,6 +928,9 @@ func (self *CGRConfig) loadFromJsonCfg(jsnCfg *CgrJsonCfg) (err error) {
if err != nil {
return err
}
if self.mailerCfg.loadFromJsonCfg(jsnMailerCfg); err != nil {
return err
}
jsnSureTaxCfg, err := jsnCfg.SureTaxJsonCfg()
if err != nil {
@@ -1096,21 +1096,6 @@ func (self *CGRConfig) loadFromJsonCfg(jsnCfg *CgrJsonCfg) (err error) {
}
}
///depricated^^^
if jsnMailerCfg != nil {
if jsnMailerCfg.Server != nil {
self.MailerServer = *jsnMailerCfg.Server
}
if jsnMailerCfg.Auth_user != nil {
self.MailerAuthUser = *jsnMailerCfg.Auth_user
}
if jsnMailerCfg.Auth_password != nil {
self.MailerAuthPass = *jsnMailerCfg.Auth_password
}
if jsnMailerCfg.From_address != nil {
self.MailerFromAddr = *jsnMailerCfg.From_address
}
}
return nil
}
@@ -1242,6 +1227,10 @@ func (cfg *CGRConfig) CdrStatsCfg() *CdrStatsCfg {
return cfg.cdrStatsCfg
}
func (cfg *CGRConfig) MailerCfg() *MailerCfg {
return cfg.mailerCfg
}
func (cfg *CGRConfig) AnalyzerSCfg() *AnalyzerSCfg {
return cfg.analyzerSCfg
}

View File

@@ -965,17 +965,17 @@ func TestCgrCfgJSONDefaultsDiameterAgentCfg(t *testing.T) {
}
func TestCgrCfgJSONDefaultsMailer(t *testing.T) {
if cgrCfg.MailerServer != "localhost" {
t.Error(cgrCfg.MailerServer)
if cgrCfg.MailerCfg().MailerServer != "localhost" {
t.Error(cgrCfg.MailerCfg().MailerServer)
}
if cgrCfg.MailerAuthUser != "cgrates" {
t.Error(cgrCfg.MailerAuthUser)
if cgrCfg.MailerCfg().MailerAuthUser != "cgrates" {
t.Error(cgrCfg.MailerCfg().MailerAuthUser)
}
if cgrCfg.MailerAuthPass != "CGRateS.org" {
t.Error(cgrCfg.MailerAuthPass)
if cgrCfg.MailerCfg().MailerAuthPass != "CGRateS.org" {
t.Error(cgrCfg.MailerCfg().MailerAuthPass)
}
if cgrCfg.MailerFromAddr != "cgr-mailer@localhost.localdomain" {
t.Error(cgrCfg.MailerFromAddr)
if cgrCfg.MailerCfg().MailerFromAddr != "cgr-mailer@localhost.localdomain" {
t.Error(cgrCfg.MailerCfg().MailerFromAddr)
}
}

48
config/mailercfg.go Normal file
View File

@@ -0,0 +1,48 @@
/*
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
// Mailer config section
type MailerCfg struct {
MailerServer string // The server to use when sending emails out
MailerAuthUser string // Authenticate to email server using this user
MailerAuthPass string // Authenticate to email server with this password
MailerFromAddr string // From address used when sending emails out
}
//loadFromJsonCfg loads Database config from JsonCfg
func (mailcfg *MailerCfg) loadFromJsonCfg(jsnMailerCfg *MailerJsonCfg) (err error) {
if jsnMailerCfg == nil {
return nil
}
if jsnMailerCfg.Server != nil {
mailcfg.MailerServer = *jsnMailerCfg.Server
}
if jsnMailerCfg.Auth_user != nil {
mailcfg.MailerAuthUser = *jsnMailerCfg.Auth_user
}
if jsnMailerCfg.Auth_password != nil {
mailcfg.MailerAuthPass = *jsnMailerCfg.Auth_password
}
if jsnMailerCfg.From_address != nil {
mailcfg.MailerFromAddr = *jsnMailerCfg.From_address
}
return nil
}

61
config/mailercfg_test.go Normal file
View File

@@ -0,0 +1,61 @@
/*
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 (
"reflect"
"strings"
"testing"
)
func TestMailerCfgloadFromJsonCfg(t *testing.T) {
var mailcfg, expected MailerCfg
if err := mailcfg.loadFromJsonCfg(nil); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(mailcfg, expected) {
t.Errorf("Expected: %+v ,recived: %+v", expected, mailcfg)
}
if err := mailcfg.loadFromJsonCfg(new(MailerJsonCfg)); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(mailcfg, expected) {
t.Errorf("Expected: %+v ,recived: %+v", expected, mailcfg)
}
cfgJSONStr := `{
"mailer": {
"server": "localhost", // the server to use when sending emails out
"auth_user": "cgrates", // authenticate to email server using this user
"auth_password": "CGRateS.org", // authenticate to email server with this password
"from_address": "cgr-mailer@localhost.localdomain" // from address used when sending emails out
},
}`
expected = MailerCfg{
MailerServer: "localhost",
MailerAuthUser: "cgrates",
MailerAuthPass: "CGRateS.org",
MailerFromAddr: "cgr-mailer@localhost.localdomain",
}
if jsnCfg, err := NewCgrJsonCfgFromReader(strings.NewReader(cfgJSONStr)); err != nil {
t.Error(err)
} else if jsnMailCfg, err := jsnCfg.MailerJsonCfg(); err != nil {
t.Error(err)
} else if err = mailcfg.loadFromJsonCfg(jsnMailCfg); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, mailcfg) {
t.Errorf("Expected: %+v , recived: %+v", expected, mailcfg)
}
}

View File

@@ -442,10 +442,10 @@ func mailAsync(ub *Account, sq *CDRStatsQueueTriggered, a *Action, acs Actions)
message = []byte(fmt.Sprintf("To: %s\r\nSubject: [CGR Notification] Threshold hit on CDRStatsQueueId: %s\r\n\r\nTime: \r\n\t%s\r\n\r\nCDRStatsQueueId:\r\n\t%s\r\n\r\nMetrics:\r\n\t%+v\r\n\r\nTrigger:\r\n\t%+v\r\n\r\nYours faithfully,\r\nCGR CDR Stats Monitor\r\n",
toAddrStr, sq.Id, time.Now(), sq.Id, sq.Metrics, sq.Trigger))
}
auth := smtp.PlainAuth("", cgrCfg.MailerAuthUser, cgrCfg.MailerAuthPass, strings.Split(cgrCfg.MailerServer, ":")[0]) // We only need host part, so ignore port
auth := smtp.PlainAuth("", cgrCfg.MailerCfg().MailerAuthUser, cgrCfg.MailerCfg().MailerAuthPass, strings.Split(cgrCfg.MailerCfg().MailerServer, ":")[0]) // We only need host part, so ignore port
go func() {
for i := 0; i < 5; i++ { // Loop so we can increase the success rate on best effort
if err := smtp.SendMail(cgrCfg.MailerServer, auth, cgrCfg.MailerFromAddr, toAddrs, message); err == nil {
if err := smtp.SendMail(cgrCfg.MailerCfg().MailerServer, auth, cgrCfg.MailerCfg().MailerFromAddr, toAddrs, message); err == nil {
break
} else if i == 4 {
if ub != nil {