11import os
22import json
33import httpx
4+ from psycopg import AsyncConnection
45
56DEFAULT_HEADERS = {"Content-Type" : "application/json" }
67DRYCC_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
144148async 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 :
0 commit comments