Skip to content

Commit 7142541

Browse files
committed
chore(grafana): set datasource read only
1 parent 5dafcb0 commit 7142541

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

rootfs/usr/share/grafana/oauth2/hook/grafana.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import json
33
import httpx
4+
from psycopg import AsyncConnection
45

56
DEFAULT_HEADERS = {"Content-Type": "application/json"}
67
DRYCC_GRAFANA_REFRESH = os.environ.get('DRYCC_GRAFANA_REFRESH', '60s')
@@ -112,14 +113,16 @@ async def sync_datasource(context: dict, token: dict, userinfo: dict):
112113
datasource["secureJsonData"] = {
113114
"httpHeaderValue1": f"Token {drycc_token["token"]}",
114115
}
116+
await _set_datasource_read_only(False, datasource["id"])
115117
await client.put(
116118
api_url(f"/api/datasources/uid/{datasource["uid"]}"),
117119
headers=api_headers(context, userinfo),
118120
json=datasource,
119121
)
122+
await _set_datasource_read_only(True, datasource["id"])
120123
return
121124
drycc_token = await _get_or_create_drycc_token(None, token)
122-
await client.post(
125+
resp = await client.post(
123126
api_url("/api/datasources"),
124127
headers=api_headers(context, userinfo),
125128
json={
@@ -139,6 +142,7 @@ async def sync_datasource(context: dict, token: dict, userinfo: dict):
139142
},
140143
},
141144
)
145+
await _set_datasource_read_only(True, resp.json()["datasource"]["id"])
142146

143147

144148
async def sync_dashboard(context: dict, token: dict, userinfo: dict):
@@ -158,7 +162,17 @@ async def sync_dashboard(context: dict, token: dict, userinfo: dict):
158162
)
159163

160164

161-
async def _get_or_create_drycc_token(drycc_token_uuid, token: dict):
165+
async def _set_datasource_read_only(read_only: bool, id: str):
166+
async with await AsyncConnection.connect(os.environ.get("GF_DATABASE_URL")) as conn:
167+
async with conn.cursor() as cursor:
168+
await cursor.execute(
169+
"UPDATE data_source SET read_only=%s WHERE id=%s",
170+
(read_only, id)
171+
)
172+
await conn.commit()
173+
174+
175+
async def _get_or_create_drycc_token(drycc_token_uuid: str, token: dict):
162176
headers = {"Authorization": f"Bearer {token["access_token"]}"}
163177
async with httpx.AsyncClient() as client:
164178
if drycc_token_uuid:

rootfs/usr/share/grafana/oauth2/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ redis[asyncio]>=6.2.0
33
uvicorn[standard]>=0.15.0
44
authlib>=1.0.0
55
httpx>=0.23.0
6+
psycopg[binary]>=3.2.9
67
python-jose>=3.3.0
78
python-multipart>=0.0.5
89
itsdangerous>=2.2.0

0 commit comments

Comments
 (0)