From af342ba00782f164a3907aeddcb957bd9fc448c7 Mon Sep 17 00:00:00 2001 From: Darya Plotnytska Date: Tue, 15 Oct 2024 16:26:24 +0200 Subject: [PATCH] is: Use default if page limit is zero --- pkg/identityserver/application_access.go | 16 +++++- pkg/identityserver/application_registry.go | 8 ++- pkg/identityserver/client_access.go | 8 ++- pkg/identityserver/client_registry.go | 8 ++- pkg/identityserver/config.go | 8 +-- pkg/identityserver/end_device_registry.go | 8 ++- pkg/identityserver/gateway_access.go | 16 +++++- pkg/identityserver/gateway_registry.go | 8 ++- pkg/identityserver/invitation_registry.go | 8 ++- pkg/identityserver/notification_registry.go | 8 ++- pkg/identityserver/oauth_registry.go | 16 +++++- pkg/identityserver/organization_access.go | 16 +++++- pkg/identityserver/organization_registry.go | 8 ++- pkg/identityserver/registry_search.go | 60 ++++++++++++++++---- pkg/identityserver/user_access.go | 8 ++- pkg/identityserver/user_bookmark_registry.go | 8 ++- pkg/identityserver/user_registry.go | 8 ++- pkg/identityserver/user_session_registry.go | 8 ++- 18 files changed, 192 insertions(+), 36 deletions(-) diff --git a/pkg/identityserver/application_access.go b/pkg/identityserver/application_access.go index 1ad3ea2a44..ff8256ad0e 100644 --- a/pkg/identityserver/application_access.go +++ b/pkg/identityserver/application_access.go @@ -125,7 +125,13 @@ func (is *IdentityServer) listApplicationAPIKeys( } ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) @@ -398,7 +404,13 @@ func (is *IdentityServer) listApplicationCollaborators( ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) diff --git a/pkg/identityserver/application_registry.go b/pkg/identityserver/application_registry.go index 605810f675..49cab6666d 100644 --- a/pkg/identityserver/application_registry.go +++ b/pkg/identityserver/application_registry.go @@ -230,7 +230,13 @@ func (is *IdentityServer) listApplications( // nolint:gocyclo } ctx = store.WithOrder(ctx, req.Order) var total uint64 - paginateCtx := store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + paginateCtx := store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) diff --git a/pkg/identityserver/client_access.go b/pkg/identityserver/client_access.go index ad9770cb45..d64ac8abab 100644 --- a/pkg/identityserver/client_access.go +++ b/pkg/identityserver/client_access.go @@ -203,7 +203,13 @@ func (is *IdentityServer) listClientCollaborators( ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) diff --git a/pkg/identityserver/client_registry.go b/pkg/identityserver/client_registry.go index c3a3920361..61166a2579 100644 --- a/pkg/identityserver/client_registry.go +++ b/pkg/identityserver/client_registry.go @@ -249,7 +249,13 @@ func (is *IdentityServer) listClients( ctx = store.WithOrder(ctx, req.Order) var total uint64 - paginateCtx := store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + paginateCtx := store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) diff --git a/pkg/identityserver/config.go b/pkg/identityserver/config.go index 8dbbe565cf..ba3c1183a1 100644 --- a/pkg/identityserver/config.go +++ b/pkg/identityserver/config.go @@ -34,10 +34,6 @@ import ( ) // Config for the Identity Server. - -type Pagination struct { - DefaultLimit uint32 `name:"default-limit" description:"The default limit applied to paginated requests if not specified"` -} type Config struct { DatabaseURI string `name:"database-uri" description:"Database connection URI"` UserRegistration struct { @@ -128,6 +124,10 @@ type Config struct { Pagination Pagination `name:"pagination" description:"Pagination settings"` } +type Pagination struct { + DefaultLimit uint32 `name:"default-limit" description:"The default limit applied to paginated requests if not specified"` +} + type emailTemplatesConfig struct { Source string `name:"source" description:"Source of the email template files (directory, url, blob)"` // nolint:lll Static map[string][]byte `name:"-"` diff --git a/pkg/identityserver/end_device_registry.go b/pkg/identityserver/end_device_registry.go index 499fa17541..86e0fe0706 100644 --- a/pkg/identityserver/end_device_registry.go +++ b/pkg/identityserver/end_device_registry.go @@ -312,7 +312,13 @@ func (is *IdentityServer) listEndDevices(ctx context.Context, req *ttnpb.ListEnd } ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) diff --git a/pkg/identityserver/gateway_access.go b/pkg/identityserver/gateway_access.go index 7bf451a8c9..8c7f0eebb8 100644 --- a/pkg/identityserver/gateway_access.go +++ b/pkg/identityserver/gateway_access.go @@ -121,7 +121,13 @@ func (is *IdentityServer) listGatewayAPIKeys( } ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) @@ -389,7 +395,13 @@ func (is *IdentityServer) listGatewayCollaborators( ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) diff --git a/pkg/identityserver/gateway_registry.go b/pkg/identityserver/gateway_registry.go index 4d624cf3b3..bbcb681cd7 100644 --- a/pkg/identityserver/gateway_registry.go +++ b/pkg/identityserver/gateway_registry.go @@ -406,7 +406,13 @@ func (is *IdentityServer) listGateways( // nolint:gocyclo ctx = store.WithOrder(ctx, req.Order) var total uint64 - paginateCtx := store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + paginateCtx := store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) diff --git a/pkg/identityserver/invitation_registry.go b/pkg/identityserver/invitation_registry.go index 0662344d2a..bfe20c31f2 100644 --- a/pkg/identityserver/invitation_registry.go +++ b/pkg/identityserver/invitation_registry.go @@ -88,7 +88,13 @@ func (is *IdentityServer) listInvitations(ctx context.Context, req *ttnpb.ListIn return nil, errNoInviteRights.New() } var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) diff --git a/pkg/identityserver/notification_registry.go b/pkg/identityserver/notification_registry.go index d67c57f768..648877e69b 100644 --- a/pkg/identityserver/notification_registry.go +++ b/pkg/identityserver/notification_registry.go @@ -340,7 +340,13 @@ func (is *IdentityServer) listNotifications(ctx context.Context, req *ttnpb.List res := &ttnpb.ListNotificationsResponse{} err := is.store.Transact(ctx, func(ctx context.Context, st store.Store) (err error) { var total uint64 - paginateCtx := store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + paginateCtx := store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) diff --git a/pkg/identityserver/oauth_registry.go b/pkg/identityserver/oauth_registry.go index 62e390fa3b..086c4381f3 100644 --- a/pkg/identityserver/oauth_registry.go +++ b/pkg/identityserver/oauth_registry.go @@ -30,7 +30,13 @@ func (is *IdentityServer) listOAuthClientAuthorizations(ctx context.Context, req } ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) @@ -60,7 +66,13 @@ func (is *IdentityServer) listOAuthAccessTokens(ctx context.Context, req *ttnpb. } ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) diff --git a/pkg/identityserver/organization_access.go b/pkg/identityserver/organization_access.go index d3f76e362b..c533b40d34 100644 --- a/pkg/identityserver/organization_access.go +++ b/pkg/identityserver/organization_access.go @@ -129,7 +129,13 @@ func (is *IdentityServer) listOrganizationAPIKeys( } ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) @@ -402,7 +408,13 @@ func (is *IdentityServer) listOrganizationCollaborators( ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) diff --git a/pkg/identityserver/organization_registry.go b/pkg/identityserver/organization_registry.go index 6387e0a08a..173af91e36 100644 --- a/pkg/identityserver/organization_registry.go +++ b/pkg/identityserver/organization_registry.go @@ -203,7 +203,13 @@ func (is *IdentityServer) listOrganizations( ctx = store.WithOrder(ctx, req.Order) var total uint64 - paginateCtx := store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + paginateCtx := store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) diff --git a/pkg/identityserver/registry_search.go b/pkg/identityserver/registry_search.go index f6440e8e69..d65d10b33e 100644 --- a/pkg/identityserver/registry_search.go +++ b/pkg/identityserver/registry_search.go @@ -73,7 +73,13 @@ func (rs *registrySearch) SearchApplications( ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = rs.IdentityServer.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) @@ -101,7 +107,7 @@ func (rs *registrySearch) SearchApplications( return err } } - ctx = store.WithPagination(ctx, 0, 0, nil) // Reset pagination (already done in EntitySearch.FindApplications). + ctx = store.WithPagination(ctx, rs.IdentityServer.config.Pagination.DefaultLimit, 0, nil) // Reset pagination (already done in EntitySearch.FindApplications). res.Applications, err = st.FindApplications(ctx, entityIDs, req.FieldMask.GetPaths()) if err != nil { return err @@ -176,7 +182,13 @@ func (rs *registrySearch) SearchClients( ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = rs.IdentityServer.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) @@ -204,7 +216,7 @@ func (rs *registrySearch) SearchClients( return err } } - ctx = store.WithPagination(ctx, 0, 0, nil) // Reset pagination (already done in EntitySearch.FindClients). + ctx = store.WithPagination(ctx, 0, rs.IdentityServer.config.Pagination.DefaultLimit, nil) // Reset pagination (already done in EntitySearch.FindClients). res.Clients, err = st.FindClients(ctx, entityIDs, req.FieldMask.GetPaths()) if err != nil { return err @@ -282,7 +294,13 @@ func (rs *registrySearch) SearchGateways( ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = rs.IdentityServer.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) @@ -310,7 +328,7 @@ func (rs *registrySearch) SearchGateways( return err } } - ctx = store.WithPagination(ctx, 0, 0, nil) // Reset pagination (already done in EntitySearch.FindGateways). + ctx = store.WithPagination(ctx, 0, rs.IdentityServer.config.Pagination.DefaultLimit, nil) // Reset pagination (already done in EntitySearch.FindGateways). res.Gateways, err = st.FindGateways(ctx, entityIDs, req.FieldMask.GetPaths()) if err != nil { return err @@ -391,7 +409,13 @@ func (rs *registrySearch) SearchOrganizations( ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = rs.IdentityServer.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) @@ -419,7 +443,7 @@ func (rs *registrySearch) SearchOrganizations( return err } } - ctx = store.WithPagination(ctx, 0, 0, nil) // Reset pagination (already done in EntitySearch.FindOrganizations). + ctx = store.WithPagination(ctx, rs.IdentityServer.config.Pagination.DefaultLimit, 0, nil) // Reset pagination (already done in EntitySearch.FindOrganizations). res.Organizations, err = st.FindOrganizations(ctx, entityIDs, req.FieldMask.GetPaths()) if err != nil { return err @@ -492,7 +516,13 @@ func (rs *registrySearch) SearchUsers( ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = rs.IdentityServer.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) @@ -516,7 +546,7 @@ func (rs *registrySearch) SearchUsers( if len(ids) == 0 { return nil } - ctx = store.WithPagination(ctx, 0, 0, nil) // Reset pagination (already done in EntitySearch.FindUsers). + ctx = store.WithPagination(ctx, rs.IdentityServer.config.Pagination.DefaultLimit, 0, nil) // Reset pagination (already done in EntitySearch.FindUsers). res.Users, err = st.FindUsers(ctx, ids, req.FieldMask.GetPaths()) if err != nil { return err @@ -606,7 +636,13 @@ func (rs *registrySearch) SearchEndDevices(ctx context.Context, req *ttnpb.Searc ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = rs.IdentityServer.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) @@ -622,7 +658,7 @@ func (rs *registrySearch) SearchEndDevices(ctx context.Context, req *ttnpb.Searc if len(ids) == 0 { return nil } - ctx = store.WithPagination(ctx, 0, 0, nil) // Reset pagination (already done in EntitySearch.FindEndDevices). + ctx = store.WithPagination(ctx, rs.IdentityServer.config.Pagination.DefaultLimit, 0, nil) // Reset pagination (already done in EntitySearch.FindEndDevices). res.EndDevices, err = st.FindEndDevices(ctx, ids, req.FieldMask.GetPaths()) if err != nil { return err diff --git a/pkg/identityserver/user_access.go b/pkg/identityserver/user_access.go index b3e22c385d..2b9192a8f2 100644 --- a/pkg/identityserver/user_access.go +++ b/pkg/identityserver/user_access.go @@ -106,7 +106,13 @@ func (is *IdentityServer) listUserAPIKeys( } ctx = store.WithOrder(ctx, req.Order) var total uint64 - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) diff --git a/pkg/identityserver/user_bookmark_registry.go b/pkg/identityserver/user_bookmark_registry.go index 887d5ded02..8c19323ed5 100644 --- a/pkg/identityserver/user_bookmark_registry.go +++ b/pkg/identityserver/user_bookmark_registry.go @@ -54,8 +54,14 @@ func (is *IdentityServer) listUserBookmarks( } var total uint64 + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } ctx = store.WithOrder(ctx, req.Order) - ctx = store.WithPagination(ctx, req.Limit, req.Page, &total) + ctx = store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) diff --git a/pkg/identityserver/user_registry.go b/pkg/identityserver/user_registry.go index 3864b7d705..d491aa4205 100644 --- a/pkg/identityserver/user_registry.go +++ b/pkg/identityserver/user_registry.go @@ -351,7 +351,13 @@ func (is *IdentityServer) listUsers(ctx context.Context, req *ttnpb.ListUsersReq } ctx = store.WithOrder(ctx, req.Order) var total uint64 - paginateCtx := store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + paginateCtx := store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total) diff --git a/pkg/identityserver/user_session_registry.go b/pkg/identityserver/user_session_registry.go index cdfbf65331..40e122c1ce 100644 --- a/pkg/identityserver/user_session_registry.go +++ b/pkg/identityserver/user_session_registry.go @@ -29,7 +29,13 @@ func (is *IdentityServer) listUserSessions(ctx context.Context, req *ttnpb.ListU } ctx = store.WithOrder(ctx, req.Order) var total uint64 - paginateCtx := store.WithPagination(ctx, req.Limit, req.Page, &total) + var limit uint32 + if req.Limit == 0 { + limit = is.config.Pagination.DefaultLimit + } else { + limit = req.Limit + } + paginateCtx := store.WithPagination(ctx, limit, req.Page, &total) defer func() { if err == nil { setTotalHeader(ctx, total)