# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/03_instance.ipynb.

# %% auto #0
__all__ = ['GPUInstance']

# %% ../nbs/03_instance.ipynb #43bfb04b-d169-4901-8e76-07c4cb4a386d
from loguru import logger
import ovh
from fastcore.basics import patch

# %% ../nbs/03_instance.ipynb #49ee9208-6677-402f-a029-02a224c27cab
class GPUInstance():
    def __init__(self, client:ovh.client, project_base_url:str, region:str, name:str, id:str, status:str, gpu_type:str, flavor_id:str, image_id:str, consumption_mode:str, created:str):
        """
        OVH GPU Instance class, enabling to interact with the instance (start/stop/delete)
    
        Parameters
        ----------
        client: ovh.client, required
            OVH client
        project_base_url: str, required
            OVH project base url
        region : str, required
            OVH GPU Instance region
        name : str, required
            OVH GPU Instance name
        id : str, required
            OVH GPU Instance id
        status : str, required
            OVH GPU Instance status
        gpu_type : str, required
            OVH GPU Instance gpu_type
        flavor_id : str, required
            OVH GPU Instance flavor_id
        image_id : str, required
            OVH GPU Instance image_id
        consumption_type : str, required
            OVH GPU Instance consumption mode
        created : str, required
            OVH GPU Instance creation date
        """
        self.client = client
        self.project_base_url = project_base_url
        self.region = region
        self.name = name
        self.id = id
        self.status = status
        self.gpu_type = gpu_type
        self.flavor_id = flavor_id
        self.image_id = image_id
        self.consumption_mode = consumption_mode
        self.planCode = f'{gpu_type}.{consumption_mode}'
        self.created = created

    def display_details(self):
        """Display all instance attributes"""
        for key, value in self.__dict__.items():
            print(f"{key}: {value}")

# %% ../nbs/03_instance.ipynb #3966a669-90cf-464d-bb22-f570897280c3
@patch
def delete(self:GPUInstance):
    """
    Enables an OVH GPU instance to delete it's self
    
    Parameters
    ----------
    instance_id: str, required
        OVH instance ID
    """
    logger.info(f"\n Deleting instance")
    self.client.delete(f'{self.project_base_url}/instance/{self.id}')
    self.status = 'DELETED'
    logger.info(f"\n GPU Instance: {self.id} is being deleted")
    return None

# %% ../nbs/03_instance.ipynb #c0cdc947-ac84-47f7-8c15-e03af1aaa5ce
@patch
def start(self:GPUInstance):
    """
    Enables an OVH GPU instance to start it's self
    
    Parameters
    ----------
    instance_id: str, required
        OVH instance ID
    """
    logger.info(f"\n Starting instance")
    self.client.post(f'{self.project_base_url}/instance/{self.id}/start')
    self.status='ACTIVE'
    logger.info(f"\n GPU Instance: {self.id} is being started")
    return None

# %% ../nbs/03_instance.ipynb #38e5357b-510d-4f66-a054-7e888dd84d14
@patch
def stop(self:GPUInstance):
    """
    Enables an OVH GPU instance to start it's self
    
    Parameters
    ----------
    instance_id: str, required
        OVH instance ID
    """
    logger.info(f"\n Stopping instance")
    self.client.post(f'{self.project_base_url}/instance/{self.id}/stop')
    self.status='SHUTOFF'
    logger.info(f"\n GPU Instance: {self.id} is being stopped")
    return None
