Remove group all checks for accounts during startup

Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
bcmmbaga 2024-11-22 18:07:23 +03:00
parent 2115e2c3f0
commit ea51ce876e
No known key found for this signature in database
GPG Key ID: 511EED5C928AD547
4 changed files with 30 additions and 26 deletions

View File

@ -1048,39 +1048,21 @@ func BuildManager(
metrics: metrics,
requestBuffer: NewAccountRequestBuffer(ctx, store),
}
allAccounts := store.GetAllAccounts(ctx)
totalAccounts, err := store.GetTotalAccounts(ctx, LockingStrengthShare)
if err != nil {
return nil, err
}
// enable single account mode only if configured by user and number of existing accounts is not grater than 1
am.singleAccountMode = singleAccountModeDomain != "" && len(allAccounts) <= 1
am.singleAccountMode = singleAccountModeDomain != "" && totalAccounts <= 1
if am.singleAccountMode {
if !isDomainValid(singleAccountModeDomain) {
return nil, status.Errorf(status.InvalidArgument, "invalid domain \"%s\" provided for a single account mode. Please review your input for --single-account-mode-domain", singleAccountModeDomain)
}
am.singleAccountModeDomain = singleAccountModeDomain
log.WithContext(ctx).Infof("single account mode enabled, accounts number %d", len(allAccounts))
log.WithContext(ctx).Infof("single account mode enabled, accounts number %d", totalAccounts)
} else {
log.WithContext(ctx).Infof("single account mode disabled, accounts number %d", len(allAccounts))
}
// if account doesn't have a default group
// we create 'all' group and add all peers into it
// also we create default rule with source as destination
for _, account := range allAccounts {
shouldSave := false
_, err := account.GetGroupAll()
if err != nil {
if err := addAllGroup(account); err != nil {
return nil, err
}
shouldSave = true
}
if shouldSave {
err = store.SaveAccount(ctx, account)
if err != nil {
return nil, err
}
}
log.WithContext(ctx).Infof("single account mode disabled, accounts number %d", totalAccounts)
}
goCacheClient := gocache.New(CacheExpirationMax, 30*time.Minute)

View File

@ -877,6 +877,17 @@ func (s *SqlStore) GetAccountCreatedBy(ctx context.Context, lockStrength Locking
return createdBy, nil
}
func (s *SqlStore) GetTotalAccounts(ctx context.Context, lockStrength LockingStrength) (int64, error) {
var count int64
result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}).Model(&Account{}).Count(&count)
if result.Error != nil {
log.WithContext(ctx).Errorf("failed to get total accounts from store: %s", result.Error)
return 0, status.Errorf(status.Internal, "failed to get total accounts from store")
}
return count, nil
}
// SaveUserLastLogin stores the last login time for a user in DB.
func (s *SqlStore) SaveUserLastLogin(ctx context.Context, accountID, userID string, lastLogin time.Time) error {
var user User

View File

@ -2662,3 +2662,13 @@ func TestSqlStore_SaveAccountSettings(t *testing.T) {
require.NoError(t, err)
require.Equal(t, settings, saveSettings)
}
func TestSqlStore_GetTotalAccounts(t *testing.T) {
store, cleanup, err := NewTestStoreFromSQL(context.Background(), "testdata/store.sql", t.TempDir())
t.Cleanup(cleanup)
require.NoError(t, err)
totalAccounts, err := store.GetTotalAccounts(context.Background(), LockingStrengthShare)
require.NoError(t, err)
require.Equal(t, int64(1), totalAccounts)
}

View File

@ -58,6 +58,7 @@ type Store interface {
GetAccountSettings(ctx context.Context, lockStrength LockingStrength, accountID string) (*Settings, error)
GetAccountDNSSettings(ctx context.Context, lockStrength LockingStrength, accountID string) (*DNSSettings, error)
GetAccountCreatedBy(ctx context.Context, lockStrength LockingStrength, accountID string) (string, error)
GetTotalAccounts(ctx context.Context, lockStrength LockingStrength) (int64, error)
SaveAccount(ctx context.Context, account *Account) error
DeleteAccount(ctx context.Context, account *Account) error
UpdateAccountDomainAttributes(ctx context.Context, accountID string, domain string, category string, isPrimaryDomain bool) error