<reponame>Alex4386/XenXenXenSe<filename>API/v1/SR/list.py<gh_stars>1-10
from http.client import RemoteDisconnected
from xmlrpc.client import Fault

from fastapi import APIRouter, HTTPException
from XenGarden.session import create_session
from XenGarden.SR import SR

from API.v1.SR.serialize import serialize
from app.settings import Settings

router = APIRouter()


@router.get("/{cluster_id}/sr/list")
async def sr_list(cluster_id: str):
    """ Get All from Storage Repos """
    try:
        try:
            session = create_session(
                _id=cluster_id, get_xen_clusters=Settings.get_xen_clusters()
            )
        except KeyError as key_error:
            raise HTTPException(
                status_code=400, detail=f"{key_error} is not a valid path"
            )

        srs = SR.get_all(session=session)

        __sant_sr = []
        sant_sr = __sant_sr.append
        for sr in srs:
            sant_sr(serialize(sr))

        ret = dict(success=True, data=__sant_sr)

        session.xenapi.session.logout()
        return ret
    except Fault as xml_rpc_error:
        raise HTTPException(
            status_code=int(xml_rpc_error.faultCode),
            detail=xml_rpc_error.faultString,
        )
    except RemoteDisconnected as rd_error:
        raise HTTPException(status_code=500, detail=rd_error.strerror)

from http.client import RemoteDisconnected
from xmlrpc.client import Fault

from fastapi import APIRouter, HTTPException
from XenGarden.session import create_session
from XenGarden.SR import SR

from API.v1.SR.serialize import serialize
from app.settings import Settings

router = APIRouter()


@router.get("/{cluster_id}/sr/list")
async def sr_list(cluster_id: str):
    """ Get All from Storage Repos """
    try:
        try:
            session = create_session(
                _id=cluster_id, get_xen_clusters=Settings.get_xen_clusters()
            )
        except KeyError as key_error:
            raise HTTPException(
                status_code=400, detail=f"{key_error} is not a valid path"
            )

        srs = SR.get_all(session=session)

        __sant_sr = []
        sant_sr = __sant_sr.append
        for sr in srs:
            sant_sr(serialize(sr))

        ret = dict(success=True, data=__sant_sr)

        session.xenapi.session.logout()
        return ret
    except Fault as xml_rpc_error:
        raise HTTPException(
            status_code=int(xml_rpc_error.faultCode),
            detail=xml_rpc_error.faultString,
        )
    except RemoteDisconnected as rd_error:
        raise HTTPException(status_code=500, detail=rd_error.strerror)
Get All from Storage Repos