Skip to content
Snippets Groups Projects
Commit 4bdf220a authored by jartigag's avatar jartigag
Browse files

update_folder_permissions()

parent 9c93d2a6
No related branches found
No related tags found
No related merge requests found
......@@ -10,7 +10,7 @@ docstring = """
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
......@@ -19,7 +19,7 @@ Options:
-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)
......
......@@ -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
......
......@@ -2,6 +2,7 @@ from grafana_backup.commons import print_horizontal_line
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
......@@ -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):
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)
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% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment