Browse Source

Merge "Fix bug when passing url and key into grafana"

Jenkins 3 years ago
parent
commit
57e4e17f20
3 changed files with 12 additions and 5 deletions
  1. 2
    1
      grafana_dashboards/builder.py
  2. 5
    4
      grafana_dashboards/grafana/__init__.py
  3. 5
    0
      tests/test_builder.py

+ 2
- 1
grafana_dashboards/builder.py View File

@@ -29,7 +29,8 @@ class Builder(object):
29 29
             config.get('cache', 'cachedir'),
30 30
             config.getboolean('cache', 'enabled'))
31 31
         self.grafana = Grafana(
32
-            config.get('grafana', 'apikey'), config.get('grafana', 'url'))
32
+            url=config.get('grafana', 'url'),
33
+            key=config.get('grafana', 'apikey'))
33 34
         self.parser = YamlParser()
34 35
 
35 36
     def delete_dashboard(self, path):

+ 5
- 4
grafana_dashboards/grafana/__init__.py View File

@@ -33,8 +33,10 @@ class Grafana(object):
33 33
         :type key: str
34 34
 
35 35
         """
36
+        self.server = url
37
+        self.auth = None
36 38
 
37
-        base_url = urljoin(url, 'api/dashboards/db/')
39
+        base_url = urljoin(self.server, 'api/dashboards/db/')
38 40
         session = requests.Session()
39 41
         session.headers.update({
40 42
             'Content-Type': 'application/json',
@@ -42,8 +44,7 @@ class Grafana(object):
42 44
         # NOTE(pabelanger): Grafana 2.1.0 added basic auth support so now the
43 45
         # api key is optional.
44 46
         if key:
45
-            session.headers.update({
46
-                'Authorization': 'Bearer %s' % key,
47
-            })
47
+            self.auth = {'Authorization': 'Bearer %s' % key}
48
+            session.headers.update(self.auth)
48 49
 
49 50
         self.dashboard = Dashboard(base_url, session)

+ 5
- 0
tests/test_builder.py View File

@@ -39,6 +39,11 @@ class TestCaseBuilder(TestCase):
39 39
         builder2.delete_dashboard(path)
40 40
         self.assertEqual(mock_grafana.call_count, 1)
41 41
 
42
+    def test_grafana_defaults(self):
43
+        self.assertEqual(
44
+            self.builder.grafana.server, 'http://grafana.example.org')
45
+        self.assertEqual(self.builder.grafana.auth, None)
46
+
42 47
     @mock.patch('grafana_dashboards.grafana.Dashboard.create')
43 48
     def test_update_dashboard(self, mock_grafana):
44 49
         path = os.path.join(

Loading…
Cancel
Save