diff --git a/consumer_api/app.py b/consumer_api/app.py index dbedea3..60c5a5a 100644 --- a/consumer_api/app.py +++ b/consumer_api/app.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 import sentry_sdk +from sentry_sdk.integrations.flask import FlaskIntegration from flask import Flask import os from db import redis_client @@ -14,11 +15,22 @@ __copyright__ = "Copyright 2020, GoldenPogácsa Team" __module_name__ = "app" __version__text__ = "1" -sentry_sdk.init("https://0a106e104e114bc9a3fa47f9cb0db2f4@sentry.kmlabz.com/10") +# Setup sentry +SENTRY_DSN = os.environ.get("SENTRY_DSN") +if SENTRY_DSN: + sentry_sdk.init( + dsn=SENTRY_DSN, + integrations=[FlaskIntegration()], + send_default_pii=True, + release=os.environ.get('RELEASE_ID', 'test'), + environment=os.environ.get('RELEASEMODE', 'dev') + ) app = Flask(__name__) app.config['REDIS_URL'] = os.environ['REDIS_URL'] app.config['LOCAL_UUID'] = os.environ['LOCAL_UUID'] +app.config['CUSTOMER_TIMEOUT'] = int(os.environ.get('CUSTOMER_TIMEOUT', 30)) +app.config['PRODUCER_TIMEOUT'] = int(os.environ.get('PRODUCER_TIMEOUT', 60)) redis_client.init_app(app) diff --git a/consumer_api/tests/__init__.py b/consumer_api/tests/__init__.py new file mode 100644 index 0000000..883d9c7 --- /dev/null +++ b/consumer_api/tests/__init__.py @@ -0,0 +1,6 @@ +import os + +# Setup environment variables for testing +os.environ["INITIAL_SERVERS"] = "192.168.111.22" +os.environ["LOCAL_UUID"] = "d8b2e5e2-f675-4194-9324-af58e4b70c54" +os.environ["REDIS_URL"] = "redis://192.168.111.121/0" diff --git a/consumer_api/tests/consumers_view_test.py b/consumer_api/tests/consumers_view_test.py new file mode 100644 index 0000000..dbb1b3d --- /dev/null +++ b/consumer_api/tests/consumers_view_test.py @@ -0,0 +1,17 @@ +import pytest + +from flask import current_app + + +@pytest.fixture +def client(): + current_app.config['TESTING'] = True + + with current_app.test_client() as client: + yield client + + +def test_response_length(client): + r = client.get('/consumers') + + assert len(r) == 0 diff --git a/consumer_api/tests/log_view_test.py b/consumer_api/tests/log_view_test.py new file mode 100644 index 0000000..195be38 --- /dev/null +++ b/consumer_api/tests/log_view_test.py @@ -0,0 +1,30 @@ +import json +import os + +import pytest + +from flask import current_app + + +@pytest.fixture +def client(): + current_app.config['TESTING'] = True + + with current_app.test_client() as client: + yield client + + +def test_log_code_get(client): + r = client.get('/log') + + assert r.status_code == 405 + + +def test_log_code_post(client): + data = { + "uuid": os.environ["LOCAL_UUID"], + "message": "Hello There!" + } + r = client.post('/log', data = json.dumps(data)) + + assert r.status_code == 204 diff --git a/consumer_api/tests/sync_view_test.py b/consumer_api/tests/sync_view_test.py new file mode 100644 index 0000000..a7839f0 --- /dev/null +++ b/consumer_api/tests/sync_view_test.py @@ -0,0 +1,30 @@ +import json +import os + +import pytest + +from flask import current_app + + +@pytest.fixture +def client(): + current_app.config['TESTING'] = True + + with current_app.test_client() as client: + yield client + + +def test_log_code_get(client): + r = client.get('/sync') + + assert r.status_code == 405 + + +def test_log_code_post(client): + data = { + "uuid": os.environ["LOCAL_UUID"], + "message": "Hello There!" + } + r = client.post('/sync', data = json.dumps(data)) + + assert r.status_code == 204 diff --git a/consumer_api/views/consumers_view.py b/consumer_api/views/consumers_view.py index 98349c6..9888702 100644 --- a/consumer_api/views/consumers_view.py +++ b/consumer_api/views/consumers_view.py @@ -8,11 +8,12 @@ from flask_classful import FlaskView class ConsumersView(FlaskView): def get(self): - # load the currently available consumer list from the redis database - consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8')) + keys = redis_client.keys('consumer_*') - # jsonify and return the list of active consumers - if 'full' in request.args: - return jsonify(consumer_list) - else: - return jsonify([v['ip'] for k, v in consumer_list.items()]) + list_of_customer_ips = [] + + for key in keys: + info = json.loads((redis_client.get(key) or b"{}").decode('utf-8')) + list_of_customer_ips.append(info['ip']) + + return jsonify(list_of_customer_ips) diff --git a/consumer_api/views/log_view.py b/consumer_api/views/log_view.py index 69a08c3..9b0491c 100644 --- a/consumer_api/views/log_view.py +++ b/consumer_api/views/log_view.py @@ -19,15 +19,16 @@ class LogView(FlaskView): if not last_known_remote_ip: current_app.logger.info(f"New producer {remote_uuid} at {remote_ip}") elif last_known_remote_ip != remote_ip: - current_app.logger.info(f"IP address of producer {remote_uuid} have changed: {last_known_remote_ip} -> {remote_ip}") + current_app.logger.info( + f"IP address of producer {remote_uuid} have changed: {last_known_remote_ip} -> {remote_ip}") # update expirity redis_client.set(prod_key, remote_ip.encode('utf-8')) - redis_client.expire(prod_key, 240) + redis_client.expire(prod_key, current_app.config["PRODUCER_TIMEOUT"]) # print out message current_app.logger.info(f"New message: {request.json['message']}") # return HTTP 204 - No content message - return Response(status=204) + return Response(status = 204) diff --git a/consumer_api/views/sync_view.py b/consumer_api/views/sync_view.py index 3c2f771..bfd860c 100644 --- a/consumer_api/views/sync_view.py +++ b/consumer_api/views/sync_view.py @@ -1,4 +1,5 @@ import json +import time from flask import request, current_app, jsonify from flask_classful import FlaskView from db import redis_client @@ -10,22 +11,25 @@ class SyncView(FlaskView): remote_uuid = request.json['uuid'] remote_ip = request.remote_addr - # load the currently available consumer list from the redis database - consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8')) + cust_key = f"consumer_{remote_uuid}" - if remote_uuid not in consumer_list.keys(): - # display newly registered consumer + last_known_info = json.loads((redis_client.get(cust_key) or b"{}").decode('utf-8')) + + if not last_known_info: current_app.logger.info(f"New consumer registered (unknown UUID): {remote_uuid} at {remote_ip}") + else: - if consumer_list[remote_uuid]['ip'] != remote_ip: - # log address changes + if last_known_info['ip'] != remote_ip: current_app.logger.info(f"Address of consumer {remote_uuid} changed to {remote_ip}") - # update consumer list redis databasse - consumer_list.update( - {remote_uuid: {"ip": remote_ip}} - ) - redis_client.set("consumer_list", json.dumps(consumer_list).encode('utf-8')) + info = { + "uuid": remote_uuid, + "ip": remote_ip, + "last_seen": time.time() + } + + redis_client.set(cust_key, json.dumps(info).encode('utf-8')) + redis_client.expire(cust_key, current_app.config["CUSTOMER_TIMEOUT"]) # return with the current UUID response = {