From 484d09cfbc68b1171128138305a6e5d6c98c710f Mon Sep 17 00:00:00 2001 From: Gonzalo Rafuls Date: Fri, 10 Jan 2025 15:04:38 +0100 Subject: [PATCH] feat: added endpoint for free vlans Change-Id: I449298583aa23413bfda694538ed3ea285c74cf5 --- docs/quads-api.md | 2 ++ src/quads/server/blueprints/vlans.py | 10 ++++++++++ src/quads/server/dao/vlan.py | 17 +++++++++++++---- src/quads/server/swagger.yaml | 20 ++++++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/docs/quads-api.md b/docs/quads-api.md index a9e9f300..368e3fee 100644 --- a/docs/quads-api.md +++ b/docs/quads-api.md @@ -105,6 +105,8 @@ curl -X POST -u $USERNAME:$PASSWORD -H 'accept: application/json' 'http://localh - `/api/v3/schedules` Retrieve a list of all schedules - `/api/v3/available` List available hosts, usually used with `--schedule-start YYYY-MM-DD HH` and `--schedule-end YYYY-MM-DD HH` - `/api/v3/interfaces` List interfaces of QUADS host(s) + - `/api/v3/vlans` Retrieve a list of all vlans + - `/api/v3/vlans/free` Retrieve a list of all available vlans - `/api/v3/clouds/summary` Obtain a full summary of clouds, tickets, descriptions - `/api/v3/moves` Obtain a list of hosts with their current and future clouds diff --git a/src/quads/server/blueprints/vlans.py b/src/quads/server/blueprints/vlans.py index 97fff95c..07cd8cb3 100644 --- a/src/quads/server/blueprints/vlans.py +++ b/src/quads/server/blueprints/vlans.py @@ -26,6 +26,16 @@ def get_vlan(vlan_id: int) -> Response: @vlan_bp.route("/") def get_vlans() -> Response: _vlans = VlanDao.get_vlans() + if not _vlans: + return jsonify([]) + return jsonify([_vlan.as_dict() for _vlan in _vlans]) + + +@vlan_bp.route("/free") +def get_free_vlans() -> Response: + _vlans = VlanDao.get_free_vlans() + if not _vlans: + return jsonify([]) return jsonify([_vlan.as_dict() for _vlan in _vlans]) diff --git a/src/quads/server/dao/vlan.py b/src/quads/server/dao/vlan.py index 9cf7ed0a..9b7bda48 100644 --- a/src/quads/server/dao/vlan.py +++ b/src/quads/server/dao/vlan.py @@ -1,7 +1,7 @@ from typing import List from quads.server.dao.baseDao import BaseDao -from quads.server.models import Vlan, db +from quads.server.models import Assignment, Vlan, db class VlanDao(BaseDao): @@ -25,7 +25,16 @@ def get_vlan(vlan_id: int) -> Vlan: @staticmethod def get_vlans() -> List[Vlan]: - # TODO:Union with assignments table on assignments.vlan_id=Vlan.vlan_id where assignments.active=True - # to include a column with the assignment ID - vlans = db.session.query(Vlan).all() + vlans = db.session.query(Vlan).order_by(Vlan.vlan_id).all() + return vlans + + @staticmethod + def get_free_vlans() -> List[Vlan]: + vlans = ( + db.session.query(Vlan) + .outerjoin(Assignment, (Vlan.id == Assignment.vlan_id) & (Assignment.active == True)) + .filter(Assignment.id == None) # noqa: E711 + .order_by(Vlan.vlan_id) + .all() + ) return vlans diff --git a/src/quads/server/swagger.yaml b/src/quads/server/swagger.yaml index a3d74de9..b2c23bc4 100644 --- a/src/quads/server/swagger.yaml +++ b/src/quads/server/swagger.yaml @@ -1110,6 +1110,26 @@ paths: security: - BearerAuth: [ ] + /vlans/free/: + get: + summary: Returns a list with all the free vlans + tags: + - Vlans + responses: + '200': + description: OK + content: + application/json: + schema: + items: + $ref: '#/components/schemas/Vlan' + default: + description: Unexpected error + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + /vlans/{vlan_id}/: get: summary: Returns a detail of a specific vlan by id