1- from verda .helpers import stringify_class_object_properties
1+ from dataclasses import dataclass
2+
3+ from dataclasses_json import Undefined , dataclass_json
24
35IMAGES_ENDPOINT = '/images'
46
57
8+ @dataclass_json (undefined = Undefined .EXCLUDE )
9+ @dataclass
610class 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.
11+ """Represents an OS image available for instances.
2912
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
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+ """
4319
44- @property
45- def image_type (self ) -> str :
46- """Get the image type.
47-
48- :return: image type
49- :rtype: str
50- """
51- return self ._image_type
52-
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
20+ id : str
21+ name : str
22+ image_type : str
23+ details : list [str ]
6124
6225 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 )
26+ return self .to_json (indent = 2 )
6927
7028
7129class ImagesService :
@@ -74,15 +32,16 @@ class ImagesService:
7432 def __init__ (self , http_client ) -> None :
7533 self ._http_client = http_client
7634
77- def get (self ) -> list [Image ]:
35+ def get (self , instance_type : str | None = None ) -> list [Image ]:
7836 """Get the available instance images.
7937
80- :return: list of images objects
81- :rtype: list[Image]
38+ Args:
39+ instance_type: Filter OS images by instance type, e.g. '1A100.22V'.
40+ Default is all instance images.
41+
42+ Returns:
43+ List of Image objects.
8244 """
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
45+ params = {'instance_type' : instance_type } if instance_type else None
46+ images = self ._http_client .get (IMAGES_ENDPOINT , params = params ).json ()
47+ return [Image .from_dict (image ) for image in images ]
0 commit comments