Alex Papanicolaou
10/17/2024, 4:01 AMMarvin
10/17/2024, 4:01 AMAlex Papanicolaou
10/17/2024, 4:01 AMAlex Papanicolaou
10/17/2024, 3:31 PMMarvin
10/17/2024, 3:31 PMAlex Papanicolaou
10/17/2024, 3:56 PMAlex Papanicolaou
10/17/2024, 6:26 PMMarvin
10/17/2024, 6:27 PMpython
import requests
from yaml import safe_load
def create_or_update_automation():
with open("automation_payload.yaml", "r") as fh:
payload = safe_load(fh)
# Example code to find existing automations and update or create one
automations = <http://requests.post|requests.post>("/automations/filter")
existing_automation = next((a["id"] for a in automations if a["name"] == payload["name"]), None)
if existing_automation:
print(f"Automation '{payload['name']}' already exists and will be updated")
requests.put(f"/automations/{existing_automation}", payload=payload)
else:
print(f"Creating automation '{payload['name']}'")
<http://requests.post|requests.post>("/automations/", payload=payload)
if __name__ == "__main__":
create_or_update_automation()
This example demonstrates the basic use of the API via HTTP requests. You can extend this by integrating webhooks to trigger automations based on events. Full details are available here.
Prefect 3.x:
In Prefect 3.x, you have access to a more refined API and can use the Python SDK's Automation
class directly. Here is an example:
python
from prefect.automations import Automation
from prefect.events.schemas.automations import EventTrigger
from prefect.server.events.actions import CancelFlowRun
# Creating an automation
automation = Automation(
name="Example Automation",
trigger=EventTrigger(
expect={"event.name"},
match={"attribute": "value"},
posture="Reactive",
threshold=3,
),
actions=[CancelFlowRun()]
).create()
print(automation)
In this snippet, the Automation
class is used to define an automation with specified triggers and actions, such as canceling a flow run when certain conditions are met. The full example and details can be found here.
Feel free to modify these examples to fit your specific use case! If you need further assistance, don't hesitate to ask.