From 6b2e6bf392f76e0d52aed511f03a16c6862459b4 Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Thu, 29 Dec 2022 09:00:52 +0100 Subject: [PATCH] added optional query parameter to '/-/origins' and '/-/leases' for linked leases/origin --- README.md | 12 ++++++++++-- app/main.py | 19 +++++++++++++++---- app/orm.py | 18 ++++++++++++++++++ 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6e06e8b..06f5691 100644 --- a/README.md +++ b/README.md @@ -25,14 +25,22 @@ Status endpoint, used for *healthcheck*. Shows also current version and commit h OpenAPI specifications rendered from `GET /openapi.json`. -### `GET /-/origins` +### `GET /-/origins?leases=false` List registered origins. -### `GET /-/leases` +| Query Parameter | Default | Usage | +|-----------------|---------|--------------------------------------| +| `leases` | `false` | Include referenced leases per origin | + +### `GET /-/leases?origin=false` List current leases. +| Query Parameter | Default | Usage | +|-----------------|---------|-------------------------------------| +| `origin` | `false` | Include referenced origin per lease | + ### `GET /client-token` Generate client token, (see [installation](#installation)). diff --git a/app/main.py b/app/main.py index e92c70c..646feaf 100644 --- a/app/main.py +++ b/app/main.py @@ -77,17 +77,28 @@ async def status(request: Request): @app.get('/-/origins') -async def _origins(request: Request): +async def _origins(request: Request, leases: bool = False): session = sessionmaker(bind=db)() - response = list(map(lambda x: jsonable_encoder(x), session.query(Origin).all())) + response = [] + for origin in session.query(Origin).all(): + x = origin.serialize() + if leases: + x['leases'] = list(map(lambda _: _.serialize(), Lease.find_by_origin_ref(db, origin.origin_ref))) + response.append(x) session.close() return JSONResponse(response) @app.get('/-/leases') -async def _leases(request: Request): +async def _leases(request: Request, origin: bool = False): session = sessionmaker(bind=db)() - response = list(map(lambda x: jsonable_encoder(x), session.query(Lease).all())) + response = [] + for lease in session.query(Lease).all(): + x = lease.serialize() + if origin: + # assume that each lease has a valid origin record + x['origin'] = session.query(Origin).filter(Origin.origin_ref == lease.origin_ref).first().serialize() + response.append(x) session.close() return JSONResponse(response) diff --git a/app/orm.py b/app/orm.py index dc38e37..c451b29 100644 --- a/app/orm.py +++ b/app/orm.py @@ -21,6 +21,15 @@ class Origin(Base): def __repr__(self): return f'Origin(origin_ref={self.origin_ref}, hostname={self.hostname})' + def serialize(self) -> dict: + return { + 'origin_ref': self.origin_ref, + 'hostname': self.hostname, + 'guest_driver_version': self.guest_driver_version, + 'os_platform': self.os_platform, + 'os_version': self.os_version, + } + @staticmethod def create_statement(engine: Engine): from sqlalchemy.schema import CreateTable @@ -59,6 +68,15 @@ class Lease(Base): def __repr__(self): return f'Lease(origin_ref={self.origin_ref}, lease_ref={self.lease_ref}, expires={self.lease_expires})' + def serialize(self) -> dict: + return { + 'lease_ref': self.lease_ref, + 'origin_ref': self.origin_ref, + 'lease_created': self.lease_created.isoformat(), + 'lease_expires': self.lease_expires.isoformat(), + 'lease_updated': self.lease_updated.isoformat(), + } + @staticmethod def create_statement(engine: Engine): from sqlalchemy.schema import CreateTable