Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ensure_folder function also update an outdated folder title #126

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading