From f19a78538ac4bd2f7f9e496d7d33db068cef21bd Mon Sep 17 00:00:00 2001 From: TeoV Date: Fri, 4 Dec 2020 13:36:57 +0200 Subject: [PATCH] Add in remaing places infrastructure for ActionProfile --- apier/v1/tpactionprofiles.go | 86 ++++++++++++++++++++++++++++++++++ engine/storage_mongo_datadb.go | 2 + engine/tpexporter.go | 12 +++++ engine/tpreader.go | 1 + 4 files changed, 101 insertions(+) create mode 100644 apier/v1/tpactionprofiles.go diff --git a/apier/v1/tpactionprofiles.go b/apier/v1/tpactionprofiles.go new file mode 100644 index 000000000..7b316e017 --- /dev/null +++ b/apier/v1/tpactionprofiles.go @@ -0,0 +1,86 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package v1 + +import ( + "github.com/cgrates/cgrates/utils" +) + +// SetTPActionProfile creates a new TPActionProfile within a tariff plan +func (apierSv1 *APIerSv1) SetTPActionProfile(attrs *utils.TPActionProfile, reply *string) error { + if missing := utils.MissingStructFields(attrs, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { + return utils.NewErrMandatoryIeMissing(missing...) + } + if err := apierSv1.StorDb.SetTPActionProfiles([]*utils.TPActionProfile{attrs}); err != nil { + return utils.NewErrServerError(err) + } + *reply = utils.OK + return nil +} + +// GetTPActionProfile queries specific TPActionProfile on tariff plan +func (apierSv1 *APIerSv1) GetTPActionProfile(attr *utils.TPTntID, reply *utils.TPActionProfile) error { + if missing := utils.MissingStructFields(attr, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { //Params missing + return utils.NewErrMandatoryIeMissing(missing...) + } + spp, err := apierSv1.StorDb.GetTPActionProfiles(attr.TPid, attr.Tenant, attr.ID) + if err != nil { + if err.Error() != utils.ErrNotFound.Error() { + err = utils.NewErrServerError(err) + } + return err + } + *reply = *spp[0] + return nil +} + +type AttrGetTPActionProfileIDs struct { + TPid string // Tariff plan id + utils.PaginatorWithSearch +} + +// GetTPRouteProfileIDs queries TPActionProfiles identities on specific tariff plan. +func (apierSv1 *APIerSv1) GetTPActionProfileIDs(attrs *AttrGetTPActionProfileIDs, reply *[]string) error { + if missing := utils.MissingStructFields(attrs, []string{"TPid"}); len(missing) != 0 { //Params missing + return utils.NewErrMandatoryIeMissing(missing...) + } + ids, err := apierSv1.StorDb.GetTpTableIds(attrs.TPid, utils.TBLTPActionProfiles, + utils.TPDistinctIds{"tenant", "id"}, nil, &attrs.PaginatorWithSearch) + if err != nil { + if err.Error() != utils.ErrNotFound.Error() { + err = utils.NewErrServerError(err) + } + return err + } + *reply = ids + return nil +} + +// RemoveTPActionProfile removes specific TPActionProfile on Tariff plan +func (apierSv1 *APIerSv1) RemoveTPActionProfile(attrs *utils.TPTntID, reply *string) error { + if missing := utils.MissingStructFields(attrs, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { //Params missing + return utils.NewErrMandatoryIeMissing(missing...) + } + if err := apierSv1.StorDb.RemTpData(utils.TBLTPActionProfiles, attrs.TPid, + map[string]string{"tenant": attrs.Tenant, "id": attrs.ID}); err != nil { + return utils.NewErrServerError(err) + } + *reply = utils.OK + return nil +} diff --git a/engine/storage_mongo_datadb.go b/engine/storage_mongo_datadb.go index 534a7e821..45faa29c9 100644 --- a/engine/storage_mongo_datadb.go +++ b/engine/storage_mongo_datadb.go @@ -698,6 +698,8 @@ func (ms *MongoStorage) GetKeysForPrefix(prefix string) (result []string, err er result, err = ms.getField3(sctx, ColIndx, utils.ActionPlanIndexes, "key") case utils.ActionProfilesFilterIndexPrfx: result, err = ms.getField3(sctx, ColIndx, utils.ActionProfilesFilterIndexPrfx, "key") + case utils.RateProfilesFilterIndexPrfx: + result, err = ms.getField3(sctx, ColIndx, utils.RateProfilesFilterIndexPrfx, "key") case utils.RateFilterIndexPrfx: result, err = ms.getField3(sctx, ColIndx, utils.RateFilterIndexPrfx, "key") case utils.FilterIndexPrfx: diff --git a/engine/tpexporter.go b/engine/tpexporter.go index 4ea4626b7..3b336bf17 100644 --- a/engine/tpexporter.go +++ b/engine/tpexporter.go @@ -332,6 +332,18 @@ func (self *TPExporter) Run() error { } } + storDataActionProfiles, err := self.storDb.GetTPActionProfiles(self.tpID, "", "") + if err != nil && err.Error() != utils.ErrNotFound.Error() { + utils.Logger.Warning(fmt.Sprintf("<%s> error: %s, when getting %s from stordb for export", utils.ApierS, err, utils.TpRateProfiles)) + withError = true + } + for _, sd := range storDataActionProfiles { + sdModels := APItoModelTPActionProfile(sd) + for _, sdModel := range sdModels { + toExportMap[utils.ActionProfilesCsv] = append(toExportMap[utils.ActionProfilesCsv], sdModel) + } + } + if len(toExportMap) == 0 { // if we don't have anything to export we return not found error return utils.ErrNotFound } diff --git a/engine/tpreader.go b/engine/tpreader.go index 39ea65c45..99801f2a5 100644 --- a/engine/tpreader.go +++ b/engine/tpreader.go @@ -2563,6 +2563,7 @@ func (tpr *TpReader) ReloadCache(caching string, verbose bool, opts map[string]i utils.ChargerProfileIDs: chargerIDs, utils.DispatcherProfileIDs: dppIDs, utils.DispatcherHostIDs: dphIDs, + utils.RateProfileIDs: ratePrfIDs, utils.ActionProfileIDs: actionPrfIDs, }, }