Merge pull request #8 from thouveng/use-json-for-basic-auth
Use JSON format with basic auth
This commit is contained in:
commit
72e186cba8
1 changed files with 37 additions and 56 deletions
|
@ -40,6 +40,7 @@ Basic auth setup
|
||||||
'''
|
'''
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
import json
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from salt.ext.six import string_types
|
from salt.ext.six import string_types
|
||||||
|
@ -115,18 +116,11 @@ def present(name,
|
||||||
is_default=is_default)
|
is_default=is_default)
|
||||||
|
|
||||||
if datasource:
|
if datasource:
|
||||||
if profile.get('grafana_token', False):
|
|
||||||
requests.put(
|
requests.put(
|
||||||
_get_url(profile, datasource['id']),
|
_get_url(profile, datasource['id']),
|
||||||
data,
|
data=json.dumps(data),
|
||||||
headers=_get_headers(profile),
|
|
||||||
timeout=profile.get('grafana_timeout', 3),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
requests.put(
|
|
||||||
_get_url(profile, datasource['id']),
|
|
||||||
data,
|
|
||||||
auth=_get_auth(profile),
|
auth=_get_auth(profile),
|
||||||
|
headers=_get_headers(profile),
|
||||||
timeout=profile.get('grafana_timeout', 3),
|
timeout=profile.get('grafana_timeout', 3),
|
||||||
)
|
)
|
||||||
ret['result'] = True
|
ret['result'] = True
|
||||||
|
@ -137,18 +131,11 @@ def present(name,
|
||||||
ret['changes'] = None
|
ret['changes'] = None
|
||||||
ret['comment'] = 'Data source {0} already up-to-date'.format(name)
|
ret['comment'] = 'Data source {0} already up-to-date'.format(name)
|
||||||
else:
|
else:
|
||||||
if profile.get('grafana_token', False):
|
|
||||||
requests.post(
|
requests.post(
|
||||||
'{0}/api/datasources'.format(profile['grafana_url']),
|
'{0}/api/datasources'.format(profile['grafana_url']),
|
||||||
data,
|
data=json.dumps(data),
|
||||||
headers=_get_headers(profile),
|
|
||||||
timeout=profile.get('grafana_timeout', 3),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
requests.post(
|
|
||||||
'{0}/api/datasources'.format(profile['grafana_url']),
|
|
||||||
data,
|
|
||||||
auth=_get_auth(profile),
|
auth=_get_auth(profile),
|
||||||
|
headers=_get_headers(profile),
|
||||||
timeout=profile.get('grafana_timeout', 3),
|
timeout=profile.get('grafana_timeout', 3),
|
||||||
)
|
)
|
||||||
ret['result'] = True
|
ret['result'] = True
|
||||||
|
@ -176,16 +163,10 @@ def absent(name, profile='grafana'):
|
||||||
ret['comment'] = 'Data source {0} already absent'.format(name)
|
ret['comment'] = 'Data source {0} already absent'.format(name)
|
||||||
return ret
|
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(
|
requests.delete(
|
||||||
_get_url(profile, datasource['id']),
|
_get_url(profile, datasource['id']),
|
||||||
auth=_get_auth(profile),
|
auth=_get_auth(profile),
|
||||||
|
headers=_get_headers(profile),
|
||||||
timeout=profile.get('grafana_timeout', 3),
|
timeout=profile.get('grafana_timeout', 3),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -203,16 +184,10 @@ def _get_url(profile, datasource_id):
|
||||||
|
|
||||||
|
|
||||||
def _get_datasource(profile, name):
|
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(
|
response = requests.get(
|
||||||
'{0}/api/datasources'.format(profile['grafana_url']),
|
'{0}/api/datasources'.format(profile['grafana_url']),
|
||||||
auth=_get_auth(profile),
|
auth=_get_auth(profile),
|
||||||
|
headers=_get_headers(profile),
|
||||||
timeout=profile.get('grafana_timeout', 3),
|
timeout=profile.get('grafana_timeout', 3),
|
||||||
)
|
)
|
||||||
data = response.json()
|
data = response.json()
|
||||||
|
@ -223,13 +198,19 @@ def _get_datasource(profile, name):
|
||||||
|
|
||||||
|
|
||||||
def _get_headers(profile):
|
def _get_headers(profile):
|
||||||
return {
|
|
||||||
'Accept': 'application/json',
|
headers = {'Content-type': 'application/json'}
|
||||||
'Authorization': 'Bearer {0}'.format(profile['grafana_token'])
|
|
||||||
}
|
if profile.get('grafana_token', False):
|
||||||
|
headers['Authorization'] = 'Bearer {0}'.format(profile['grafana_token'])
|
||||||
|
|
||||||
|
return headers
|
||||||
|
|
||||||
|
|
||||||
def _get_auth(profile):
|
def _get_auth(profile):
|
||||||
|
if profile.get('grafana_token', False):
|
||||||
|
return None
|
||||||
|
|
||||||
return requests.auth.HTTPBasicAuth(
|
return requests.auth.HTTPBasicAuth(
|
||||||
profile['grafana_user'],
|
profile['grafana_user'],
|
||||||
profile['grafana_password']
|
profile['grafana_password']
|
||||||
|
|
Loading…
Reference in a new issue