Skip to content

Commit

Permalink
added uid_support to datasource backups (#158)
Browse files Browse the repository at this point in the history
* added uid_support to datasource backups

* break out uid_support for datasources

Co-authored-by: Aaron Johnson <[email protected]>
  • Loading branch information
acjohnson and Aaron Johnson authored Dec 1, 2021
1 parent 7f1080f commit 78155fe
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 27 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# [1.2.2] - 2021-11-30

### Added

### Changed
- #158 added uid_support to datasource backups

### Removed

# [1.2.1] - 2021-10-15

### Added
Expand Down
10 changes: 6 additions & 4 deletions grafana_backup/api_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ def main(settings):
if not status == 200:
return (status, json_resp, None, None)

uid_support = uid_feature_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
if isinstance(uid_support, str):
raise Exception(uid_support)
dashboard_uid_support, datasource_uid_support = uid_feature_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
if isinstance(dashboard_uid_support, str):
raise Exception(dashboard_uid_support)
if isinstance(datasource_uid_support, str):
raise Exception(datasource_uid_support)

paging_support = paging_feature_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
if isinstance(paging_support, str):
Expand All @@ -32,4 +34,4 @@ def main(settings):
print("[Pre-Check] Server status is NOT OK !!: {0}".format(json_resp))
print_horizontal_line()

return (status, json_resp, uid_support, paging_support)
return (status, json_resp, dashboard_uid_support, datasource_uid_support, paging_support)
25 changes: 20 additions & 5 deletions grafana_backup/dashboardApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,31 @@ def uid_feature_check(grafana_url, http_get_headers, verify_ssl, client_cert, de
(status, content) = search_dashboard(1, 1, grafana_url, http_get_headers, verify_ssl, client_cert, debug)
if status == 200 and len(content):
if 'uid' in content[0]:
uid_support = True
dashboard_uid_support = True
else:
uid_support = False
return uid_support
dashboard_uid_support = False
else:
if len(content):
return "get dashboards failed, status: {0}, msg: {1}".format(status, content)
dashboard_uid_support = "get dashboards failed, status: {0}, msg: {1}".format(status, content)
else:
# No dashboards exist, disable uid feature
return False
dashboard_uid_support = False
# Get first datasource
print("\n[Pre-Check] grafana uid feature check: calling 'search_datasource'")
(status, content) = search_datasource(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
if status == 200 and len(content):
if 'uid' in content[0]:
datasource_uid_support = True
else:
datasource_uid_support = False
else:
if len(content):
datasource_uid_support = "get datasources failed, status: {0}, msg: {1}".format(status, content)
else:
# No datasources exist, disable uid feature
datasource_uid_support = False

return dashboard_uid_support, datasource_uid_support


def paging_feature_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug):
Expand Down
5 changes: 3 additions & 2 deletions grafana_backup/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ def main(args, settings):
'snapshots': delete_snapshots,
'annotations': delete_annotations}

(status, json_resp, uid_support, paging_support) = api_checks(settings)
(status, json_resp, dashboard_uid_support, datasource_uid_support, paging_support) = api_checks(settings)

# Do not continue if API is unavailable or token is not valid
if not status == 200:
print("server status is not ok: {0}".format(json_resp))
sys.exit(1)

settings.update({'UID_SUPPORT': uid_support})
settings.update({'DASHBOARD_UID_SUPPORT': dashboard_uid_support})
settings.update({'DATASOURCE_UID_SUPPORT': datasource_uid_support})
settings.update({'PAGING_SUPPORT': paging_support})

if arg_components:
Expand Down
2 changes: 1 addition & 1 deletion grafana_backup/delete_dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def main(args, settings):
client_cert = settings.get('CLIENT_CERT')
debug = settings.get('DEBUG')
pretty_print = settings.get('PRETTY_PRINT')
uid_support = settings.get('UID_SUPPORT')
uid_support = settings.get('DASHBOARD_UID_SUPPORT')
paging_support = settings.get('PAGING_SUPPORT')

if paging_support:
Expand Down
2 changes: 1 addition & 1 deletion grafana_backup/delete_datasources.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def main(args, settings):
verify_ssl = settings.get('VERIFY_SSL')
grafana_url = settings.get('GRAFANA_URL')
client_cert = settings.get('CLIENT_CERT')
uid_support = settings.get('UID_SUPPORT')
uid_support = settings.get('DATASOURCE_UID_SUPPORT')
pretty_print = settings.get('PRETTY_PRINT')
http_get_headers = settings.get('HTTP_POST_HEADERS')

Expand Down
2 changes: 1 addition & 1 deletion grafana_backup/delete_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def main(args, settings):
client_cert = settings.get('CLIENT_CERT')
debug = settings.get('DEBUG')
pretty_print = settings.get('PRETTY_PRINT')
uid_support = settings.get('UID_SUPPORT')
uid_support = settings.get('DASHBOARD_UID_SUPPORT')

folders = get_all_folders_in_grafana(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
print_horizontal_line()
Expand Down
5 changes: 3 additions & 2 deletions grafana_backup/pause_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@


def main(args, settings):
(status, json_resp, uid_support, paging_support) = api_checks(settings)
(status, json_resp, dashboard_uid_support, datasource_uid_support, paging_support) = api_checks(settings)

# Do not continue if API is unavailable or token is not valid
if not status == 200:
print("server status is not ok: {0}".format(json_resp))
sys.exit(1)

settings.update({'UID_SUPPORT': uid_support})
settings.update({'DASHBOARD_UID_SUPPORT': dashboard_uid_support})
settings.update({'DATASOURCE_UID_SUPPORT': datasource_uid_support})
settings.update({'PAGING_SUPPORT': paging_support})

debug = settings.get('DEBUG')
Expand Down
2 changes: 1 addition & 1 deletion grafana_backup/restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def open_compressed_backup(compressed_backup):
azure_storage_container_name = settings.get('AZURE_STORAGE_CONTAINER_NAME')
gcs_bucket_name = settings.get('GCS_BUCKET_NAME')

(status, json_resp, uid_support, paging_support) = api_checks(settings)
(status, json_resp, dashboard_uid_support, datasource_uid_support, paging_support) = api_checks(settings)

# Do not continue if API is unavailable or token is not valid
if not status == 200:
Expand Down
5 changes: 3 additions & 2 deletions grafana_backup/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ def main(args, settings):
'versions': save_versions,
'annotations': save_annotations}

(status, json_resp, uid_support, paging_support) = api_checks(settings)
(status, json_resp, dashboard_uid_support, datasource_uid_support, paging_support) = api_checks(settings)

# Do not continue if API is unavailable or token is not valid
if not status == 200:
print("server status is not ok: {0}".format(json_resp))
sys.exit(1)

settings.update({'UID_SUPPORT': uid_support})
settings.update({'DASHBOARD_UID_SUPPORT': dashboard_uid_support})
settings.update({'DATASOURCE_UID_SUPPORT': datasource_uid_support})
settings.update({'PAGING_SUPPORT': paging_support})

if arg_components:
Expand Down
2 changes: 1 addition & 1 deletion grafana_backup/save_dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def main(args, settings):
client_cert = settings.get('CLIENT_CERT')
debug = settings.get('DEBUG')
pretty_print = settings.get('PRETTY_PRINT')
uid_support = settings.get('UID_SUPPORT')
uid_support = settings.get('DASHBOARD_UID_SUPPORT')
paging_support = settings.get('PAGING_SUPPORT')

folder_path = '{0}/dashboards/{1}'.format(backup_dir, timestamp)
Expand Down
12 changes: 8 additions & 4 deletions grafana_backup/save_datasources.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ def main(args, settings):
client_cert = settings.get('CLIENT_CERT')
debug = settings.get('DEBUG')
pretty_print = settings.get('PRETTY_PRINT')
uid_support = settings.get('DATASOURCE_UID_SUPPORT')

folder_path = '{0}/datasources/{1}'.format(backup_dir, timestamp)
'datasources_{0}.txt'.format(timestamp)

if not os.path.exists(folder_path):
os.makedirs(folder_path)

get_all_datasources_and_save(folder_path, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print)
get_all_datasources_and_save(folder_path, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support)
print_horizontal_line()


Expand All @@ -28,14 +28,18 @@ def save_datasource(file_name, datasource_setting, folder_path, pretty_print):
print("datasource:{0} is saved to {1}".format(file_name, file_path))


def get_all_datasources_and_save(folder_path, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print):
def get_all_datasources_and_save(folder_path, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support):
status_code_and_content = search_datasource(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
if status_code_and_content[0] == 200:
datasources = status_code_and_content[1]
print("There are {0} datasources:".format(len(datasources)))
for datasource in datasources:
print(datasource)
save_datasource(datasource['name'], datasource, folder_path, pretty_print)
if uid_support:
datasource_name = datasource['uid']
else:
datasource_name = datasource['name']
save_datasource(datasource_name, datasource, folder_path, pretty_print)
else:
print("query datasource failed, status: {0}, msg: {1}".format(status_code_and_content[0],
status_code_and_content[1]))
2 changes: 1 addition & 1 deletion grafana_backup/save_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def main(args, settings):
client_cert = settings.get('CLIENT_CERT')
debug = settings.get('DEBUG')
pretty_print = settings.get('PRETTY_PRINT')
uid_support = settings.get('UID_SUPPORT')
uid_support = settings.get('DASHBOARD_UID_SUPPORT')

folder_path = '{0}/folders/{1}'.format(backup_dir, timestamp)
log_file = 'folders_{0}.txt'.format(timestamp)
Expand Down
2 changes: 1 addition & 1 deletion grafana_backup/save_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def main(args, settings):
client_cert = settings.get('CLIENT_CERT')
debug = settings.get('DEBUG')
pretty_print = settings.get('PRETTY_PRINT')
uid_support = settings.get('UID_SUPPORT')
uid_support = settings.get('DASHBOARD_UID_SUPPORT')

folder_path = '{0}/dashboard_versions/{1}'.format(backup_dir, timestamp)
log_file = 'dashboard_versions_{0}.txt'.format(timestamp)
Expand Down
2 changes: 1 addition & 1 deletion grafana_backup/unpause_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def main(args, settings):
alerts_file = args.get('<alerts_filename>', None)
print("got alerts_file {0}".format(alerts_file))

(status, json_resp, uid_support, paging_support) = api_checks(settings)
(status, json_resp, dashboard_uid_support, datasource_uid_support, paging_support) = api_checks(settings)

# Do not continue if API is unavailable or token is not valid
if not status == 200:
Expand Down

0 comments on commit 78155fe

Please sign in to comment.