1
0
Fork 0

Merge pull request #8 from thouveng/use-json-for-basic-auth

Use JSON format with basic auth
This commit is contained in:
thouveng 2016-11-03 17:39:00 +01:00 committed by GitHub
commit 72e186cba8
1 changed files with 37 additions and 56 deletions

View File

@ -40,6 +40,7 @@ Basic auth setup
'''
from __future__ import absolute_import
import json
import requests
from salt.ext.six import string_types
@ -115,18 +116,11 @@ def present(name,
is_default=is_default)
if datasource:
if profile.get('grafana_token', False):
requests.put(
_get_url(profile, datasource['id']),
data,
headers=_get_headers(profile),
timeout=profile.get('grafana_timeout', 3),
)
else:
requests.put(
_get_url(profile, datasource['id']),
data,
data=json.dumps(data),
auth=_get_auth(profile),
headers=_get_headers(profile),
timeout=profile.get('grafana_timeout', 3),
)
ret['result'] = True
@ -137,18 +131,11 @@ def present(name,
ret['changes'] = None
ret['comment'] = 'Data source {0} already up-to-date'.format(name)
else:
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.post(
'{0}/api/datasources'.format(profile['grafana_url']),
data,
data=json.dumps(data),
auth=_get_auth(profile),
headers=_get_headers(profile),
timeout=profile.get('grafana_timeout', 3),
)
ret['result'] = True
@ -176,16 +163,10 @@ def absent(name, profile='grafana'):
ret['comment'] = 'Data source {0} already absent'.format(name)
return ret
if profile.get('grafana_token', False):
requests.delete(
_get_url(profile, datasource['id']),
headers=_get_headers(profile),
timeout=profile.get('grafana_timeout', 3),
)
else:
requests.delete(
_get_url(profile, datasource['id']),
auth=_get_auth(profile),
headers=_get_headers(profile),
timeout=profile.get('grafana_timeout', 3),
)
@ -203,16 +184,10 @@ def _get_url(profile, datasource_id):
def _get_datasource(profile, name):
if profile.get('grafana_token', False):
response = requests.get(
'{0}/api/datasources'.format(profile['grafana_url']),
headers=_get_headers(profile),
timeout=profile.get('grafana_timeout', 3),
)
else:
response = requests.get(
'{0}/api/datasources'.format(profile['grafana_url']),
auth=_get_auth(profile),
headers=_get_headers(profile),
timeout=profile.get('grafana_timeout', 3),
)
data = response.json()
@ -223,13 +198,19 @@ def _get_datasource(profile, name):
def _get_headers(profile):
return {
'Accept': 'application/json',
'Authorization': 'Bearer {0}'.format(profile['grafana_token'])
}
headers = {'Content-type': 'application/json'}
if profile.get('grafana_token', False):
headers['Authorization'] = 'Bearer {0}'.format(profile['grafana_token'])
return headers
def _get_auth(profile):
if profile.get('grafana_token', False):
return None
return requests.auth.HTTPBasicAuth(
profile['grafana_user'],
profile['grafana_password']