Hi, How can I programmatically retrieve deployment...
# prefect-community
p
Hi, How can I programmatically retrieve deployment id from deployment name. Eg, retrieve id 'b86c7264-beae-4485-a68c-290286e92f00' by providing 'LiveLots-ETL/dev', is there Python API to achieve this?
o
Perhaps this can get you started?
Copy code
async def default_test():
    async with get_client() as client:
        deployments = await client.read_deployments()
        for deployment in deployments:
            print(deployment.name)
            print(deployment.id)
🙌 2
👍 1
I don't think there is any other way than comparing the name of each deployment to the name you're looking for, that's what I ended up doing anyhow.
m
You should also be able to filter by deployment name in the read_deployments api call
Copy code
from prefect.orion.schemas.filters import DeploymentFilterName

filter_name = DeploymentFilterName(["deploymentName"])

async def default_test():
    async with get_client() as client:
        deployments = await client.read_deployments(deployment_filter=filter_name)
🙌 3
p
Thanks folks, I am able to use your code snippet to retrieve deployment id from combination of flow name and deployment name
Copy code
async def get_deployment_id(flow_name, deployment_name):
    async with get_client() as client:
        flows = await client.read_flows()
        for flow in flows:
            if flow.name == flow_name:
                flow_id = flow.id


        deployments = await client.read_deployments()
        for deployment in deployments:
            if deployment.flow_id == flow_id:
                if deployment.name == deployment_name:
                    return deployment.id
1
275 Views