Skip to content

Commit

Permalink
update_folder_permissions()
Browse files Browse the repository at this point in the history
  • Loading branch information
jartigag committed Jun 29, 2021
1 parent 9c93d2a commit 4bdf220
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions grafana_backup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Usage:
grafana-backup save [--config=<filename>] [--components=<folders,dashboards,datasources,alert-channels,organizations,users>] [--no-archive]
grafana-backup restore <archive_file> [--config=<filename>] [--components=<folders,dashboards,datasources,alert-channels,organizations,users>]
grafana-backup restore <archive_file> [--config=<filename>] [--components=<folders,folders_permissions,dashboards,datasources,alert-channels,organizations,users>]
grafana-backup [--config=<filename>]
grafana-backup -h | --help
grafana-backup --version
Expand All @@ -19,7 +19,7 @@
-h --help Show this help message and exit
--version Get version information and exit
--config=<filename> Override default configuration path
--components=<folders,dashboards,datasources,alert-channels,organizations,users> Comma separated list of individual components to backup
--components=<folders,folders_permissions,dashboards,datasources,alert-channels,organizations,users> Comma separated list of individual components to backup
rather than backing up all components by default
--no-archive Skip archive creation and do not delete unarchived files
(used for troubleshooting purposes)
Expand Down
6 changes: 6 additions & 0 deletions grafana_backup/dashboardApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ def get_folder_permissions(uid, grafana_url, http_get_headers, verify_ssl, clien
return (status_code, content)


def update_folder_permissions(payload, grafana_url, http_post_headers, verify_ssl, client_cert, debug):
items = json.dumps({'items': payload})
return send_grafana_post('{0}/api/folders/{1}/permissions'.format(grafana_url,payload[0]['uid']), items, http_post_headers, verify_ssl, client_cert,
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
Expand Down
28 changes: 28 additions & 0 deletions grafana_backup/restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from grafana_backup.create_org import main as create_org
from grafana_backup.api_checks import main as api_checks
from grafana_backup.create_folder import main as create_folder
from grafana_backup.update_folder_permissions import main as update_folder_permissions
from grafana_backup.create_datasource import main as create_datasource
from grafana_backup.create_dashboard import main as create_dashboard
from grafana_backup.create_alert_channel import main as create_alert_channel
Expand Down Expand Up @@ -117,3 +118,30 @@ def restore_components(args, settings, restore_functions, tmpdir):
file_path = os.path.join(root, filename)
print('restoring {0}: {1}'.format(ext, file_path))
restore_functions[ext](args, settings, file_path)


def restore_from_dir(args, arg_components, settings, restore_dir):

This comment has been minimized.

Copy link
@acjohnson

acjohnson Oct 15, 2021

Collaborator

wish I would have noticed this sooner but it appears this function is unused and added by @jartigag any reason this can't be deleted?

This comment has been minimized.

Copy link
@acjohnson

acjohnson Oct 15, 2021

Collaborator

PR #140


restore_functions = { 'folder': create_folder,
'folder_permissions': update_folder_permissions,
'datasource': create_datasource,
'dashboard': create_dashboard,
'alert_channel': create_alert_channel,
'organization': create_org,
'user': create_user}

if arg_components:
arg_components_list = arg_components.split(',')

# Restore only the components that provided via an argument
# but must also exist in extracted archive
for ext in arg_components_list:
for file_path in glob('{0}/**/*.{1}'.format(restore_dir, ext[:-1]), recursive=True):
print('restoring {0}: {1}'.format(ext, file_path))
restore_functions[ext[:-1]](args, settings, file_path)
else:
# Restore every component included in extracted archive
for ext in restore_functions.keys():
for file_path in glob('{0}/**/*.{1}'.format(restore_dir, ext), recursive=True):
print('restoring {0}: {1}'.format(ext, file_path))
restore_functions[ext](args, settings, file_path)
18 changes: 18 additions & 0 deletions grafana_backup/update_folder_permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import json
from grafana_backup.dashboardApi import update_folder_permissions


def main(args, settings, file_path):
grafana_url = settings.get('GRAFANA_URL')
http_post_headers = settings.get('HTTP_POST_HEADERS')
verify_ssl = settings.get('VERIFY_SSL')
client_cert = settings.get('CLIENT_CERT')
debug = settings.get('DEBUG')

with open(file_path, 'r') as f:
data = f.read()

folder_permissions = json.loads(data)
if folder_permissions:
result = update_folder_permissions(folder_permissions, grafana_url, http_post_headers, verify_ssl, client_cert, debug)
print("update folder permissions {0}, status: {1}, msg: {2}".format(folder_permissions[0].get('title', ''), result[0], result[1]))

0 comments on commit 4bdf220

Please sign in to comment.