get_balance console renamed to get_balances

returns json formatted userbalance object
This commit is contained in:
Radu Ioan Fericean
2014-02-04 15:28:02 +02:00
parent 58d485f46f
commit 709594ffa9
3 changed files with 31 additions and 45 deletions

View File

@@ -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
}

View File

@@ -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)
}
}

View File

@@ -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 <tenant> <account> [<*monetary|*sms|*internet|*internet_time|*minutes> [<direction>]]")
func (self *CmdGetBalances) Usage(name string) string {
return fmt.Sprintf("\n\tUsage: cgr-console [cfg_opts...{-h}] get_balances <tenant> <account>")
}
// 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
}