added optional query parameter to '/-/origins' and '/-/leases' for linked leases/origin

This commit is contained in:
Oscar Krause 2022-12-29 09:00:52 +01:00
parent 913da290f1
commit 6b2e6bf392
3 changed files with 43 additions and 6 deletions

View File

@ -25,14 +25,22 @@ Status endpoint, used for *healthcheck*. Shows also current version and commit h
OpenAPI specifications rendered from `GET /openapi.json`. OpenAPI specifications rendered from `GET /openapi.json`.
### `GET /-/origins` ### `GET /-/origins?leases=false`
List registered origins. List registered origins.
### `GET /-/leases` | Query Parameter | Default | Usage |
|-----------------|---------|--------------------------------------|
| `leases` | `false` | Include referenced leases per origin |
### `GET /-/leases?origin=false`
List current leases. List current leases.
| Query Parameter | Default | Usage |
|-----------------|---------|-------------------------------------|
| `origin` | `false` | Include referenced origin per lease |
### `GET /client-token` ### `GET /client-token`
Generate client token, (see [installation](#installation)). Generate client token, (see [installation](#installation)).

View File

@ -77,17 +77,28 @@ async def status(request: Request):
@app.get('/-/origins') @app.get('/-/origins')
async def _origins(request: Request): async def _origins(request: Request, leases: bool = False):
session = sessionmaker(bind=db)() 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() session.close()
return JSONResponse(response) return JSONResponse(response)
@app.get('/-/leases') @app.get('/-/leases')
async def _leases(request: Request): async def _leases(request: Request, origin: bool = False):
session = sessionmaker(bind=db)() 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() session.close()
return JSONResponse(response) return JSONResponse(response)

View File

@ -21,6 +21,15 @@ class Origin(Base):
def __repr__(self): def __repr__(self):
return f'Origin(origin_ref={self.origin_ref}, hostname={self.hostname})' 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 @staticmethod
def create_statement(engine: Engine): def create_statement(engine: Engine):
from sqlalchemy.schema import CreateTable from sqlalchemy.schema import CreateTable
@ -59,6 +68,15 @@ class Lease(Base):
def __repr__(self): def __repr__(self):
return f'Lease(origin_ref={self.origin_ref}, lease_ref={self.lease_ref}, expires={self.lease_expires})' 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 @staticmethod
def create_statement(engine: Engine): def create_statement(engine: Engine):
from sqlalchemy.schema import CreateTable from sqlalchemy.schema import CreateTable