From ec196db4205943f95f87934e7d31342b42d38ec4 Mon Sep 17 00:00:00 2001 From: Authapon Kongkaew Date: Sat, 10 Aug 2019 10:41:11 +0700 Subject: [PATCH 1/2] Refactor code --- GrafanaSnapshot/Base.py | 15 -------- GrafanaSnapshot/DeleteSnapshot.py | 21 ----------- GrafanaSnapshot/__init__.py | 3 +- GrafanaSnapshot/feature/__init__.py | 2 ++ GrafanaSnapshot/feature/base.py | 3 ++ .../snapshots.py} | 26 +++++++++++--- GrafanaSnapshot/snapshot_face.py | 36 +++++++++++++++++++ Pipfile | 1 - README.md | 13 +++---- requirements.txt | 1 - setup.py | 2 +- ...eteSnapshot.py => test_delete_snapshot.py} | 19 +++++----- ...eSnapshot.py => test_generate_snapshot.py} | 15 +++----- 13 files changed, 85 insertions(+), 72 deletions(-) delete mode 100644 GrafanaSnapshot/Base.py delete mode 100644 GrafanaSnapshot/DeleteSnapshot.py create mode 100644 GrafanaSnapshot/feature/__init__.py create mode 100644 GrafanaSnapshot/feature/base.py rename GrafanaSnapshot/{GenerateSnapshot.py => feature/snapshots.py} (72%) create mode 100644 GrafanaSnapshot/snapshot_face.py rename tests/{test_deleteSnapshot.py => test_delete_snapshot.py} (67%) rename tests/{test_generateSnapshot.py => test_generate_snapshot.py} (85%) diff --git a/GrafanaSnapshot/Base.py b/GrafanaSnapshot/Base.py deleted file mode 100644 index 1d7545f..0000000 --- a/GrafanaSnapshot/Base.py +++ /dev/null @@ -1,15 +0,0 @@ -from grafana_api.grafana_face import GrafanaFace - - -class Base(object): - def __init__(self, auth, host, port, protocol): - """ - Init auth - :param auth: API Token (https://grafana.com/docs/http_api/auth/#create-api-token) - :param host: Host of the API server Ex. 127.0.0.1 - :param port: Ex. 3000 (default port) - :param protocol: http or https - .. code-block:: python - grafana = GenerateSnapshot(auth='', host='xx', port=3000, protocol="https") - """ - self.api = GrafanaFace(auth=auth, host=host, port=port, protocol=protocol, url_path_prefix='', verify=False) diff --git a/GrafanaSnapshot/DeleteSnapshot.py b/GrafanaSnapshot/DeleteSnapshot.py deleted file mode 100644 index dbb6938..0000000 --- a/GrafanaSnapshot/DeleteSnapshot.py +++ /dev/null @@ -1,21 +0,0 @@ -from .Base import Base - - -class DeleteSnapshot(Base): - - def delete(self, delete_key=None, key=None): - - """ - - Delete snapshot with delete_key or snapshot key - :param delete_key: - :param key: - :return: - """ - - if delete_key: - return self.api.snapshots.delete_snapshot_by_delete_key(delete_key) - elif key: - return self.api.snapshots.delete_snapshot_by_key(key) - else: - return None diff --git a/GrafanaSnapshot/__init__.py b/GrafanaSnapshot/__init__.py index b427243..0d3c03d 100644 --- a/GrafanaSnapshot/__init__.py +++ b/GrafanaSnapshot/__init__.py @@ -1,3 +1,2 @@ # -*- coding: utf-8 -*- -from .GenerateSnapshot import GenerateSnapshot -from .DeleteSnapshot import DeleteSnapshot +from .snapshot_face import SnapshotFace diff --git a/GrafanaSnapshot/feature/__init__.py b/GrafanaSnapshot/feature/__init__.py new file mode 100644 index 0000000..0cfdb3d --- /dev/null +++ b/GrafanaSnapshot/feature/__init__.py @@ -0,0 +1,2 @@ +from .base import Base +from .snapshots import Snapshots diff --git a/GrafanaSnapshot/feature/base.py b/GrafanaSnapshot/feature/base.py new file mode 100644 index 0000000..cec1c1c --- /dev/null +++ b/GrafanaSnapshot/feature/base.py @@ -0,0 +1,3 @@ +class Base(object): + def __init__(self, api): + self.api = api diff --git a/GrafanaSnapshot/GenerateSnapshot.py b/GrafanaSnapshot/feature/snapshots.py similarity index 72% rename from GrafanaSnapshot/GenerateSnapshot.py rename to GrafanaSnapshot/feature/snapshots.py index 33e7c91..92dc9e4 100644 --- a/GrafanaSnapshot/GenerateSnapshot.py +++ b/GrafanaSnapshot/feature/snapshots.py @@ -1,13 +1,12 @@ -from .Base import Base +from .base import Base import urllib3 import datetime - urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) -class GenerateSnapshot(Base): +class Snapshots(Base): - def generate(self, tags, time_from, time_to, expires=300): + def create_snapshot(self, tags, time_from, time_to, expires=300): """ Generate Grafana snapshot with expires @@ -17,7 +16,7 @@ def generate(self, tags, time_from, time_to, expires=300): :param expires: :return: """ - + dashboards_info = self.api.search.search_dashboards(tag=tags) dashboards = {} for dashboard_info in dashboards_info: @@ -40,6 +39,23 @@ def generate(self, tags, time_from, time_to, expires=300): return snapshot_list + def delete(self, delete_key=None, key=None): + + """ + + Delete snapshot with delete_key or snapshot key + :param delete_key: + :param key: + :return: + """ + + if delete_key: + return self.api.snapshots.delete_snapshot_by_delete_key(delete_key) + elif key: + return self.api.snapshots.delete_snapshot_by_key(key) + else: + return None + @staticmethod def __time_str_from_unix_ms(unix_ms): return datetime.datetime.utcfromtimestamp(int(unix_ms / 1000)).strftime("%Y-%m-%dT%H:%M:%S.000Z") diff --git a/GrafanaSnapshot/snapshot_face.py b/GrafanaSnapshot/snapshot_face.py new file mode 100644 index 0000000..72a751d --- /dev/null +++ b/GrafanaSnapshot/snapshot_face.py @@ -0,0 +1,36 @@ +from grafana_api.grafana_face import GrafanaFace + +from .feature import ( + Snapshots, +) + + +class SnapshotFace: + def __init__( + self, + auth, + host="localhost", + port=None, + url_path_prefix="", + protocol="https", + verify=False, + ): + """ + Init auth + :param auth: API Token (https://grafana.com/docs/http_api/auth/#create-api-token) + :param host: Host of the API server Ex. 127.0.0.1 + :param port: Ex. 3000 (default port) + :param protocol: http or https + .. code-block:: python + grafana = GenerateSnapshot(auth='', host='xx', port=3000, protocol="https") + """ + self.api = GrafanaFace( + auth, + host=host, + port=port, + url_path_prefix=url_path_prefix, + protocol=protocol, + verify=verify, + ) + + self.snapshots = Snapshots(self.api) diff --git a/Pipfile b/Pipfile index 65fdddd..f90f757 100644 --- a/Pipfile +++ b/Pipfile @@ -11,4 +11,3 @@ coverage = "~=4.5" mock = {version = "*", markers = "python_version <= '2.7'"} pylint = ">=1.9" requests-mock = "~=1.6" -unittest-xml-reporting = "~=2.5" diff --git a/README.md b/README.md index 4788f54..950a797 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# GrafanaSnapshot Package [![Build Status](https://travis-ci.com/ascendcorp/GrafanaSnapshot.svg?branch=master)](https://travis-ci.com/ascendcorp/GrafanaSnapshot) +# grafana-snapshot Package [![Build Status](https://travis-ci.com/ascendcorp/grafana-snapshot.svg?branch=master)](https://travis-ci.com/ascendcorp/grafana-snapshot) ## Install @@ -12,12 +12,13 @@ You can test by creating a python file: from GrafanaSnapshot.GenerateSnapshot import GenerateSnapshot if __name__ == "__main__": - grafana = GenerateSnapshot(auth='', host='xx', port=3000, protocol="https") - result = grafana.generate(tags="tags", time_from=1563183710618, time_to=1563185212275) - + grafana = SnapshotFace(auth='xxxxx', host='localhost', port='3000', protocol='https') + + ## Create snaphot + results = grafana.snapshots.create_snapshot(tags="test_tag", time_from=1563183710618, time_to=1563185212275) + ## Delete snaphot by key - grafana = DeleteSnapshot(auth='xxxxx', host='localhost', port='3000', protocol='http') - result = grafana.delete(delete_key='some_delete_key', key=None) + result = grafana.snapshots.delete(delete_key='some_delete_key', key=None) ``` diff --git a/requirements.txt b/requirements.txt index c29b944..7ef1114 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ -unittest-xml-reporting urllib3 requests pytest diff --git a/setup.py b/setup.py index c945a87..c0124e2 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ def get_version(): license="MIT", packages=find_packages(), install_requires=['requests', 'grafana_api'], - tests_require=['tox', 'coverage', 'wheel', 'requests_mock', 'xmlrunner', 'pytest', 'unittest-xml-reporting'], + tests_require=['tox', 'coverage', 'wheel', 'requests_mock', 'xmlrunner', 'pytest'], classifiers=[ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", diff --git a/tests/test_deleteSnapshot.py b/tests/test_delete_snapshot.py similarity index 67% rename from tests/test_deleteSnapshot.py rename to tests/test_delete_snapshot.py index 170aa75..f1cb381 100644 --- a/tests/test_deleteSnapshot.py +++ b/tests/test_delete_snapshot.py @@ -1,7 +1,6 @@ import unittest - import requests_mock -from GrafanaSnapshot.DeleteSnapshot import DeleteSnapshot +from GrafanaSnapshot.snapshot_face import SnapshotFace class TestDeleteSnapshot(unittest.TestCase): @@ -13,8 +12,8 @@ def test_delete_snapshot_with_delete_key(self, m): json="{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}" ) - grafana = DeleteSnapshot(auth='xxxxx', host='localhost', port='3000', protocol='http') - result = grafana.delete(delete_key='some_delete_key', key=None) + grafana = SnapshotFace(auth='xxxxx', host='localhost', port='3000', protocol='http') + result = grafana.snapshots.delete(delete_key='some_delete_key', key=None) expected = "{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}" self.assertEqual(result, expected) @@ -26,8 +25,8 @@ def test_delete_snapshot_with_key(self, m): json="{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}" ) - grafana = DeleteSnapshot(auth='xxxxx', host='localhost', port='3000', protocol='http') - result = grafana.delete(delete_key=None, key='some_key') + grafana = SnapshotFace(auth='xxxxx', host='localhost', port='3000', protocol='http') + result = grafana.snapshots.delete(delete_key=None, key='some_key') expected = "{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}" self.assertEqual(result, expected) @@ -39,13 +38,13 @@ def test_delete_snapshot_with_both(self, m): json="{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}" ) - grafana = DeleteSnapshot(auth='xxxxx', host='localhost', port='3000', protocol='http') - result = grafana.delete(delete_key='some_delete_key', key='some_key') + grafana = SnapshotFace(auth='xxxxx', host='localhost', port='3000', protocol='http') + result = grafana.snapshots.delete(delete_key='some_delete_key', key='some_key') expected = "{'message': \"Snapshot deleted. It might take an hour before it\'s cleared from any CDN caches.\"}" self.assertEqual(result, expected) def test_delete_snapshot_without_key_and_delete_key(self): - grafana = DeleteSnapshot(auth='xxxxx', host='localhost', port='3000', protocol='http') + grafana = SnapshotFace(auth='xxxxx', host='localhost', port='3000', protocol='http') - self.assertIsNone(grafana.delete()) + self.assertIsNone(grafana.snapshots.delete()) diff --git a/tests/test_generateSnapshot.py b/tests/test_generate_snapshot.py similarity index 85% rename from tests/test_generateSnapshot.py rename to tests/test_generate_snapshot.py index 336bf44..2010bc1 100644 --- a/tests/test_generateSnapshot.py +++ b/tests/test_generate_snapshot.py @@ -1,7 +1,6 @@ import unittest - import requests_mock -from GrafanaSnapshot.GenerateSnapshot import GenerateSnapshot +from GrafanaSnapshot.snapshot_face import SnapshotFace class TestGenerateSnapshot(unittest.TestCase): @@ -57,8 +56,8 @@ def test_generate(self, m): }, ) - grafana = GenerateSnapshot(auth="xxxxx", port=3000, host="localhost", protocol="http") - results = grafana.generate(tags="test_tag", time_from=1563183710618, time_to=1563185212275) + grafana = SnapshotFace(auth="xxxxx", port=3000, host="localhost", protocol="http") + results = grafana.snapshots.create_snapshot(tags="test_tag", time_from=1563183710618, time_to=1563185212275) self.assertEqual(len(results), 1) @requests_mock.Mocker() @@ -111,11 +110,7 @@ def test_generate_with_expire(self, m): }, ) - grafana = GenerateSnapshot(auth="xxxxx", port=3000, host="localhost", protocol="http") - results = grafana.generate(tags="test_tag", time_from=1563183710618, time_to=1563185212275, expires=500) + grafana = SnapshotFace(auth="xxxxx", port=3000, host="localhost", protocol="http") + results = grafana.snapshots.create_snapshot(tags="test_tag", time_from=1563183710618, time_to=1563185212275, expires=500) self.assertEqual(len(results), 1) - -if __name__ == "__main__": - import xmlrunner - unittest.main(testRunner=xmlrunner.XMLTestRunner(output="test-reports")) From e6c248cd9297e4b23b80b5e56ec61b802585e497 Mon Sep 17 00:00:00 2001 From: Authapon Kongkaew Date: Sat, 10 Aug 2019 10:43:56 +0700 Subject: [PATCH 2/2] remove xmlrunner --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index c0124e2..ad564dd 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ def get_version(): license="MIT", packages=find_packages(), install_requires=['requests', 'grafana_api'], - tests_require=['tox', 'coverage', 'wheel', 'requests_mock', 'xmlrunner', 'pytest'], + tests_require=['tox', 'coverage', 'wheel', 'requests_mock', 'pytest'], classifiers=[ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License",