11from typing import List , Union , Optional , Dict
2+ from datacrunch .helpers import stringify_class_object_properties
23
34INSTANCES_ENDPOINT = '/instances'
45
@@ -24,7 +25,8 @@ def __init__(self,
2425 os_volume_id : str ,
2526 gpu_memory : dict ,
2627 location : str = "FIN1" ,
27- startup_script_id : str = None
28+ startup_script_id : str = None ,
29+ is_spot : bool = False
2830 ) -> None :
2931 """Initialize the instance object
3032
@@ -64,6 +66,8 @@ def __init__(self,
6466 :type location: str, optional
6567 :param startup_script_id: startup script id, defaults to None
6668 :type startup_script_id: str, optional
69+ :param is_spot: is this a spot instance, defaults to None
70+ :type is_spot: bool, optional
6771 """
6872 self ._id = id
6973 self ._instance_type = instance_type
@@ -83,6 +87,7 @@ def __init__(self,
8387 self ._storage = storage
8488 self ._os_volume_id = os_volume_id
8589 self ._gpu_memory = gpu_memory
90+ self ._is_spot = is_spot
8691
8792 @property
8893 def id (self ) -> str :
@@ -246,6 +251,22 @@ def gpu_memory(self) -> dict:
246251 """
247252 return self ._gpu_memory
248253
254+ @property
255+ def is_spot (self ) -> bool :
256+ """Is this a spot instance
257+
258+ :return: is spot details
259+ :rtype: bool
260+ """
261+ return self ._is_spot
262+
263+ def __str__ (self ) -> str :
264+ """Returns a string of the json representation of the instance
265+
266+ :return: json representation of the instance
267+ :rtype: str
268+ """
269+ return stringify_class_object_properties (self )
249270
250271class InstancesService :
251272 """A service for interacting with the instances endpoint"""
@@ -281,7 +302,8 @@ def get(self, status: str = None) -> List[Instance]:
281302 memory = instance_dict ['memory' ],
282303 storage = instance_dict ['storage' ],
283304 os_volume_id = instance_dict ['os_volume_id' ] if 'os_volume_id' in instance_dict else None ,
284- gpu_memory = instance_dict ['gpu_memory' ] if 'gpu_memory' in instance_dict else None
305+ gpu_memory = instance_dict ['gpu_memory' ] if 'gpu_memory' in instance_dict else None ,
306+ is_spot = instance_dict ['is_spot' ] if 'is_spot' in instance_dict else False
285307 ), instances_dict ))
286308 return instances
287309
@@ -313,8 +335,8 @@ def get_by_id(self, id: str) -> Instance:
313335 memory = instance_dict ['memory' ],
314336 storage = instance_dict ['storage' ],
315337 os_volume_id = instance_dict ['os_volume_id' ] if 'os_volume_id' in instance_dict else None ,
316- gpu_memory = instance_dict ['gpu_memory' ] if 'gpu_memory' in instance_dict else None
317-
338+ gpu_memory = instance_dict ['gpu_memory' ] if 'gpu_memory' in instance_dict else None ,
339+ is_spot = instance_dict [ 'is_spot' ] if 'is_spot' in instance_dict else False
318340 )
319341 return instance
320342
@@ -327,7 +349,8 @@ def create(self,
327349 location : str = "FIN1" ,
328350 startup_script_id : str = None ,
329351 volumes : List [Dict ] = None ,
330- os_volume : Dict = None ) -> Instance :
352+ os_volume : Dict = None ,
353+ is_spot : bool = False ) -> Instance :
331354 """Creates (deploys) a new instance
332355
333356 :param instance_type: instance type. e.g. '8V100.48M'
@@ -348,6 +371,8 @@ def create(self,
348371 :type volumes: List[Dict], optional
349372 :param os_volume: OS volume details, defaults to None
350373 :type os_volume: Dict, optional
374+ :param is_spot: Is spot instance
375+ :type is_spot: bool, optional
351376 :return: the new instance object
352377 :rtype: id
353378 """
@@ -360,7 +385,8 @@ def create(self,
360385 "description" : description ,
361386 "location" : location ,
362387 "os_volume" : os_volume ,
363- "volumes" : volumes
388+ "volumes" : volumes ,
389+ "is_spot" : is_spot
364390 }
365391 id = self ._http_client .post (INSTANCES_ENDPOINT , json = payload ).text
366392 instance = self .get_by_id (id )
0 commit comments