1
0
Fork 0
mirror of synced 2024-11-22 17:05:34 -05:00

Use JSON format with basic auth

This commit is contained in:
Guillaume Thouvenin 2016-11-03 14:52:16 +01:00
parent f3252e1850
commit e3e6e3f1d9

View file

@ -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,20 +116,13 @@ 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=json.dumps(data),
data, auth=_get_auth(profile),
headers=_get_headers(profile), headers=_get_headers(profile),
timeout=profile.get('grafana_timeout', 3), timeout=profile.get('grafana_timeout', 3),
) )
else:
requests.put(
_get_url(profile, datasource['id']),
data,
auth=_get_auth(profile),
timeout=profile.get('grafana_timeout', 3),
)
ret['result'] = True ret['result'] = True
ret['changes'] = _diff(datasource, data) ret['changes'] = _diff(datasource, data)
if ret['changes']['new'] or ret['changes']['old']: if ret['changes']['new'] or ret['changes']['old']:
@ -137,20 +131,13 @@ 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=json.dumps(data),
data, auth=_get_auth(profile),
headers=_get_headers(profile), headers=_get_headers(profile),
timeout=profile.get('grafana_timeout', 3), timeout=profile.get('grafana_timeout', 3),
) )
else:
requests.post(
'{0}/api/datasources'.format(profile['grafana_url']),
data,
auth=_get_auth(profile),
timeout=profile.get('grafana_timeout', 3),
)
ret['result'] = True ret['result'] = True
ret['comment'] = 'New data source {0} added'.format(name) ret['comment'] = 'New data source {0} added'.format(name)
ret['changes'] = data ret['changes'] = data
@ -176,18 +163,12 @@ 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(
requests.delete( _get_url(profile, datasource['id']),
_get_url(profile, datasource['id']), auth=_get_auth(profile),
headers=_get_headers(profile), headers=_get_headers(profile),
timeout=profile.get('grafana_timeout', 3), timeout=profile.get('grafana_timeout', 3),
) )
else:
requests.delete(
_get_url(profile, datasource['id']),
auth=_get_auth(profile),
timeout=profile.get('grafana_timeout', 3),
)
ret['result'] = True ret['result'] = True
ret['comment'] = 'Data source {0} was deleted'.format(name) ret['comment'] = 'Data source {0} was deleted'.format(name)
@ -203,18 +184,12 @@ 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(
response = requests.get( '{0}/api/datasources'.format(profile['grafana_url']),
'{0}/api/datasources'.format(profile['grafana_url']), auth=_get_auth(profile),
headers=_get_headers(profile), headers=_get_headers(profile),
timeout=profile.get('grafana_timeout', 3), timeout=profile.get('grafana_timeout', 3),
) )
else:
response = requests.get(
'{0}/api/datasources'.format(profile['grafana_url']),
auth=_get_auth(profile),
timeout=profile.get('grafana_timeout', 3),
)
data = response.json() data = response.json()
for datasource in data: for datasource in data:
if datasource['name'] == name: if datasource['name'] == name:
@ -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']