Skip to content

Commit ba5d58a

Browse files
authored
Merge pull request #5 from ascendcorp/feature/refactor-structure
Feature/refactor structure
2 parents d810dbc + e6c248c commit ba5d58a

File tree

13 files changed

+85
-72
lines changed

13 files changed

+85
-72
lines changed

GrafanaSnapshot/Base.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

GrafanaSnapshot/DeleteSnapshot.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

GrafanaSnapshot/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
# -*- coding: utf-8 -*-
2-
from .GenerateSnapshot import GenerateSnapshot
3-
from .DeleteSnapshot import DeleteSnapshot
2+
from .snapshot_face import SnapshotFace

GrafanaSnapshot/feature/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .base import Base
2+
from .snapshots import Snapshots

GrafanaSnapshot/feature/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Base(object):
2+
def __init__(self, api):
3+
self.api = api

GrafanaSnapshot/GenerateSnapshot.py renamed to GrafanaSnapshot/feature/snapshots.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
from .Base import Base
1+
from .base import Base
22
import urllib3
33
import datetime
4-
54
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
65

76

8-
class GenerateSnapshot(Base):
7+
class Snapshots(Base):
98

10-
def generate(self, tags, time_from, time_to, expires=300):
9+
def create_snapshot(self, tags, time_from, time_to, expires=300):
1110

1211
"""
1312
Generate Grafana snapshot with expires
@@ -17,7 +16,7 @@ def generate(self, tags, time_from, time_to, expires=300):
1716
:param expires:
1817
:return:
1918
"""
20-
19+
2120
dashboards_info = self.api.search.search_dashboards(tag=tags)
2221
dashboards = {}
2322
for dashboard_info in dashboards_info:
@@ -40,6 +39,23 @@ def generate(self, tags, time_from, time_to, expires=300):
4039

4140
return snapshot_list
4241

42+
def delete(self, delete_key=None, key=None):
43+
44+
"""
45+
46+
Delete snapshot with delete_key or snapshot key
47+
:param delete_key:
48+
:param key:
49+
:return:
50+
"""
51+
52+
if delete_key:
53+
return self.api.snapshots.delete_snapshot_by_delete_key(delete_key)
54+
elif key:
55+
return self.api.snapshots.delete_snapshot_by_key(key)
56+
else:
57+
return None
58+
4359
@staticmethod
4460
def __time_str_from_unix_ms(unix_ms):
4561
return datetime.datetime.utcfromtimestamp(int(unix_ms / 1000)).strftime("%Y-%m-%dT%H:%M:%S.000Z")

GrafanaSnapshot/snapshot_face.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from grafana_api.grafana_face import GrafanaFace
2+
3+
from .feature import (
4+
Snapshots,
5+
)
6+
7+
8+
class SnapshotFace:
9+
def __init__(
10+
self,
11+
auth,
12+
host="localhost",
13+
port=None,
14+
url_path_prefix="",
15+
protocol="https",
16+
verify=False,
17+
):
18+
"""
19+
Init auth
20+
:param auth: API Token (https://grafana.com/docs/http_api/auth/#create-api-token)
21+
:param host: Host of the API server Ex. 127.0.0.1
22+
:param port: Ex. 3000 (default port)
23+
:param protocol: http or https
24+
.. code-block:: python
25+
grafana = GenerateSnapshot(auth='', host='xx', port=3000, protocol="https")
26+
"""
27+
self.api = GrafanaFace(
28+
auth,
29+
host=host,
30+
port=port,
31+
url_path_prefix=url_path_prefix,
32+
protocol=protocol,
33+
verify=verify,
34+
)
35+
36+
self.snapshots = Snapshots(self.api)

Pipfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ coverage = "~=4.5"
1111
mock = {version = "*", markers = "python_version <= '2.7'"}
1212
pylint = ">=1.9"
1313
requests-mock = "~=1.6"
14-
unittest-xml-reporting = "~=2.5"

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# GrafanaSnapshot Package [![Build Status](https://travis-ci.com/ascendcorp/GrafanaSnapshot.svg?branch=master)](https://travis-ci.com/ascendcorp/GrafanaSnapshot)
1+
# grafana-snapshot Package [![Build Status](https://travis-ci.com/ascendcorp/grafana-snapshot.svg?branch=master)](https://travis-ci.com/ascendcorp/grafana-snapshot)
22

33

44
## Install
@@ -12,12 +12,13 @@ You can test by creating a python file:
1212
from GrafanaSnapshot.GenerateSnapshot import GenerateSnapshot
1313

1414
if __name__ == "__main__":
15-
grafana = GenerateSnapshot(auth='', host='xx', port=3000, protocol="https")
16-
result = grafana.generate(tags="tags", time_from=1563183710618, time_to=1563185212275)
17-
15+
grafana = SnapshotFace(auth='xxxxx', host='localhost', port='3000', protocol='https')
16+
17+
## Create snaphot
18+
results = grafana.snapshots.create_snapshot(tags="test_tag", time_from=1563183710618, time_to=1563185212275)
19+
1820
## Delete snaphot by key
19-
grafana = DeleteSnapshot(auth='xxxxx', host='localhost', port='3000', protocol='http')
20-
result = grafana.delete(delete_key='some_delete_key', key=None)
21+
result = grafana.snapshots.delete(delete_key='some_delete_key', key=None)
2122
```
2223

2324

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
unittest-xml-reporting
21
urllib3
32
requests
43
pytest

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def get_version():
3232
license="MIT",
3333
packages=find_packages(),
3434
install_requires=['requests', 'grafana_api'],
35-
tests_require=['tox', 'coverage', 'wheel', 'requests_mock', 'xmlrunner', 'pytest', 'unittest-xml-reporting'],
35+
tests_require=['tox', 'coverage', 'wheel', 'requests_mock', 'pytest'],
3636
classifiers=[
3737
"Programming Language :: Python :: 3",
3838
"License :: OSI Approved :: MIT License",

tests/test_deleteSnapshot.py renamed to tests/test_delete_snapshot.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import unittest
2-
32
import requests_mock
4-
from GrafanaSnapshot.DeleteSnapshot import DeleteSnapshot
3+
from GrafanaSnapshot.snapshot_face import SnapshotFace
54

65

76
class TestDeleteSnapshot(unittest.TestCase):
@@ -13,8 +12,8 @@ def test_delete_snapshot_with_delete_key(self, m):
1312
json="{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}"
1413
)
1514

16-
grafana = DeleteSnapshot(auth='xxxxx', host='localhost', port='3000', protocol='http')
17-
result = grafana.delete(delete_key='some_delete_key', key=None)
15+
grafana = SnapshotFace(auth='xxxxx', host='localhost', port='3000', protocol='http')
16+
result = grafana.snapshots.delete(delete_key='some_delete_key', key=None)
1817

1918
expected = "{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}"
2019
self.assertEqual(result, expected)
@@ -26,8 +25,8 @@ def test_delete_snapshot_with_key(self, m):
2625
json="{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}"
2726
)
2827

29-
grafana = DeleteSnapshot(auth='xxxxx', host='localhost', port='3000', protocol='http')
30-
result = grafana.delete(delete_key=None, key='some_key')
28+
grafana = SnapshotFace(auth='xxxxx', host='localhost', port='3000', protocol='http')
29+
result = grafana.snapshots.delete(delete_key=None, key='some_key')
3130

3231
expected = "{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}"
3332
self.assertEqual(result, expected)
@@ -39,13 +38,13 @@ def test_delete_snapshot_with_both(self, m):
3938
json="{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}"
4039
)
4140

42-
grafana = DeleteSnapshot(auth='xxxxx', host='localhost', port='3000', protocol='http')
43-
result = grafana.delete(delete_key='some_delete_key', key='some_key')
41+
grafana = SnapshotFace(auth='xxxxx', host='localhost', port='3000', protocol='http')
42+
result = grafana.snapshots.delete(delete_key='some_delete_key', key='some_key')
4443

4544
expected = "{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}"
4645
self.assertEqual(result, expected)
4746

4847
def test_delete_snapshot_without_key_and_delete_key(self):
49-
grafana = DeleteSnapshot(auth='xxxxx', host='localhost', port='3000', protocol='http')
48+
grafana = SnapshotFace(auth='xxxxx', host='localhost', port='3000', protocol='http')
5049

51-
self.assertIsNone(grafana.delete())
50+
self.assertIsNone(grafana.snapshots.delete())

tests/test_generateSnapshot.py renamed to tests/test_generate_snapshot.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import unittest
2-
32
import requests_mock
4-
from GrafanaSnapshot.GenerateSnapshot import GenerateSnapshot
3+
from GrafanaSnapshot.snapshot_face import SnapshotFace
54

65

76
class TestGenerateSnapshot(unittest.TestCase):
@@ -57,8 +56,8 @@ def test_generate(self, m):
5756
},
5857
)
5958

60-
grafana = GenerateSnapshot(auth="xxxxx", port=3000, host="localhost", protocol="http")
61-
results = grafana.generate(tags="test_tag", time_from=1563183710618, time_to=1563185212275)
59+
grafana = SnapshotFace(auth="xxxxx", port=3000, host="localhost", protocol="http")
60+
results = grafana.snapshots.create_snapshot(tags="test_tag", time_from=1563183710618, time_to=1563185212275)
6261
self.assertEqual(len(results), 1)
6362

6463
@requests_mock.Mocker()
@@ -111,11 +110,7 @@ def test_generate_with_expire(self, m):
111110
},
112111
)
113112

114-
grafana = GenerateSnapshot(auth="xxxxx", port=3000, host="localhost", protocol="http")
115-
results = grafana.generate(tags="test_tag", time_from=1563183710618, time_to=1563185212275, expires=500)
113+
grafana = SnapshotFace(auth="xxxxx", port=3000, host="localhost", protocol="http")
114+
results = grafana.snapshots.create_snapshot(tags="test_tag", time_from=1563183710618, time_to=1563185212275, expires=500)
116115
self.assertEqual(len(results), 1)
117116

118-
119-
if __name__ == "__main__":
120-
import xmlrunner
121-
unittest.main(testRunner=xmlrunner.XMLTestRunner(output="test-reports"))

0 commit comments

Comments
 (0)