From 2da2294c0f6975e84130ff7add7be4e1335d39a5 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 17 Apr 2014 16:59:17 +0300 Subject: [PATCH] add_balance --- cmd/cgr-console/cgr-console.go | 3 +- console/add_account.go | 5 +- console/add_balance.go | 87 ++++++++++++++++++++++++++++++++++ console/command_test.go | 10 +++- 4 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 console/add_balance.go diff --git a/cmd/cgr-console/cgr-console.go b/cmd/cgr-console/cgr-console.go index be10af181..7766b7bc6 100644 --- a/cmd/cgr-console/cgr-console.go +++ b/cmd/cgr-console/cgr-console.go @@ -135,9 +135,8 @@ func main() { } if f, err := os.Create(history_fn); err != nil { - fmt.Println("Error writing history file: ", err) + log.Print("Error writing history file: ", err) } else { - log.Print("Writing history: ", history_fn) line.WriteHistory(f) f.Close() } diff --git a/console/add_account.go b/console/add_account.go index 10a6345f2..86f7cb9f3 100644 --- a/console/add_account.go +++ b/console/add_account.go @@ -24,7 +24,6 @@ import ( "reflect" "github.com/cgrates/cgrates/apier" - "github.com/cgrates/cgrates/engine" ) func init() { @@ -41,7 +40,7 @@ type CmdAddAccount struct { } func (self *CmdAddAccount) Usage() string { - jsn, _ := json.Marshal(engine.CallDescriptor{Direction: "*out"}) + jsn, _ := json.Marshal(apier.AttrSetAccount{Direction: "*out"}) return "\n\tUsage: add_account " + FromJSON(jsn, self.ClientArgs()) + "\n" } @@ -76,7 +75,7 @@ func (self *CmdAddAccount) RpcResult() interface{} { } func (self *CmdAddAccount) ClientArgs() (args []string) { - val := reflect.ValueOf(apier.AttrSetAccount{}).Elem() + val := reflect.ValueOf(&apier.AttrSetAccount{}).Elem() for i := 0; i < val.NumField(); i++ { typeField := val.Type().Field(i) diff --git a/console/add_balance.go b/console/add_balance.go new file mode 100644 index 000000000..7309c7280 --- /dev/null +++ b/console/add_balance.go @@ -0,0 +1,87 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2013 ITsysCOM + +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 +*/ + +package console + +import ( + "encoding/json" + "fmt" + "reflect" + + "github.com/cgrates/cgrates/apier" + "github.com/cgrates/cgrates/engine" +) + +func init() { + commands["add_balance"] = &CmdAddBalance{ + rpcMethod: "ApierV1.AddBalance", + rpcParams: &apier.AttrAddBalance{BalanceType: engine.CREDIT}, + } +} + +// Commander implementation +type CmdAddBalance struct { + rpcMethod string + rpcParams *apier.AttrAddBalance + rpcResult string +} + +func (self *CmdAddBalance) Usage() string { + jsn, _ := json.Marshal(apier.AttrAddBalance{Direction: "*out"}) + return "\n\tUsage: add_balance " + FromJSON(jsn, self.ClientArgs()) + "\n" +} + +// Parses command line args and builds CmdBalance value +func (self *CmdAddBalance) FromArgs(args string, verbose bool) error { + if len(args) == 0 { + return fmt.Errorf(self.Usage()) + } + // defaults + self.rpcParams = &apier.AttrAddBalance{Direction: "*out"} + + if err := json.Unmarshal(ToJSON(args), &self.rpcParams); err != nil { + return err + } + if verbose { + jsn, _ := json.Marshal(self.rpcParams) + fmt.Println("add_balance ", FromJSON(jsn, self.ClientArgs())) + } + return nil +} + +func (self *CmdAddBalance) RpcMethod() string { + return self.rpcMethod +} + +func (self *CmdAddBalance) RpcParams() interface{} { + return self.rpcParams +} + +func (self *CmdAddBalance) RpcResult() interface{} { + return &self.rpcResult +} + +func (self *CmdAddBalance) ClientArgs() (args []string) { + val := reflect.ValueOf(&apier.AttrAddBalance{}).Elem() + + for i := 0; i < val.NumField(); i++ { + typeField := val.Type().Field(i) + args = append(args, typeField.Name) + } + return +} diff --git a/console/command_test.go b/console/command_test.go index ebba75c66..63e3b710a 100644 --- a/console/command_test.go +++ b/console/command_test.go @@ -47,9 +47,17 @@ func TestToJSONEmpty(t *testing.T) { } func TestFromJSON(t *testing.T) { - line := FromJSON([]byte(`{"TimeStart":"Test","Crazy":1,"Mama":true,"Test":1}`)) + line := FromJSON([]byte(`{"TimeStart":"Test","Crazy":1,"Mama":true,"Test":1}`), []string{"TimeStart", "Test", "Crazy", "Mama", "Test"}) expected := `TimeStart="Test" Crazy=1 Mama=true Test=1` if line != expected { t.Errorf("Expected: %s got: '%s'", expected, line) } } + +func TestFromJSONInterestingFields(t *testing.T) { + line := FromJSON([]byte(`{"TimeStart":"Test","Crazy":1,"Mama":true,"Test":1}`), []string{"TimeStart", "Test"}) + expected := `TimeStart="Test" Test=1` + if line != expected { + t.Errorf("Expected: %s got: '%s'", expected, line) + } +}