From 709594ffa9b2d04e8bdc84398ce0c7e7f3f810e2 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 4 Feb 2014 15:28:02 +0200 Subject: [PATCH] get_balance console renamed to get_balances returns json formatted userbalance object --- apier/v1/apier.go | 14 ++------- apier/v1/apier_local_test.go | 29 +++++++++--------- console/{get_balance.go => get_balances.go} | 33 ++++++++------------- 3 files changed, 31 insertions(+), 45 deletions(-) rename console/{get_balance.go => get_balances.go} (64%) diff --git a/apier/v1/apier.go b/apier/v1/apier.go index 6ef9928a1..9ab9d216f 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -61,7 +61,7 @@ func (self *ApierV1) GetRatingPlan(rplnId string, reply *engine.RatingPlan) erro return nil } -type AttrGetBalance struct { +type AttrGetUserBalance struct { Tenant string Account string BalanceId string @@ -69,22 +69,14 @@ type AttrGetBalance struct { } // Get balance -func (self *ApierV1) GetBalance(attr *AttrGetBalance, reply *float64) error { +func (self *ApierV1) GetUserBalance(attr *AttrGetUserBalance, reply *engine.UserBalance) error { tag := fmt.Sprintf("%s:%s:%s", attr.Direction, attr.Tenant, attr.Account) userBalance, err := self.AccountDb.GetUserBalance(tag) if err != nil { return err } - if attr.Direction == "" { - attr.Direction = engine.OUTBOUND - } - - if balance, balExists := userBalance.BalanceMap[attr.BalanceId+attr.Direction]; !balExists { - *reply = 0.0 - } else { - *reply = balance.GetTotalValue() - } + *reply = *userBalance return nil } diff --git a/apier/v1/apier_local_test.go b/apier/v1/apier_local_test.go index febfabf9b..4864eff69 100644 --- a/apier/v1/apier_local_test.go +++ b/apier/v1/apier_local_test.go @@ -21,9 +21,6 @@ package apier import ( "flag" "fmt" - "github.com/cgrates/cgrates/config" - "github.com/cgrates/cgrates/engine" - "github.com/cgrates/cgrates/utils" "net/http" "net/rpc" "net/url" @@ -33,6 +30,10 @@ import ( "strings" "testing" "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" ) // ToDo: Replace rpc.Client with internal rpc server and Apier using internal map as both data and stor so we can run the tests non-local @@ -1119,36 +1120,36 @@ func TestApierGetBalance(t *testing.T) { if !*testLocal { return } - var reply float64 - attrs := &AttrGetBalance{Tenant: "cgrates.org", Account: "1001", BalanceId: "*monetary", Direction: "*out"} + var reply *engine.UserBalance + attrs := &AttrGetUserBalance{Tenant: "cgrates.org", Account: "1001", BalanceId: "*monetary", Direction: "*out"} if err := rater.Call("ApierV1.GetBalance", attrs, &reply); err != nil { t.Error("Got error on ApierV1.GetBalance: ", err.Error()) - } else if reply != 11.5 { // We expect 11.5 since we have added in the previous test 1.5 + } else if reply.BalanceMap[attrs.BalanceId+attrs.Direction].GetTotalValue() != 11.5 { // We expect 11.5 since we have added in the previous test 1.5 t.Errorf("Calling ApierV1.GetBalance expected: 11.5, received: %f", reply) } - attrs = &AttrGetBalance{Tenant: "cgrates.org", Account: "dan", BalanceId: "*monetary", Direction: "*out"} + attrs = &AttrGetUserBalance{Tenant: "cgrates.org", Account: "dan", BalanceId: "*monetary", Direction: "*out"} if err := rater.Call("ApierV1.GetBalance", attrs, &reply); err != nil { t.Error("Got error on ApierV1.GetBalance: ", err.Error()) - } else if reply != 1.5 { + } else if reply.BalanceMap[attrs.BalanceId+attrs.Direction].GetTotalValue() != 1.5 { t.Errorf("Calling ApierV1.GetBalance expected: 1.5, received: %f", reply) } // The one we have topped up though executeAction - attrs = &AttrGetBalance{Tenant: "cgrates.org", Account: "dan2", BalanceId: "*monetary", Direction: "*out"} + attrs = &AttrGetUserBalance{Tenant: "cgrates.org", Account: "dan2", BalanceId: "*monetary", Direction: "*out"} if err := rater.Call("ApierV1.GetBalance", attrs, &reply); err != nil { t.Error("Got error on ApierV1.GetBalance: ", err.Error()) - } else if reply != 10 { + } else if reply.BalanceMap[attrs.BalanceId+attrs.Direction].GetTotalValue() != 10 { t.Errorf("Calling ApierV1.GetBalance expected: 10, received: %f", reply) } - attrs = &AttrGetBalance{Tenant: "cgrates.org", Account: "dan3", BalanceId: "*monetary", Direction: "*out"} + attrs = &AttrGetUserBalance{Tenant: "cgrates.org", Account: "dan3", BalanceId: "*monetary", Direction: "*out"} if err := rater.Call("ApierV1.GetBalance", attrs, &reply); err != nil { t.Error("Got error on ApierV1.GetBalance: ", err.Error()) - } else if reply != 3.6 { + } else if reply.BalanceMap[attrs.BalanceId+attrs.Direction].GetTotalValue() != 3.6 { t.Errorf("Calling ApierV1.GetBalance expected: 3.6, received: %f", reply) } - attrs = &AttrGetBalance{Tenant: "cgrates.org", Account: "dan6", BalanceId: "*monetary", Direction: "*out"} + attrs = &AttrGetUserBalance{Tenant: "cgrates.org", Account: "dan6", BalanceId: "*monetary", Direction: "*out"} if err := rater.Call("ApierV1.GetBalance", attrs, &reply); err != nil { t.Error("Got error on ApierV1.GetBalance: ", err.Error()) - } else if reply != 1 { + } else if reply.BalanceMap[attrs.BalanceId+attrs.Direction].GetTotalValue() != 1 { t.Errorf("Calling ApierV1.GetBalance expected: 1, received: %f", reply) } } diff --git a/console/get_balance.go b/console/get_balances.go similarity index 64% rename from console/get_balance.go rename to console/get_balances.go index cd0b3df75..9804afa39 100644 --- a/console/get_balance.go +++ b/console/get_balances.go @@ -26,31 +26,31 @@ import ( ) func init() { - commands["get_balance"] = &CmdGetBalance{} + commands["get_balance"] = &CmdGetBalances{} } // Commander implementation -type CmdGetBalance struct { +type CmdGetBalances struct { rpcMethod string - rpcParams *apier.AttrGetBalance - rpcResult float64 + rpcParams *apier.AttrGetUserBalance + rpcResult *engine.UserBalance } // name should be exec's name -func (self *CmdGetBalance) Usage(name string) string { - return fmt.Sprintf("\n\tUsage: cgr-console [cfg_opts...{-h}] get_balance [<*monetary|*sms|*internet|*internet_time|*minutes> []]") +func (self *CmdGetBalances) Usage(name string) string { + return fmt.Sprintf("\n\tUsage: cgr-console [cfg_opts...{-h}] get_balances ") } // set param defaults -func (self *CmdGetBalance) defaults() error { - self.rpcMethod = "ApierV1.GetBalance" - self.rpcParams = &apier.AttrGetBalance{BalanceId: engine.CREDIT} +func (self *CmdGetBalances) defaults() error { + self.rpcMethod = "ApierV1.GetUserBalance" + self.rpcParams = &apier.AttrGetUserBalance{BalanceId: engine.CREDIT} self.rpcParams.Direction = "*out" return nil } // Parses command line args and builds CmdBalance value -func (self *CmdGetBalance) FromArgs(args []string) error { +func (self *CmdGetBalances) FromArgs(args []string) error { if len(args) < 4 { return fmt.Errorf(self.Usage("")) } @@ -58,24 +58,17 @@ func (self *CmdGetBalance) FromArgs(args []string) error { self.defaults() self.rpcParams.Tenant = args[2] self.rpcParams.Account = args[3] - if len(args) > 4 { - self.rpcParams.BalanceId = args[4] - } - if len(args) > 5 { - - self.rpcParams.Direction = args[5] - } return nil } -func (self *CmdGetBalance) RpcMethod() string { +func (self *CmdGetBalances) RpcMethod() string { return self.rpcMethod } -func (self *CmdGetBalance) RpcParams() interface{} { +func (self *CmdGetBalances) RpcParams() interface{} { return self.rpcParams } -func (self *CmdGetBalance) RpcResult() interface{} { +func (self *CmdGetBalances) RpcResult() interface{} { return &self.rpcResult }