Content-Length: 383408 | pFad | http://github.com/netbirdio/netbird/pull/2955/commits/ea51ce876eca0c24f543756a57521a0a2dffef01

8B [management] Refactor account to use store methods by bcmmbaga · Pull Request #2955 · netbirdio/netbird · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[management] Refactor account to use store methods #2955

Draft
wants to merge 13 commits into
base: routes-get-account-refactoring
Choose a base branch
from
Prev Previous commit
Next Next commit
Remove group all checks for accounts during startup
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
  • Loading branch information
bcmmbaga committed Nov 22, 2024
commit ea51ce876eca0c24f543756a57521a0a2dffef01
34 changes: 8 additions & 26 deletions management/server/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions management/server/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions management/server/sql_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions management/server/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/netbirdio/netbird/pull/2955/commits/ea51ce876eca0c24f543756a57521a0a2dffef01

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy