func TestLoadAccAliases(t *testing.T) { if len(csvr.accAliases) != 2 { t.Error("Failed to load acc aliases: ", csvr.accAliases) } if csvr.accAliases[utils.AccountAliasKey("vdf", "a1")] != "minitsboy" || csvr.accAliases[utils.AccountAliasKey("vdf", "a2")] != "minitsboy" { t.Error("Error loading acc aliases: ", csvr.accAliases) } }
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) } aliasesChanged := []string{} 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()) } aliasesChanged = append(aliasesChanged, engine.ACC_ALIAS_PREFIX+utils.AccountAliasKey(attrs.Tenant, alias)) } didNotChange := []string{} if err := self.AccountDb.CacheAccounting(didNotChange, didNotChange, aliasesChanged, didNotChange); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) } *reply = utils.OK return nil }
// Returns the key used to retrive the user balance involved in this call func (cd *CallDescriptor) GetAccountKey() string { subj := cd.Subject if cd.Account != "" { // check if subject is alias if realSubject, err := cache2go.GetCached(ACC_ALIAS_PREFIX + utils.AccountAliasKey(cd.Tenant, subj)); err == nil { cd.Account = realSubject.(string) } subj = cd.Account } return fmt.Sprintf("%s:%s:%s", cd.Direction, cd.Tenant, subj) }
func TestGetAccountAliases(t *testing.T) { if err := accountingStorage.SetAccAlias(utils.AccountAliasKey("cgrates.org", "2001"), "1001"); err != nil { t.Error(err) } if err := accountingStorage.SetAccAlias(utils.AccountAliasKey("cgrates.org", "2002"), "1001"); err != nil { t.Error(err) } if err := accountingStorage.SetAccAlias(utils.AccountAliasKey("itsyscom.com", "2003"), "1001"); err != nil { t.Error(err) } expectAliases := sort.StringSlice([]string{"2001", "2002"}) expectAliases.Sort() if aliases, err := accountingStorage.GetAccountAliases("cgrates.org", "1001", true); err != nil { t.Error(err) } else { aliases := sort.StringSlice(aliases) aliases.Sort() if !reflect.DeepEqual(aliases, expectAliases) { t.Errorf("Expecting: %v, received: %v", expectAliases, aliases) } } }
func (csvr *CSVReader) LoadAccountActions() (err error) { csvReader, fp, err := csvr.readerFunc(csvr.accountactionsFn, csvr.sep, utils.ACCOUNT_ACTIONS_NRCOLS) if err != nil { log.Print("Could not load account actions file: ", err) // allow writing of the other values return nil } if fp != nil { defer fp.Close() } for record, err := csvReader.Read(); err == nil; record, err = csvReader.Read() { tenant, account, direction := record[0], record[1], record[2] // extract aliases from subject aliases := strings.Split(account, ";") csvr.dirtyAccAliases = append(csvr.dirtyAccAliases, &TenantAccount{Tenant: tenant, Account: aliases[0]}) if len(aliases) > 1 { account = aliases[0] for _, alias := range aliases[1:] { csvr.accAliases[utils.AccountAliasKey(tenant, alias)] = account } } tag := fmt.Sprintf("%s:%s:%s", direction, tenant, account) if _, alreadyDefined := csvr.accountActions[tag]; alreadyDefined { return fmt.Errorf("Duplicate account action found: %s", tag) } aTriggers, exists := csvr.actionsTriggers[record[4]] if record[4] != "" && !exists { // only return error if there was something there for the tag return fmt.Errorf("Could not get action triggers for tag %s", record[4]) } ub := &Account{ Id: tag, ActionTriggers: aTriggers, } csvr.accountActions[tag] = ub aTimings, exists := csvr.actionsTimings[record[3]] if !exists { log.Printf("Could not get action plan for tag %s", record[3]) // must not continue here } for _, at := range aTimings { at.AccountIds = append(at.AccountIds, tag) } } return nil }
func (dbr *DbReader) LoadAccountActions() (err error) { acs, err := dbr.storDb.GetTpAccountActions(&utils.TPAccountActions{TPid: dbr.tpid}) if err != nil { return err } for _, aa := range acs { if _, alreadyDefined := dbr.accountActions[aa.KeyId()]; alreadyDefined { return fmt.Errorf("Duplicate account action found: %s", aa.KeyId()) } // extract aliases from subject aliases := strings.Split(aa.Account, ";") dbr.dirtyAccAliases = append(dbr.dirtyAccAliases, &TenantAccount{Tenant: aa.Tenant, Account: aliases[0]}) if len(aliases) > 1 { aa.Account = aliases[0] for _, alias := range aliases[1:] { dbr.accAliases[utils.AccountAliasKey(aa.Tenant, alias)] = aa.Account } } aTriggers, exists := dbr.actionsTriggers[aa.ActionTriggersId] if !exists { return fmt.Errorf("Could not get action triggers for tag %v", aa.ActionTriggersId) } ub := &Account{ Id: aa.KeyId(), ActionTriggers: aTriggers, } dbr.accountActions[aa.KeyId()] = ub aTimings, exists := dbr.actionsTimings[aa.ActionPlanId] if !exists { log.Printf("Could not get action timing for tag %v", aa.ActionPlanId) // must not continue here } for _, at := range aTimings { at.AccountIds = append(at.AccountIds, aa.KeyId()) } } return nil }