Skip to content

Commit a70173a

Browse files
feat(service-account): add pagination to list command (#1771)
1 parent dbf14b7 commit a70173a

5 files changed

Lines changed: 82 additions & 2 deletions

File tree

docs/commands/rhoas_service-account_list.md

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cmd/serviceaccount/list/list.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"fmt"
66
"time"
77

8+
"github.com/redhat-developer/app-services-cli/internal/build"
9+
"github.com/redhat-developer/app-services-cli/pkg/cmd/serviceaccount/svcaccountcmdutil/validation"
10+
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil"
811
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
912
"github.com/redhat-developer/app-services-cli/pkg/core/config"
1013
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
@@ -28,6 +31,8 @@ type options struct {
2831

2932
output string
3033
enableAuthV2 bool
34+
page int32
35+
size int32
3136
}
3237

3338
// svcAcctRow contains the properties used to
@@ -62,6 +67,18 @@ func NewListCommand(f *factory.Factory) *cobra.Command {
6267
return flagutil.InvalidValueError("output", opts.output, flagutil.ValidOutputFormats...)
6368
}
6469

70+
validator := &validation.Validator{
71+
Localizer: opts.localizer,
72+
}
73+
74+
if err := validator.ValidatePage(opts.page); err != nil {
75+
return err
76+
}
77+
78+
if err := validator.ValidateSize(opts.size); err != nil {
79+
return err
80+
}
81+
6582
return runList(opts)
6683
},
6784
}
@@ -71,6 +88,10 @@ func NewListCommand(f *factory.Factory) *cobra.Command {
7188

7289
_ = cmd.Flags().MarkDeprecated("enable-auth-v2", opts.localizer.MustLocalize("serviceAccount.common.flag.deprecated.enableAuthV2"))
7390

91+
cmd.Flags().Int32VarP(&opts.page, "page", "", int32(cmdutil.ConvertPageValueToInt32(build.DefaultPageNumber)), opts.localizer.MustLocalize("serviceAccount.list.flag.page.description"))
92+
// Default has been set to 100 to preserve how list worked before
93+
cmd.Flags().Int32VarP(&opts.size, "size", "", 100, opts.localizer.MustLocalize("serviceAccount.list.flag.size.description"))
94+
7495
flagutil.EnableOutputFlagCompletion(cmd)
7596

7697
return cmd
@@ -82,7 +103,14 @@ func runList(opts *options) (err error) {
82103
return err
83104
}
84105

85-
serviceaccounts, _, err := conn.API().ServiceAccountMgmt().GetServiceAccounts(opts.Context).Execute()
106+
a := conn.API().ServiceAccountMgmt().GetServiceAccounts(opts.Context)
107+
108+
// Calculate offset based on page and size provided
109+
calculatedFirst := (opts.page - 1) * opts.size
110+
a = a.First(calculatedFirst)
111+
a = a.Max(opts.size)
112+
113+
serviceaccounts, _, err := a.Execute()
86114
if err != nil {
87115
return err
88116
}

pkg/cmd/serviceaccount/svcaccountcmdutil/validation/validation.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package validation
22

33
import (
4+
"regexp"
5+
46
"github.com/redhat-developer/app-services-cli/pkg/core/errors"
57
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
6-
"regexp"
78
)
89

910
const (
@@ -56,3 +57,37 @@ func (v *Validator) ValidateUUID(val interface{}) error {
5657

5758
return v.Localizer.MustLocalizeError("serviceAccount.common.validation.id.error.invalidID", localize.NewEntry("ID", id))
5859
}
60+
61+
// ValidatePage validates if page number is valid
62+
func (v *Validator) ValidatePage(val interface{}) error {
63+
page, ok := val.(int32)
64+
if !ok {
65+
return errors.NewCastError(val, "int32")
66+
}
67+
68+
if page < 1 {
69+
return v.Localizer.MustLocalizeError("serviceAccount.list.validation.page.error", localize.NewEntry("Page", page))
70+
}
71+
72+
return nil
73+
74+
}
75+
76+
// ValidateSize validates if a valid size is provided
77+
func (v *Validator) ValidateSize(val interface{}) error {
78+
size, ok := val.(int32)
79+
if !ok {
80+
return errors.NewCastError(val, "int32")
81+
}
82+
83+
if size > 100 {
84+
return v.Localizer.MustLocalizeError("serviceAccount.list.validation.size.error.max", localize.NewEntry("Size", size))
85+
}
86+
87+
if size < 1 {
88+
return v.Localizer.MustLocalizeError("serviceAccount.list.validation.size.error.min", localize.NewEntry("Size", size))
89+
}
90+
91+
return nil
92+
93+
}

pkg/core/localize/locales/en/cmd/serviceaccount.en.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ one = 'Format in which to display the service account (choose from: "json", "yml
2424
[serviceAccount.list.flag.output.description]
2525
one = 'Format in which to display the service accounts (choose from: "json", "yml", "yaml")'
2626

27+
[serviceAccount.list.flag.page.description]
28+
one = 'Current page number for the list'
29+
30+
[serviceAccount.list.flag.size.description]
31+
one = 'Maximum number of items to be returned per page'
32+
2733
[serviceAccount.common.flag.deprecated.enableAuthV2]
2834
one = '''migration has been completed to the new Service Account SDK
2935

pkg/core/localize/locales/en/cmd/serviceaccount_list.en.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,12 @@ one = 'unable to list service accounts'
2929
[serviceAccount.list.log.info.noneFound]
3030
description = 'Info message when no service accounts were found'
3131
one = 'No service accounts were found.'
32+
33+
[serviceAccount.list.validation.page.error]
34+
one = 'invalid page number {{.Page}}, minimum value is 1'
35+
36+
[serviceAccount.list.validation.size.error.min]
37+
one = 'invalid size {{.Size}}, minimum value is 1'
38+
39+
[serviceAccount.list.validation.size.error.max]
40+
one = 'invalid size {{.Size}}, maximum value is 100'

0 commit comments

Comments
 (0)