started *cgr_rpc action

This commit is contained in:
Radu Ioan Fericean
2016-04-08 13:42:28 +03:00
parent 1f647e4ada
commit 81d8715463
4 changed files with 90 additions and 4 deletions

View File

@@ -74,6 +74,7 @@ const (
CDRLOG = "*cdrlog"
SET_DDESTINATIONS = "*set_ddestinations"
TRANSFER_MONETARY_DEFAULT = "*transfer_monetary_default"
CGR_RPC = "*cgr_rpc"
)
func (a *Action) Clone() *Action {
@@ -140,6 +141,8 @@ func getActionFunc(typ string) (actionTypeFunc, bool) {
return setBalanceAction, true
case TRANSFER_MONETARY_DEFAULT:
return transferMonetaryDefaultAction, true
case CGR_RPC:
return cgrRPCAction, true
}
return nil, false
}
@@ -647,6 +650,22 @@ func transferMonetaryDefaultAction(acc *Account, sq *StatsQueueTriggered, a *Act
return nil
}
type RPCRequest struct {
Server string
Transport string
Attempts int
Async bool
Arg map[string]interface{}
}
func cgrRPCAction(account *Account, sq *StatsQueueTriggered, a *Action, acs Actions) error {
rpcRequest := RPCRequest{}
if err := json.Unmarshal([]byte(a.ExtraParameters), &rpcRequest); err != nil {
return err
}
return nil
}
// Structure to store actions according to weight
type Actions []*Action

View File

@@ -2102,6 +2102,8 @@ func TestActionCdrlogBalanceValue(t *testing.T) {
ID: "cgrates.org:bv",
BalanceMap: map[string]Balances{
utils.MONETARY: Balances{&Balance{
ID: "*default",
Uuid: "25a02c82-f09f-4c6e-bacf-8ed4b076475a",
Value: 10,
}},
},
@@ -2114,16 +2116,29 @@ func TestActionCdrlogBalanceValue(t *testing.T) {
Timing: &RateInterval{},
actions: []*Action{
&Action{
Id: "RECUR_FOR_V3HSILLMILLD1G",
ActionType: TOPUP,
Balance: &BalanceFilter{Value: utils.Float64Pointer(1.1), Type: utils.StringPointer(utils.MONETARY)},
Balance: &BalanceFilter{
ID: utils.StringPointer("*default"),
Uuid: utils.StringPointer("25a02c82-f09f-4c6e-bacf-8ed4b076475a"),
Value: utils.Float64Pointer(1.1),
Type: utils.StringPointer(utils.MONETARY),
},
},
&Action{
Id: "RECUR_FOR_V3HSILLMILLD5G",
ActionType: DEBIT,
Balance: &BalanceFilter{Value: utils.Float64Pointer(2.1), Type: utils.StringPointer(utils.MONETARY)},
Balance: &BalanceFilter{
ID: utils.StringPointer("*default"),
Uuid: utils.StringPointer("25a02c82-f09f-4c6e-bacf-8ed4b076475a"),
Value: utils.Float64Pointer(2.1),
Type: utils.StringPointer(utils.MONETARY),
},
},
&Action{
Id: "c",
ActionType: CDRLOG,
ExtraParameters: `{"BalanceValue":"BalanceValue"}`,
ExtraParameters: `{"BalanceID":"BalanceID","BalanceUUID":"BalanceUUID","ActionID":"ActionID","BalanceValue":"BalanceValue"}`,
},
},
}
@@ -2140,7 +2155,7 @@ func TestActionCdrlogBalanceValue(t *testing.T) {
if len(cdrs) != 2 ||
cdrs[0].ExtraFields["BalanceValue"] != "11.1" ||
cdrs[1].ExtraFields["BalanceValue"] != "9" {
t.Errorf("Wrong cdrlogs: %+v", cdrs[1])
t.Errorf("Wrong cdrlogs: %", utils.ToIJSON(cdrs))
}
}

View File

@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package utils
import (
"errors"
"reflect"
"strconv"
"strings"
@@ -171,6 +172,28 @@ func FromMapStringString(m map[string]string, in interface{}) {
return
}
func FromMapStringInterface(m map[string]interface{}, in interface{}) error {
v := reflect.ValueOf(in)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
for fieldName, fieldValue := range m {
field := v.FieldByName(fieldName)
if field.IsValid() {
if !field.IsValid() || !field.CanSet() {
continue
}
structFieldType := field.Type()
val := reflect.ValueOf(fieldValue)
if structFieldType != val.Type() {
return errors.New("Provided value type didn't match obj field type")
}
field.Set(val)
}
}
return nil
}
// Update struct with map fields, returns not matching map keys, s is a struct to be updated
func UpdateStructWithStrMap(s interface{}, m map[string]string) []string {
notMatched := []string{}

View File

@@ -84,3 +84,32 @@ func TestStructExtraFields(t *testing.T) {
t.Errorf("expected: %v got: %v", ts.ExtraFields, efMap)
}
}
func TestStructFromMapStringInterface(t *testing.T) {
ts := &struct {
Name string
Class *string
List []string
Elements struct {
Type string
Value float64
}
}{}
s := "test2"
m := map[string]interface{}{
"Name": "test1",
"Class": &s,
"List": []string{"test3", "test4"},
"Elements": struct {
Type string
Value float64
}{
Type: "test5",
Value: 9.8,
},
}
if err := FromMapStringInterface(m, ts); err != nil {
t.Logf("ts: %+v", ToJSON(ts))
t.Error("Error converting map to struct: ", err)
}
}