diff --git a/apier/aliases.go b/apier/aliases.go new file mode 100644 index 000000000..ad7e12add --- /dev/null +++ b/apier/aliases.go @@ -0,0 +1,117 @@ +/* +Real-time Charging System for Telecom & ISP environments +Copyright (C) 2012-2014 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 apier + +import ( + "errors" + "fmt" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +type AttrAddRatingSubjectAliases struct { + Tenant, Subject string + Aliases []string +} + +type AttrAddAccountAliases struct { + Tenant, Account string + Aliases []string +} + +// Retrieve aliases configured for a rating profile subject +func (self *ApierV1) AddRatingSubjectAliases(attrs AttrAddRatingSubjectAliases, reply *string) error { + if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Subject", "Aliases"}); len(missing) != 0 { + return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) + } + for _, alias := range attrs.Aliases { + if err := self.RatingDb.SetRpAlias(utils.RatingSubjectAliasKey(attrs.Tenant, alias), attrs.Subject); err != nil { + return fmt.Errorf("%s:%s:%s", utils.ERR_SERVER_ERROR, alias, err.Error()) + } + } + *reply = utils.OK + return nil +} + +// Retrieve aliases configured for a rating profile subject +func (self *ApierV1) GetRatingSubjectAliases(attrs engine.TenantRatingSubject, reply *[]string) error { + if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Subject"}); len(missing) != 0 { + return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) + } + if aliases, err := self.RatingDb.GetRPAliases(attrs.Tenant, attrs.Subject); err != nil { + return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) + } else if len(aliases) == 0 { // Need it since otherwise we get some unexpected errrors in the client + return errors.New(utils.ERR_NOT_FOUND) + } else { + *reply = aliases + } + return nil +} + +// Retrieve aliases configured for a rating profile subject +func (self *ApierV1) RemRatingSubjectAliases(tenantRatingSubject engine.TenantRatingSubject, reply *string) error { + if missing := utils.MissingStructFields(&tenantRatingSubject, []string{"Tenant", "Subject"}); len(missing) != 0 { + return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) + } + if err := self.RatingDb.RemoveRpAliases([]*engine.TenantRatingSubject{&tenantRatingSubject}); err != nil { + return fmt.Errorf("%s:% s", utils.ERR_SERVER_ERROR, err.Error()) + } + *reply = utils.OK + return nil +} + +func (self *ApierV1) AddAccountAliases(attrs AttrAddAccountAliases, reply *string) error { + if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account", "Aliases"}); len(missing) != 0 { + return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) + } + for _, alias := range attrs.Aliases { + if err := self.AccountDb.SetAccAlias(utils.AccountAliasKey(attrs.Tenant, alias), attrs.Account); err != nil { + return fmt.Errorf("%s:%s:%s", utils.ERR_SERVER_ERROR, alias, err.Error()) + } + } + *reply = utils.OK + return nil +} + +// Retrieve aliases configured for an account +func (self *ApierV1) GetAccountAliases(attrs engine.TenantAccount, reply *[]string) error { + if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account"}); len(missing) != 0 { + return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) + } + if aliases, err := self.AccountDb.GetAccountAliases(attrs.Tenant, attrs.Account); err != nil { + return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) + } else if len(aliases) == 0 { + return errors.New(utils.ERR_NOT_FOUND) + } else { + *reply = aliases + } + return nil +} + +// Retrieve aliases configured for a rating profile subject +func (self *ApierV1) RemAccountAliases(tenantAccount engine.TenantAccount, reply *string) error { + if missing := utils.MissingStructFields(&tenantAccount, []string{"Tenant", "Account"}); len(missing) != 0 { + return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) + } + if err := self.AccountDb.RemoveAccAliases([]*engine.TenantAccount{&tenantAccount}); err != nil { + return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) + } + *reply = utils.OK + return nil +} diff --git a/engine/loader_csv_test.go b/engine/loader_csv_test.go index 0174e565e..19806f84b 100644 --- a/engine/loader_csv_test.go +++ b/engine/loader_csv_test.go @@ -911,9 +911,9 @@ func TestLoadRpAliases(t *testing.T) { if len(csvr.rpAliases) != 3 { t.Error("Failed to load rp aliases: ", csvr.rpAliases) } - if csvr.rpAliases[utils.RatingProfileAliasKey("vdf", "a1")] != "minu" || - csvr.rpAliases[utils.RatingProfileAliasKey("vdf", "a2")] != "minu" || - csvr.rpAliases[utils.RatingProfileAliasKey("vdf", "a3")] != "minu" { + if csvr.rpAliases[utils.RatingSubjectAliasKey("vdf", "a1")] != "minu" || + csvr.rpAliases[utils.RatingSubjectAliasKey("vdf", "a2")] != "minu" || + csvr.rpAliases[utils.RatingSubjectAliasKey("vdf", "a3")] != "minu" { t.Error("Error loading rp aliases: ", csvr.rpAliases) } } diff --git a/engine/storage_test.go b/engine/storage_test.go index 7222b0b5d..f434c1695 100644 --- a/engine/storage_test.go +++ b/engine/storage_test.go @@ -113,7 +113,7 @@ func TestCacheRefresh(t *testing.T) { } func TestCacheAliases(t *testing.T) { - if subj, err := cache2go.GetCached(RP_ALIAS_PREFIX + utils.RatingProfileAliasKey("vdf", "a3")); err != nil || subj != "minu" { + if subj, err := cache2go.GetCached(RP_ALIAS_PREFIX + utils.RatingSubjectAliasKey("vdf", "a3")); err != nil || subj != "minu" { t.Error("Error caching alias: ", subj, err) } } @@ -129,13 +129,13 @@ func TestStoreInterfaces(t *testing.T) { } func TestGetRPAliases(t *testing.T) { - if err := dataStorage.SetRpAlias(utils.RatingProfileAliasKey("cgrates.org", "2001"), "1001"); err != nil { + if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2001"), "1001"); err != nil { t.Error(err) } - if err := dataStorage.SetRpAlias(utils.RatingProfileAliasKey("cgrates.org", "2002"), "1001"); err != nil { + if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2002"), "1001"); err != nil { t.Error(err) } - if err := dataStorage.SetRpAlias(utils.RatingProfileAliasKey("itsyscom.com", "2003"), "1001"); err != nil { + if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("itsyscom.com", "2003"), "1001"); err != nil { t.Error(err) } expectAliases := sort.StringSlice([]string{"2001", "2002"}) @@ -152,13 +152,13 @@ func TestGetRPAliases(t *testing.T) { } func TestRemRSubjAliases(t *testing.T) { - if err := dataStorage.SetRpAlias(utils.RatingProfileAliasKey("cgrates.org", "2001"), "1001"); err != nil { + if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2001"), "1001"); err != nil { t.Error(err) } - if err := dataStorage.SetRpAlias(utils.RatingProfileAliasKey("cgrates.org", "2002"), "1001"); err != nil { + if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2002"), "1001"); err != nil { t.Error(err) } - if err := dataStorage.SetRpAlias(utils.RatingProfileAliasKey("itsyscom.com", "2003"), "1001"); err != nil { + if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("itsyscom.com", "2003"), "1001"); err != nil { t.Error(err) } if err := dataStorage.RemoveRpAliases([]*TenantRatingSubject{&TenantRatingSubject{Tenant: "cgrates.org", Subject: "1001"}}); err != nil {