redsquare
12/14/2022, 12:25 PMAnna Geller
import asyncio
from prefect.cli.cloud import get_current_workspace
from prefect.client.cloud import get_cloud_client, CloudUnauthorizedError
async def get_workspace() -> str:
    try:
        async with get_cloud_client() as client:
            workspaces = await client.read_workspaces()
            current_workspace = get_current_workspace(workspaces)
            workspace_handle = current_workspace.split("/")[-1]
            return workspace_handle
    except CloudUnauthorizedError:
        return "default"  # means: local Orion instance and the default CLI Profile
def get_env() -> str:
    """
    This could be replaced by some other logic to return whether you run sth in dev vs. prod
    :return: string representing the environment, same as assigned to block names
    """
    return asyncio.run(get_workspace())redsquare
12/14/2022, 1:14 PMAnna Geller
from prefect.blocks.core import Block
from prefect.blocks.notifications import SlackWebhook
from prefect.settings import PREFECT_API_URL
from pydantic import Field
from typing import Any
from uuid import UUID
from dataplatform.environment import get_env
class Workspace(Block):
    """
    Manage alerts and workspace metadata
    Args:
        name: environment name e.g. dev, staging, prod
        metadata: key-value pairs representing workspace information
    Example:
        Load a stored JSON value:
        ```python
        from dataplatform.blocks import Workspace
        workspace = Workspace.load("BLOCK_NAME")
        workspace.send_alert("Alert from Prefect! :rocket:")redsquare
12/14/2022, 5:00 PM