From 49ddb7ebd779ad86f66006c73726db2a62d20111 Mon Sep 17 00:00:00 2001 From: Jacob Boddey Date: Thu, 27 Jul 2023 07:28:04 +0100 Subject: [PATCH 1/3] Allow CORS --- framework/python/src/api/api.py | 13 ++++++++++++- framework/python/src/common/device.py | 1 + framework/python/src/core/testrun.py | 3 ++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/framework/python/src/api/api.py b/framework/python/src/api/api.py index d877a5b33..a6ef9c965 100644 --- a/framework/python/src/api/api.py +++ b/framework/python/src/api/api.py @@ -13,6 +13,7 @@ # limitations under the License. from fastapi import FastAPI, APIRouter, Response, Request, status +from fastapi.middleware.cors import CORSMiddleware import json from json import JSONDecodeError import psutil @@ -52,8 +53,18 @@ def __init__(self, test_run): self._router.add_api_route("/devices", self.get_devices) self._router.add_api_route("/device", self.save_device, methods=["POST"]) + # TODO: Make this configurable in system.json + origins = ["http://localhost:4200"] + self._app = FastAPI() self._app.include_router(self._router) + self._app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], + ) self._api_thread = threading.Thread(target=self._start, name="Test Run API", @@ -65,7 +76,7 @@ def start(self): LOGGER.info("API waiting for requests") def _start(self): - uvicorn.run(self._app, log_config=None) + uvicorn.run(self._app, log_config=None, port=3000) def stop(self): LOGGER.info("Stopping API") diff --git a/framework/python/src/common/device.py b/framework/python/src/common/device.py index b70099519..46927f793 100644 --- a/framework/python/src/common/device.py +++ b/framework/python/src/common/device.py @@ -20,6 +20,7 @@ class Device(): """Represents a physical device and it's configuration.""" + folder_url: str = None mac_addr: str = None manufacturer: str = None model: str = None diff --git a/framework/python/src/core/testrun.py b/framework/python/src/core/testrun.py index 6016fbfe7..84b2fee5c 100644 --- a/framework/python/src/core/testrun.py +++ b/framework/python/src/core/testrun.py @@ -121,7 +121,8 @@ def _load_devices(self, device_dir): mac_addr = device_config_json.get(DEVICE_MAC_ADDR) test_modules = device_config_json.get(DEVICE_TEST_MODULES) - device = Device(manufacturer=device_manufacturer, + device = Device(folder_url=os.path.join(device_dir, device_folder), + manufacturer=device_manufacturer, model=device_model, mac_addr=mac_addr, test_modules=test_modules) From e057afeabfa0338bf1ad716d45e5ec636a0eefff Mon Sep 17 00:00:00 2001 From: Jacob Boddey Date: Thu, 27 Jul 2023 08:08:35 +0100 Subject: [PATCH 2/3] Fix add device --- framework/python/src/core/testrun.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/python/src/core/testrun.py b/framework/python/src/core/testrun.py index 84b2fee5c..7aab943ff 100644 --- a/framework/python/src/core/testrun.py +++ b/framework/python/src/core/testrun.py @@ -251,7 +251,7 @@ def _device_discovered(self, mac_addr): f'Discovered {device.manufacturer} {device.model} on the network') else: device = Device(mac_addr=mac_addr) - self._devices.append(device) + self.get_session().add_device(device) LOGGER.info( f'A new device has been discovered with mac address {mac_addr}') From 2712ccc40e39b7cc218e1dca617cd5c4e596c08d Mon Sep 17 00:00:00 2001 From: Jacob Boddey Date: Tue, 15 Aug 2023 15:18:36 +0100 Subject: [PATCH 3/3] Configurable API port --- framework/python/src/api/api.py | 2 +- framework/python/src/common/session.py | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/framework/python/src/api/api.py b/framework/python/src/api/api.py index 30f606b7b..379652574 100644 --- a/framework/python/src/api/api.py +++ b/framework/python/src/api/api.py @@ -76,7 +76,7 @@ def start(self): LOGGER.info("API waiting for requests") def _start(self): - uvicorn.run(self._app, log_config=None, port=3000) + uvicorn.run(self._app, log_config=None, port=self._session.get_api_port()) def stop(self): LOGGER.info("Stopping API") diff --git a/framework/python/src/common/session.py b/framework/python/src/common/session.py index 13e4b09fb..1eef8b98d 100644 --- a/framework/python/src/common/session.py +++ b/framework/python/src/common/session.py @@ -25,6 +25,7 @@ MONITOR_PERIOD_KEY = 'monitor_period' STARTUP_TIMEOUT_KEY = 'startup_timeout' LOG_LEVEL_KEY = 'log_level' +API_PORT_KEY = 'api_port' MAX_DEVICE_REPORTS_KEY = 'max_device_reports' class TestRunSession(): @@ -68,7 +69,8 @@ def _get_default_config(self): 'startup_timeout': 60, 'monitor_period': 30, 'runtime': 120, - 'max_device_reports': 5 + 'max_device_reports': 5, + 'api_port': 8000 } def get_config(self): @@ -101,6 +103,9 @@ def _load_config(self): if LOG_LEVEL_KEY in config_file_json: self._config[LOG_LEVEL_KEY] = config_file_json.get(LOG_LEVEL_KEY) + if API_PORT_KEY in config_file_json: + self._config[API_PORT_KEY] = config_file_json.get(API_PORT_KEY) + if MAX_DEVICE_REPORTS_KEY in config_file_json: self._config[MAX_DEVICE_REPORTS_KEY] = config_file_json.get(MAX_DEVICE_REPORTS_KEY) @@ -132,6 +137,9 @@ def get_monitor_period(self): def get_startup_timeout(self): return self._config.get(STARTUP_TIMEOUT_KEY) + def get_api_port(self): + return self._config.get(API_PORT_KEY) + def get_max_device_reports(self): return self._config.get(MAX_DEVICE_REPORTS_KEY)