Skip to content

Commit

Permalink
Updated RemoveTPRatingProfile API
Browse files Browse the repository at this point in the history
  • Loading branch information
Trial97 authored and danbogos committed Apr 2, 2020
1 parent b92e96d commit a90d5c1
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 43 deletions.
13 changes: 6 additions & 7 deletions apier/v1/apier.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,17 +405,16 @@ func (apiv1 *APIerSv1) SetRatingProfile(attrs utils.AttrSetRatingProfile, reply
return fmt.Errorf("%s:RatingPlanActivation:%v", utils.ErrMandatoryIeMissing.Error(), missing)
}
}
tpRpf := utils.TPRatingProfile{Tenant: attrs.Tenant,
Category: attrs.Category, Subject: attrs.Subject}
keyId := tpRpf.KeyId()
keyID := utils.ConcatenatedKey(utils.META_OUT,
attrs.Tenant, attrs.Category, attrs.Subject)
var rpfl *engine.RatingProfile
if !attrs.Overwrite {
if rpfl, err = apiv1.DataManager.GetRatingProfile(keyId, false, utils.NonTransactional); err != nil && err != utils.ErrNotFound {
if rpfl, err = apiv1.DataManager.GetRatingProfile(keyID, false, utils.NonTransactional); err != nil && err != utils.ErrNotFound {
return utils.NewErrServerError(err)
}
}
if rpfl == nil {
rpfl = &engine.RatingProfile{Id: keyId, RatingPlanActivations: make(engine.RatingPlanActivations, 0)}
rpfl = &engine.RatingProfile{Id: keyID, RatingPlanActivations: make(engine.RatingPlanActivations, 0)}
}
for _, ra := range attrs.RatingPlanActivations {
at, err := utils.ParseTimeDetectLayout(ra.ActivationTime,
Expand All @@ -433,8 +432,8 @@ func (apiv1 *APIerSv1) SetRatingProfile(attrs utils.AttrSetRatingProfile, reply
&engine.RatingPlanActivation{
ActivationTime: at,
RatingPlanId: ra.RatingPlanId,
FallbackKeys: utils.FallbackSubjKeys(tpRpf.Tenant,
tpRpf.Category, ra.FallbackSubjects)})
FallbackKeys: utils.FallbackSubjKeys(attrs.Tenant,
attrs.Category, ra.FallbackSubjects)})
}
if err := apiv1.DataManager.SetRatingProfile(rpfl, utils.NonTransactional); err != nil {
return utils.NewErrServerError(err)
Expand Down
4 changes: 2 additions & 2 deletions apier/v1/apier_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,13 @@ func testApierTPRatingProfile(t *testing.T) {
}
// Test get
var rplyRpf *utils.TPRatingProfile
if err := rater.Call(utils.APIerSv1GetTPRatingProfile, AttrGetTPRatingProfile{TPid: rpfTst.TPid, RatingProfileID: rpfTst.GetId()}, &rplyRpf); err != nil {
if err := rater.Call(utils.APIerSv1GetTPRatingProfile, AttrGetTPRatingProfile{TPid: rpfTst.TPid, RatingProfileID: utils.ConcatenatedKey(rpfTst.LoadId, rpfTst.Tenant, rpfTst.Category, rpfTst.Subject)}, &rplyRpf); err != nil {
t.Error("Calling APIerSv1.GetTPRatingProfiles, got error: ", err.Error())
} else if !reflect.DeepEqual(rpfTst, rplyRpf) {
t.Errorf("Calling APIerSv1.GetTPRatingProfiles expected: %v, received: %v", rpfTst, rplyRpf)
}
// Test remove
if err := rater.Call(utils.APIerSv1RemoveTPRatingProfile, AttrGetTPRatingProfile{TPid: rpfTst.TPid, RatingProfileID: rpfTst.GetId()}, &reply); err != nil {
if err := rater.Call(utils.APIerSv1RemoveTPRatingProfile, AttrGetTPRatingProfile{TPid: rpfTst.TPid, RatingProfileID: utils.ConcatenatedKey(rpfTst.LoadId, rpfTst.Tenant, rpfTst.Category, rpfTst.Subject)}, &reply); err != nil {
t.Error("Calling APIerSv1.RemoveTPRatingProfile, got error: ", err.Error())
} else if reply != utils.OK {
t.Error("Calling APIerSv1.RemoveTPRatingProfile received: ", reply)
Expand Down
9 changes: 4 additions & 5 deletions apier/v1/tpratingprofiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package v1
// This file deals with tp_rate_profiles management over APIs

import (
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)

Expand Down Expand Up @@ -91,7 +90,7 @@ func (api *APIerSv1) GetTPRatingProfile(attrs AttrGetTPRatingProfile, reply *uti
return utils.NewErrMandatoryIeMissing(missing...)
}
tmpRpf := &utils.TPRatingProfile{TPid: attrs.TPid}
if err := tmpRpf.SetRatingProfilesId(attrs.RatingProfileID); err != nil {
if err := tmpRpf.SetRatingProfileID(attrs.RatingProfileID); err != nil {
return err
}
rpfs, err := api.StorDb.GetTPRatingProfiles(tmpRpf)
Expand Down Expand Up @@ -134,13 +133,13 @@ func (api *APIerSv1) RemoveTPRatingProfile(attrs AttrGetTPRatingProfile, reply *
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "RatingProfileID"}); len(missing) != 0 { //Params missing
return utils.NewErrMandatoryIeMissing(missing...)
}
tmpRpf := engine.TpRatingProfile{}
if err = tmpRpf.SetRatingProfileId(attrs.RatingProfileID); err != nil {
tmpRpf := new(utils.TPRatingProfile)
if err = tmpRpf.SetRatingProfileID(attrs.RatingProfileID); err != nil {
return
}
err = api.StorDb.RemTpData(utils.TBLTPRateProfiles,
attrs.TPid, map[string]string{
"loadid": tmpRpf.Loadid,
"loadid": tmpRpf.LoadId,
"tenant": tmpRpf.Tenant,
"category": tmpRpf.Category,
"subject": tmpRpf.Subject,
Expand Down
4 changes: 2 additions & 2 deletions apier/v1/tpratingprofiles_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
tpRatingProfile *utils.TPRatingProfile
tpRatingProfileDelay int
tpRatingProfileConfigDIR string //run tests for specific configuration
tpRatingProfileID = "RPrf:*out:Tenant1:Category:Subject"
tpRatingProfileID = "RPrf:Tenant1:Category:Subject"
)

var sTestsTPRatingProfiles = []func(t *testing.T){
Expand Down Expand Up @@ -279,7 +279,7 @@ func testTPRatingProfilesGetTPRatingProfileIds(t *testing.T) {
func testTPRatingProfilesRemoveTPRatingProfile(t *testing.T) {
var resp string
if err := tpRatingProfileRPC.Call(utils.APIerSv1RemoveTPRatingProfile,
&AttrGetTPRatingProfile{TPid: "TPRProf1", RatingProfileID: tpRatingProfile.GetId()}, &resp); err != nil {
&AttrGetTPRatingProfile{TPid: "TPRProf1", RatingProfileID: utils.ConcatenatedKey(tpRatingProfile.LoadId, tpRatingProfile.Tenant, tpRatingProfile.Category, tpRatingProfile.Subject)}, &resp); err != nil {
t.Error(err)
} else if resp != utils.OK {
t.Error("Unexpected reply returned", resp)
Expand Down
3 changes: 1 addition & 2 deletions apier/v2/apier.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,13 @@ func (apiv2 *APIerSv2) LoadRatingProfile(attrs AttrLoadRatingProfile, reply *str
if len(attrs.TPid) == 0 {
return utils.NewErrMandatoryIeMissing("TPid")
}
tpRpf := &utils.TPRatingProfile{TPid: attrs.TPid}
dbReader, err := engine.NewTpReader(apiv2.DataManager.DataDB(), apiv2.StorDb,
attrs.TPid, apiv2.Config.GeneralCfg().DefaultTimezone,
apiv2.Config.ApierCfg().CachesConns, apiv2.Config.ApierCfg().SchedulerConns)
if err != nil {
return utils.NewErrServerError(err)
}
if err := dbReader.LoadRatingProfilesFiltered(tpRpf); err != nil {
if err := dbReader.LoadRatingProfilesFiltered(&utils.TPRatingProfile{TPid: attrs.TPid}); err != nil {
return utils.NewErrServerError(err)
}
*reply = utils.OK
Expand Down
17 changes: 0 additions & 17 deletions engine/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,6 @@ type TpRatingProfile struct {
CreatedAt time.Time
}

func (rpf *TpRatingProfile) SetRatingProfileId(id string) error {
ids := strings.Split(id, utils.CONCATENATED_KEY_SEP)
if len(ids) != 5 {
return fmt.Errorf("Wrong TP Rating Profile Id: %s", id)
}
rpf.Loadid = ids[0]
rpf.Tenant = ids[2]
rpf.Category = ids[3]
rpf.Subject = ids[4]
return nil
}

func (rpf *TpRatingProfile) GetRatingProfileId() string {
return utils.ConcatenatedKey(rpf.Loadid, utils.META_OUT,
rpf.Tenant, rpf.Category, rpf.Subject)
}

type TpAction struct {
Id int64
Tpid string
Expand Down
10 changes: 5 additions & 5 deletions utils/apitpdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,15 @@ func (rpf *TPRatingProfile) GetId() string {
rpf.Tenant, rpf.Category, rpf.Subject)
}

func (rpf *TPRatingProfile) SetRatingProfilesId(id string) error {
func (rpf *TPRatingProfile) SetRatingProfileID(id string) error {
ids := strings.Split(id, CONCATENATED_KEY_SEP)
if len(ids) != 5 {
if len(ids) != 4 {
return fmt.Errorf("Wrong TPRatingProfileId: %s", id)
}
rpf.LoadId = ids[0]
rpf.Tenant = ids[2]
rpf.Category = ids[3]
rpf.Subject = ids[4]
rpf.Tenant = ids[1]
rpf.Category = ids[2]
rpf.Subject = ids[3]
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions utils/apitpdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,13 @@ func TestTPRatingProfileKeys(t *testing.T) {
func TestTPRatingProfileSetRatingProfilesId(t *testing.T) {
//empty check
tpRatingProfile := new(TPRatingProfile)
tpRatingProfile.SetRatingProfilesId("")
tpRatingProfile.SetRatingProfileID("")
eOut := new(TPRatingProfile)
if !reflect.DeepEqual(eOut, tpRatingProfile) {
t.Errorf("Expected %+v, received %+v", eOut, tpRatingProfile)
}
//test check
tpRatingProfile.SetRatingProfilesId("1:2:3:4:5")
tpRatingProfile.SetRatingProfileID("1:3:4:5")
eOut.LoadId = "1"
eOut.Tenant = "3"
eOut.Category = "4"
Expand All @@ -266,7 +266,7 @@ func TestTPRatingProfileSetRatingProfilesId(t *testing.T) {
t.Errorf("Expected %+v, received %+v", eOut, tpRatingProfile)
}
//wrong TPRatingProfile sent
err := tpRatingProfile.SetRatingProfilesId("1:2:3:4:5:6")
err := tpRatingProfile.SetRatingProfileID("1:2:3:4:5:6")
if err == nil {
t.Error("Wrong TPRatingProfileId sent and no error received")
}
Expand Down

0 comments on commit a90d5c1

Please sign in to comment.