diff --git a/apier/v1/dispatcher.go b/apier/v1/dispatcher.go index fd4acadc8..68f2ad108 100755 --- a/apier/v1/dispatcher.go +++ b/apier/v1/dispatcher.go @@ -110,6 +110,16 @@ func (dT *DispatcherThresholdSv1) ProcessEvent(args *dispatchers.ArgsProcessEven return dT.dS.ThresholdSv1ProcessEvent(args, tIDs) } +func (dT *DispatcherThresholdSv1) GetThresholdIDs(args *dispatchers.TntWithApiKey, + tIDs *[]string) error { + return dT.dS.ThresholdSv1GetThresholdIDs(args, tIDs) +} + +func (dT *DispatcherThresholdSv1) GetThreshold(args *dispatchers.TntIDWithApiKey, + th *engine.Threshold) error { + return dT.dS.ThresholdSv1GetThreshold(args, th) +} + func NewDispatcherStatSv1(dps *dispatchers.DispatcherService) *DispatcherStatSv1 { return &DispatcherStatSv1{dS: dps} } diff --git a/data/tariffplans/dispatchers/Attributes.csv b/data/tariffplans/dispatchers/Attributes.csv index 6683e3f89..dbaa73716 100644 --- a/data/tariffplans/dispatchers/Attributes.csv +++ b/data/tariffplans/dispatchers/Attributes.csv @@ -3,7 +3,7 @@ cgrates.org,ATTR_1001_SIMPLEAUTH,simpleauth,*string:Account:1001,,Password,*any, cgrates.org,ATTR_API_ATTR_FAKE_AUTH,*auth,*string:APIKey:12345,,APIMethods,*any,,true,false,20 cgrates.org,ATTR_API_ATTR_AUTH,*auth,*string:APIKey:attr12345,,APIMethods,*any,AttributeSv1.Ping&AttributeSv1.GetAttributeForEvent&AttributeSv1.ProcessEvent,true,false,20 cgrates.org,ATTR_API_CHRG_AUTH,*auth,*string:APIKey:chrg12345,,APIMethods,*any,ChargerSv1.Ping&ChargerSv1.GetChargersForEvent&ChargerSv1.ProcessEvent,true,false,20 -cgrates.org,ATTR_API_THR_AUTH,*auth,*string:APIKey:thr12345,,APIMethods,*any,ThresholdSv1.Ping&ThresholdSv1.GetThresholdsForEvent&ThresholdSv1.ProcessEvent,true,false,20 +cgrates.org,ATTR_API_THR_AUTH,*auth,*string:APIKey:thr12345,,APIMethods,*any,ThresholdSv1.Ping&ThresholdSv1.GetThresholdsForEvent&ThresholdSv1.ProcessEvent&ThresholdSv1.GetThreshold&ThresholdSv1.GetThresholdIDs,true,false,20 cgrates.org,ATTR_API_SUP_AUTH,*auth,*string:APIKey:sup12345,,APIMethods,*any,SupplierSv1.Ping&SupplierSv1.GetSuppliers,true,false,20 cgrates.org,ATTR_API_STAT_AUTH,*auth,*string:APIKey:stat12345,,APIMethods,*any,StatSv1.Ping&StatSv1.GetStatQueuesForEvent&StatSv1.GetQueueStringMetrics&StatSv1.ProcessEvent,true,false,20 cgrates.org,ATTR_API_RES_AUTH,*auth,*string:APIKey:res12345,,APIMethods,*any,ResourceSv1.Ping&ResourceSv1.GetResourcesForEvent,true,false,20 diff --git a/dispatchers/thresholds.go b/dispatchers/thresholds.go index 629fb227b..71d6c4969 100755 --- a/dispatchers/thresholds.go +++ b/dispatchers/thresholds.go @@ -19,6 +19,8 @@ along with this program. If not, see package dispatchers import ( + "time" + "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" ) @@ -60,3 +62,28 @@ func (dS *DispatcherService) ThresholdSv1ProcessEvent(args *ArgsProcessEventWith return dS.Dispatch(&args.CGREvent, utils.MetaThresholds, args.RouteID, utils.ThresholdSv1ProcessEvent, args.ArgsProcessEvent, tIDs) } + +func (dS *DispatcherService) ThresholdSv1GetThresholdIDs(args *TntWithApiKey, tIDs *[]string) (err error) { + if dS.attrS != nil { + if err = dS.authorize(utils.ThresholdSv1GetThresholdIDs, + args.Tenant, args.APIKey, utils.TimePointer(time.Now())); err != nil { + return + } + } + return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaThresholds, args.RouteID, + utils.ThresholdSv1GetThresholdIDs, args.TenantArg, tIDs) +} + +func (dS *DispatcherService) ThresholdSv1GetThreshold(args *TntIDWithApiKey, th *engine.Threshold) (err error) { + if dS.attrS != nil { + if err = dS.authorize(utils.ThresholdSv1GetThreshold, + args.TenantID.Tenant, + args.APIKey, utils.TimePointer(time.Now())); err != nil { + return + } + } + return dS.Dispatch(&utils.CGREvent{ + Tenant: args.Tenant, + ID: args.ID, + }, utils.MetaThresholds, args.RouteID, utils.ThresholdSv1GetThreshold, args.TenantID, th) +} diff --git a/dispatchers/thresholds_it_test.go b/dispatchers/thresholds_it_test.go index 293da4ee7..9efe4be61 100755 --- a/dispatchers/thresholds_it_test.go +++ b/dispatchers/thresholds_it_test.go @@ -23,6 +23,7 @@ package dispatchers import ( "path" "reflect" + "sort" "testing" "time" @@ -37,6 +38,7 @@ var sTestsDspTh = []func(t *testing.T){ testDspThPing, testDspThTestAuthKey, testDspThTestAuthKey2, + testDspThTestAuthKey3, } //Test start here @@ -219,3 +221,50 @@ func testDspThTestAuthKey2(t *testing.T) { t.Errorf("expecting: %+v, received: %+v", (*eTh)[0].Hits, (*th)[0].Hits) } } + +func testDspThTestAuthKey3(t *testing.T) { + var th *engine.Thresholds + eTh := &engine.Thresholds{ + &engine.Threshold{ + Tenant: "cgrates.org", + ID: "THD_ACNT_1002", + Hits: 1, + }, + } + if err := dispEngine.RCP.Call(utils.ThresholdSv1GetThreshold, &TntIDWithApiKey{ + TenantID: utils.TenantID{ + Tenant: "cgrates.org", + ID: "THD_ACNT_1002", + }, + DispatcherResource: DispatcherResource{ + APIKey: "thr12345", + }, + }, &th); err != nil { + t.Error(err) + } else if !reflect.DeepEqual((*eTh)[0].Tenant, (*th)[0].Tenant) { + t.Errorf("expecting: %+v, received: %+v", (*eTh)[0].Tenant, (*th)[0].Tenant) + } else if !reflect.DeepEqual((*eTh)[0].ID, (*th)[0].ID) { + t.Errorf("expecting: %+v, received: %+v", (*eTh)[0].ID, (*th)[0].ID) + } else if !reflect.DeepEqual((*eTh)[0].Hits, (*th)[0].Hits) { + t.Errorf("expecting: %+v, received: %+v", (*eTh)[0].Hits, (*th)[0].Hits) + } + + var ids []string + eIDs := []string{"THD_ACNT_1002", "THD_ACNT_1002"} + nowTime := time.Now() + + if err := dispEngine.RCP.Call(utils.ThresholdSv1GetThresholdIDs, &TntWithApiKey{ + TenantArg: utils.TenantArg{ + Tenant: "cgrates.org", + }, + DispatcherResource: DispatcherResource{ + APIKey: "thr12345", + }, + }, &ids); err != nil { + t.Fatal(err) + } + sort.Strings(ids) + if !reflect.DeepEqual(eIDs, ids) { + t.Errorf("expecting: %+v, received: %+v", eIDs, ids) + } +}