Skip to content

Commit

Permalink
image(all): added ability to specify recursion when getting all images
Browse files Browse the repository at this point in the history
Signed-off-by: Dawson Greeley <[email protected]>
  • Loading branch information
MrDaGree committed Aug 11, 2023
1 parent fb501c0 commit 5ec8884
Showing 1 changed file with 24 additions and 6 deletions.
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}

Check warning on line 99 in pylxd/models/image.py

View check run for this annotation

Codecov / codecov/patch

pylxd/models/image.py#L99

Added line #L99 was not covered by tests
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)

Check warning on line 112 in pylxd/models/image.py

View check run for this annotation

Codecov / codecov/patch

pylxd/models/image.py#L106-L112

Added lines #L106 - L112 were not covered by tests
else:
fingerprint = image.split("/")[-1]
images.append(cls(client, fingerprint=fingerprint))
return images

@classmethod
Expand Down

0 comments on commit 5ec8884

Please sign in to comment.