Skip to content

Fix restoring library elements that have more than one folder #199

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

Merged
merged 1 commit into from
Mar 24, 2023
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
27 changes: 21 additions & 6 deletions grafana_backup/create_library_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,24 @@ def main(args, settings, file_path):
# Library Elements can only be created referencing a folder id. However, this folder id is not unique across Grafana
# instances. Therefore, we need to first find the folder id by the given folder uid.
library_element = json.loads(data)
folder_uid = library_element['meta']['folderUid']
folder_id = get_folder(folder_uid, grafana_url, http_post_headers, verify_ssl, client_cert, debug)[1]['id']
library_element['folderId'] = folder_id
result = create_library_element(json.dumps(library_element), grafana_url, http_post_headers, verify_ssl,
client_cert, debug)
print("create library_elements: {0}, status: {1}, msg: {2}".format(library_element['name'], result[0], result[1]))
folder_uid = library_element["meta"]["folderUid"]
folder_id_response = get_folder(
folder_uid, grafana_url, http_post_headers, verify_ssl, client_cert, debug
)[1]
if isinstance(folder_id_response, list):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also test len(folder_id_response) here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I don't think so: there is always at least one, probably more than one in the array case.

I don't know what to do if the grafana API started returning arrays that didn't contain any folders at all.

library_element["folderUid"] = folder_id_response[0]['uid']
else:
library_element["folderUid"] = folder_id_response['uid']
result = create_library_element(
json.dumps(library_element),
grafana_url,
http_post_headers,
verify_ssl,
client_cert,
debug,
)
print(
"create library_elements: {0}, status: {1}, msg: {2}".format(
library_element["name"], result[0], result[1]
)
)