I’m trying to migrate a couple of dashboards from one server to another. To accomplish this I write a pretty simple script using python
and requests
library.
class Grafana:
def __init__(self, url, key):
self.headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + key
}
self.url = url
def get_all_dashboard_uris(self):
return (dashboard['uri'] for dashboard in requests.get(self.url+'api/search?query=', headers=self.headers).json())
def import_dashboard(self, dashboard_uri):
return requests.get(self.url+'api/dashboards/'+dashboard_uri, headers=self.headers).json()['dashboard']
def create_dashboard(self, dashboard_object):
dashboard_object['id'] = None #id = null to create a new dashboard
#(https://grafana.com/docs/grafana/v4.1/http_api/dashboard/)
return requests.post(self.url+'api/dashboards/db', headers=self.headers, json={'dashboard': dashboard_object})
if __name__ == '__main__':
initial_grafana = Grafana(url=URL, key=KEY)
final_grafana = Grafana(url=URL_TEST, key=KEY_TEST)
for dashboard_uri in initial_grafana.get_all_dashboard_uris():
dash = initial_grafana.import_dashboard(dashboard_uri)
result = final_grafana.create_dashboard(initial_grafana.import_dashboard(dashboard_uri))
print(result.status_code, result.text)
print('')
The problem is that this prints 403 {"message":"Permission denied"}
for all dashboards. I face this problem even if a try to create the dashboard with the exact same paramaters as the documentation suggests:
def create_test_dashboard(self):
dashboard_object = {
'dashboard': {
'id': None,
'title': 'Production Overview',
'tags': ['templated'],
'timezone': 'browser',
'rows': [{}],
'schemaVersion': 6,
'version': 0
},
'overwrite': False
}
return requests.post(self.url+'api/dashboards/db', headers=self.headers, json=dashboard_object)
if __name__ == '__main__':
final_grafana = Grafana(url=URL_TEST, key=KEY_TEST)
result = final_grafana.create_test_dashboard()
Some quick notes that I think it’s important:
- I have no problem if I use the
import_dashboard
method in thefinal_grafana
object; - No problem at all if I create the dashboard manually using a
.json
file; - I’m using an admin role user;
- I’m using Grafana 4.2.
Any ideas on how to solve this?