2323from .cpu import CpuInstanceType
2424from .environment import EnvironmentVars
2525
26+
2627# Environment variables are loaded from the .env file
2728def get_env_vars () -> Dict [str , str ]:
2829 """
@@ -232,7 +233,7 @@ async def is_ready_for_requests(self, give_up_threshold=10) -> bool:
232233
233234 Raises:
234235 ValueError: If the serverless resource is not deployed.
235- RuntimeError: If the health status is THROTTLED, UNHEALTHY, or UNKNOWN
236+ RuntimeError: If the health status is THROTTLED, UNHEALTHY, or UNKNOWN
236237 after exceeding the give_up_threshold.
237238 """
238239 if not self .is_deployed ():
@@ -379,7 +380,9 @@ class ServerlessEndpoint(ServerlessResource):
379380 @model_validator (mode = "after" )
380381 def set_serverless_template (self ):
381382 if not any ([self .imageName , self .template , self .templateId ]):
382- raise ValueError ("Either imageName, template, or templateId must be provided" )
383+ raise ValueError (
384+ "Either imageName, template, or templateId must be provided"
385+ )
383386
384387 if not self .templateId and not self .template :
385388 self .template = PodTemplate (
@@ -404,6 +407,7 @@ class CpuServerlessEndpoint(ServerlessEndpoint):
404407 Represents a CPU-only serverless endpoint distinct from a live serverless.
405408 Inherits from ServerlessEndpoint.
406409 """
410+
407411 instanceIds : Optional [List [CpuInstanceType ]] = [CpuInstanceType .CPU3G_2_8 ]
408412
409413
@@ -471,33 +475,32 @@ class ServerlessHealth(BaseModel):
471475 def is_ready (self ) -> bool :
472476 return self .workers .status == Status .READY
473477
474- class NetworkVolumeConfig (BaseModel ):
475- """
476- NetworkvolumeConfig Represents a network volume configuration for serverless resources.
477- """
478- size : int # Size in GB
479- datacenter_id : str
480- name : str
481-
482478
483- class NetworkVolumeResource (DeployableResource ):
479+ class NetworkVolume (DeployableResource ):
484480 """
485481 NetworkVolume resource for creating and managing Runpod netowrk volumes.
486-
482+
487483 This class handles the creation, deployment, and management of network volumes
488484 that can be attached to serverless resources.
489485
490486 """
491- config : NetworkVolumeConfig
492- volume_id : Optional [str ] = Field (default = None )
493-
494-
487+
488+ dataCenterId : Optional [str ] = None
489+ id : Optional [str ] = Field (default = None )
490+ name : Optional [str ] = None
491+ size : Optional [int ] = None # Size in GB
492+
493+ @property
494+ def is_created (self ) -> bool :
495+ " Returns True if the network volume already exists. " ""
496+ return self .id is not None
497+
495498 @property
496499 def url (self ) -> str :
497500 """
498501 Returns the URL for the network volume resource.
499502 """
500- if not self .volume_id :
503+ if not self .id :
501504 raise ValueError ("Network volume ID is not set" )
502505 return f"{ CONSOLE_BASE_URL } /user/storage"
503506
@@ -506,23 +509,21 @@ async def create_network_volume(self) -> str:
506509 Creates a network volume using the provided configuration.
507510 Returns the volume ID.
508511 """
509- print (self .config )
510512 async with RunpodRestClient () as client :
511513 # Create the network volume
512- volume = await client .create_network_volume (
513- datacenter_id = self .config .datacenter_id ,
514- name = self .config .name ,
515- size = self .config .size
514+ result = await client .create_network_volume (
515+ datacenter_id = self .dataCenterId , name = self .name , size = self .size
516516 )
517- log .info (f"Created network volume: { volume ['id' ]} " )
518- return volume ["id" ]
519-
517+ log .info (f"Created network volume: { result ['id' ]} " )
518+ if volume := self .__class__ (** result ):
519+ return volume .id
520+
520521 def is_deployed (self ) -> bool :
521522 """
522523 Checks if the network volume resource is deployed and available.
523524 """
524- return self .volume_id is not None
525-
525+ return self .id is not None
526+
526527 async def deploy (self ) -> "DeployableResource" :
527528 """
528529 Deploys the network volume resource using the provided configuration.
@@ -532,17 +533,16 @@ async def deploy(self) -> "DeployableResource":
532533 # If the resource is already deployed, return it
533534 if self .is_deployed ():
534535 log .debug (f"{ self } exists" )
535- return self
536+ return self . id
536537
537538 # Create the network volume
538- self .volume_id = await self .create_network_volume ()
539+ self .id = await self .create_network_volume ()
539540
540541 if self .is_deployed ():
541- return self
542+ return self . id
542543
543544 raise ValueError ("Deployment failed, no volume was created." )
544545
545546 except Exception as e :
546547 log .error (f"{ self } failed to deploy: { e } " )
547548 raise
548-
0 commit comments