|
1 | 1 | import os |
| 2 | +import time |
2 | 3 | import json |
3 | 4 | import httpx |
4 | 5 | from string import Template |
@@ -83,25 +84,44 @@ async def sync_role(context: dict, token: dict, userinfo: dict): |
83 | 84 |
|
84 | 85 |
|
85 | 86 | async def sync_default(context: dict, token: dict, userinfo: dict): |
| 87 | + alertmanager_default_config = json.dumps({ |
| 88 | + "alertmanager_config": { |
| 89 | + "route": { |
| 90 | + "receiver": "grafana-default-email", |
| 91 | + "group_by": ["grafana_folder", "alertname"] |
| 92 | + }, |
| 93 | + "receivers": [{ |
| 94 | + "name": "grafana-default-email", |
| 95 | + "grafana_managed_receiver_configs": [{ |
| 96 | + "uid": "", |
| 97 | + "name": "email receiver", |
| 98 | + "type": "email", |
| 99 | + "settings": { |
| 100 | + "addresses": f"{userinfo["email"]}" |
| 101 | + } |
| 102 | + }] |
| 103 | + }] |
| 104 | + } |
| 105 | + }) |
86 | 106 | async with httpx.AsyncClient() as client: |
87 | 107 | await client.post( |
88 | 108 | api_url("/api/folders"), |
89 | 109 | headers=api_headers(context, userinfo), |
90 | 110 | json={"uid": "drycc", "title": "drycc"}, |
91 | 111 | ) |
92 | | - await client.post( |
93 | | - api_url("/api/v1/provisioning/contact-points"), |
94 | | - headers=api_headers(context, userinfo), |
95 | | - json={ |
96 | | - "uid": "grafana-default-email", |
97 | | - "name": "grafana-default-email", |
98 | | - "type": "email", |
99 | | - "settings": { |
100 | | - "addresses": f"{userinfo["email"]}" |
101 | | - }, |
102 | | - "disableResolveMessage": False |
103 | | - }, |
104 | | - ) |
| 112 | + sql_tpl = f""" |
| 113 | + INSERT INTO alert_configuration ( |
| 114 | + alertmanager_configuration, configuration_version, created_at, "default", org_id |
| 115 | + ) |
| 116 | + VALUES ( |
| 117 | + '{alertmanager_default_config}', 'v1', {int(time.time())}, TRUE, {context["org_id"]} |
| 118 | + ) |
| 119 | + ON CONFLICT (org_id) DO NOTHING; |
| 120 | + """ |
| 121 | + async with await AsyncConnection.connect(os.environ.get("GF_DATABASE_URL")) as conn: |
| 122 | + async with conn.cursor() as cursor: |
| 123 | + await cursor.execute(sql_tpl) |
| 124 | + await conn.commit() |
105 | 125 |
|
106 | 126 |
|
107 | 127 | async def sync_alerting(context: dict, token: dict, userinfo: dict): |
|
0 commit comments