From cfb7f55220fad988c0a98704dddf8c044ae56706 Mon Sep 17 00:00:00 2001 From: Vitalii Date: Wed, 15 Nov 2023 15:58:55 +0200 Subject: [PATCH] satellite/console: update project usage-limits endpoint to also return buckets count/limit Extended {projectID}/usage-limits endpoint to also return buckets count/limit. Issue: https://github.com/storj/storj/issues/6492 Change-Id: Ibc8956a2c10c9f383324e049f4b093410cbea899 --- satellite/api.go | 1 + .../consoleweb/consoleql/mutation_test.go | 1 + .../consoleweb/consoleql/query_test.go | 1 + satellite/console/projectusagelimits.go | 2 ++ satellite/console/service.go | 24 +++++++++++++++++-- satellite/console/service_test.go | 12 ++++++++++ satellite/payments/stripe/accounts_test.go | 1 + 7 files changed, 40 insertions(+), 2 deletions(-) diff --git a/satellite/api.go b/satellite/api.go index 971c0eb0c..bdfb1ce6d 100644 --- a/satellite/api.go +++ b/satellite/api.go @@ -590,6 +590,7 @@ func NewAPI(log *zap.Logger, full *identity.FullIdentity, db DB, peer.Mail.Service, externalAddress, consoleConfig.SatelliteName, + config.Metainfo.ProjectLimits.MaxBuckets, consoleConfig.Config, ) if err != nil { diff --git a/satellite/console/consoleweb/consoleql/mutation_test.go b/satellite/console/consoleweb/consoleql/mutation_test.go index 7f433e708..784dbf17c 100644 --- a/satellite/console/consoleweb/consoleql/mutation_test.go +++ b/satellite/console/consoleweb/consoleql/mutation_test.go @@ -118,6 +118,7 @@ func TestGraphqlMutation(t *testing.T) { nil, "", "", + sat.Config.Metainfo.ProjectLimits.MaxBuckets, console.Config{ PasswordCost: console.TestPasswordCost, DefaultProjectLimit: 5, diff --git a/satellite/console/consoleweb/consoleql/query_test.go b/satellite/console/consoleweb/consoleql/query_test.go index f158114ab..7dcd8cf89 100644 --- a/satellite/console/consoleweb/consoleql/query_test.go +++ b/satellite/console/consoleweb/consoleql/query_test.go @@ -102,6 +102,7 @@ func TestGraphqlQuery(t *testing.T) { nil, "", "", + sat.Config.Metainfo.ProjectLimits.MaxBuckets, console.Config{ PasswordCost: console.TestPasswordCost, DefaultProjectLimit: 5, diff --git a/satellite/console/projectusagelimits.go b/satellite/console/projectusagelimits.go index b404bde59..06681efbf 100644 --- a/satellite/console/projectusagelimits.go +++ b/satellite/console/projectusagelimits.go @@ -15,6 +15,8 @@ type ProjectUsageLimits struct { SegmentLimit int64 `json:"segmentLimit"` RateUsed int64 `json:"rateUsed"` SegmentUsed int64 `json:"segmentUsed"` + BucketsUsed int64 `json:"bucketsUsed"` + BucketsLimit int64 `json:"bucketsLimit"` } // UsageLimits represents storage, bandwidth, and segment limits imposed on an entity. diff --git a/satellite/console/service.go b/satellite/console/service.go index c5b3c5fb2..7bdc7b63f 100644 --- a/satellite/console/service.go +++ b/satellite/console/service.go @@ -181,7 +181,8 @@ type Service struct { satelliteAddress string satelliteName string - config Config + config Config + maxProjectBuckets int nowFn func() time.Time } @@ -204,7 +205,7 @@ type Payments struct { } // NewService returns new instance of Service. -func NewService(log *zap.Logger, store DB, restKeys RESTKeys, projectAccounting accounting.ProjectAccounting, projectUsage *accounting.Service, buckets buckets.DB, accounts payments.Accounts, depositWallets payments.DepositWallets, billing billing.TransactionsDB, analytics *analytics.Service, tokens *consoleauth.Service, mailService *mailservice.Service, satelliteAddress string, satelliteName string, config Config) (*Service, error) { +func NewService(log *zap.Logger, store DB, restKeys RESTKeys, projectAccounting accounting.ProjectAccounting, projectUsage *accounting.Service, buckets buckets.DB, accounts payments.Accounts, depositWallets payments.DepositWallets, billing billing.TransactionsDB, analytics *analytics.Service, tokens *consoleauth.Service, mailService *mailservice.Service, satelliteAddress string, satelliteName string, maxProjectBuckets int, config Config) (*Service, error) { if store == nil { return nil, errs.New("store can't be nil") } @@ -250,6 +251,7 @@ func NewService(log *zap.Logger, store DB, restKeys RESTKeys, projectAccounting mailService: mailService, satelliteAddress: satelliteAddress, satelliteName: satelliteName, + maxProjectBuckets: maxProjectBuckets, config: config, nowFn: time.Now, }, nil @@ -2914,6 +2916,8 @@ func (s *Service) GetProjectUsageLimits(ctx context.Context, projectID uuid.UUID SegmentCount: prObjectsSegments.SegmentCount, SegmentLimit: prUsageLimits.SegmentLimit, SegmentUsed: prUsageLimits.SegmentUsed, + BucketsUsed: prUsageLimits.BucketsUsed, + BucketsLimit: prUsageLimits.BucketsLimit, }, nil } @@ -2993,6 +2997,20 @@ func (s *Service) getProjectUsageLimits(ctx context.Context, projectID uuid.UUID return nil, err } + bucketsUsed, err := s.buckets.CountBuckets(ctx, projectID) + if err != nil { + return nil, err + } + + bucketsLimit, err := s.store.Projects().GetMaxBuckets(ctx, projectID) + if err != nil { + return nil, err + } + + if bucketsLimit == nil { + bucketsLimit = &s.maxProjectBuckets + } + return &ProjectUsageLimits{ StorageLimit: storageLimit.Int64(), BandwidthLimit: bandwidthLimit.Int64(), @@ -3000,6 +3018,8 @@ func (s *Service) getProjectUsageLimits(ctx context.Context, projectID uuid.UUID BandwidthUsed: bandwidthUsed, SegmentLimit: segmentLimit.Int64(), SegmentUsed: segmentUsed, + BucketsUsed: int64(bucketsUsed), + BucketsLimit: int64(*bucketsLimit), }, nil } diff --git a/satellite/console/service_test.go b/satellite/console/service_test.go index 21b811961..e1f888251 100644 --- a/satellite/console/service_test.go +++ b/satellite/console/service_test.go @@ -397,6 +397,7 @@ func TestService(t *testing.T) { t.Run("GetProjectUsageLimits", func(t *testing.T) { bandwidthLimit := sat.Config.Console.UsageLimits.Bandwidth.Free storageLimit := sat.Config.Console.UsageLimits.Storage.Free + bucketsLimit := int64(sat.Config.Metainfo.ProjectLimits.MaxBuckets) limits1, err := service.GetProjectUsageLimits(userCtx2, up2Proj.ID) require.NoError(t, err) @@ -410,8 +411,12 @@ func TestService(t *testing.T) { // limits gotten by ID and publicID should be the same require.Equal(t, storageLimit.Int64(), limits1.StorageLimit) require.Equal(t, bandwidthLimit.Int64(), limits1.BandwidthLimit) + require.Equal(t, int64(1), limits1.BucketsUsed) + require.Equal(t, bucketsLimit, limits1.BucketsLimit) require.Equal(t, storageLimit.Int64(), limits2.StorageLimit) require.Equal(t, bandwidthLimit.Int64(), limits2.BandwidthLimit) + require.Equal(t, int64(1), limits2.BucketsUsed) + require.Equal(t, bucketsLimit, limits2.BucketsLimit) // update project's limits updatedStorageLimit := memory.Size(100) + memory.TB @@ -423,6 +428,10 @@ func TestService(t *testing.T) { err = sat.DB.Console().Projects().Update(ctx, up2Proj) require.NoError(t, err) + updatedBucketsLimit := 20 + err = sat.DB.Console().Projects().UpdateBucketLimit(ctx, up2Proj.ID, updatedBucketsLimit) + require.NoError(t, err) + limits1, err = service.GetProjectUsageLimits(userCtx2, up2Proj.ID) require.NoError(t, err) require.NotNil(t, limits1) @@ -435,8 +444,10 @@ func TestService(t *testing.T) { // limits gotten by ID and publicID should be the same require.Equal(t, updatedStorageLimit.Int64(), limits1.StorageLimit) require.Equal(t, updatedBandwidthLimit.Int64(), limits1.BandwidthLimit) + require.Equal(t, int64(updatedBucketsLimit), limits1.BucketsLimit) require.Equal(t, updatedStorageLimit.Int64(), limits2.StorageLimit) require.Equal(t, updatedBandwidthLimit.Int64(), limits2.BandwidthLimit) + require.Equal(t, int64(updatedBucketsLimit), limits2.BucketsLimit) bucket := "testbucket1" err = planet.Uplinks[1].CreateBucket(ctx, sat, bucket) @@ -465,6 +476,7 @@ func TestService(t *testing.T) { limits2, err = service.GetProjectUsageLimits(userCtx2, up2Proj.PublicID) require.NoError(t, err) require.NotNil(t, limits2) + require.Equal(t, int64(2), limits2.BucketsUsed) require.Equal(t, allocatedAmount, limits2.BandwidthUsed) // set now as fourth day of the month. diff --git a/satellite/payments/stripe/accounts_test.go b/satellite/payments/stripe/accounts_test.go index b438c5440..e066f8c65 100644 --- a/satellite/payments/stripe/accounts_test.go +++ b/satellite/payments/stripe/accounts_test.go @@ -96,6 +96,7 @@ func TestSignupCouponCodes(t *testing.T) { nil, "", "", + sat.Config.Metainfo.ProjectLimits.MaxBuckets, console.Config{PasswordCost: console.TestPasswordCost, DefaultProjectLimit: 5}, )