From d7580eae2259e68308b6a6a294e938b6ad80709a Mon Sep 17 00:00:00 2001 From: Ubuntu <jorge.sasiain@ehu.eus> Date: Wed, 29 Mar 2023 14:25:13 +0000 Subject: [PATCH] Fix missing config.load in get_known_routers; add size to string-type columns in db; read alembic db uri from settings file --- .gitignore | 2 + docs/source/_static/openapi.json | 390 ++++++++++++++++++ resource_management/alembic.ini | 2 +- resource_management/migrations/env.py | 7 + .../versions/1ec562888fd6_initial_tables.py | 6 +- resource_management/routes/interfaces.py | 4 + 6 files changed, 407 insertions(+), 4 deletions(-) create mode 100644 docs/source/_static/openapi.json diff --git a/.gitignore b/.gitignore index 754ad9d..6cf2af7 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ coverage.xml docs/build .vscode + +config.json diff --git a/docs/source/_static/openapi.json b/docs/source/_static/openapi.json new file mode 100644 index 0000000..51b197c --- /dev/null +++ b/docs/source/_static/openapi.json @@ -0,0 +1,390 @@ +{ + "openapi": "3.0.2", + "info": { + "title": "FastAPI", + "version": "0.1.0" + }, + "paths": { + "/api/version": { + "get": { + "summary": "Version", + "operationId": "version_api_version_get", + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Version" + } + } + } + } + } + } + }, + "/api/interfaces/initialize-router/{fqdn}": { + "post": { + "summary": "Load New Router Interfaces", + "operationId": "load_new_router_interfaces_api_interfaces_initialize_router__fqdn__post", + "parameters": [ + { + "required": true, + "schema": { + "title": "Fqdn", + "type": "string" + }, + "name": "fqdn", + "in": "path" + } + ], + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/InterfacesSummary" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } + }, + "/api/interfaces/reconcile-router/{fqdn}": { + "post": { + "summary": "Reconcile Current Router Interfaces", + "operationId": "reconcile_current_router_interfaces_api_interfaces_reconcile_router__fqdn__post", + "parameters": [ + { + "required": true, + "schema": { + "title": "Fqdn", + "type": "string" + }, + "name": "fqdn", + "in": "path" + } + ], + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": {} + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } + }, + "/api/interfaces/routers": { + "get": { + "summary": "Get Known Routers", + "operationId": "get_known_routers_api_interfaces_routers_get", + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ListOfRouters" + } + } + } + } + } + } + }, + "/api/interfaces/next-lag/{fqdn}": { + "post": { + "summary": "Reserve Next Lag", + "description": "compute the next available lag name for the given router\n\nTODO: _next_lag_name is a placeholder for\n whatever logic turns out to be right", + "operationId": "reserve_next_lag_api_interfaces_next_lag__fqdn__post", + "parameters": [ + { + "required": true, + "schema": { + "title": "Fqdn", + "type": "string" + }, + "name": "fqdn", + "in": "path" + } + ], + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/NextLAG" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } + }, + "/api/interfaces/next-physical/{fqdn}/{lag_name}": { + "post": { + "summary": "Reserve Physical Bundle Member", + "description": "compute the next available lag name for the given router\n\nTODO: _find_available_physical is a placeholder for\n whatever logic turns out to be right (e.g. speeds, etc)", + "operationId": "reserve_physical_bundle_member_api_interfaces_next_physical__fqdn___lag_name__post", + "parameters": [ + { + "required": true, + "schema": { + "title": "Fqdn", + "type": "string" + }, + "name": "fqdn", + "in": "path" + }, + { + "required": true, + "schema": { + "title": "Lag Name", + "type": "string" + }, + "name": "lag_name", + "in": "path" + } + ], + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/NextPhysicalInterface" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } + } + }, + "components": { + "schemas": { + "HTTPValidationError": { + "title": "HTTPValidationError", + "type": "object", + "properties": { + "detail": { + "title": "Detail", + "type": "array", + "items": { + "$ref": "#/components/schemas/ValidationError" + } + } + } + }, + "InterfaceCounts": { + "title": "InterfaceCounts", + "required": [ + "total", + "available" + ], + "type": "object", + "properties": { + "total": { + "title": "Total", + "type": "integer" + }, + "available": { + "title": "Available", + "type": "integer" + } + } + }, + "InterfacesSummary": { + "title": "InterfacesSummary", + "required": [ + "fqdn", + "lag", + "physical" + ], + "type": "object", + "properties": { + "fqdn": { + "title": "Fqdn", + "type": "string" + }, + "lag": { + "title": "Lag", + "type": "integer" + }, + "physical": { + "$ref": "#/components/schemas/InterfaceCounts" + } + } + }, + "ListOfRouters": { + "title": "ListOfRouters", + "required": [ + "routers" + ], + "type": "object", + "properties": { + "routers": { + "title": "Routers", + "type": "array", + "items": { + "$ref": "#/components/schemas/RouterDetails" + } + } + } + }, + "NextLAG": { + "title": "NextLAG", + "required": [ + "fqdn", + "name" + ], + "type": "object", + "properties": { + "fqdn": { + "title": "Fqdn", + "type": "string" + }, + "name": { + "title": "Name", + "type": "string" + } + } + }, + "NextPhysicalInterface": { + "title": "NextPhysicalInterface", + "required": [ + "fqdn", + "lag", + "name" + ], + "type": "object", + "properties": { + "fqdn": { + "title": "Fqdn", + "type": "string" + }, + "lag": { + "title": "Lag", + "type": "string" + }, + "name": { + "title": "Name", + "type": "string" + } + } + }, + "RouterDetails": { + "title": "RouterDetails", + "required": [ + "fqdn" + ], + "type": "object", + "properties": { + "fqdn": { + "title": "Fqdn", + "type": "string" + } + } + }, + "ValidationError": { + "title": "ValidationError", + "required": [ + "loc", + "msg", + "type" + ], + "type": "object", + "properties": { + "loc": { + "title": "Location", + "type": "array", + "items": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "integer" + } + ] + } + }, + "msg": { + "title": "Message", + "type": "string" + }, + "type": { + "title": "Error Type", + "type": "string" + } + } + }, + "Version": { + "title": "Version", + "required": [ + "api", + "module" + ], + "type": "object", + "properties": { + "api": { + "title": "Api", + "pattern": "\\d+\\.\\d+", + "type": "string" + }, + "module": { + "title": "Module", + "pattern": "\\d+\\.\\d+", + "type": "string" + } + } + } + } + } +} \ No newline at end of file diff --git a/resource_management/alembic.ini b/resource_management/alembic.ini index 22d9b90..64ca481 100644 --- a/resource_management/alembic.ini +++ b/resource_management/alembic.ini @@ -3,6 +3,6 @@ # make sure the right line is un / commented depending on which schema you want # a migration for script_location = migrations -sqlalchemy.url = sqlite:////absolute/path/to/foo.db +sqlalchemy.url = %(DB_URI)s # or here is an example of a mysql dsn # sqlalchemy.url = mysql://dummy:dummy-pass@localhost/resources_db_name diff --git a/resource_management/migrations/env.py b/resource_management/migrations/env.py index dd3e851..dd251e8 100644 --- a/resource_management/migrations/env.py +++ b/resource_management/migrations/env.py @@ -5,6 +5,8 @@ from sqlalchemy import pool from alembic import context +from resource_management import config as _app_config + # this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config @@ -25,6 +27,11 @@ target_metadata = None # my_important_option = config.get_main_option("my_important_option") # ... etc. +app_config = _app_config.load() +db_uri = app_config["db"] +section = config.config_ini_section +config.set_section_option(section, "DB_URI", db_uri) + def run_migrations_offline() -> None: """Run migrations in 'offline' mode. diff --git a/resource_management/migrations/versions/1ec562888fd6_initial_tables.py b/resource_management/migrations/versions/1ec562888fd6_initial_tables.py index 34fabf1..50249f4 100644 --- a/resource_management/migrations/versions/1ec562888fd6_initial_tables.py +++ b/resource_management/migrations/versions/1ec562888fd6_initial_tables.py @@ -26,7 +26,7 @@ def upgrade() -> None: op.create_table( 'lags', sa.Column('id', sa.Integer, primary_key=True), - sa.Column('name', sa.String, nullable=False), + sa.Column('name', sa.String(256), nullable=False), sa.Column( 'availability', sa.Enum('AVAILABLE', 'USED', 'RESERVED', name='lag_availability'), @@ -40,7 +40,7 @@ def upgrade() -> None: op.create_table( 'physical_interfaces', sa.Column('id', sa.Integer, primary_key=True), - sa.Column('name', sa.String, nullable=False), + sa.Column('name', sa.String(256), nullable=False), sa.Column( 'availability', sa.Enum('AVAILABLE', 'USED', 'RESERVED', name='lag_availability'), @@ -59,7 +59,7 @@ def upgrade() -> None: op.create_table( 'logical_interfaces', sa.Column('id', sa.Integer, primary_key=True), - sa.Column('name', sa.String, nullable=False), + sa.Column('name', sa.String(256), nullable=False), sa.Column( 'physical_id', sa.Integer, diff --git a/resource_management/routes/interfaces.py b/resource_management/routes/interfaces.py index dd0b442..019b8be 100644 --- a/resource_management/routes/interfaces.py +++ b/resource_management/routes/interfaces.py @@ -86,6 +86,10 @@ async def reconcile_current_router_interfaces(fqdn: str): @router.get('/routers') async def get_known_routers() -> ListOfRouters: + + params = config.load() + db.init_db_model(params['db']) + with db.session_scope() as session: rows = session.query(model.Router.fqdn).all() return {'routers': [{'fqdn': r[0]} for r in rows]} -- GitLab