Skip to content

Commit 69fcbab

Browse files
committed
added option to query images by instance_type, refactored to dataclasses
1 parent 3dc3f19 commit 69fcbab

1 file changed

Lines changed: 26 additions & 70 deletions

File tree

verda/images/_images.py

Lines changed: 26 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,26 @@
1-
from verda.helpers import stringify_class_object_properties
1+
from dataclasses import dataclass
2+
3+
from dataclasses_json import Undefined, dataclass_json
24

35
IMAGES_ENDPOINT = '/images'
46

57

8+
@dataclass_json(undefined=Undefined.EXCLUDE)
9+
@dataclass
610
class Image:
7-
"""An image model class."""
8-
9-
def __init__(self, id: str, name: str, image_type: str, details: list[str]) -> None:
10-
"""Initialize an image object.
11-
12-
:param id: image id
13-
:type id: str
14-
:param name: image name
15-
:type name: str
16-
:param image_type: image type, e.g. 'ubuntu-20.04-cuda-11.0'
17-
:type image_type: str
18-
:param details: image details
19-
:type details: list[str]
20-
"""
21-
self._id = id
22-
self._name = name
23-
self._image_type = image_type
24-
self._details = details
25-
26-
@property
27-
def id(self) -> str:
28-
"""Get the image id.
29-
30-
:return: image id
31-
:rtype: str
32-
"""
33-
return self._id
34-
35-
@property
36-
def name(self) -> str:
37-
"""Get the image name.
38-
39-
:return: image name
40-
:rtype: str
41-
"""
42-
return self._name
43-
44-
@property
45-
def image_type(self) -> str:
46-
"""Get the image type.
11+
"""Represents an OS image available for instances.
4712
48-
:return: image type
49-
:rtype: str
50-
"""
51-
return self._image_type
13+
Attributes:
14+
id: Unique identifier for the image.
15+
name: Human-readable name of the image.
16+
image_type: Image type identifier, e.g. 'ubuntu-20.04-cuda-11.0'.
17+
details: List of additional image details.
18+
"""
5219

53-
@property
54-
def details(self) -> list[str]:
55-
"""Get the image details.
56-
57-
:return: image details
58-
:rtype: list[str]
59-
"""
60-
return self._details
61-
62-
def __str__(self) -> str:
63-
"""Returns a string of the json representation of the image.
64-
65-
:return: json representation of the image
66-
:rtype: str
67-
"""
68-
return stringify_class_object_properties(self)
20+
id: str
21+
name: str
22+
image_type: str
23+
details: list[str]
6924

7025

7126
class ImagesService:
@@ -74,15 +29,16 @@ class ImagesService:
7429
def __init__(self, http_client) -> None:
7530
self._http_client = http_client
7631

77-
def get(self) -> list[Image]:
32+
def get(self, instance_type: str | None = None) -> list[Image]:
7833
"""Get the available instance images.
7934
80-
:return: list of images objects
81-
:rtype: list[Image]
35+
Args:
36+
instance_type: Filter OS images by instance type, e.g. '1A100.22V'.
37+
Default is all instance images.
38+
39+
Returns:
40+
List of Image objects.
8241
"""
83-
images = self._http_client.get(IMAGES_ENDPOINT).json()
84-
image_objects = [
85-
Image(image['id'], image['name'], image['image_type'], image['details'])
86-
for image in images
87-
]
88-
return image_objects
42+
params = {'instance_type': instance_type} if instance_type else None
43+
images = self._http_client.get(IMAGES_ENDPOINT, params=params).json()
44+
return [Image.from_dict(image) for image in images]

0 commit comments

Comments
 (0)