Rework grafana object for future functionality
Currently we only support dashboards for Grafana, this is about to change. For example, we also need to add the ability to create datasources and even users. Here we are breaking out the logic into more specific functions. Additionaly, this helps when we eventually break out this code into python-grafana. Change-Id: I10a618adbf9052c8dccda38fefb7abf3a148d3b6 Signed-off-by: Paul Belanger <pabelanger@redhat.com>
This commit is contained in:
parent
4e643e5d06
commit
3a32d511bb
|
@ -6,3 +6,7 @@ API Reference
|
||||||
.. automodule:: grafana_dashboards.grafana
|
.. automodule:: grafana_dashboards.grafana
|
||||||
:members:
|
:members:
|
||||||
:undoc-members:
|
:undoc-members:
|
||||||
|
|
||||||
|
.. automodule:: grafana_dashboards.grafana.dashboard
|
||||||
|
:members:
|
||||||
|
:undoc-members:
|
||||||
|
|
|
@ -74,7 +74,7 @@ class Builder(object):
|
||||||
for name in dashboards:
|
for name in dashboards:
|
||||||
data, md5 = self.parser.get_dashboard(name)
|
data, md5 = self.parser.get_dashboard(name)
|
||||||
if self.cache.has_changed(name, md5) or not self.cache_enabled:
|
if self.cache.has_changed(name, md5) or not self.cache_enabled:
|
||||||
self.grafana.create_dashboard(name, data, overwrite=True)
|
self.grafana.dashboard.create(name, data, overwrite=True)
|
||||||
self.cache.set(name, md5)
|
self.cache.set(name, md5)
|
||||||
else:
|
else:
|
||||||
LOG.debug("'%s' has not changed" % name)
|
LOG.debug("'%s' has not changed" % name)
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
# Copyright 2015 Red Hat, Inc.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
try:
|
||||||
|
from urllib.parse import urljoin
|
||||||
|
except ImportError:
|
||||||
|
from urlparse import urljoin
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from grafana_dashboards.grafana.dashboard import Dashboard
|
||||||
|
|
||||||
|
|
||||||
|
class Grafana(object):
|
||||||
|
|
||||||
|
def __init__(self, url, key=None):
|
||||||
|
"""Create object for grafana instance
|
||||||
|
|
||||||
|
:param url: URL for Grafana server
|
||||||
|
:type url: str
|
||||||
|
:param key: API token used for authenticate
|
||||||
|
:type key: str
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
base_url = urljoin(url, 'api/dashboards/db/')
|
||||||
|
session = requests.Session()
|
||||||
|
session.headers.update({
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
})
|
||||||
|
# NOTE(pabelanger): Grafana 2.1.0 added basic auth support so now the
|
||||||
|
# api key is optional.
|
||||||
|
if key:
|
||||||
|
session.headers.update({
|
||||||
|
'Authorization': 'Bearer %s' % key,
|
||||||
|
})
|
||||||
|
|
||||||
|
self.dashboard = Dashboard(base_url, session)
|
|
@ -19,33 +19,14 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from urlparse import urljoin
|
from urlparse import urljoin
|
||||||
|
|
||||||
import requests
|
|
||||||
from requests import exceptions
|
from requests import exceptions
|
||||||
|
|
||||||
|
|
||||||
class Grafana(object):
|
class Dashboard(object):
|
||||||
|
|
||||||
def __init__(self, url, key=None):
|
def __init__(self, url, session):
|
||||||
"""Create object for grafana instance
|
self.url = url
|
||||||
|
self.session = session
|
||||||
:param url: URL for Grafana server
|
|
||||||
:type url: str
|
|
||||||
:param key: API token used for authenticate
|
|
||||||
:type key: str
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
self.url = urljoin(url, 'api/dashboards/db/')
|
|
||||||
self.session = requests.Session()
|
|
||||||
self.session.headers.update({
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
})
|
|
||||||
# NOTE(pabelanger): Grafana 2.1.0 added basic auth support so now the
|
|
||||||
# api key is optional.
|
|
||||||
if key:
|
|
||||||
self.session.headers.update({
|
|
||||||
'Authorization': 'Bearer %s' % key,
|
|
||||||
})
|
|
||||||
|
|
||||||
def assert_dashboard_exists(self, name):
|
def assert_dashboard_exists(self, name):
|
||||||
"""Raise an exception if dashboard does not exist
|
"""Raise an exception if dashboard does not exist
|
||||||
|
@ -58,7 +39,7 @@ class Grafana(object):
|
||||||
if not self.is_dashboard(name):
|
if not self.is_dashboard(name):
|
||||||
raise Exception('dashboard[%s] does not exist' % name)
|
raise Exception('dashboard[%s] does not exist' % name)
|
||||||
|
|
||||||
def create_dashboard(self, name, data, overwrite=False):
|
def create(self, name, data, overwrite=False):
|
||||||
"""Create a new dashboard
|
"""Create a new dashboard
|
||||||
|
|
||||||
:param name: URL friendly title of the dashboard
|
:param name: URL friendly title of the dashboard
|
||||||
|
@ -85,7 +66,7 @@ class Grafana(object):
|
||||||
res.raise_for_status()
|
res.raise_for_status()
|
||||||
self.assert_dashboard_exists(name)
|
self.assert_dashboard_exists(name)
|
||||||
|
|
||||||
def get_dashboard(self, name):
|
def get(self, name):
|
||||||
"""Get a dashboard
|
"""Get a dashboard
|
||||||
|
|
||||||
:param name: URL friendly title of the dashboard
|
:param name: URL friendly title of the dashboard
|
||||||
|
@ -113,7 +94,7 @@ class Grafana(object):
|
||||||
:rtype: bool
|
:rtype: bool
|
||||||
|
|
||||||
"""
|
"""
|
||||||
res = self.get_dashboard(name)
|
res = self.get(name)
|
||||||
if res and res['meta']['slug'] == name:
|
if res and res['meta']['slug'] == name:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
|
@ -26,7 +26,7 @@ class TestCaseBuilder(TestCase):
|
||||||
super(TestCaseBuilder, self).setUp()
|
super(TestCaseBuilder, self).setUp()
|
||||||
self.builder = builder.Builder(self.config)
|
self.builder = builder.Builder(self.config)
|
||||||
|
|
||||||
@mock.patch('grafana_dashboards.grafana.Grafana.create_dashboard')
|
@mock.patch('grafana_dashboards.grafana.Dashboard.create')
|
||||||
def test_update_dashboard(self, mock_grafana):
|
def test_update_dashboard(self, mock_grafana):
|
||||||
dashboard = os.path.join(
|
dashboard = os.path.join(
|
||||||
os.path.dirname(__file__), 'fixtures/builder/dashboard-0001.yaml')
|
os.path.dirname(__file__), 'fixtures/builder/dashboard-0001.yaml')
|
||||||
|
|
|
@ -49,12 +49,12 @@ class TestCaseGrafana(TestCase):
|
||||||
|
|
||||||
def test_init(self):
|
def test_init(self):
|
||||||
grafana = Grafana(self.url)
|
grafana = Grafana(self.url)
|
||||||
self.assertNotIn('Authorization', grafana.session.headers)
|
self.assertNotIn('Authorization', grafana.dashboard.session.headers)
|
||||||
|
|
||||||
def test_init_apikey(self):
|
def test_init_apikey(self):
|
||||||
apikey = 'eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk'
|
apikey = 'eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk'
|
||||||
grafana = Grafana(self.url, apikey)
|
grafana = Grafana(self.url, apikey)
|
||||||
headers = grafana.session.headers
|
headers = grafana.dashboard.session.headers
|
||||||
self.assertIn('Authorization', headers)
|
self.assertIn('Authorization', headers)
|
||||||
self.assertEqual(headers['Authorization'], 'Bearer %s' % apikey)
|
self.assertEqual(headers['Authorization'], 'Bearer %s' % apikey)
|
||||||
|
|
||||||
|
@ -64,7 +64,8 @@ class TestCaseGrafana(TestCase):
|
||||||
'/api/dashboards/db/new-dashboard', json=DASHBOARD_NOT_FOUND,
|
'/api/dashboards/db/new-dashboard', json=DASHBOARD_NOT_FOUND,
|
||||||
status_code=404)
|
status_code=404)
|
||||||
self.assertRaises(
|
self.assertRaises(
|
||||||
Exception, self.grafana.assert_dashboard_exists, 'new-dashboard')
|
Exception, self.grafana.dashboard.assert_dashboard_exists,
|
||||||
|
'new-dashboard')
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
@requests_mock.Mocker()
|
||||||
def test_create_dashboard_new(self, mock_requests):
|
def test_create_dashboard_new(self, mock_requests):
|
||||||
|
@ -84,7 +85,7 @@ class TestCaseGrafana(TestCase):
|
||||||
},
|
},
|
||||||
"slug": 'new-dashboard',
|
"slug": 'new-dashboard',
|
||||||
}
|
}
|
||||||
self.grafana.create_dashboard(
|
self.grafana.dashboard.create(
|
||||||
name=data['slug'], data=data['dashboard'])
|
name=data['slug'], data=data['dashboard'])
|
||||||
self.assertEqual(mock_requests.call_count, 3)
|
self.assertEqual(mock_requests.call_count, 3)
|
||||||
|
|
||||||
|
@ -99,7 +100,7 @@ class TestCaseGrafana(TestCase):
|
||||||
},
|
},
|
||||||
"slug": 'new-dashboard',
|
"slug": 'new-dashboard',
|
||||||
}
|
}
|
||||||
self.grafana.create_dashboard(
|
self.grafana.dashboard.create(
|
||||||
name=data['slug'], data=data['dashboard'], overwrite=True)
|
name=data['slug'], data=data['dashboard'], overwrite=True)
|
||||||
self.assertEqual(mock_requests.call_count, 2)
|
self.assertEqual(mock_requests.call_count, 2)
|
||||||
|
|
||||||
|
@ -115,7 +116,7 @@ class TestCaseGrafana(TestCase):
|
||||||
"slug": 'new-dashboard',
|
"slug": 'new-dashboard',
|
||||||
}
|
}
|
||||||
self.assertRaises(
|
self.assertRaises(
|
||||||
Exception, self.grafana.create_dashboard, name=data['slug'],
|
Exception, self.grafana.dashboard.create, name=data['slug'],
|
||||||
data=data['dashboard'], overwrite=False)
|
data=data['dashboard'], overwrite=False)
|
||||||
|
|
||||||
self.assertEqual(mock_requests.call_count, 1)
|
self.assertEqual(mock_requests.call_count, 1)
|
||||||
|
|
Loading…
Reference in New Issue