From f8e6020be698b9265ca864c2ff9c6d32db18673e Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 15 Sep 2020 13:22:28 +0200 Subject: [PATCH 01/13] endpoint versions --- ereuse_devicehub/config.py | 4 ++- .../resources/versions/__init__.py | 0 .../resources/versions/versions.py | 35 +++++++++++++++++++ setup.py | 4 +-- 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 ereuse_devicehub/resources/versions/__init__.py create mode 100644 ereuse_devicehub/resources/versions/versions.py diff --git a/ereuse_devicehub/config.py b/ereuse_devicehub/config.py index b1db6e9e..c8ff9e44 100644 --- a/ereuse_devicehub/config.py +++ b/ereuse_devicehub/config.py @@ -12,6 +12,7 @@ from ereuse_devicehub.resources import action, agent, deliverynote, inventory, \ from ereuse_devicehub.resources.device import definitions from ereuse_devicehub.resources.documents import documents from ereuse_devicehub.resources.enums import PriceSoftware +from ereuse_devicehub.resources.versions import versions class DevicehubConfig(Config): @@ -24,7 +25,8 @@ class DevicehubConfig(Config): import_resource(deliverynote), import_resource(proof), import_resource(documents), - import_resource(inventory)), + import_resource(inventory), + import_resource(versions)), ) PASSWORD_SCHEMES = {'pbkdf2_sha256'} # type: Set[str] SQLALCHEMY_DATABASE_URI = 'postgresql://dhub:ereuse@localhost/devicehub' # type: str diff --git a/ereuse_devicehub/resources/versions/__init__.py b/ereuse_devicehub/resources/versions/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ereuse_devicehub/resources/versions/versions.py b/ereuse_devicehub/resources/versions/versions.py new file mode 100644 index 00000000..516a08c6 --- /dev/null +++ b/ereuse_devicehub/resources/versions/versions.py @@ -0,0 +1,35 @@ +import flask +import teal.marshmallow +import ereuse_utils + +from typing import Callable, Iterable, Tuple +from flask import make_response, g +from teal.resource import Resource + + +class VersionDef(Resource): + __type__ = 'Version' + SCHEMA = None + VIEW = None # We do not want to create default / documents endpoint + AUTH = False + + def __init__(self, app, + import_name=__name__, + static_url_path=None, + url_prefix=None, + subdomain=None, + url_defaults=None, + root_path=None, + cli_commands: Iterable[Tuple[Callable, str or None]] = tuple()): + + super().__init__(app, import_name, static_url_path, + url_prefix, subdomain, url_defaults, root_path, cli_commands) + d = {'id': None} + get = {'GET'} + + self.add_url_rule('/', defaults=d, view_func=self.view, methods=get) + + def view(self): + #import pdb; pdb.set_trace() + v = "{}".format(ereuse_utils.version('ereuse-devicehub')) + return {'devicehub': v} diff --git a/setup.py b/setup.py index 2ef5907b..5d7871a4 100644 --- a/setup.py +++ b/setup.py @@ -9,10 +9,10 @@ test_requires = [ setup( name='ereuse-devicehub', - version='1.0a1', + version='0.1.0a1', url='https://github.com/ereuse/devicehub-teal', project_urls={ - 'Documentation': 'http://devicheub.ereuse.org', + 'Documentation': 'http://devicehub.ereuse.org', 'Code': 'http://github.com/ereuse/devicehub-teal', 'Issue tracker': 'https://tree.taiga.io/project/ereuseorg-devicehub/issues?q=rules' }, From 5cb0dad77d00bf1c9c7a875aef1453bd2bad535f Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 15 Sep 2020 17:44:12 +0200 Subject: [PATCH 02/13] add VersionView as view --- .../resources/versions/versions.py | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/ereuse_devicehub/resources/versions/versions.py b/ereuse_devicehub/resources/versions/versions.py index 516a08c6..9f29668a 100644 --- a/ereuse_devicehub/resources/versions/versions.py +++ b/ereuse_devicehub/resources/versions/versions.py @@ -1,10 +1,18 @@ import flask +import json import teal.marshmallow import ereuse_utils from typing import Callable, Iterable, Tuple from flask import make_response, g -from teal.resource import Resource +from teal.resource import Resource, View + + +class VersionView(View): + def get(self, *args, **kwargs): + """Get version.""" + v = "{}".format(ereuse_utils.version('ereuse-devicehub')) + return json.dumps({'devicehub': v}) class VersionDef(Resource): @@ -15,21 +23,19 @@ class VersionDef(Resource): def __init__(self, app, import_name=__name__, + static_folder='static', static_url_path=None, + template_folder='templates', url_prefix=None, subdomain=None, url_defaults=None, root_path=None, cli_commands: Iterable[Tuple[Callable, str or None]] = tuple()): - - super().__init__(app, import_name, static_url_path, + super().__init__(app, import_name, static_folder, static_url_path, template_folder, url_prefix, subdomain, url_defaults, root_path, cli_commands) - d = {'id': None} + + d = {'devicehub': '0.1.0a'} get = {'GET'} - self.add_url_rule('/', defaults=d, view_func=self.view, methods=get) - - def view(self): - #import pdb; pdb.set_trace() - v = "{}".format(ereuse_utils.version('ereuse-devicehub')) - return {'devicehub': v} + version_view = VersionView.as_view('stockDocumentView', definition=self) + self.add_url_rule('/', defaults=d, view_func=version_view, methods=get) From 255701467add05159351b8ac0924768306af6e68 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 16 Sep 2020 17:40:16 +0200 Subject: [PATCH 03/13] fixing name of VersionView --- ereuse_devicehub/resources/versions/versions.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ereuse_devicehub/resources/versions/versions.py b/ereuse_devicehub/resources/versions/versions.py index 9f29668a..20052e44 100644 --- a/ereuse_devicehub/resources/versions/versions.py +++ b/ereuse_devicehub/resources/versions/versions.py @@ -1,7 +1,7 @@ import flask import json import teal.marshmallow -import ereuse_utils +import pkg_resources from typing import Callable, Iterable, Tuple from flask import make_response, g @@ -11,7 +11,8 @@ from teal.resource import Resource, View class VersionView(View): def get(self, *args, **kwargs): """Get version.""" - v = "{}".format(ereuse_utils.version('ereuse-devicehub')) + + v = "{}".format(pkg_resources.require('ereuse-devicehub')[0].version) return json.dumps({'devicehub': v}) @@ -37,5 +38,5 @@ class VersionDef(Resource): d = {'devicehub': '0.1.0a'} get = {'GET'} - version_view = VersionView.as_view('stockDocumentView', definition=self) + version_view = VersionView.as_view('VersionView', definition=self) self.add_url_rule('/', defaults=d, view_func=version_view, methods=get) From 51f51b1e92a28a1a563ecf8fd09db9e930388f1d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 17 Sep 2020 21:09:22 +0200 Subject: [PATCH 04/13] get version of ereuse-tag --- .../resources/versions/versions.py | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/ereuse_devicehub/resources/versions/versions.py b/ereuse_devicehub/resources/versions/versions.py index 20052e44..17dea637 100644 --- a/ereuse_devicehub/resources/versions/versions.py +++ b/ereuse_devicehub/resources/versions/versions.py @@ -1,19 +1,35 @@ import flask import json +import requests import teal.marshmallow import pkg_resources from typing import Callable, Iterable, Tuple +from urllib.parse import urlparse from flask import make_response, g from teal.resource import Resource, View +from ereuse_devicehub.resources.inventory.model import Inventory + + +def get_tag_version(): + """Get version of microservice ereuse-tag.""" + path = "/versions/version/" + url = urlparse(Inventory.current.tag_provider.to_text())._replace(path=path) + res = requests.get(url.geturl()) + if res.status_code == 200: + return json.loads(res.content) + else: + return {} class VersionView(View): def get(self, *args, **kwargs): - """Get version.""" - - v = "{}".format(pkg_resources.require('ereuse-devicehub')[0].version) - return json.dumps({'devicehub': v}) + """Get version of DeviceHub and ereuse-tag.""" + dh_version = pkg_resources.require('ereuse-devicehub')[0].version + tag_version = get_tag_version() + versions = {'devicehub': dh_version} + versions.update(tag_version) + return json.dumps(versions) class VersionDef(Resource): From c21d54dcb5ad26fcbdecf902cff23e0dc17b3f93 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 17 Sep 2020 21:48:57 +0200 Subject: [PATCH 05/13] adding version in __init__.py instead of setup.py --- ereuse_devicehub/__init__.py | 1 + setup.py | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/ereuse_devicehub/__init__.py b/ereuse_devicehub/__init__.py index e69de29b..7574bfd1 100644 --- a/ereuse_devicehub/__init__.py +++ b/ereuse_devicehub/__init__.py @@ -0,0 +1 @@ +__version__ = "1.0a1" diff --git a/setup.py b/setup.py index 5d7871a4..618caa30 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,11 @@ -from pathlib import Path +import re +from pathlib import Path from setuptools import find_packages, setup +with open("ereuse_devicehub/__init__.py", encoding="utf8") as f: + version = re.search(r'__version__ = "(.*?)"', f.read()).group(1) + test_requires = [ 'pytest', 'requests_mock' @@ -9,7 +13,7 @@ test_requires = [ setup( name='ereuse-devicehub', - version='0.1.0a1', + version=version, url='https://github.com/ereuse/devicehub-teal', project_urls={ 'Documentation': 'http://devicehub.ereuse.org', From 3d171626f3bb65e0a8caf44c0446904ba6d4cf87 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 18 Sep 2020 11:05:42 +0200 Subject: [PATCH 06/13] adding logger info when the connection is fail --- .../resources/versions/versions.py | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/ereuse_devicehub/resources/versions/versions.py b/ereuse_devicehub/resources/versions/versions.py index 17dea637..6ff50d35 100644 --- a/ereuse_devicehub/resources/versions/versions.py +++ b/ereuse_devicehub/resources/versions/versions.py @@ -12,11 +12,17 @@ from teal.resource import Resource, View from ereuse_devicehub.resources.inventory.model import Inventory -def get_tag_version(): +def get_tag_version(app): """Get version of microservice ereuse-tag.""" + path = "/versions/version/" url = urlparse(Inventory.current.tag_provider.to_text())._replace(path=path) - res = requests.get(url.geturl()) + try: + res = requests.get(url.geturl()) + except requests.exceptions.ConnectionError: + app.logger.error("The microservice Tags is down!!") + return {} + if res.status_code == 200: return json.loads(res.content) else: @@ -25,9 +31,10 @@ def get_tag_version(): class VersionView(View): def get(self, *args, **kwargs): """Get version of DeviceHub and ereuse-tag.""" + dh_version = pkg_resources.require('ereuse-devicehub')[0].version - tag_version = get_tag_version() - versions = {'devicehub': dh_version} + tag_version = get_tag_version(self.resource_def.app) + versions = {'devicehub': dh_version, "ereuse_tag": "0.0.0"} versions.update(tag_version) return json.dumps(versions) @@ -40,9 +47,9 @@ class VersionDef(Resource): def __init__(self, app, import_name=__name__, - static_folder='static', + static_folder=None, static_url_path=None, - template_folder='templates', + template_folder=None, url_prefix=None, subdomain=None, url_defaults=None, @@ -51,7 +58,7 @@ class VersionDef(Resource): super().__init__(app, import_name, static_folder, static_url_path, template_folder, url_prefix, subdomain, url_defaults, root_path, cli_commands) - d = {'devicehub': '0.1.0a'} + d = {'devicehub': '1.0a'} get = {'GET'} version_view = VersionView.as_view('VersionView', definition=self) From aaaf1180286c401f573dbe14ecc1f3521cb1f4cf Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 18 Sep 2020 11:32:20 +0200 Subject: [PATCH 07/13] adding test of get version --- tests/test_endpoints.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py index 6c15c050..33abc009 100644 --- a/tests/test_endpoints.py +++ b/tests/test_endpoints.py @@ -1,8 +1,10 @@ import datetime +import pkg_resources +import pytest + from uuid import UUID from flask import g -import pytest from colour import Color from ereuse_utils.naming import Naming from ereuse_utils.test import ANY @@ -116,6 +118,16 @@ def test_users(user: UserClient, client: Client): assert res.status_code == 405 +@pytest.mark.mvp +def test_get_version(app: Devicehub, client: Client): + """Checks GETting versions of services.""" + + content, res = client.get("/versions/", None) + dh_version = pkg_resources.require('ereuse-devicehub')[0].version + version = {'devicehub': dh_version, 'ereuse_tag': '0.0.0'} + assert res.status_code == 200 + assert content == version + @pytest.mark.mvp def test_get_device(app: Devicehub, user: UserClient, user2: UserClient): """Checks GETting a d.Desktop with its components.""" From 773bf3a200dc6b02b823b7ffbe5503acb72bb2be Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 21 Sep 2020 11:14:50 +0200 Subject: [PATCH 08/13] fixing test_basic --- tests/test_basic.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_basic.py b/tests/test_basic.py index dc0ce248..fe3dd1da 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -99,7 +99,8 @@ def test_api_docs(client: Client): '/video-scalers/{id}/merge/', '/videoconferences/{id}/merge/', '/videos/{id}/merge/', - '/wireless-access-points/{id}/merge/' + '/wireless-access-points/{id}/merge/', + '/versions/' } assert docs['info'] == {'title': 'Devicehub', 'version': '0.2'} assert docs['components']['securitySchemes']['bearerAuth'] == { From ea6691247a451a05dc4ee64da48075bf02650559 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 21 Sep 2020 11:34:55 +0200 Subject: [PATCH 09/13] change pkg_resources for open __init__ --- ereuse_devicehub/resources/versions/versions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ereuse_devicehub/resources/versions/versions.py b/ereuse_devicehub/resources/versions/versions.py index 6ff50d35..dd30869f 100644 --- a/ereuse_devicehub/resources/versions/versions.py +++ b/ereuse_devicehub/resources/versions/versions.py @@ -1,8 +1,8 @@ +import re import flask import json import requests import teal.marshmallow -import pkg_resources from typing import Callable, Iterable, Tuple from urllib.parse import urlparse @@ -31,8 +31,8 @@ def get_tag_version(app): class VersionView(View): def get(self, *args, **kwargs): """Get version of DeviceHub and ereuse-tag.""" - - dh_version = pkg_resources.require('ereuse-devicehub')[0].version + with open("ereuse_devicehub/__init__.py", encoding="utf8") as f: + dh_version = re.search(r'__version__ = "(.*?)"', f.read()).group(1) tag_version = get_tag_version(self.resource_def.app) versions = {'devicehub': dh_version, "ereuse_tag": "0.0.0"} versions.update(tag_version) From 7f9be17c92addb8e02d9b298c44402dd6d81a34f Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 21 Sep 2020 11:46:02 +0200 Subject: [PATCH 10/13] change pkg_resources for open __init__ in test too --- tests/test_endpoints.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py index 33abc009..a10989ac 100644 --- a/tests/test_endpoints.py +++ b/tests/test_endpoints.py @@ -123,7 +123,8 @@ def test_get_version(app: Devicehub, client: Client): """Checks GETting versions of services.""" content, res = client.get("/versions/", None) - dh_version = pkg_resources.require('ereuse-devicehub')[0].version + with open("ereuse_devicehub/__init__.py", encoding="utf8") as f: + dh_version = re.search(r'__version__ = "(.*?)"', f.read()).group(1) version = {'devicehub': dh_version, 'ereuse_tag': '0.0.0'} assert res.status_code == 200 assert content == version From 66f963e90e04ff06cc4e2243c2c7c1c5d746feaa Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 21 Sep 2020 11:48:48 +0200 Subject: [PATCH 11/13] bugfix adding re in test --- tests/test_endpoints.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py index a10989ac..eb38db9c 100644 --- a/tests/test_endpoints.py +++ b/tests/test_endpoints.py @@ -1,5 +1,5 @@ import datetime -import pkg_resources +import re import pytest from uuid import UUID From 6c457547108b2552ae8e574fbfc0184261ee2374 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 22 Sep 2020 11:36:01 +0200 Subject: [PATCH 12/13] fixing the default version --- ereuse_devicehub/resources/versions/versions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ereuse_devicehub/resources/versions/versions.py b/ereuse_devicehub/resources/versions/versions.py index dd30869f..bcdfca06 100644 --- a/ereuse_devicehub/resources/versions/versions.py +++ b/ereuse_devicehub/resources/versions/versions.py @@ -58,7 +58,7 @@ class VersionDef(Resource): super().__init__(app, import_name, static_folder, static_url_path, template_folder, url_prefix, subdomain, url_defaults, root_path, cli_commands) - d = {'devicehub': '1.0a'} + d = {'devicehub': '0.0.0'} get = {'GET'} version_view = VersionView.as_view('VersionView', definition=self) From fed6b57bbeefd3652aed6e8809a99cd4dbfa7bdd Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 22 Sep 2020 11:57:39 +0200 Subject: [PATCH 13/13] import __version__ instead of open file __init__ --- ereuse_devicehub/resources/versions/versions.py | 9 ++++----- setup.py | 7 ++----- tests/test_endpoints.py | 7 +++---- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/ereuse_devicehub/resources/versions/versions.py b/ereuse_devicehub/resources/versions/versions.py index bcdfca06..f2b8d393 100644 --- a/ereuse_devicehub/resources/versions/versions.py +++ b/ereuse_devicehub/resources/versions/versions.py @@ -1,4 +1,3 @@ -import re import flask import json import requests @@ -10,6 +9,7 @@ from flask import make_response, g from teal.resource import Resource, View from ereuse_devicehub.resources.inventory.model import Inventory +from ereuse_devicehub import __version__ def get_tag_version(app): @@ -31,10 +31,9 @@ def get_tag_version(app): class VersionView(View): def get(self, *args, **kwargs): """Get version of DeviceHub and ereuse-tag.""" - with open("ereuse_devicehub/__init__.py", encoding="utf8") as f: - dh_version = re.search(r'__version__ = "(.*?)"', f.read()).group(1) + tag_version = get_tag_version(self.resource_def.app) - versions = {'devicehub': dh_version, "ereuse_tag": "0.0.0"} + versions = {'devicehub': __version__, "ereuse_tag": "0.0.0"} versions.update(tag_version) return json.dumps(versions) @@ -58,7 +57,7 @@ class VersionDef(Resource): super().__init__(app, import_name, static_folder, static_url_path, template_folder, url_prefix, subdomain, url_defaults, root_path, cli_commands) - d = {'devicehub': '0.0.0'} + d = {'devicehub': __version__, "ereuse_tag": "0.0.0"} get = {'GET'} version_view = VersionView.as_view('VersionView', definition=self) diff --git a/setup.py b/setup.py index 618caa30..6a0db9b9 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,7 @@ -import re - from pathlib import Path from setuptools import find_packages, setup +from ereuse_devicehub import __version__ -with open("ereuse_devicehub/__init__.py", encoding="utf8") as f: - version = re.search(r'__version__ = "(.*?)"', f.read()).group(1) test_requires = [ 'pytest', @@ -13,7 +10,7 @@ test_requires = [ setup( name='ereuse-devicehub', - version=version, + version=__version__, url='https://github.com/ereuse/devicehub-teal', project_urls={ 'Documentation': 'http://devicehub.ereuse.org', diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py index eb38db9c..9c74a139 100644 --- a/tests/test_endpoints.py +++ b/tests/test_endpoints.py @@ -1,5 +1,4 @@ import datetime -import re import pytest from uuid import UUID @@ -13,6 +12,7 @@ from sqlalchemy.util import OrderedSet from teal.db import ResourceNotFound from teal.enums import Layouts +from ereuse_devicehub import __version__ from ereuse_devicehub.client import Client, UserClient from ereuse_devicehub.db import db from ereuse_devicehub.devicehub import Devicehub @@ -123,9 +123,8 @@ def test_get_version(app: Devicehub, client: Client): """Checks GETting versions of services.""" content, res = client.get("/versions/", None) - with open("ereuse_devicehub/__init__.py", encoding="utf8") as f: - dh_version = re.search(r'__version__ = "(.*?)"', f.read()).group(1) - version = {'devicehub': dh_version, 'ereuse_tag': '0.0.0'} + + version = {'devicehub': __version__, 'ereuse_tag': '0.0.0'} assert res.status_code == 200 assert content == version