class GrafanaRestClient(object):
_instance = None
- def _raise_for_validation(self):
+ @staticmethod
+ def _raise_for_validation(url, user, password):
msg = 'No {} found or misconfigured, please consult the ' \
'documentation about how to configure Grafana for the dashboard.'
- o = urlparse(self._url)
+ o = urlparse(url)
if not (o.netloc and o.scheme):
raise LookupError(msg.format('URL'))
- auth_method = 'password' if self._token is None else 'token'
- if auth_method == 'password' and not all((self._user, self._password)):
- raise LookupError(msg.format('username and/or password'))
- elif auth_method == 'token' and not self._token:
- raise LookupError(msg.format('token'))
+ if not all((user, password)):
+ raise LookupError(msg.format('credentials'))
- def __init__(self, url, username=None, password=None, token=None):
+ def __init__(self, url, username, password):
"""
:type url: str
:type username: str
:type password: str
"""
+ self._raise_for_validation(url, username, password)
self._url = url.rstrip('/')
self._user = username
self._password = password
- self._token = token
-
- self._raise_for_validation()
@classmethod
def instance(cls):
:rtype: GrafanaRestClient
"""
if not cls._instance:
- kwargs = {}
- if Settings.GRAFANA_API_AUTH_METHOD.lower() == 'password':
- kwargs['username'] = Settings.GRAFANA_API_USERNAME
- kwargs['password'] = Settings.GRAFANA_API_PASSWORD
- elif Settings.GRAFANA_API_AUTH_METHOD.lower() == 'token':
- kwargs['token'] = Settings.GRAFANA_API_TOKEN
- else:
- raise LookupError('No or unknown authentication method '
- 'provided. Please consult the documentation '
- 'about how to configure the '
- 'Grafana integration correctly.')
- cls._instance = GrafanaRestClient(Settings.GRAFANA_API_URL,
- **kwargs)
+ url = Settings.GRAFANA_API_URL
+ user = Settings.GRAFANA_API_USERNAME
+ password = Settings.GRAFANA_API_PASSWORD
+
+ cls._instance = GrafanaRestClient(url, user, password)
return cls._instance
headers = {k: v for k, v in cherrypy.request.headers.items()
if k.lower() in ('content-type', 'accept')}
- auth = None
- if self._token:
- headers['Authorization'] = 'Bearer {}'.format(self._token)
- else:
- auth = (self._user, self._password)
-
response = requests.request(
method,
url,
params=params,
data=data,
headers=headers,
- auth=auth)
+ auth=(self._user, self._password))
logger.debug("proxying method=%s path=%s params=%s data=%s", method,
path, params, data)
class Grafana(TestCase):
def test_missing_credentials(self):
- with six.assertRaisesRegex(self, LookupError,
- r'username and/or password'):
+ with six.assertRaisesRegex(self, LookupError, r'^No credentials.*'):
GrafanaRestClient(
url='http://localhost:3000', username='', password='admin')
- with six.assertRaisesRegex(self, LookupError, r'token'):
- GrafanaRestClient(
- url='http://localhost:3000',
- token='',
- )
with six.assertRaisesRegex(self, LookupError, r'^No URL.*'):
GrafanaRestClient(
url='//localhost:3000', username='admin', password='admin')
-@Controller('/grafana/mocked', secure=False)
+@Controller('grafana/mocked', secure=False)
class GrafanaMockInstance(BaseController):
@Proxy()
def __call__(self, path, **params):
settings = {
'GRAFANA_API_URL': 'http://localhost:{}/grafana/mocked/'.format(54583),
'GRAFANA_API_USERNAME': 'admin',
- 'GRAFANA_API_PASSWORD': 'admin',
- 'GRAFANA_API_AUTH_METHOD': 'password',
+ 'GRAFANA_API_PASSWORD': 'admin'
}
mgr.get_config.side_effect = settings.get
GrafanaProxy._cp_config['tools.authenticate.on'] = False # pylint: disable=protected-access