Skip to content
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

image(all): added ability to specify recursion when getting all images #558

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
30 changes: 24 additions & 6 deletions pylxd/models/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,32 @@ def get_by_alias(cls, client, alias):
return cls.get(client, fingerprint)

@classmethod
def all(cls, client):
"""Get all images."""
response = client.api.images.get()
def all(cls, client, recursion=0):
"""Get all images.

This method returns an Image array. If recursion is unset,
only the name of each instance will be set. If recursion is at least 1 this method will pre-fetch additional instance attributes for
all instances in the array.
"""
params = {}
if recursion != 0:
params = {"recursion": recursion}
response = client.api.images.get(params=params)

images = []
for url in response.json()["metadata"]:
fingerprint = url.split("/")[-1]
images.append(cls(client, fingerprint=fingerprint))
for image in response.json()["metadata"]:
if isinstance(image, dict):
# User specified recursion so returning all data for each image at once
image_class = cls(client, fingerprint=image["fingerprint"])
for key, data in image.items():
try:
setattr(image_class, key, data)
except AttributeError:
pass
images.append(image_class)
else:
fingerprint = image.split("/")[-1]
images.append(cls(client, fingerprint=fingerprint))
return images

@classmethod
Expand Down