Skip to content

CLOUDP-62030: Add pagination to listing projects and orgs #155

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

Merged
merged 8 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.6.2
github.com/stretchr/testify v1.4.0 // indirect
go.mongodb.org/ops-manager v0.5.0
go.mongodb.org/ops-manager v0.5.1
golang.org/x/crypto v0.0.0-20191108234033-bd318be0434a // indirect
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4 // indirect
golang.org/x/text v0.3.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGr
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.mongodb.org/ops-manager v0.5.0 h1:2T2CxZICAh/Y77Z+WrNT7XN2QlELDr3mSyCfmcPdOo0=
go.mongodb.org/ops-manager v0.5.0/go.mod h1:PpGSUCpa7ncGGh+JWFKuAeW87V5RdOdFPMsEgH1uyHg=
go.mongodb.org/ops-manager v0.5.1 h1:P+Wi9r6JiSZEpuRfLrAem1v2zZ7Ew3cIZbHLibMVmKM=
go.mongodb.org/ops-manager v0.5.1/go.mod h1:PpGSUCpa7ncGGh+JWFKuAeW87V5RdOdFPMsEgH1uyHg=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
Expand Down
9 changes: 7 additions & 2 deletions internal/cli/iam_projects_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

type iamProjectsListOpts struct {
globalOpts
listOpts
store store.ProjectLister
}

Expand All @@ -38,10 +39,11 @@ func (opts *iamProjectsListOpts) init() error {
func (opts *iamProjectsListOpts) Run() error {
var projects interface{}
var err error
listOptions := opts.newListOptions()
if opts.OrgID() != "" && config.Service() == config.OpsManagerService {
projects, err = opts.store.GetOrgProjects(opts.OrgID())
projects, err = opts.store.GetOrgProjects(opts.OrgID(), listOptions)
} else {
projects, err = opts.store.GetAllProjects()
projects, err = opts.store.GetAllProjects(listOptions)
}
if err != nil {
return err
Expand All @@ -64,6 +66,9 @@ func IAMProjectsListBuilder() *cobra.Command {
},
}

cmd.Flags().IntVar(&opts.pageNum, flags.Page, 0, usage.Page)
cmd.Flags().IntVar(&opts.itemsPerPage, flags.Limit, 0, usage.Limit)

cmd.Flags().StringVar(&opts.orgID, flags.OrgID, "", usage.OrgID)

return cmd
Expand Down
31 changes: 17 additions & 14 deletions internal/cli/iam_projects_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,35 @@ func TestIAMProjectsList_Run(t *testing.T) {

expected := &mongodbatlas.Projects{}

listOpts := &iamProjectsListOpts{
store: mockStore,
}

t.Run("No OrgID is given", func(t *testing.T) {
mockStore.
EXPECT().
GetAllProjects().
GetAllProjects(listOpts.newListOptions()).
Return(expected, nil).
Times(1)

listOpts := &iamProjectsListOpts{
store: mockStore,
}
err := listOpts.Run()
if err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
})

t.Run("An OrgID is given for OM", func(t *testing.T) {
listOpts := &iamProjectsListOpts{
store: mockStore,
}
listOpts.orgID = "1"

mockStore.
EXPECT().
GetOrgProjects("1").
GetOrgProjects("1", listOpts.newListOptions()).
Return(expected, nil).
Times(1)

listOpts := &iamProjectsListOpts{
store: mockStore,
}
listOpts.orgID = "1"
config.SetService(config.OpsManagerService)
err := listOpts.Run()
if err != nil {
Expand All @@ -66,16 +68,17 @@ func TestIAMProjectsList_Run(t *testing.T) {
})

t.Run("An OrgID is given for Atlas", func(t *testing.T) {
listOpts := &iamProjectsListOpts{
store: mockStore,
}
listOpts.orgID = "1"

mockStore.
EXPECT().
GetAllProjects().
GetAllProjects(listOpts.newListOptions()).
Return(expected, nil).
Times(1)

listOpts := &iamProjectsListOpts{
store: mockStore,
}
listOpts.orgID = "1"
config.SetService(config.CloudService)
err := listOpts.Run()
if err != nil {
Expand Down
33 changes: 17 additions & 16 deletions internal/mocks/mock_projects.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions internal/store/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
)

type ProjectLister interface {
GetAllProjects() (interface{}, error)
GetOrgProjects(string) (interface{}, error)
GetAllProjects(*atlas.ListOptions) (interface{}, error)
GetOrgProjects(string, *atlas.ListOptions) (interface{}, error)
}

type OrgProjectLister interface {
Expand All @@ -47,24 +47,24 @@ type ProjectStore interface {
}

// GetAllProjects encapsulate the logic to manage different cloud providers
func (s *Store) GetAllProjects() (interface{}, error) {
func (s *Store) GetAllProjects(opts *atlas.ListOptions) (interface{}, error) {
switch s.service {
case config.CloudService:
result, _, err := s.client.(*atlas.Client).Projects.GetAllProjects(context.Background(), nil)
result, _, err := s.client.(*atlas.Client).Projects.GetAllProjects(context.Background(), opts)
return result, err
case config.CloudManagerService, config.OpsManagerService:
result, _, err := s.client.(*opsmngr.Client).Projects.List(context.Background(), nil)
result, _, err := s.client.(*opsmngr.Client).Projects.List(context.Background(), opts)
return result, err
default:
return nil, fmt.Errorf("unsupported service: %s", s.service)
}
}

// GetOrgProjects encapsulate the logic to manage different cloud providers
func (s *Store) GetOrgProjects(orgID string) (interface{}, error) {
func (s *Store) GetOrgProjects(orgID string, opts *atlas.ListOptions) (interface{}, error) {
switch s.service {
case config.CloudManagerService, config.OpsManagerService:
result, _, err := s.client.(*opsmngr.Client).Organizations.GetProjects(context.Background(), orgID)
result, _, err := s.client.(*opsmngr.Client).Organizations.GetProjects(context.Background(), orgID, opts)
return result, err
default:
return nil, fmt.Errorf("unsupported service: %s", s.service)
Expand Down