Skip to content

Commit

Permalink
feat(core): pagination of LIST commands (#447)
Browse files Browse the repository at this point in the history
1. Fixes a few bad BATS tests that used `--regexp` with the wrong values
but passed because of more generous `assert_output` instead of
`assert_line`
2. Adds pagination info to LIST command flags and tables in a footer
3. Adds basic BATS tests to all LIST commands
4. Adds a comprehensive BATS test for pagination scenarios to
`attributes` alone to avoid test redundancy
  • Loading branch information
jakedoublev authored Dec 5, 2024
1 parent 1c8c739 commit 673a064
Show file tree
Hide file tree
Showing 37 changed files with 358 additions and 121 deletions.
22 changes: 21 additions & 1 deletion cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import (
// devCmd is the command for playground-style development
var devCmd = man.Docs.GetCommand("dev")

var metadataLabels []string
var (
metadataLabels []string
defaultListFlagLimit int32 = 300
defaultListFlagOffset int32 = 0
)

func dev_designSystem(cmd *cobra.Command, args []string) {
fmt.Print("Design system\n=============\n\n")
Expand Down Expand Up @@ -116,6 +120,22 @@ func injectLabelFlags(cmd *cobra.Command, isUpdate bool) {
}
}

// Adds reusable limit/offset flags to a Policy LIST command
func injectListPaginationFlags(listDoc *man.Doc) {
listDoc.Flags().Int32P(
listDoc.GetDocFlag("limit").Name,
listDoc.GetDocFlag("limit").Shorthand,
defaultListFlagLimit,
listDoc.GetDocFlag("limit").Description,
)
listDoc.Flags().Int32P(
listDoc.GetDocFlag("offset").Name,
listDoc.GetDocFlag("offset").Shorthand,
defaultListFlagOffset,
listDoc.GetDocFlag("offset").Description,
)
}

// Read bytes from stdin without blocking by checking size first
func readPipedStdin() []byte {
stat, err := os.Stdin.Stat()
Expand Down
7 changes: 6 additions & 1 deletion cmd/kas-grants.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ func policy_listKasGrants(cmd *cobra.Command, args []string) {
h := NewHandler(c)
defer h.Close()
kasF := c.Flags.GetOptionalString("kas")
limit := c.Flags.GetRequiredInt32("limit")
offset := c.Flags.GetRequiredInt32("offset")
var (
kasID string
kasURI string
Expand All @@ -177,7 +179,7 @@ func policy_listKasGrants(cmd *cobra.Command, args []string) {
}
}

grants, err := h.ListKasGrants(cmd.Context(), kasID, kasURI)
grants, page, err := h.ListKasGrants(cmd.Context(), kasID, kasURI, limit, offset)
if err != nil {
cli.ExitWithError("Failed to list assigned KAS Grants", err)
}
Expand Down Expand Up @@ -224,6 +226,7 @@ func policy_listKasGrants(cmd *cobra.Command, args []string) {
}
}
t = t.WithRows(rows)
t = cli.WithListPaginationFooter(t, page)

// Do not supporting printing the 'get --id=...' helper message as grants are atypical
// with no individual ID.
Expand Down Expand Up @@ -304,6 +307,8 @@ func init() {
listCmd.GetDocFlag("kas").Default,
listCmd.GetDocFlag("kas").Description,
)
injectListPaginationFlags(listCmd)

cmd := man.Docs.GetCommand("policy/kas-grants",
man.WithSubcommands(assignCmd, unassignCmd, listCmd),
)
Expand Down
7 changes: 6 additions & 1 deletion cmd/kas-registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ func policy_listKeyAccessRegistries(cmd *cobra.Command, args []string) {
h := NewHandler(c)
defer h.Close()

list, err := h.ListKasRegistryEntries()
limit := c.Flags.GetRequiredInt32("limit")
offset := c.Flags.GetRequiredInt32("offset")

list, page, err := h.ListKasRegistryEntries(limit, offset)
if err != nil {
cli.ExitWithError("Failed to list Registered KAS entries", err)
}
Expand All @@ -84,6 +87,7 @@ func policy_listKeyAccessRegistries(cmd *cobra.Command, args []string) {
}))
}
t = t.WithRows(rows)
t = cli.WithListPaginationFooter(t, page)
HandleSuccess(cmd, "", t, list)
}

Expand Down Expand Up @@ -268,6 +272,7 @@ func init() {
listDoc := man.Docs.GetCommand("policy/kas-registry/list",
man.WithRun(policy_listKeyAccessRegistries),
)
injectListPaginationFlags(listDoc)

createDoc := man.Docs.GetCommand("policy/kas-registry/create",
man.WithRun(policy_createKeyAccessRegistry),
Expand Down
7 changes: 6 additions & 1 deletion cmd/policy-attributeNamespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func policy_listAttributeNamespaces(cmd *cobra.Command, args []string) {
defer h.Close()

state := cli.GetState(cmd)
list, err := h.ListNamespaces(state)
limit := c.Flags.GetRequiredInt32("limit")
offset := c.Flags.GetRequiredInt32("offset")

list, page, err := h.ListNamespaces(state, limit, offset)
if err != nil {
cli.ExitWithError("Failed to list namespaces", err)
}
Expand All @@ -72,6 +75,7 @@ func policy_listAttributeNamespaces(cmd *cobra.Command, args []string) {
)
}
t = t.WithRows(rows)
t = cli.WithListPaginationFooter(t, page)
HandleSuccess(cmd, "", t, list)
}

Expand Down Expand Up @@ -281,6 +285,7 @@ func init() {
listCmd.GetDocFlag("state").Default,
listCmd.GetDocFlag("state").Description,
)
injectListPaginationFlags(listCmd)

createDoc := man.Docs.GetCommand("policy/attributes/namespaces/create",
man.WithRun(policy_createAttributeNamespace),
Expand Down
8 changes: 7 additions & 1 deletion cmd/policy-attributeValues.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ func policy_listAttributeValue(cmd *cobra.Command, args []string) {
c := cli.New(cmd, args)
h := NewHandler(c)
defer h.Close()

attrId := c.FlagHelper.GetRequiredID("attribute-id")
state := cli.GetState(cmd)
vals, err := h.ListAttributeValues(attrId, state)
limit := c.Flags.GetRequiredInt32("limit")
offset := c.Flags.GetRequiredInt32("offset")

vals, page, err := h.ListAttributeValues(attrId, state, limit, offset)
if err != nil {
cli.ExitWithError("Failed to list attribute values", err)
}
Expand All @@ -80,6 +84,7 @@ func policy_listAttributeValue(cmd *cobra.Command, args []string) {
}))
}
t = t.WithRows(rows)
t = cli.WithListPaginationFooter(t, page)
HandleSuccess(cmd, "", t, vals)
}

Expand Down Expand Up @@ -264,6 +269,7 @@ func init() {
listCmd.GetDocFlag("state").Default,
listCmd.GetDocFlag("state").Description,
)
injectListPaginationFlags(listCmd)

updateCmd := man.Docs.GetCommand("policy/attributes/values/update",
man.WithRun(policy_updateAttributeValue),
Expand Down
7 changes: 6 additions & 1 deletion cmd/policy-attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ func policy_listAttributes(cmd *cobra.Command, args []string) {
defer h.Close()

state := cli.GetState(cmd)
attrs, err := h.ListAttributes(state)
limit := c.Flags.GetRequiredInt32("limit")
offset := c.Flags.GetRequiredInt32("offset")

attrs, page, err := h.ListAttributes(state, limit, offset)
if err != nil {
cli.ExitWithError("Failed to list attributes", err)
}
Expand Down Expand Up @@ -123,6 +126,7 @@ func policy_listAttributes(cmd *cobra.Command, args []string) {
}))
}
t = t.WithRows(rows)
t = cli.WithListPaginationFooter(t, page)
HandleSuccess(cmd, "", t, attrs)
}

Expand Down Expand Up @@ -349,6 +353,7 @@ func init() {
listDoc.GetDocFlag("state").Default,
listDoc.GetDocFlag("state").Description,
)
injectListPaginationFlags(listDoc)

// Update an attribute
updateDoc := man.Docs.GetCommand("policy/attributes/update",
Expand Down
7 changes: 6 additions & 1 deletion cmd/policy-resourceMappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ func policy_listResourceMappings(cmd *cobra.Command, args []string) {
h := NewHandler(c)
defer h.Close()

rmList, err := h.ListResourceMappings()
limit := c.Flags.GetRequiredInt32("limit")
offset := c.Flags.GetRequiredInt32("offset")

rmList, page, err := h.ListResourceMappings(cmd.Context(), limit, offset)
if err != nil {
cli.ExitWithError("Failed to list resource mappings", err)
}
Expand Down Expand Up @@ -101,6 +104,7 @@ func policy_listResourceMappings(cmd *cobra.Command, args []string) {
}))
}
t = t.WithRows(rows)
t = cli.WithListPaginationFooter(t, page)
HandleSuccess(cmd, "", t, rmList)
}

Expand Down Expand Up @@ -189,6 +193,7 @@ func init() {
listDoc := man.Docs.GetCommand("policy/resource-mappings/list",
man.WithRun(policy_listResourceMappings),
)
injectListPaginationFlags(listDoc)

updateDoc := man.Docs.GetCommand("policy/resource-mappings/update",
man.WithRun(policy_updateResourceMapping),
Expand Down
7 changes: 6 additions & 1 deletion cmd/policy-subjectConditionSets.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ func policy_listSubjectConditionSets(cmd *cobra.Command, args []string) {
h := NewHandler(c)
defer h.Close()

scsList, err := h.ListSubjectConditionSets()
limit := c.Flags.GetRequiredInt32("limit")
offset := c.Flags.GetRequiredInt32("offset")

scsList, page, err := h.ListSubjectConditionSets(limit, offset)
if err != nil {
cli.ExitWithError("Error listing subject condition sets", err)
}
Expand Down Expand Up @@ -171,6 +174,7 @@ func policy_listSubjectConditionSets(cmd *cobra.Command, args []string) {
}))
}
t = t.WithRows(rows)
t = cli.WithListPaginationFooter(t, page)
HandleSuccess(cmd, "", t, scsList)
}

Expand Down Expand Up @@ -340,6 +344,7 @@ func init() {
listDoc := man.Docs.GetCommand("policy/subject-condition-sets/list",
man.WithRun(policy_listSubjectConditionSets),
)
injectListPaginationFlags(listDoc)

updateDoc := man.Docs.GetCommand("policy/subject-condition-sets/update",
man.WithRun(policy_updateSubjectConditionSet),
Expand Down
7 changes: 6 additions & 1 deletion cmd/policy-subjectMappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ func policy_listSubjectMappings(cmd *cobra.Command, args []string) {
h := NewHandler(c)
defer h.Close()

list, err := h.ListSubjectMappings()
limit := c.Flags.GetRequiredInt32("limit")
offset := c.Flags.GetRequiredInt32("offset")

list, page, err := h.ListSubjectMappings(limit, offset)
if err != nil {
cli.ExitWithError("Failed to get subject mappings", err)
}
Expand Down Expand Up @@ -98,6 +101,7 @@ func policy_listSubjectMappings(cmd *cobra.Command, args []string) {
}))
}
t = t.WithRows(rows)
t = cli.WithListPaginationFooter(t, page)
HandleSuccess(cmd, "", t, list)
}

Expand Down Expand Up @@ -353,6 +357,7 @@ func init() {
listDoc := man.Docs.GetCommand("policy/subject-mappings/list",
man.WithRun(policy_listSubjectMappings),
)
injectListPaginationFlags(listDoc)

createDoc := man.Docs.GetCommand("policy/subject-mappings/create",
man.WithRun(policy_createSubjectMapping),
Expand Down
6 changes: 6 additions & 0 deletions docs/man/policy/attributes/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ command:
- inactive
- any
default: active
- name: limit
shorthand: l
description: Limit retrieved count
- name: offset
shorthand: o
description: Offset (page) quantity from start of the list
---

By default, the list will only provide `active` attributes if unspecified, but the filter can be controlled with the `--state` flag.
Expand Down
6 changes: 6 additions & 0 deletions docs/man/policy/attributes/namespaces/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ command:
- name: state
shorthand: s
description: Filter by state [active, inactive, any]
- name: limit
shorthand: l
description: Limit retrieved count
- name: offset
shorthand: o
description: Offset (page) quantity from start of the list
---

For more general information, see the `namespaces` subcommand.
6 changes: 6 additions & 0 deletions docs/man/policy/attributes/values/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ command:
- inactive
- any
default: active
- name: limit
shorthand: l
description: Limit retrieved count
- name: offset
shorthand: o
description: Offset (page) quantity from start of the list
---

By default, the list will only provide `active` values if unspecified, but the filter can be controlled with the `--state` flag.
Expand Down
6 changes: 6 additions & 0 deletions docs/man/policy/kas-grants/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ command:
- name: kas
shorthand: k
description: The optional ID or URI of a KAS to filter the list
- name: limit
shorthand: l
description: Limit retrieved count
- name: offset
shorthand: o
description: Offset (page) quantity from start of the list
---

List the Grants of Registered Key Access Servers (KASes) to attribute namespaces, definitions,
Expand Down
7 changes: 7 additions & 0 deletions docs/man/policy/kas-registry/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ command:
name: list
aliases:
- l
flags:
- name: limit
shorthand: l
description: Limit retrieved count
- name: offset
shorthand: o
description: Offset (page) quantity from start of the list
---

For more information about registration of Key Access Servers, see the manual for `kas-registry`.
7 changes: 7 additions & 0 deletions docs/man/policy/resource-mappings/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ command:
name: list
aliases:
- l
flags:
- name: limit
shorthand: l
description: Limit retrieved count
- name: offset
shorthand: o
description: Offset (page) quantity from start of the list
---

For more information about resource mappings, see the `resource-mappings` subcommand.
7 changes: 7 additions & 0 deletions docs/man/policy/subject-condition-sets/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ command:
name: list
aliases:
- l
flags:
- name: limit
shorthand: l
description: Limit retrieved count
- name: offset
shorthand: o
description: Offset (page) quantity from start of the list
---

For more information about subject condition sets, see the `subject-condition-sets` subcommand.
7 changes: 7 additions & 0 deletions docs/man/policy/subject-mappings/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ command:
name: list
aliases:
- l
flags:
- name: limit
shorthand: l
description: Limit retrieved count
- name: offset
shorthand: o
description: Offset (page) quantity from start of the list
---

For more information about subject mappings, see the `subject-mappings` subcommand.
Loading

0 comments on commit 673a064

Please sign in to comment.