1+ import time
12from typing import List , Union , Optional , Dict , Literal
23from dataclasses import dataclass
34from dataclasses_json import dataclass_json
4- from datacrunch .helpers import stringify_class_object_properties
5- from datacrunch .constants import Locations
5+ from datacrunch .constants import Locations , InstanceStatus
66
77INSTANCES_ENDPOINT = '/instances'
88
@@ -21,16 +21,16 @@ class Instance:
2121 price_per_hour: Cost per hour of running the instance.
2222 hostname: Network hostname of the instance.
2323 description: Human-readable description of the instance.
24- ip: IP address assigned to the instance.
2524 status: Current operational status of the instance.
2625 created_at: Timestamp of instance creation.
2726 ssh_key_ids: List of SSH key IDs associated with the instance.
2827 cpu: CPU configuration details.
2928 gpu: GPU configuration details.
3029 memory: Memory configuration details.
3130 storage: Storage configuration details.
32- os_volume_id: ID of the operating system volume.
3331 gpu_memory: GPU memory configuration details.
32+ ip: IP address assigned to the instance.
33+ os_volume_id: ID of the operating system volume.
3434 location: Datacenter location code (default: Locations.FIN_01).
3535 image: Image ID or type used for the instance.
3636 startup_script_id: ID of the startup script to run.
@@ -44,31 +44,25 @@ class Instance:
4444 price_per_hour : float
4545 hostname : str
4646 description : str
47- ip : str
4847 status : str
4948 created_at : str
5049 ssh_key_ids : List [str ]
5150 cpu : dict
5251 gpu : dict
5352 memory : dict
5453 storage : dict
55- os_volume_id : str
5654 gpu_memory : dict
55+ # Can be None if instance is still not provisioned
56+ ip : Optional [str ] = None
57+ # Can be None if instance is still not provisioned
58+ os_volume_id : Optional [str ] = None
5759 location : str = Locations .FIN_01
5860 image : Optional [str ] = None
5961 startup_script_id : Optional [str ] = None
6062 is_spot : bool = False
6163 contract : Optional [Contract ] = None
6264 pricing : Optional [Pricing ] = None
6365
64- def __str__ (self ) -> str :
65- """Returns a JSON string representation of the instance.
66-
67- Returns:
68- JSON string containing all instance properties.
69- """
70- return stringify_class_object_properties (self )
71-
7266
7367class InstancesService :
7468 """Service for managing cloud instances through the API.
@@ -173,7 +167,22 @@ def create(self,
173167 if pricing :
174168 payload ['pricing' ] = pricing
175169 id = self ._http_client .post (INSTANCES_ENDPOINT , json = payload ).text
176- return self .get_by_id (id )
170+
171+ # Wait for instance to enter provisioning state with timeout
172+ MAX_WAIT_TIME = 60 # Maximum wait time in seconds
173+ POLL_INTERVAL = 0.5 # Time between status checks
174+
175+ start_time = time .time ()
176+ while True :
177+ instance = self .get_by_id (id )
178+ if instance .status == InstanceStatus .PROVISIONING :
179+ return instance
180+
181+ if time .time () - start_time > MAX_WAIT_TIME :
182+ raise TimeoutError (
183+ f"Instance { id } did not enter provisioning state within { MAX_WAIT_TIME } seconds" )
184+
185+ time .sleep (POLL_INTERVAL )
177186
178187 def action (self , id_list : Union [List [str ], str ], action : str , volume_ids : Optional [List [str ]] = None ) -> None :
179188 """Performs an action on one or more instances.
0 commit comments