diff --git a/grafana_backup/create_dashboard.py b/grafana_backup/create_dashboard.py index c120bf656503cf81670dd42482997b159de34626..fb2a20e918a1065eea4a37767212dcbfc77a64ef 100755 --- a/grafana_backup/create_dashboard.py +++ b/grafana_backup/create_dashboard.py @@ -1,6 +1,6 @@ import json -from grafana_backup.dashboardApi import get_folder_id_from_old_folder_url, create_dashboard from grafana_backup.commons import to_python2_and_3_compatible_string +from grafana_backup.dashboardApi import get_folder_id, create_dashboard def main(args, settings, file_path): @@ -18,7 +18,7 @@ def main(args, settings, file_path): payload = { 'dashboard': content['dashboard'], - 'folderId': get_folder_id_from_old_folder_url(content['meta']['folderUrl'], grafana_url, http_post_headers, verify_ssl, client_cert, debug), + 'folderId': get_folder_id(content, grafana_url, http_post_headers, verify_ssl, client_cert, debug), 'overwrite': True } diff --git a/grafana_backup/dashboardApi.py b/grafana_backup/dashboardApi.py index f5082d4854209fa29b06fca6572f83c1285092eb..7dd1ec4fd80e51e95640fabfd4876de3adc8f3cb 100755 --- a/grafana_backup/dashboardApi.py +++ b/grafana_backup/dashboardApi.py @@ -254,19 +254,28 @@ def update_folder_permissions(payload, grafana_url, http_post_headers, verify_ss debug) -def get_folder_id_from_old_folder_url(folder_url, grafana_url, http_post_headers, verify_ssl, client_cert, debug): - if folder_url != "": - # Get folder uid - matches = re.search('dashboards\/[A-Za-z0-9]{1}\/(.*)\/.*', folder_url) - uid = matches.group(1) - - response = get_folder(uid, grafana_url, http_post_headers, verify_ssl, client_cert, debug) +def get_folder_id(dashboard, grafana_url, http_post_headers, verify_ssl, client_cert, debug): + folder_uid = "" + try: + folder_uid = dashboard['meta']['folderUid'] + except (KeyError): + matches = re.search('dashboards\/f\/(.*)\/.*', dashboard['meta']['folderUrl']) + folder_uid = matches.group(1) + + if (folder_uid != ""): + print("debug: quering with uid {}".format(folder_uid)) + response = get_folder(folder_uid, grafana_url, http_post_headers, verify_ssl, client_cert, debug) if isinstance(response[1], dict): folder_data = response[1] else: folder_data = json.loads(response[1]) - return folder_data['id'] - return 0 + + try: + return folder_data['id'] + except (KeyError): + return 0 + else: + return 0 def create_folder(payload, grafana_url, http_post_headers, verify_ssl, client_cert, debug):