Skip to content

Commit b079412

Browse files
author
Sergio García Prado
committed
ISSUE #54
* Refactor `ApiGatewayConfig` and configuration file structure.
1 parent a98b074 commit b079412

File tree

9 files changed

+43
-161
lines changed

9 files changed

+43
-161
lines changed

minos/api_gateway/rest/cli.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ def start(
3434
typer.echo(f"Error loading config: {exc!r}")
3535
raise typer.Exit(code=1)
3636

37-
services = (
38-
ApiGatewayRestService(address=config.rest.connection.host, port=config.rest.connection.port, config=config),
39-
)
37+
services = (ApiGatewayRestService(address=config.rest.host, port=config.rest.port, config=config),)
4038
try:
4139
EntrypointLauncher(config=config, services=services).launch()
4240
except Exception as exc:

minos/api_gateway/rest/config.py

Lines changed: 8 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,24 @@
1919
ApiGatewayConfigException,
2020
)
2121

22-
CONNECTION = collections.namedtuple("Connection", "host port")
23-
ENDPOINT = collections.namedtuple("Endpoint", "name route method controller action")
24-
REST = collections.namedtuple("Rest", "connection endpoints")
25-
DISCOVERY_CONNECTION = collections.namedtuple("DiscoveryConnection", "host port path")
26-
DATABASE = collections.namedtuple("Database", "host port password")
27-
DISCOVERY = collections.namedtuple("Discovery", "connection endpoints database")
22+
REST = collections.namedtuple("Rest", "host port cors")
23+
DISCOVERY = collections.namedtuple("Discovery", "host port")
2824
CORS = collections.namedtuple("Cors", "enabled")
2925

3026
_ENVIRONMENT_MAPPER = {
3127
"rest.host": "API_GATEWAY_REST_HOST",
3228
"rest.port": "API_GATEWAY_REST_PORT",
33-
"cors.enabled": "API_GATEWAY_CORS_ENABLED",
29+
"rest.cors.enabled": "API_GATEWAY_REST_CORS_ENABLED",
3430
"discovery.host": "DISCOVERY_SERVICE_HOST",
3531
"discovery.port": "DISCOVERY_SERVICE_PORT",
36-
"discovery.db.host": "DISCOVERY_SERVICE_DB_HOST",
37-
"discovery.db.port": "DISCOVERY_SERVICE_DB_PORT",
38-
"discovery.db.password": "DISCOVERY_SERVICE_DB_PASSWORD",
3932
}
4033

4134
_PARAMETERIZED_MAPPER = {
4235
"rest.host": "api_gateway_rest_host",
4336
"rest.port": "api_gateway_rest_port",
44-
"cors.enabled": "api_gateway_cors_enabled",
37+
"rest.cors.enabled": "api_gateway_rest_cors_enabled",
4538
"discovery.host": "discovery_service_host",
4639
"discovery.port": "discovery_service_port",
47-
"discovery.db.host": "discovery_service_db_host",
48-
"discovery.db.port": "discovery_service_db_port",
49-
"discovery.db.password": "discovery_service_db_password",
5040
}
5141

5242

@@ -103,78 +93,20 @@ def rest(self) -> REST:
10393
10494
:return: A ``REST`` NamedTuple instance.
10595
"""
106-
connection = self._rest_connection
107-
endpoints = self._rest_endpoints
108-
return REST(connection=connection, endpoints=endpoints)
96+
return REST(host=self._get("rest.host"), port=int(self._get("rest.port")), cors=self._cors)
10997

11098
@property
111-
def _rest_connection(self):
112-
connection = CONNECTION(host=self._get("rest.host"), port=int(self._get("rest.port")))
113-
return connection
114-
115-
@property
116-
def _rest_endpoints(self) -> list[ENDPOINT]:
117-
info = self._get("rest.endpoints")
118-
endpoints = [self._rest_endpoints_entry(endpoint) for endpoint in info]
119-
return endpoints
120-
121-
@staticmethod
122-
def _rest_endpoints_entry(endpoint: dict[str, t.Any]) -> ENDPOINT:
123-
return ENDPOINT(
124-
name=endpoint["name"],
125-
route=endpoint["route"],
126-
method=endpoint["method"].upper(),
127-
controller=endpoint["controller"],
128-
action=endpoint["action"],
129-
)
130-
131-
@property
132-
def cors(self) -> CORS:
99+
def _cors(self) -> CORS:
133100
"""Get the cors config.
134101
135102
:return: A ``CORS`` NamedTuple instance.
136103
"""
137-
return CORS(enabled=self._get("cors.enabled"))
104+
return CORS(enabled=self._get("rest.cors.enabled"))
138105

139106
@property
140107
def discovery(self) -> DISCOVERY:
141108
"""Get the rest config.
142109
143110
:return: A ``REST`` NamedTuple instance.
144111
"""
145-
connection = self._discovery_connection
146-
endpoints = self._discovery_endpoints
147-
database = self._discovery_database
148-
return DISCOVERY(connection=connection, endpoints=endpoints, database=database)
149-
150-
@property
151-
def _discovery_connection(self):
152-
connection = DISCOVERY_CONNECTION(
153-
host=self._get("discovery.host"), port=int(self._get("discovery.port")), path=self._get("discovery.path")
154-
)
155-
return connection
156-
157-
@property
158-
def _discovery_database(self):
159-
connection = DATABASE(
160-
host=self._get("discovery.db.host"),
161-
port=int(self._get("discovery.db.port")),
162-
password=self._get("discovery.db.password"),
163-
)
164-
return connection
165-
166-
@property
167-
def _discovery_endpoints(self) -> list[ENDPOINT]:
168-
info = self._get("discovery.endpoints")
169-
endpoints = [self._discovery_endpoints_entry(endpoint) for endpoint in info]
170-
return endpoints
171-
172-
@staticmethod
173-
def _discovery_endpoints_entry(endpoint: dict[str, t.Any]) -> ENDPOINT:
174-
return ENDPOINT(
175-
name=endpoint["name"],
176-
route=endpoint["route"],
177-
method=endpoint["method"].upper(),
178-
controller=endpoint["controller"],
179-
action=endpoint["action"],
180-
)
112+
return DISCOVERY(host=self._get("discovery.host"), port=int(self._get("discovery.port")))

minos/api_gateway/rest/handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
async def orchestrate(request: web.Request) -> web.Response:
2727
""" Orchestrate discovery and microservice call """
28-
discovery_host = request.app["config"].discovery.connection.host
29-
discovery_port = request.app["config"].discovery.connection.port
28+
discovery_host = request.app["config"].discovery.host
29+
discovery_port = request.app["config"].discovery.port
3030

3131
verb = request.method
3232
url = f"/{request.match_info['endpoint']}"

minos/api_gateway/rest/service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, address: str, port: int, config: ApiGatewayConfig):
2727

2828
async def create_application(self) -> web.Application:
2929
middlewares = list()
30-
if self.config.cors.enabled:
30+
if self.config.rest.cors.enabled:
3131
middlewares = [cors_middleware(allow_all=True)]
3232

3333
app = web.Application(middlewares=middlewares)

tests/config.yml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
rest:
22
host: localhost
33
port: 55909
4-
endpoints: []
4+
cors:
5+
enabled: false
56
discovery:
67
host: localhost
78
port: 5567
8-
path: ""
9-
endpoints: []
10-
db:
11-
host: localhost
12-
port: 6379
13-
password:
14-
cors:
15-
enabled: false
169

tests/test_api_gateway/test_rest/test_authentication.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@
2626
class TestApiGatewayAuthentication(AioHTTPTestCase):
2727
CONFIG_FILE_PATH = BASE_PATH / "config.yml"
2828

29-
@mock.patch.dict(os.environ, {"API_GATEWAY_CORS_ENABLED": "true"})
29+
@mock.patch.dict(os.environ, {"API_GATEWAY_REST_CORS_ENABLED": "true"})
3030
def setUp(self) -> None:
3131
self.config = ApiGatewayConfig(self.CONFIG_FILE_PATH)
3232

33-
self.discovery = MockServer(
34-
host=self.config.discovery.connection.host, port=self.config.discovery.connection.port,
35-
)
33+
self.discovery = MockServer(host=self.config.discovery.host, port=self.config.discovery.port,)
3634
self.discovery.add_json_response(
3735
"/microservices", {"address": "localhost", "port": "5568", "status": True},
3836
)
@@ -62,7 +60,7 @@ async def get_application(self):
6260
Override the get_app method to return your application.
6361
"""
6462
rest_service = ApiGatewayRestService(
65-
address=self.config.rest.connection.host, port=self.config.rest.connection.port, config=self.config
63+
address=self.config.rest.host, port=self.config.rest.port, config=self.config
6664
)
6765

6866
return await rest_service.create_application()
@@ -102,9 +100,7 @@ class TestAuthUnreachable(AioHTTPTestCase):
102100
def setUp(self) -> None:
103101
self.config = ApiGatewayConfig(self.CONFIG_FILE_PATH)
104102

105-
self.discovery = MockServer(
106-
host=self.config.discovery.connection.host, port=self.config.discovery.connection.port,
107-
)
103+
self.discovery = MockServer(host=self.config.discovery.host, port=self.config.discovery.port,)
108104
self.discovery.add_json_response(
109105
"/microservices", {"address": "localhost", "port": "5568", "status": True},
110106
)
@@ -129,7 +125,7 @@ async def get_application(self):
129125
Override the get_app method to return your application.
130126
"""
131127
rest_service = ApiGatewayRestService(
132-
address=self.config.rest.connection.host, port=self.config.rest.connection.port, config=self.config
128+
address=self.config.rest.host, port=self.config.rest.port, config=self.config
133129
)
134130

135131
return await rest_service.create_application()

tests/test_api_gateway/test_rest/test_config.py

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -32,90 +32,61 @@ def test_config_rest(self):
3232
config = ApiGatewayConfig(path=self.config_file_path)
3333
rest = config.rest
3434

35-
broker = rest.connection
36-
self.assertEqual("localhost", broker.host)
37-
self.assertEqual(55909, broker.port)
35+
self.assertEqual("localhost", rest.host)
36+
self.assertEqual(55909, rest.port)
3837

3938
def test_config_discovery(self):
4039
config = ApiGatewayConfig(path=self.config_file_path)
4140
discovery = config.discovery
4241

43-
conn = discovery.connection
44-
self.assertEqual("localhost", conn.host)
45-
self.assertEqual(5567, conn.port)
46-
47-
db = discovery.database
48-
self.assertEqual("localhost", db.host)
49-
self.assertEqual(6379, db.port)
50-
self.assertEqual(None, db.password)
51-
52-
self.assertEqual("", discovery.connection.path)
42+
self.assertEqual("localhost", discovery.host)
43+
self.assertEqual(5567, discovery.port)
5344

5445
def test_config_cors(self):
5546
config = ApiGatewayConfig(path=self.config_file_path)
56-
cors = config.cors
47+
cors = config.rest.cors
5748

5849
self.assertIsInstance(cors.enabled, bool)
5950
self.assertFalse(cors.enabled)
6051

6152
@mock.patch.dict(os.environ, {"DISCOVERY_SERVICE_HOST": "::1"})
6253
def test_overwrite_with_environment_discovery_host(self):
6354
config = ApiGatewayConfig(path=self.config_file_path)
64-
conn = config.discovery.connection
65-
self.assertEqual("::1", conn.host)
55+
self.assertEqual("::1", config.discovery.host)
6656

6757
@mock.patch.dict(os.environ, {"DISCOVERY_SERVICE_PORT": "4040"})
6858
def test_overwrite_with_environment_discovery_port(self):
6959
config = ApiGatewayConfig(path=self.config_file_path)
70-
conn = config.discovery.connection
71-
self.assertEqual(4040, conn.port)
72-
73-
@mock.patch.dict(os.environ, {"DISCOVERY_SERVICE_DB_HOST": "::1"})
74-
def test_overwrite_with_environment_discovery_db_host(self):
75-
config = ApiGatewayConfig(path=self.config_file_path)
76-
conn = config.discovery.database
77-
self.assertEqual("::1", conn.host)
78-
79-
@mock.patch.dict(os.environ, {"DISCOVERY_SERVICE_DB_PORT": "3030"})
80-
def test_overwrite_with_environment_discovery_db_port(self):
81-
config = ApiGatewayConfig(path=self.config_file_path)
82-
conn = config.discovery.database
83-
self.assertEqual(3030, conn.port)
84-
85-
@mock.patch.dict(os.environ, {"DISCOVERY_SERVICE_DB_PASSWORD": "1234"})
86-
def test_overwrite_with_environment_discovery_db_password(self):
87-
config = ApiGatewayConfig(path=self.config_file_path)
88-
conn = config.discovery.database
89-
self.assertEqual("1234", conn.password)
60+
self.assertEqual(4040, config.discovery.port)
9061

9162
@mock.patch.dict(os.environ, {"API_GATEWAY_REST_HOST": "::1"})
9263
def test_overwrite_with_environment(self):
9364
config = ApiGatewayConfig(path=self.config_file_path)
9465
rest = config.rest
95-
self.assertEqual("::1", rest.connection.host)
66+
self.assertEqual("::1", rest.host)
9667

9768
@mock.patch.dict(os.environ, {"API_GATEWAY_REST_HOST": "::1"})
9869
def test_overwrite_with_environment_false(self):
9970
config = ApiGatewayConfig(path=self.config_file_path, with_environment=False)
10071
rest = config.rest
101-
self.assertEqual("localhost", rest.connection.host)
72+
self.assertEqual("localhost", rest.host)
10273

103-
@mock.patch.dict(os.environ, {"API_GATEWAY_CORS_ENABLED": "false"})
74+
@mock.patch.dict(os.environ, {"API_GATEWAY_REST_CORS_ENABLED": "false"})
10475
def test_overwrite_with_environment_cors(self):
10576
config = ApiGatewayConfig(path=self.config_file_path)
106-
cors = config.cors
77+
cors = config.rest.cors
10778

10879
self.assertIsInstance(cors.enabled, bool)
10980
self.assertFalse(cors.enabled)
11081

11182
def test_overwrite_with_parameter(self):
11283
config = ApiGatewayConfig(path=self.config_file_path, api_gateway_rest_host="::1")
11384
rest = config.rest
114-
self.assertEqual("::1", rest.connection.host)
85+
self.assertEqual("::1", rest.host)
11586

11687
def test_overwrite_with_parameter_cors(self):
117-
config = ApiGatewayConfig(path=self.config_file_path, api_gateway_cors_enabled=False)
118-
cors = config.cors
88+
config = ApiGatewayConfig(path=self.config_file_path, api_gateway_rest_cors_enabled=False)
89+
cors = config.rest.cors
11990

12091
self.assertIsInstance(cors.enabled, bool)
12192
self.assertFalse(cors.enabled)

tests/test_api_gateway/test_rest/test_cors.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@ class TestApiGatewayCORS(AioHTTPTestCase):
3333
TEST_DENIED_ORIGIN = "https://www.google.com"
3434
TEST_ORIGIN = "http://localhost:3000"
3535

36-
@mock.patch.dict(os.environ, {"API_GATEWAY_CORS_ENABLED": "true"})
36+
@mock.patch.dict(os.environ, {"API_GATEWAY_REST_CORS_ENABLED": "true"})
3737
def setUp(self) -> None:
3838
self.config = ApiGatewayConfig(self.CONFIG_FILE_PATH)
3939

40-
self.discovery = MockServer(
41-
host=self.config.discovery.connection.host, port=self.config.discovery.connection.port,
42-
)
40+
self.discovery = MockServer(host=self.config.discovery.host, port=self.config.discovery.port,)
4341
self.discovery.add_json_response(
4442
"/microservices", {"address": "localhost", "port": "5568", "status": True},
4543
)
@@ -64,7 +62,7 @@ async def get_application(self):
6462
Override the get_app method to return your application.
6563
"""
6664
rest_service = ApiGatewayRestService(
67-
address=self.config.rest.connection.host, port=self.config.rest.connection.port, config=self.config
65+
address=self.config.rest.host, port=self.config.rest.port, config=self.config
6866
)
6967

7068
return await rest_service.create_application()

0 commit comments

Comments
 (0)