Skip to content

Commit e28bc59

Browse files
committed
refactored docstrings
1 parent a6c8ef5 commit e28bc59

File tree

1 file changed

+112
-84
lines changed

1 file changed

+112
-84
lines changed

datacrunch/instances/instances.py

Lines changed: 112 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,31 @@
1313
@dataclass_json
1414
@dataclass
1515
class Instance:
16-
"""An instance model class"""
16+
"""Represents a cloud instance with its configuration and state.
17+
18+
Attributes:
19+
id (str): Unique identifier for the instance.
20+
instance_type (str): Type of the instance (e.g., '8V100.48V').
21+
price_per_hour (float): Cost per hour of running the instance.
22+
hostname (str): Network hostname of the instance.
23+
description (str): Human-readable description of the instance.
24+
ip (str): IP address assigned to the instance.
25+
status (str): Current operational status of the instance.
26+
created_at (str): Timestamp of instance creation.
27+
ssh_key_ids (List[str]): List of SSH key IDs associated with the instance.
28+
cpu (dict): CPU configuration details.
29+
gpu (dict): GPU configuration details.
30+
memory (dict): Memory configuration details.
31+
storage (dict): Storage configuration details.
32+
os_volume_id (str): ID of the operating system volume.
33+
gpu_memory (dict): GPU memory configuration details.
34+
location (str): Datacenter location code (default: Locations.FIN_01).
35+
image (Optional[str]): Image ID or type used for the instance.
36+
startup_script_id (Optional[str]): ID of the startup script to run.
37+
is_spot (bool): Whether the instance is a spot instance.
38+
contract (Optional[Contract]): Contract type for the instance. (e.g. 'LONG_TERM', 'PAY_AS_YOU_GO', 'SPOT')
39+
pricing (Optional[Pricing]): Pricing model for the instance. (e.g. 'DYNAMIC_PRICE', 'FIXED_PRICE')
40+
"""
1741

1842
id: str
1943
instance_type: str
@@ -38,39 +62,54 @@ class Instance:
3862
pricing: Optional[Pricing] = None
3963

4064
def __str__(self) -> str:
41-
"""Returns a string of the json representation of the instance
65+
"""Returns a JSON string representation of the instance.
4266
43-
:return: json representation of the instance
44-
:rtype: str
67+
Returns:
68+
str: JSON string containing all instance properties.
4569
"""
4670
return stringify_class_object_properties(self)
4771

4872

4973
class InstancesService:
50-
"""A service for interacting with the instances endpoint"""
74+
"""Service for managing cloud instances through the API.
75+
76+
This service provides methods to create, retrieve, and manage cloud instances
77+
through the DataCrunch API.
78+
"""
5179

5280
def __init__(self, http_client) -> None:
81+
"""Initializes the InstancesService with an HTTP client.
82+
83+
Args:
84+
http_client: HTTP client for making API requests.
85+
"""
5386
self._http_client = http_client
5487

55-
def get(self, status: str = None) -> List[Instance]:
56-
"""Get all of the client's non-deleted instances, or instances with specific status.
88+
def get(self, status: Optional[str] = None) -> List[Instance]:
89+
"""Retrieves all non-deleted instances or instances with specific status.
5790
58-
:param status: optional, status of the instances, defaults to None
59-
:type status: str, optional
60-
:return: list of instance details objects
61-
:rtype: List[Instance]
91+
Args:
92+
status: Optional status filter for instances. If None, returns all
93+
non-deleted instances.
94+
95+
Returns:
96+
List[Instance]: List of instance objects matching the criteria.
6297
"""
6398
instances_dict = self._http_client.get(
6499
INSTANCES_ENDPOINT, params={'status': status}).json()
65100
return [Instance.from_dict(instance_dict, infer_missing=True) for instance_dict in instances_dict]
66101

67102
def get_by_id(self, id: str) -> Instance:
68-
"""Get an instance with specified id.
103+
"""Retrieves a specific instance by its ID.
104+
105+
Args:
106+
id: Unique identifier of the instance to retrieve.
107+
108+
Returns:
109+
Instance: Instance object with the specified ID.
69110
70-
:param id: instance id
71-
:type id: str
72-
:return: instance details object
73-
:rtype: Instance
111+
Raises:
112+
HTTPError: If the instance is not found or other API error occurs.
74113
"""
75114
instance_dict = self._http_client.get(
76115
INSTANCES_ENDPOINT + f'/{id}').json()
@@ -83,46 +122,37 @@ def create(self,
83122
description: str,
84123
ssh_key_ids: list = [],
85124
location: str = Locations.FIN_01,
86-
startup_script_id: str = None,
87-
volumes: List[Dict] = None,
88-
existing_volumes: List[str] = None,
89-
os_volume: Dict = None,
125+
startup_script_id: Optional[str] = None,
126+
volumes: Optional[List[Dict]] = None,
127+
existing_volumes: Optional[List[str]] = None,
128+
os_volume: Optional[Dict] = None,
90129
is_spot: bool = False,
91-
contract: Contract = None,
92-
pricing: Pricing = None,
93-
coupon: str = None) -> Instance:
94-
"""Creates (deploys) a new instance
95-
96-
:param instance_type: instance type. e.g. '8V100.48M'
97-
:type instance_type: str
98-
:param image: instance image type. e.g. 'ubuntu-20.04-cuda-11.0', or existing OS volume id
99-
:type image: str
100-
:param ssh_key_ids: list of ssh key ids
101-
:type ssh_key_ids: list
102-
:param hostname: instance hostname
103-
:type hostname: str
104-
:param description: instance description
105-
:type description: str
106-
:param location: datacenter location, defaults to "FIN-01"
107-
:type location: str, optional
108-
:param startup_script_id: startup script id, defaults to None
109-
:type startup_script_id: str, optional
110-
:param volumes: List of volume data dictionaries to create alongside the instance
111-
:type volumes: List[Dict], optional
112-
:param existing_volumes: List of existing volume ids to attach to the instance
113-
:type existing_volumes: List[str], optional
114-
:param os_volume: OS volume details, defaults to None
115-
:type os_volume: Dict, optional
116-
:param is_spot: Is spot instance
117-
:type is_spot: bool, optional
118-
:param pricing: Pricing type
119-
:type pricing: str, optional
120-
:param contract: Contract type
121-
:type contract: str, optional
122-
:param coupon: coupon code
123-
:type coupon: str, optional
124-
:return: the new instance object
125-
:rtype: Instance
130+
contract: Optional[Contract] = None,
131+
pricing: Optional[Pricing] = None,
132+
coupon: Optional[str] = None) -> Instance:
133+
"""Creates and deploys a new cloud instance.
134+
135+
Args:
136+
instance_type: Type of instance to create (e.g., '8V100.48V').
137+
image: Image type or existing OS volume ID for the instance.
138+
hostname: Network hostname for the instance.
139+
description: Human-readable description of the instance.
140+
ssh_key_ids: List of SSH key IDs to associate with the instance.
141+
location: Datacenter location code (default: Locations.FIN_01).
142+
startup_script_id: Optional ID of startup script to run.
143+
volumes: Optional list of volume configurations to create.
144+
existing_volumes: Optional list of existing volume IDs to attach.
145+
os_volume: Optional OS volume configuration details.
146+
is_spot: Whether to create a spot instance.
147+
contract: Optional contract type for the instance.
148+
pricing: Optional pricing model for the instance.
149+
coupon: Optional coupon code for discounts.
150+
151+
Returns:
152+
Instance: The newly created instance object.
153+
154+
Raises:
155+
HTTPError: If instance creation fails or other API error occurs.
126156
"""
127157
payload = {
128158
"instance_type": instance_type,
@@ -146,14 +176,15 @@ def create(self,
146176
return self.get_by_id(id)
147177

148178
def action(self, id_list: Union[List[str], str], action: str, volume_ids: Optional[List[str]] = None) -> None:
149-
"""Performs an action on a list of instances / single instance
150-
151-
:param id_list: list of instance ids, or an instance id
152-
:type id_list: Union[List[str], str]
153-
:param action: the action to perform
154-
:type action: str
155-
:param volume_ids: the volume ids to delete
156-
:type volume_ids: Optional[List[str]]
179+
"""Performs an action on one or more instances.
180+
181+
Args:
182+
id_list: Single instance ID or list of instance IDs to act upon.
183+
action: Action to perform on the instances.
184+
volume_ids: Optional list of volume IDs to delete.
185+
186+
Raises:
187+
HTTPError: If the action fails or other API error occurs.
157188
"""
158189
if type(id_list) is str:
159190
id_list = [id_list]
@@ -167,34 +198,31 @@ def action(self, id_list: Union[List[str], str], action: str, volume_ids: Option
167198
self._http_client.put(INSTANCES_ENDPOINT, json=payload)
168199
return
169200

170-
# TODO: use enum/const for location_code
171-
def is_available(self, instance_type: str, is_spot: bool = False, location_code: str = None) -> bool:
172-
"""Returns True if a specific instance type is now available for deployment
173-
174-
:param instance_type: instance type
175-
:type instance_type: str
176-
:param is_spot: Is spot instance
177-
:type is_spot: bool, optional
178-
:param location_code: datacenter location, defaults to "FIN-01"
179-
:type location_code: str, optional
180-
:return: True if available to deploy, False otherwise
181-
:rtype: bool
201+
def is_available(self, instance_type: str, is_spot: bool = False, location_code: Optional[str] = None) -> bool:
202+
"""Checks if a specific instance type is available for deployment.
203+
204+
Args:
205+
instance_type: Type of instance to check availability for.
206+
is_spot: Whether to check spot instance availability.
207+
location_code: Optional datacenter location code.
208+
209+
Returns:
210+
bool: True if the instance type is available, False otherwise.
182211
"""
183212
is_spot = str(is_spot).lower()
184213
query_params = {'isSpot': is_spot, 'location_code': location_code}
185214
url = f'/instance-availability/{instance_type}'
186215
return self._http_client.get(url, query_params).json()
187216

188-
# TODO: use enum/const for location_code
189-
def get_availabilities(self, is_spot: bool = None, location_code: str = None) -> bool:
190-
"""Returns a list of available instance types
217+
def get_availabilities(self, is_spot: Optional[bool] = None, location_code: Optional[str] = None) -> List[Dict]:
218+
"""Retrieves a list of available instance types across locations.
219+
220+
Args:
221+
is_spot: Optional flag to filter spot instance availability.
222+
location_code: Optional datacenter location code to filter by.
191223
192-
:param is_spot: Is spot instance
193-
:type is_spot: bool, optional
194-
:param location_code: datacenter location, defaults to "FIN-01"
195-
:type location_code: str, optional
196-
:return: list of available instance types in every location
197-
:rtype: list
224+
Returns:
225+
List[Dict]: List of available instance types and their details.
198226
"""
199227
is_spot = str(is_spot).lower() if is_spot is not None else None
200228
query_params = {'isSpot': is_spot, 'locationCode': location_code}

0 commit comments

Comments
 (0)