diff --git a/cmd/cgr-console/cgr-console.go b/cmd/cgr-console/cgr-console.go index 6bc2e8111..1993c3ea4 100644 --- a/cmd/cgr-console/cgr-console.go +++ b/cmd/cgr-console/cgr-console.go @@ -19,7 +19,6 @@ along with this program. If not, see package main import ( - "console" "flag" "fmt" "log" @@ -27,7 +26,8 @@ import ( "net/rpc/jsonrpc" "os" "reflect" - "timespans" + "github.com/cgrates/cgrates/timespans" + "github.com/cgrates/cgrates/console" ) var ( @@ -64,7 +64,6 @@ func main() { if rpcErr := client.Call(cmd.RpcMethod(), cmd.RpcParams(), res); rpcErr != nil { log.Fatal(rpcErr) } - - fmt.Println(reflect.ValueOf(res).Elem().String()) + fmt.Println(reflect.ValueOf(res).Elem().Interface()) } diff --git a/console/balance.go b/console/balance.go index 119d324b6..e16be120d 100644 --- a/console/balance.go +++ b/console/balance.go @@ -7,30 +7,42 @@ import ( "path/filepath" ) -type PrmsGetBalance struct { +// Data being sent to the rpc responder +type ArgsGetBalance struct { Tenant string User string Direction string - BalanceTag string + BalanceId string } + +// Received back from query +type ReplyGetBalance struct { + Tenant string + User string + Direction string + BalanceId string + Balance float64 +} + +// Commander implementation type CmdGetBalance struct { rpcMethod string - rpcParams PrmsGetBalance - rpcResult string + rpcParams ArgsGetBalance + rpcResult ReplyGetBalance idxArgsToRpcPrms map[int]string } // name should be exec's name func (self *CmdGetBalance) Usage(name string) string { - return fmt.Sprintf("\n\tUsage: %s get_balance [ []]", name) + return fmt.Sprintf("\n\tUsage: %s [cfg_opts...{-h}] get_balance [ []]", name) } // set param defaults func (self *CmdGetBalance) defaults() error { - self.idxArgsToRpcPrms = map[int]string{2: "Tenant", 3: "User", 4: "BalanceTag", 5:"Direction" } + self.idxArgsToRpcPrms = map[int]string{2: "Tenant", 3: "User", 4: "BalanceId", 5:"Direction" } self.rpcMethod = "Responder.GetBalance" - self.rpcParams.BalanceTag = "MONETARY" + self.rpcParams.BalanceId = "MONETARY" self.rpcParams.Direction = "OUT" return nil } diff --git a/console/command.go b/console/command.go index b279b93ff..7a71423c1 100644 --- a/console/command.go +++ b/console/command.go @@ -35,7 +35,7 @@ func CmdRpcPrmsFromArgs(rpcPrms interface{}, args []string, idxArgsToRpcPrms map // Process args and return right command Value or error func GetCommandValue(args []string) (Commander, error) { if len(args) < 2 { - return nil, fmt.Errorf("\n\tUsage: %s \n", filepath.Base(args[0])) + return nil, fmt.Errorf("\n\tUsage: %s [cfg_opts...{-h}] \n", filepath.Base(args[0])) } cmd := args[1] var cmdVal Commander @@ -45,7 +45,7 @@ func GetCommandValue(args []string) (Commander, error) { case "get_balance": cmdVal = &CmdGetBalance{} default: - return nil, fmt.Errorf("\n\tUsage: %s \n", filepath.Base(args[0])) + return nil, fmt.Errorf("\n\tUsage: %s [cfg_opts...{-h}] \n", filepath.Base(args[0])) } if err := cmdVal.FromArgs(args); err != nil { return nil, err diff --git a/console/status.go b/console/status.go index ed477549c..e7b4b8f74 100644 --- a/console/status.go +++ b/console/status.go @@ -11,7 +11,7 @@ type CmdStatus struct { } func (self *CmdStatus) Usage(name string) string { - return fmt.Sprintf("\n\tUsage: %s status", name) + return fmt.Sprintf("\n\tUsage: %s [cfg_opts...{-h}] status", name) } func (self *CmdStatus) defaults() error { diff --git a/timespans/responder.go b/timespans/responder.go index d60af1255..91facd7a4 100644 --- a/timespans/responder.go +++ b/timespans/responder.go @@ -22,6 +22,7 @@ import ( "errors" "fmt" "github.com/rif/balancer2go" + "github.com/cgrates/cgrates/console" "net/rpc" "reflect" "runtime" @@ -175,6 +176,29 @@ func (rs *Responder) Shutdown(arg string, reply *string) (err error) { return } +// Get balance +func (rs *Responder) GetBalance(arg console.ArgsGetBalance, reply *console.ReplyGetBalance) (err error) { + if rs.Bal != nil { + return fmt.Errorf("No balancer supported for this command right now") + } + ubKey := arg.Direction+":"+arg.Tenant+":"+arg.User + userBalance, err := storageGetter.GetUserBalance(ubKey) + if err != nil { + return err + } + if balance,balExists := userBalance.BalanceMap[arg.BalanceId]; !balExists { + // No match, balanceId not found + return fmt.Errorf("-BALANCE_NOT_FOUND") + } else { + reply.Tenant = arg.Tenant + reply.User = arg.User + reply.Direction = arg.Direction + reply.BalanceId = arg.BalanceId + reply.Balance = balance + } + return nil +} + /* The function that gets the information from the raters using balancer. */ @@ -322,3 +346,4 @@ func (rcc *RPCClientConnector) DebitSeconds(cd CallDescriptor, resp *float64) er func (rcc *RPCClientConnector) GetMaxSessionTime(cd CallDescriptor, resp *float64) error { return rcc.Client.Call("Responder.GetMaxSessionTime", cd, resp) } +