Skip to content

pagination: Add KeyedPage interface - #3947

Merged
stephenfin merged 2 commits into
gophercloud:mainfrom
shiftstack:keyed-pages
Aug 20, 2026
Merged

stephenfin merged 2 commits into
gophercloud:mainfrom
shiftstack:keyed-pages

Conversation

@stephenfin

Copy link
Copy Markdown
Contributor

Currently, pagination of list resources works by scanning resource bodies, looking for the first non-"links" resource key. This mostly works but is crude. It also doesn't work for resources that have multiple non-"links" top-level keys, such as placement's list resource provider inventories API, which has both an inventories envelope key and a separate resource_provider_generation key.

Improve this by adding a new KeyedPage interface, with a ResourceKey() string value. When a Page struct implements this interface, the static key will be used rather than us scanning the body for the key. When this interface is not implemented, we continue to fallback to the old heuristic. The latter will allow us to implement the interface incrementally, and we only do it here for NetworkPage in openstack/networking/v2/networks/results.go.

@github-actions github-actions Bot added edit:networking This PR updates networking code edit:gophercloud This PR updates common Gophercloud code semver:minor Backwards-compatible change backport-v2 This PR will be backported to v2 labels Aug 14, 2026
@coveralls

coveralls commented Aug 14, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 63.584% (+0.002%) from 63.582% — shiftstack:keyed-pages into gophercloud:main

@winiciusallan

Copy link
Copy Markdown
Contributor

@stephenfin once the conflicts are solved, I believe this will be good to go. 👌

@stephenfin

Copy link
Copy Markdown
Contributor Author

@stephenfin once the conflicts are solved, I believe this will be good to go. 👌

Done. I also split the change into separate commits to make it clearer what I'm doing here.

Commit 0e6c7c7 (gophercloud#3945) ensured how we retrieve the key from a response
body, ensuring we don't get skip this if the page is empty. However, we
still re-evaluate the key for each page rather than using the key we
determined once. This is wasteful and unnecessary complexity. Fix it
now, ahead of the introduction of KeyedPage.

Signed-off-by: Stephen Finucane <[email protected]>
Currently, pagination of list resources works by scanning resource
bodies, looking for the first non-"links" resource key. This mostly
works but is crude. It also doesn't work for resources that have
multiple non-"links" top-level keys, such as placement's list resource
provider inventories API, which has both a 'inventories' envelope key
and a separate 'resource_provider_generation' key [1].

Improve this by adding a new KeyedPage interface, with a ResourceKey()
string value. When a Page struct implements this interface, the static
key will be used rather than us scanning the body for the key. When this
interface is *not* implemented, we continue to fallback to the old
heuristic. The latter will allow us to implement the interface
incrementally, and we only do it here for NetworkPage in
openstack/networking/v2/networks/results.go.

[1] https://docs.openstack.org/api-ref/placement/#list-resource-provider-inventories

Signed-off-by: Stephen Finucane <[email protected]>
@stephenfin
stephenfin added this pull request to the merge queue Aug 20, 2026
Merged via the queue into gophercloud:main with commit ece9036 Aug 20, 2026
70 checks passed
@stephenfin
stephenfin deleted the keyed-pages branch August 20, 2026 12:23
Comment thread pagination/pager.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-v2 This PR will be backported to v2 edit:gophercloud This PR updates common Gophercloud code edit:networking This PR updates networking code semver:minor Backwards-compatible change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants