Allow basic auth when datasource is adding
This patch fixes an issue to use basic auth when the datasource is
adding for the first time. It also adds a delay when service is started.
diff --git a/_states/grafana3_datasource.py b/_states/grafana3_datasource.py
index a66836b..dbb3312 100644
--- a/_states/grafana3_datasource.py
+++ b/_states/grafana3_datasource.py
@@ -127,12 +127,20 @@
ret['changes'] = None
ret['comment'] = 'Data source {0} already up-to-date'.format(name)
else:
- requests.post(
- '{0}/api/datasources'.format(profile['grafana_url']),
- data,
- headers=_get_headers(profile),
- timeout=profile.get('grafana_timeout', 3),
- )
+ if profile.get('grafana_token', False):
+ requests.post(
+ '{0}/api/datasources'.format(profile['grafana_url']),
+ data,
+ headers=_get_headers(profile),
+ timeout=profile.get('grafana_timeout', 3),
+ )
+ else:
+ requests.put(
+ '{0}/api/datasources'.format(profile['grafana_url']),
+ data,
+ auth=_get_auth(profile),
+ timeout=profile.get('grafana_timeout', 3),
+ )
ret['result'] = True
ret['comment'] = 'New data source {0} added'.format(name)
ret['changes'] = data
diff --git a/grafana/server.sls b/grafana/server.sls
index 9e010d5..249898c 100644
--- a/grafana/server.sls
+++ b/grafana/server.sls
@@ -59,6 +59,9 @@
service.running:
- name: {{ server.service }}
- enable: true
+ # It is needed if client is trying to set datasource or dashboards before
+ # server is ready.
+ - init_delay: 5
- watch:
- file: /etc/grafana/grafana.ini