Skip to content

Commit

Permalink
Add pagination option for installation list command using skip and li…
Browse files Browse the repository at this point in the history
…mit flag

Signed-off-by: joshuabezaleel <[email protected]>
  • Loading branch information
joshuabezaleel committed Jun 6, 2022
1 parent 23dd9bc commit f0a7132
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 10 deletions.
7 changes: 6 additions & 1 deletion cmd/porter/installations.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ Optional output formats include json and yaml.`,
porter installations list -o json
porter installations list --all-namespaces,
porter installations list --label owner=myname --namespace dev
porter installations list --name myapp`,
porter installations list --name myapp
porter installations list --skip 2 --limit 2`,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.Validate()
},
Expand All @@ -64,6 +65,10 @@ Optional output formats include json and yaml.`,
"Filter the installations by a label formatted as: KEY=VALUE. May be specified multiple times.")
f.StringVarP(&opts.RawFormat, "output", "o", "plaintext",
"Specify an output format. Allowed values: plaintext, json, yaml")
f.Int64Var(&opts.Skip, "skip", 0,
"Skip the number of installations by a certain amount. Defaults to 0.")
f.Int64Var(&opts.Limit, "limit", 0,
"Limit the number of installations by a certain amount. Defaults to 0.")

return cmd
}
Expand Down
3 changes: 3 additions & 0 deletions docs/content/cli/installations_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ porter installations list [flags]
porter installations list --all-namespaces,
porter installations list --label owner=myname --namespace dev
porter installations list --name myapp
porter installations list --skip 2 --limit 2
```

### Options
Expand All @@ -37,9 +38,11 @@ porter installations list [flags]
--all-namespaces Include all namespaces in the results.
-h, --help help for list
-l, --label strings Filter the installations by a label formatted as: KEY=VALUE. May be specified multiple times.
--limit int Limit the number of installations by a certain amount. Defaults to 0.
--name string Filter the installations where the name contains the specified substring.
-n, --namespace string Filter the installations by namespace. Defaults to the global namespace.
-o, --output string Specify an output format. Allowed values: plaintext, json, yaml (default "plaintext")
--skip int Skip the number of installations by a certain amount. Defaults to 0.
```

### Options inherited from parent commands
Expand Down
3 changes: 3 additions & 0 deletions docs/content/cli/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ porter list [flags]
porter list --all-namespaces,
porter list --label owner=myname --namespace dev
porter list --name myapp
porter list --skip 2 --limit 2
```

### Options
Expand All @@ -37,9 +38,11 @@ porter list [flags]
--all-namespaces Include all namespaces in the results.
-h, --help help for list
-l, --label strings Filter the installations by a label formatted as: KEY=VALUE. May be specified multiple times.
--limit int Limit the number of installations by a certain amount. Defaults to 0.
--name string Filter the installations where the name contains the specified substring.
-n, --namespace string Filter the installations by namespace. Defaults to the global namespace.
-o, --output string Specify an output format. Allowed values: plaintext, json, yaml (default "plaintext")
--skip int Skip the number of installations by a certain amount. Defaults to 0.
```

### Options inherited from parent commands
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func getViperValue(flags *pflag.FlagSet, f *pflag.Flag) interface{} {
switch flagType {
case "int":
out, err = flags.GetInt(f.Name)
case "int64":
out, err = flags.GetInt64(f.Name)
case "string":
out, err = flags.GetString(f.Name)
case "bool":
Expand Down
4 changes: 3 additions & 1 deletion pkg/porter/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type ListOptions struct {
Namespace string
Name string
Labels []string
Skip int64
Limit int64
}

func (o *ListOptions) Validate() error {
Expand Down Expand Up @@ -218,7 +220,7 @@ func (p *Porter) ListInstallations(ctx context.Context, opts ListOptions) (Displ
ctx, log := tracing.StartSpan(ctx)
defer log.EndSpan()

installations, err := p.Installations.ListInstallations(ctx, opts.GetNamespace(), opts.Name, opts.ParseLabels())
installations, err := p.Installations.ListInstallations(ctx, opts.GetNamespace(), opts.Name, opts.ParseLabels(), opts.Skip, opts.Limit)
if err != nil {
return nil, log.Error(fmt.Errorf("could not list installations: %w", err))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/installation_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type InstallationProvider interface {
GetInstallation(ctx context.Context, namespace string, name string) (Installation, error)

// ListInstallations returns Installations sorted in ascending order by the namespace and then name.
ListInstallations(ctx context.Context, namespace string, name string, labels map[string]string) ([]Installation, error)
ListInstallations(ctx context.Context, namespace string, name string, labels map[string]string, skip int64, limit int64) ([]Installation, error)

// ListRuns returns Run documents sorted in ascending order by ID.
ListRuns(ctx context.Context, namespace string, installation string) ([]Run, map[string][]Result, error)
Expand Down
4 changes: 3 additions & 1 deletion pkg/storage/installation_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ func (s InstallationStore) Initialize(ctx context.Context) error {
return span.Error(err)
}

func (s InstallationStore) ListInstallations(ctx context.Context, namespace string, name string, labels map[string]string) ([]Installation, error) {
func (s InstallationStore) ListInstallations(ctx context.Context, namespace string, name string, labels map[string]string, skip int64, limit int64) ([]Installation, error) {
_, log := tracing.StartSpan(ctx)
defer log.EndSpan()

var out []Installation
findOpts := FindOptions{
Sort: []string{"namespace", "name"},
Filter: CreateListFiler(namespace, name, labels),
Skip: skip,
Limit: limit,
}

err := s.store.Find(ctx, CollectionInstallations, findOpts, &out)
Expand Down
36 changes: 33 additions & 3 deletions pkg/storage/installation_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestInstallationStorageProvider_Installations(t *testing.T) {
defer cp.Close()

t.Run("ListInstallations", func(t *testing.T) {
installations, err := cp.ListInstallations(context.Background(), "dev", "", nil)
installations, err := cp.ListInstallations(context.Background(), "dev", "", nil, 0, 0)
require.NoError(t, err, "ListInstallations failed")

require.Len(t, installations, 3, "Expected 3 installations")
Expand All @@ -171,6 +171,36 @@ func TestInstallationStorageProvider_Installations(t *testing.T) {
assert.Equal(t, cnab.StatusSucceeded, foo.Status.ResultStatus)
})

t.Run("ListInstallations with skip option", func(t *testing.T) {
installations, err := cp.ListInstallations(context.Background(), "dev", "", nil, 1, 0)
require.NoError(t, err, "ListInstallations failed")

require.Len(t, installations, 2, "Expected 2 installations")

bar := installations[0]
assert.Equal(t, "baz", bar.Name)
assert.Equal(t, cnab.StatusRunning, bar.Status.ResultStatus)

baz := installations[1]
assert.Equal(t, "foo", baz.Name)
assert.Equal(t, cnab.StatusSucceeded, baz.Status.ResultStatus)
})

t.Run("ListInstallations with limit option", func(t *testing.T) {
installations, err := cp.ListInstallations(context.Background(), "dev", "", nil, 0, 2)
require.NoError(t, err, "ListInstallations failed")

require.Len(t, installations, 2, "Expected 2 installations")

bar := installations[0]
assert.Equal(t, "bar", bar.Name)
assert.Equal(t, cnab.StatusSucceeded, bar.Status.ResultStatus)

baz := installations[1]
assert.Equal(t, "baz", baz.Name)
assert.Equal(t, cnab.StatusRunning, baz.Status.ResultStatus)
})

t.Run("GetInstallation", func(t *testing.T) {
foo, err := cp.GetInstallation(context.Background(), "dev", "foo")
require.NoError(t, err, "GetInstallation failed")
Expand All @@ -190,14 +220,14 @@ func TestInstallationStorageProvider_DeleteInstallation(t *testing.T) {
cp := generateInstallationData(t)
defer cp.Close()

installations, err := cp.ListInstallations(context.Background(), "dev", "", nil)
installations, err := cp.ListInstallations(context.Background(), "dev", "", nil, 0, 0)
require.NoError(t, err, "ListInstallations failed")
assert.Len(t, installations, 3, "expected 3 installations")

err = cp.RemoveInstallation(context.Background(), "dev", "foo")
require.NoError(t, err, "RemoveInstallation failed")

installations, err = cp.ListInstallations(context.Background(), "dev", "", nil)
installations, err = cp.ListInstallations(context.Background(), "dev", "", nil, 0, 0)
require.NoError(t, err, "ListInstallations failed")
assert.Len(t, installations, 2, "expected foo to be deleted")

Expand Down
6 changes: 3 additions & 3 deletions pkg/storage/migrations/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestManager_NoMigrationEmptyHome(t *testing.T) {
defer mgr.Close()
claimStore := storage.NewInstallationStore(mgr)

_, err := claimStore.ListInstallations(context.Background(), "", "", nil)
_, err := claimStore.ListInstallations(context.Background(), "", "", nil, 0, 0)
require.NoError(t, err, "ListInstallations failed")

credStore := storage.NewCredentialStore(mgr, nil)
Expand Down Expand Up @@ -101,7 +101,7 @@ storage.Schema{ID:"schema", Installations:"needs-migration", Credentials:"1.0.1"
}

t.Run("list", func(t *testing.T) {
_, err = claimStore.ListInstallations(context.Background(), "", "", nil)
_, err = claimStore.ListInstallations(context.Background(), "", "", nil, 0, 0)
checkMigrationError(t, err)
})

Expand All @@ -124,7 +124,7 @@ func TestClaimStorage_NoMigrationRequiredForEmptyHome(t *testing.T) {
defer mgr.Close()
claimStore := storage.NewInstallationStore(mgr)

names, err := claimStore.ListInstallations(context.Background(), "", "", nil)
names, err := claimStore.ListInstallations(context.Background(), "", "", nil, 0, 0)
require.NoError(t, err, "ListInstallations failed")
assert.Empty(t, names, "Expected an empty list of installations since porter home is new")
}
Expand Down

0 comments on commit f0a7132

Please sign in to comment.