Skip to content

Commit

Permalink
Merge pull request #126 from consideRatio/pr/ensure-folder-update-titles
Browse files Browse the repository at this point in the history
Make `ensure_folder` function also update an outdated folder title
  • Loading branch information
consideRatio authored Apr 13, 2024
2 parents 9ca195d + 0131b9d commit dfd56e4
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
DEFAULT_FOLDER_UID = '70E5EE84-1217-4021-A89E-1E3DE0566D93'


def grafana_request(endpoint, token, path, data=None, no_tls_verify=False):
def grafana_request(endpoint, token, path, data=None, method=None, no_tls_verify=False):
headers = {'Authorization': f'Bearer {token}', 'Content-Type': 'application/json'}
method = 'GET' if data is None else 'POST'
method = method or ('GET' if data is None else 'POST')
req = Request(f'{endpoint}/api{path}', headers=headers, method=method)

if not isinstance(data, bytes):
Expand All @@ -37,19 +37,25 @@ def grafana_request(endpoint, token, path, data=None, no_tls_verify=False):
return json.load(resp)


def ensure_folder(name, uid, api):
def ensure_folder(title, uid, api):
"""
Checks for a folder based on its UID and creates one if a folder didn't
exist.
Checks for a folder based on its UID and creates one if needed or updates
the title if needed.
"""
try:
# api ref: https://grafana.com/docs/grafana/latest/developers/http_api/folder/#get-folder-by-uid
return api(f'/folders/{uid}')
folder = api(f'/folders/{uid}')
if folder["title"] != title:
# api ref: https://grafana.com/docs/grafana/latest/developers/http_api/folder/#update-folder
api(
f'/folders/{uid}',
{'title': title, 'version': folder["version"]},
method='PUT',
)
except HTTPError as e:
if e.code == 404:
# api ref: https://grafana.com/docs/grafana/latest/developers/http_api/folder/#create-folder
folder = {'uid': uid, 'title': name}
return api('/folders', folder)
api('/folders', {'uid': uid, 'title': title})
else:
raise

Expand Down

0 comments on commit dfd56e4

Please sign in to comment.