diff --git a/apier/v1/apier.go b/apier/v1/apier.go index d666cae6d..016e725e5 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -219,7 +219,7 @@ type AttrAddAccount struct { } // Ads a new account into dataDb. If already defined, returns success. -func (self *ApierV1) AddAccount(attr *AttrAddAccount, reply *string) error { +func (self *ApierV1) AddAccount(attr AttrAddAccount, reply *string) error { if missing := utils.MissingStructFields(&attr, []string{"Tenant", "Direction", "Account", "Type", "ActionTimingsId"}); len(missing) != 0 { *reply = fmt.Sprintf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index 90a2a6061..1b7e3a8cc 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -213,33 +213,33 @@ func startHistoryScribe() { } if cfg.HistoryServerEnabled { - if cfg.HistoryListen != INTERNAL { - rpc.RegisterName("Scribe", scribeServer) - var serveFunc func(io.ReadWriteCloser) - if cfg.RPCEncoding == JSON { - serveFunc = jsonrpc.ServeConn - } else { - serveFunc = rpc.ServeConn - } - l, err := net.Listen("tcp", cfg.HistoryListen) - if err != nil { - engine.Logger.Crit(fmt.Sprintf(" Could not listen to %v: %v", cfg.HistoryListen, err)) - exitChan <- true - return - } - defer l.Close() - for { - conn, err := l.Accept() - if err != nil { - engine.Logger.Err(fmt.Sprintf(" Accept error: %v", conn)) - continue - } + if cfg.HistoryListen != INTERNAL { + rpc.RegisterName("Scribe", scribeServer) + var serveFunc func(io.ReadWriteCloser) + if cfg.RPCEncoding == JSON { + serveFunc = jsonrpc.ServeConn + } else { + serveFunc = rpc.ServeConn + } + l, err := net.Listen("tcp", cfg.HistoryListen) + if err != nil { + engine.Logger.Crit(fmt.Sprintf(" Could not listen to %v: %v", cfg.HistoryListen, err)) + exitChan <- true + return + } + defer l.Close() + for { + conn, err := l.Accept() + if err != nil { + engine.Logger.Err(fmt.Sprintf(" Accept error: %v", conn)) + continue + } - engine.Logger.Info(fmt.Sprintf(" New incoming connection: %v", conn.RemoteAddr())) - go serveFunc(conn) - } - } - } + engine.Logger.Info(fmt.Sprintf(" New incoming connection: %v", conn.RemoteAddr())) + go serveFunc(conn) + } + } + } var scribeAgent history.Scribe @@ -247,14 +247,14 @@ func startHistoryScribe() { if cfg.HistoryServer != INTERNAL { // Connect in iteration since there are chances of concurrency here for i := 0; i < 3; i++ { //ToDo: Make it globally configurable if scribeAgent, err = history.NewProxyScribe(cfg.HistoryServer, cfg.RPCEncoding); err == nil { - break //Connected so no need to reiterate - } else if (i==2 && err!= nil) { + break //Connected so no need to reiterate + } else if i == 2 && err != nil { engine.Logger.Crit(err.Error()) exitChan <- true return } - time.Sleep(time.Duration(i/2) * time.Second) - } + time.Sleep(time.Duration(i/2) * time.Second) + } } else { scribeAgent = scribeServer } diff --git a/engine/callcost.go b/engine/callcost.go index 9f003d960..367789cd4 100644 --- a/engine/callcost.go +++ b/engine/callcost.go @@ -23,9 +23,7 @@ import ( "time" ) -/* -The output structure that will be returned with the call cost information. -*/ +// The output structure that will be returned with the call cost information. type CallCost struct { Direction, TOR, Tenant, Subject, Account, Destination string Cost, ConnectFee float64 diff --git a/engine/simple_serializer.go b/engine/simple_serializer.go index 6a9e24996..2e32a2b62 100644 --- a/engine/simple_serializer.go +++ b/engine/simple_serializer.go @@ -19,7 +19,7 @@ along with this program. If not, see package engine import ( - "errors" + "fmt" "strconv" "strings" "time" @@ -30,8 +30,8 @@ type Serializer interface { Restore(string) error } -func notEnoughElements(line string) error { - return errors.New("Too few elements to restore: " + line) +func notEnoughElements(element, line string) error { + return fmt.Errorf("Too few elements to restore (%v): %v", element, line) } func (ap *ActivationPeriod) Store() (result string, err error) { @@ -132,7 +132,7 @@ func (a *Action) Store() (result string, err error) { func (a *Action) Restore(input string) (err error) { elements := strings.Split(input, "|") if len(elements) < 8 { - return notEnoughElements(input) + return notEnoughElements("Action", input) } a.Id = elements[0] a.ActionType = elements[1] @@ -260,7 +260,7 @@ func (at *ActionTrigger) Store() (result string, err error) { func (at *ActionTrigger) Restore(input string) error { elements := strings.Split(input, ";") if len(elements) != 9 { - return notEnoughElements(input) + return notEnoughElements("ActionTrigger", input) } at.Id = elements[0] at.BalanceId = elements[1] @@ -357,7 +357,7 @@ func (ub *UserBalance) Store() (result string, err error) { func (ub *UserBalance) Restore(input string) error { elements := strings.Split(input, "|") if len(elements) < 2 { - return notEnoughElements(input) + return notEnoughElements("UserBalance", input) } ub.Id = elements[0] ub.Type = elements[1] @@ -432,7 +432,7 @@ De-serializes the unit counter for the storage. Used for key-value storages. func (uc *UnitsCounter) Restore(input string) error { elements := strings.Split(input, "/") if len(elements) != 4 { - return notEnoughElements(input) + return notEnoughElements("UnitsCounter", input) } uc.Direction = elements[0] uc.BalanceId = elements[1] @@ -544,7 +544,7 @@ func (i *Interval) Store() (result string, err error) { func (i *Interval) Restore(input string) error { is := strings.Split(input, ";") if len(is) != 11 { - return notEnoughElements(input) + return notEnoughElements("Interval", input) } if err := i.Years.Restore(is[0]); err != nil { return err @@ -590,7 +590,7 @@ func (mb *MinuteBucket) Restore(input string) error { mb.DestinationId = elements[4] return nil } - return notEnoughElements(input) + return notEnoughElements("MinuteBucket", input) } func (wds WeekDays) Store() (result string, err error) { diff --git a/utils/utils_test.go b/utils/utils_test.go index 20efa7c47..6f3efd74f 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -166,3 +166,31 @@ func TestParseDateRFC3339(t *testing.T) { t.Error("error parsing date: ", expected.Sub(date)) } } + +func TestMissingStructFieldsCorrect(t *testing.T) { + var attr = struct { + Tenant string + Direction string + Account string + Type string + ActionTimingsId string + }{"bevoip.eu", "OUT", "danconns0001", "prepaid", "mama"} + if missing := MissingStructFields(&attr, + []string{"Tenant", "Direction", "Account", "Type", "ActionTimingsId"}); len(missing) != 0 { + t.Error("Found missing field on correct struct", missing) + } +} + +func TestMissingStructFieldsIncorrect(t *testing.T) { + var attr = struct { + Tenant string + Direction string + Account string + Type string + ActionTimingsId string + }{Tenant: "bevoip.eu", Direction: "OUT", Account: "danconns0001", Type: "prepaid"} + if missing := MissingStructFields(&attr, + []string{"Tenant", "Direction", "Account", "Type", "ActionTimingsId"}); len(missing) != 1 || missing[0] != "ActionTimingsId" { + t.Error("Found missing field on correct struct", missing) + } +}