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:
Paul Belanger 2015-10-09 13:50:43 -04:00
parent 4e643e5d06
commit 3a32d511bb
6 changed files with 69 additions and 34 deletions

View File

@ -6,3 +6,7 @@ API Reference
.. automodule:: grafana_dashboards.grafana
:members:
:undoc-members:
.. automodule:: grafana_dashboards.grafana.dashboard
:members:
:undoc-members:

View File

@ -74,7 +74,7 @@ class Builder(object):
for name in dashboards:
data, md5 = self.parser.get_dashboard(name)
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)
else:
LOG.debug("'%s' has not changed" % name)

View File

@ -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)

View File

@ -19,33 +19,14 @@ try:
except ImportError:
from urlparse import urljoin
import requests
from requests import exceptions
class Grafana(object):
class Dashboard(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
"""
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 __init__(self, url, session):
self.url = url
self.session = session
def assert_dashboard_exists(self, name):
"""Raise an exception if dashboard does not exist
@ -58,7 +39,7 @@ class Grafana(object):
if not self.is_dashboard(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
:param name: URL friendly title of the dashboard
@ -85,7 +66,7 @@ class Grafana(object):
res.raise_for_status()
self.assert_dashboard_exists(name)
def get_dashboard(self, name):
def get(self, name):
"""Get a dashboard
:param name: URL friendly title of the dashboard
@ -113,7 +94,7 @@ class Grafana(object):
:rtype: bool
"""
res = self.get_dashboard(name)
res = self.get(name)
if res and res['meta']['slug'] == name:
return True
return False

View File

@ -26,7 +26,7 @@ class TestCaseBuilder(TestCase):
super(TestCaseBuilder, self).setUp()
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):
dashboard = os.path.join(
os.path.dirname(__file__), 'fixtures/builder/dashboard-0001.yaml')

View File

@ -49,12 +49,12 @@ class TestCaseGrafana(TestCase):
def test_init(self):
grafana = Grafana(self.url)
self.assertNotIn('Authorization', grafana.session.headers)
self.assertNotIn('Authorization', grafana.dashboard.session.headers)
def test_init_apikey(self):
apikey = 'eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk'
grafana = Grafana(self.url, apikey)
headers = grafana.session.headers
headers = grafana.dashboard.session.headers
self.assertIn('Authorization', headers)
self.assertEqual(headers['Authorization'], 'Bearer %s' % apikey)
@ -64,7 +64,8 @@ class TestCaseGrafana(TestCase):
'/api/dashboards/db/new-dashboard', json=DASHBOARD_NOT_FOUND,
status_code=404)
self.assertRaises(
Exception, self.grafana.assert_dashboard_exists, 'new-dashboard')
Exception, self.grafana.dashboard.assert_dashboard_exists,
'new-dashboard')
@requests_mock.Mocker()
def test_create_dashboard_new(self, mock_requests):
@ -84,7 +85,7 @@ class TestCaseGrafana(TestCase):
},
"slug": 'new-dashboard',
}
self.grafana.create_dashboard(
self.grafana.dashboard.create(
name=data['slug'], data=data['dashboard'])
self.assertEqual(mock_requests.call_count, 3)
@ -99,7 +100,7 @@ class TestCaseGrafana(TestCase):
},
"slug": 'new-dashboard',
}
self.grafana.create_dashboard(
self.grafana.dashboard.create(
name=data['slug'], data=data['dashboard'], overwrite=True)
self.assertEqual(mock_requests.call_count, 2)
@ -115,7 +116,7 @@ class TestCaseGrafana(TestCase):
"slug": 'new-dashboard',
}
self.assertRaises(
Exception, self.grafana.create_dashboard, name=data['slug'],
Exception, self.grafana.dashboard.create, name=data['slug'],
data=data['dashboard'], overwrite=False)
self.assertEqual(mock_requests.call_count, 1)