From db7826a5d73dc232426c7736f52c5aac3aa7a13f Mon Sep 17 00:00:00 2001 From: DanB Date: Thu, 21 Feb 2019 12:58:08 +0100 Subject: [PATCH] SessionS to rewrite the tenant via AttributeS --- sessions/sessions.go | 20 ++++++++++++++++++++ utils/cgrevent.go | 10 ++++++++++ utils/consts.go | 1 + 3 files changed, 31 insertions(+) diff --git a/sessions/sessions.go b/sessions/sessions.go index 7d70cc86b..b016cd319 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -1592,6 +1592,11 @@ func (sS *SessionS) BiRPCv1AuthorizeEvent(clnt rpcclient.RpcClientConnection, if err := sS.attrS.Call(utils.AttributeSv1ProcessEvent, attrArgs, &rplyEv); err == nil { args.CGREvent = *rplyEv.CGREvent + if tntIface, has := args.CGREvent.Event[utils.MetaTenant]; has { + // special case when we want to overwrite the tenant + args.CGREvent.Tenant = tntIface.(string) + delete(args.CGREvent.Event, utils.MetaTenant) + } authReply.Attributes = &rplyEv } else if err.Error() != utils.ErrNotFound.Error() { return utils.NewErrAttributeS(err) @@ -1822,6 +1827,11 @@ func (sS *SessionS) BiRPCv1InitiateSession(clnt rpcclient.RpcClientConnection, if err := sS.attrS.Call(utils.AttributeSv1ProcessEvent, attrArgs, &rplyEv); err == nil { args.CGREvent = *rplyEv.CGREvent + if tntIface, has := args.CGREvent.Event[utils.MetaTenant]; has { + // special case when we want to overwrite the tenant + args.CGREvent.Tenant = tntIface.(string) + delete(args.CGREvent.Event, utils.MetaTenant) + } rply.Attributes = &rplyEv } else if err.Error() != utils.ErrNotFound.Error() { return utils.NewErrAttributeS(err) @@ -2022,6 +2032,11 @@ func (sS *SessionS) BiRPCv1UpdateSession(clnt rpcclient.RpcClientConnection, if err := sS.attrS.Call(utils.AttributeSv1ProcessEvent, attrArgs, &rplyEv); err == nil { args.CGREvent = *rplyEv.CGREvent + if tntIface, has := args.CGREvent.Event[utils.MetaTenant]; has { + // special case when we want to overwrite the tenant + args.CGREvent.Tenant = tntIface.(string) + delete(args.CGREvent.Event, utils.MetaTenant) + } rply.Attributes = &rplyEv } else if err.Error() != utils.ErrNotFound.Error() { return utils.NewErrAttributeS(err) @@ -2292,6 +2307,11 @@ func (sS *SessionS) BiRPCv1ProcessEvent(clnt rpcclient.RpcClientConnection, if err := sS.attrS.Call(utils.AttributeSv1ProcessEvent, attrArgs, &rplyEv); err == nil { args.CGREvent = *rplyEv.CGREvent + if tntIface, has := args.CGREvent.Event[utils.MetaTenant]; has { + // special case when we want to overwrite the tenant + args.CGREvent.Tenant = tntIface.(string) + delete(args.CGREvent.Event, utils.MetaTenant) + } rply.Attributes = &rplyEv } else if err.Error() != utils.ErrNotFound.Error() { return utils.NewErrAttributeS(err) diff --git a/utils/cgrevent.go b/utils/cgrevent.go index 9ed095bb2..041e26ee3 100644 --- a/utils/cgrevent.go +++ b/utils/cgrevent.go @@ -22,6 +22,7 @@ import ( "fmt" "reflect" "strconv" + "strings" "time" ) @@ -161,3 +162,12 @@ func (ev *CGREvent) Clone() (clned *CGREvent) { } return } + +// RemFldsWithPrefix will remove fields starting with prefix from event +func (ev *CGREvent) RemFldsWithPrefix(prfx string) { + for fldName := range ev.Event { + if strings.HasPrefix(fldName, prfx) { + delete(ev.Event, fldName) + } + } +} diff --git a/utils/consts.go b/utils/consts.go index 64e7cef27..743de0c73 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -550,6 +550,7 @@ const ( CGRDebitInterval = "CGRDebitInterval" MetaAsr = "*asr" Version = "Version" + MetaTenant = "*tenant" ) // Migrator Action