Skip to content

pylxd/models/instance: Support selective state recursion in Instance.all()#707

Open
gajeshbhat wants to merge 6 commits intocanonical:mainfrom
gajeshbhat:feature/instances-state-selective-recursion
Open

pylxd/models/instance: Support selective state recursion in Instance.all()#707
gajeshbhat wants to merge 6 commits intocanonical:mainfrom
gajeshbhat:feature/instances-state-selective-recursion

Conversation

@gajeshbhat
Copy link

Summary

Add a fields parameter to Instance.all() to support the LXD selective state recursion feature introduced via the instances_state_selective_recursion API extension.

When recursion=2 and a fields list is provided, the request uses the recursion=2;fields=<comma-joined> query syntax, which instructs LXD to return only the requested state sub-fields. This avoids fetching expensive data (e.g. full network enumeration) when only a subset is needed.

If the server does not advertise the instances_state_selective_recursion extension, the call silently falls back to a standard recursion=2 request.

Usage

# Only fetch disk state
instances = client.instances.all(recursion=2, fields=["state.disk"])

# Fetch both disk and network state
instances = client.instances.all(recursion=2, fields=["state.disk", "state.network"])

# Suppress all expensive state fields entirely
instances = client.instances.all(recursion=2, fields=[])

References

Fixes #706

@simondeziel
Copy link
Member

@gajeshbhat at first glance it looks really good. Let's see if Copilot picks something up.

May I ask you to split your commits a bit more, at least one per file and please commit unrelated changes into their own commits (like dropping the trailing spaces in doc). Thanks!

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds selective state recursion support to Instance.all() so callers can request only specific instance state sub-fields (when supported by the LXD server), reducing data transfer and avoiding expensive state collection.

Changes:

  • Extend Instance.all() with a fields parameter and conditional use of instances_state_selective_recursion.
  • Add unit tests covering selective-recursion URL construction and fallback behavior.
  • Add integration tests and documentation describing recursion/fields usage and behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pylxd/models/instance.py Adds fields argument and selective-recursion query parameter formatting when the server supports the extension.
pylxd/models/tests/test_instance.py Adds unit tests for selective recursion query encoding, multi-field joining, empty fields, and fallback/ignore paths.
integration/test_instances.py Adds integration coverage for instances.get() and instances.all() including selective-recursion scenarios (skipping when unsupported).
doc/source/instances.rst Documents all(recursion=0, fields=None) and provides examples for selective recursion behavior and fallback.
CONTRIBUTORS.rst Adds a contributor entry.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@gajeshbhat
Copy link
Author

@gajeshbhat at first glance it looks really good. Let's see if Copilot picks something up.

May I ask you to split your commits a bit more, at least one per file and please commit unrelated changes into their own commits (like dropping the trailing spaces in doc). Thanks!

Thank you @simondeziel . Definitely will make those changes and get this updated soon. :)

Signed-off-by: Gajesh Bhat <gajeshbht@gmail.com>
Signed-off-by: Gajesh Bhat <gajeshbht@gmail.com>
…all()

Add a `fields` parameter to `Instance.all()` that enables the LXD
selective state recursion feature (instances_state_selective_recursion).

When `recursion=2` and `fields` is provided, the query parameter is
encoded as `recursion=2;fields=<comma-joined-fields>`, which tells LXD
to return only the requested state sub-fields (e.g. state.disk,
state.network). This significantly reduces data transfer for callers
that only need a subset of instance state.

If the server does not advertise the required extension, the call
gracefully falls back to a standard `recursion=2` request.

Explicit input validation rejects bare str/bytes values and non-string
iterable elements, raising TypeError with a clear message.

Signed-off-by: Gajesh Bhat <gajeshbht@gmail.com>
…sion

Add eight unit tests covering:
- selective URL construction for a single field
- selective URL construction for multiple fields
- empty fields list (suppress all state fields)
- graceful fallback when extension is absent
- fields silently ignored when recursion != 2
- TypeError raised for bare str fields argument
- TypeError raised for bytes fields argument
- TypeError raised when iterable contains non-string element

Signed-off-by: Gajesh Bhat <gajeshbht@gmail.com>
…all()

Document the new `fields` parameter added to Instance.all(), including:
- updated manager method list with recursion and fields descriptions
- code examples for plain recursion=2, single-field, multi-field, and
  empty-fields selective recursion
- note on graceful fallback when the extension is absent

Signed-off-by: Gajesh Bhat <gajeshbht@gmail.com>
Add integration tests covering:
- get() by name
- all() at recursion levels 0, 1, and 2
- selective recursion with state.disk (skipped if extension absent)
- selective recursion with state.disk and state.network (skipped if extension absent)
- empty fields list (skipped if extension absent)
- graceful fallback when instances_state_selective_recursion is absent
  (skipped if extension is present)

Signed-off-by: Gajesh Bhat <gajeshbht@gmail.com>
@gajeshbhat gajeshbhat force-pushed the feature/instances-state-selective-recursion branch from b171ce8 to 89442a1 Compare March 5, 2026 01:46
@gajeshbhat
Copy link
Author

Pull request overview

Adds selective state recursion support to Instance.all() so callers can request only specific instance state sub-fields (when supported by the LXD server), reducing data transfer and avoiding expensive state collection.

Changes:

  • Extend Instance.all() with a fields parameter and conditional use of instances_state_selective_recursion.
  • Add unit tests covering selective-recursion URL construction and fallback behavior.
  • Add integration tests and documentation describing recursion/fields usage and behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pylxd/models/instance.py Adds fields argument and selective-recursion query parameter formatting when the server supports the extension.
pylxd/models/tests/test_instance.py Adds unit tests for selective recursion query encoding, multi-field joining, empty fields, and fallback/ignore paths.
integration/test_instances.py Adds integration coverage for instances.get() and instances.all() including selective-recursion scenarios (skipping when unsupported).
doc/source/instances.rst Documents all(recursion=0, fields=None) and provides examples for selective recursion behavior and fallback.
CONTRIBUTORS.rst Adds a contributor entry.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Fixed these copilot suggestions. CC: @simondeziel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add selective state recursion support to Instance.all()

3 participants