Removing AttributeS processing in Responder methods, adding AttributeS in SessionS processed requests, GetAttributes in UpdateSession

This commit is contained in:
DanB
2018-01-11 09:58:16 +01:00
parent 84187c9896
commit c946d045cf
3 changed files with 88 additions and 146 deletions

View File

@@ -245,6 +245,7 @@ func TestSSv1ItInitiateSession(t *testing.T) {
func TestSSv1ItUpdateSession(t *testing.T) {
reqUsage := 5 * time.Minute
args := &sessionmanager.V1UpdateSessionArgs{
GetAttributes: true,
UpdateSession: true,
CGREvent: utils.CGREvent{
Tenant: "cgrates.org",
@@ -265,6 +266,29 @@ func TestSSv1ItUpdateSession(t *testing.T) {
args, &rply); err != nil {
t.Error(err)
}
eAttrs := &engine.AttrSProcessEventReply{
MatchedProfile: "ATTR_ACNT_1001",
AlteredFields: []string{"OfficeGroup"},
CGREvent: &utils.CGREvent{
Tenant: "cgrates.org",
ID: "TestSSv1ItUpdateSession",
Context: utils.StringPointer(utils.MetaSessionS),
Event: map[string]interface{}{
"Account": "1001",
"Destination": "1002",
"OfficeGroup": "Marketing",
"OriginID": "TestSSv1It1",
"RequestType": "*prepaid",
"SetupTime": "2018-01-07T17:00:00Z",
"AnswerTime": "2018-01-07T17:00:10Z",
"Usage": 300000000000.0,
},
},
}
if !reflect.DeepEqual(eAttrs, rply.Attributes) {
t.Errorf("expecting: %+v, received: %+v",
utils.ToJSON(eAttrs), utils.ToJSON(rply.Attributes))
}
if *rply.MaxUsage != reqUsage {
t.Errorf("Unexpected MaxUsage: %v", rply.MaxUsage)
}

View File

@@ -86,19 +86,6 @@ func (rs *Responder) GetCost(arg *CallDescriptor, reply *CallCost) (err error) {
if arg.Subject == "" {
arg.Subject = arg.Account
}
if rs.AttributeS != nil {
var rplyEv AttrSProcessEventReply
if err = rs.AttributeS.Call(utils.AttributeSv1ProcessEvent,
arg.AsCGREvent(), &rplyEv); err != nil &&
err.Error() != utils.ErrNotFound.Error() {
return utils.NewErrAttributeS(err)
} else if err == nil {
if err = arg.UpdateFromCGREvent(rplyEv.CGREvent,
rplyEv.AlteredFields); err != nil {
return utils.NewErrAttributeS(err)
}
}
}
// replace user profile fields
if err := LoadUserProfile(arg, utils.EXTRA_FIELDS); err != nil {
return err
@@ -135,19 +122,6 @@ func (rs *Responder) Debit(arg *CallDescriptor, reply *CallCost) (err error) {
if arg.Subject == "" {
arg.Subject = arg.Account
}
if rs.AttributeS != nil {
var rplyEv AttrSProcessEventReply
if err = rs.AttributeS.Call(utils.AttributeSv1ProcessEvent,
arg.AsCGREvent(), &rplyEv); err != nil &&
err.Error() != utils.ErrNotFound.Error() {
return utils.NewErrAttributeS(err)
} else if err == nil {
if err = arg.UpdateFromCGREvent(rplyEv.CGREvent,
rplyEv.AlteredFields); err != nil {
return utils.NewErrAttributeS(err)
}
}
}
// replace user profile fields
if err := LoadUserProfile(arg, utils.EXTRA_FIELDS); err != nil {
return err
@@ -188,19 +162,6 @@ func (rs *Responder) MaxDebit(arg *CallDescriptor, reply *CallCost) (err error)
if arg.Subject == "" {
arg.Subject = arg.Account
}
if rs.AttributeS != nil {
var rplyEv AttrSProcessEventReply
if err = rs.AttributeS.Call(utils.AttributeSv1ProcessEvent,
arg.AsCGREvent(), &rplyEv); err != nil &&
err.Error() != utils.ErrNotFound.Error() {
return utils.NewErrAttributeS(err)
} else if err == nil {
if err = arg.UpdateFromCGREvent(rplyEv.CGREvent,
rplyEv.AlteredFields); err != nil {
return utils.NewErrAttributeS(err)
}
}
}
// replace user profile fields
if err := LoadUserProfile(arg, utils.EXTRA_FIELDS); err != nil {
return err
@@ -248,19 +209,6 @@ func (rs *Responder) RefundIncrements(arg *CallDescriptor, reply *float64) (err
if arg.Subject == "" {
arg.Subject = arg.Account
}
if rs.AttributeS != nil {
var rplyEv AttrSProcessEventReply
if err = rs.AttributeS.Call(utils.AttributeSv1ProcessEvent,
arg.AsCGREvent(), &rplyEv); err != nil &&
err.Error() != utils.ErrNotFound.Error() {
return utils.NewErrAttributeS(err)
} else if err == nil {
if err = arg.UpdateFromCGREvent(rplyEv.CGREvent,
rplyEv.AlteredFields); err != nil {
return utils.NewErrAttributeS(err)
}
}
}
// replace user profile fields
if err := LoadUserProfile(arg, utils.EXTRA_FIELDS); err != nil {
return err
@@ -303,19 +251,6 @@ func (rs *Responder) RefundRounding(arg *CallDescriptor, reply *float64) (err er
if arg.Subject == "" {
arg.Subject = arg.Account
}
if rs.AttributeS != nil {
var rplyEv AttrSProcessEventReply
if err = rs.AttributeS.Call(utils.AttributeSv1ProcessEvent,
arg.AsCGREvent(), &rplyEv); err != nil &&
err.Error() != utils.ErrNotFound.Error() {
return utils.NewErrAttributeS(err)
} else if err == nil {
if err = arg.UpdateFromCGREvent(rplyEv.CGREvent,
rplyEv.AlteredFields); err != nil {
return utils.NewErrAttributeS(err)
}
}
}
// replace user profile fields
if err := LoadUserProfile(arg, utils.EXTRA_FIELDS); err != nil {
return err
@@ -351,19 +286,6 @@ func (rs *Responder) GetMaxSessionTime(arg *CallDescriptor, reply *float64) (err
if arg.Subject == "" {
arg.Subject = arg.Account
}
if rs.AttributeS != nil {
var rplyEv AttrSProcessEventReply
if err = rs.AttributeS.Call(utils.AttributeSv1ProcessEvent,
arg.AsCGREvent(), &rplyEv); err != nil &&
err.Error() != utils.ErrNotFound.Error() {
return utils.NewErrAttributeS(err)
} else if err == nil {
if err = arg.UpdateFromCGREvent(rplyEv.CGREvent,
rplyEv.AlteredFields); err != nil {
return utils.NewErrAttributeS(err)
}
}
}
// replace user profile fields
if err := LoadUserProfile(arg, utils.EXTRA_FIELDS); err != nil {
return err
@@ -401,19 +323,6 @@ func (rs *Responder) GetDerivedMaxSessionTime(ev *CDR, reply *float64) (err erro
if ev.Subject == "" {
ev.Subject = ev.Account
}
if rs.AttributeS != nil {
var rplyEv AttrSProcessEventReply
if err = rs.AttributeS.Call(utils.AttributeSv1ProcessEvent,
ev.AsCGREvent(), &rplyEv); err != nil &&
err.Error() != utils.ErrNotFound.Error() {
return utils.NewErrAttributeS(err)
} else if err == nil {
if err = ev.UpdateFromCGREvent(rplyEv.CGREvent,
rplyEv.AlteredFields); err != nil {
return utils.NewErrAttributeS(err)
}
}
}
// replace user profile fields
if err := LoadUserProfile(ev, utils.EXTRA_FIELDS); err != nil {
rs.getCache().Cache(cacheKey, &cache.CacheItem{Err: err})
@@ -522,19 +431,6 @@ func (rs *Responder) GetSessionRuns(ev *CDR, sRuns *[]*SessionRun) (err error) {
if ev.Subject == "" {
ev.Subject = ev.Account
}
if rs.AttributeS != nil {
var rplyEv AttrSProcessEventReply
if err = rs.AttributeS.Call(utils.AttributeSv1ProcessEvent,
ev.AsCGREvent(), &rplyEv); err != nil &&
err.Error() != utils.ErrNotFound.Error() {
return utils.NewErrAttributeS(err)
} else if err == nil {
if err = ev.UpdateFromCGREvent(rplyEv.CGREvent,
rplyEv.AlteredFields); err != nil {
return utils.NewErrAttributeS(err)
}
}
}
//utils.Logger.Info(fmt.Sprintf("DC before: %+v", ev))
// replace user profile fields
if err := LoadUserProfile(ev, utils.EXTRA_FIELDS); err != nil {

View File

@@ -1275,24 +1275,40 @@ func (smg *SMGeneric) BiRPCV1ReplicatePassiveSessions(clnt rpcclient.RpcClientCo
}
type V1AuthorizeArgs struct {
GetMaxUsage bool
AuthorizeResources bool
GetSuppliers bool
GetAttributes bool
AuthorizeResources bool
GetMaxUsage bool
GetSuppliers bool
utils.CGREvent
utils.Paginator
}
type V1AuthorizeReply struct {
MaxUsage *time.Duration
ResourceAllocation *string
Suppliers *engine.SortedSuppliers
Attributes *engine.AttrSProcessEventReply
ResourceAllocation *string
MaxUsage *time.Duration
Suppliers *engine.SortedSuppliers
}
// BiRPCV1Authorize performs authorization for CGREvent based on specific components
func (smg *SMGeneric) BiRPCv1AuthorizeEvent(clnt rpcclient.RpcClientConnection,
args *V1AuthorizeArgs, authReply *V1AuthorizeReply) (err error) {
if args.GetAttributes {
if smg.attrS == nil {
return utils.NewErrNotConnected(utils.AttributeS)
}
if args.CGREvent.Context == nil { // populate if not already in
args.CGREvent.Context = utils.StringPointer(utils.MetaSessionS)
}
var rplyEv engine.AttrSProcessEventReply
if err = smg.attrS.Call(utils.AttributeSv1ProcessEvent,
args.CGREvent, &rplyEv); err == nil {
args.CGREvent = *rplyEv.CGREvent
authReply.Attributes = &rplyEv
} else if err.Error() != utils.ErrNotFound.Error() {
return utils.NewErrAttributeS(err)
}
}
if args.GetMaxUsage {
if smg.rals == nil {
return utils.NewErrNotConnected(utils.RALService)
@@ -1336,6 +1352,25 @@ func (smg *SMGeneric) BiRPCv1AuthorizeEvent(clnt rpcclient.RpcClientConnection,
authReply.Suppliers = &splsReply
}
}
return nil
}
type V1InitSessionArgs struct {
GetAttributes bool
AllocateResources bool
InitSession bool
utils.CGREvent
}
type V1InitSessionReply struct {
Attributes *engine.AttrSProcessEventReply
ResourceAllocation *string
MaxUsage *time.Duration
}
// BiRPCV2InitiateSession initiates a new session, returns the maximum duration the session can last
func (smg *SMGeneric) BiRPCv1InitiateSession(clnt rpcclient.RpcClientConnection,
args *V1InitSessionArgs, rply *V1InitSessionReply) (err error) {
if args.GetAttributes {
if smg.attrS == nil {
return utils.NewErrNotConnected(utils.AttributeS)
@@ -1345,30 +1380,13 @@ func (smg *SMGeneric) BiRPCv1AuthorizeEvent(clnt rpcclient.RpcClientConnection,
}
var rplyEv engine.AttrSProcessEventReply
if err = smg.attrS.Call(utils.AttributeSv1ProcessEvent,
args.CGREvent, &rplyEv); err != nil {
args.CGREvent, &rplyEv); err == nil {
args.CGREvent = *rplyEv.CGREvent
rply.Attributes = &rplyEv
} else if err.Error() != utils.ErrNotFound.Error() {
return utils.NewErrAttributeS(err)
}
authReply.Attributes = &rplyEv
}
return nil
}
type V1InitSessionArgs struct {
AllocateResources bool
InitSession bool
GetAttributes bool
utils.CGREvent
}
type V1InitSessionReply struct {
MaxUsage *time.Duration
ResourceAllocation *string
Attributes *engine.AttrSProcessEventReply
}
// BiRPCV2InitiateSession initiates a new session, returns the maximum duration the session can last
func (smg *SMGeneric) BiRPCv1InitiateSession(clnt rpcclient.RpcClientConnection,
args *V1InitSessionArgs, rply *V1InitSessionReply) (err error) {
if args.AllocateResources {
if smg.resS == nil {
return utils.NewErrNotConnected(utils.ResourceS)
@@ -1399,35 +1417,39 @@ func (smg *SMGeneric) BiRPCv1InitiateSession(clnt rpcclient.RpcClientConnection,
rply.MaxUsage = &maxUsage
}
}
if args.GetAttributes {
if smg.attrS == nil {
return utils.NewErrNotConnected(utils.AttributeS)
}
if args.CGREvent.Context == nil {
args.CGREvent.Context = utils.StringPointer(utils.MetaSessionS)
}
var rplyEv engine.AttrSProcessEventReply
if err = smg.attrS.Call(utils.AttributeSv1ProcessEvent,
args.CGREvent, &rplyEv); err != nil {
return utils.NewErrAttributeS(err)
}
rply.Attributes = &rplyEv
}
return
}
type V1UpdateSessionArgs struct {
GetAttributes bool
UpdateSession bool
utils.CGREvent
}
type V1UpdateSessionReply struct {
MaxUsage *time.Duration
Attributes *engine.AttrSProcessEventReply
MaxUsage *time.Duration
}
// BiRPCV1UpdateSession updates an existing session, returning the duration which the session can still last
func (smg *SMGeneric) BiRPCv1UpdateSession(clnt rpcclient.RpcClientConnection,
args *V1UpdateSessionArgs, rply *V1UpdateSessionReply) (err error) {
if args.GetAttributes {
if smg.attrS == nil {
return utils.NewErrNotConnected(utils.AttributeS)
}
if args.CGREvent.Context == nil { // populate if not already in
args.CGREvent.Context = utils.StringPointer(utils.MetaSessionS)
}
var rplyEv engine.AttrSProcessEventReply
if err = smg.attrS.Call(utils.AttributeSv1ProcessEvent,
args.CGREvent, &rplyEv); err == nil {
args.CGREvent = *rplyEv.CGREvent
rply.Attributes = &rplyEv
} else if err.Error() != utils.ErrNotFound.Error() {
return utils.NewErrAttributeS(err)
}
}
if args.UpdateSession {
if smg.rals == nil {
return utils.NewErrNotConnected(utils.RALService)