Skip to content

Commit

Permalink
add /api/info route, fix api docs logo
Browse files Browse the repository at this point in the history
  • Loading branch information
thatmattlove committed Jul 17, 2020
1 parent 76b42e6 commit 24cb5ab
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
17 changes: 16 additions & 1 deletion hyperglass/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from hyperglass.api.events import on_startup, on_shutdown
from hyperglass.api.routes import (
docs,
info,
query,
queries,
routers,
Expand All @@ -36,6 +37,7 @@
)
from hyperglass.api.models.response import (
QueryError,
InfoResponse,
QueryResponse,
RoutersResponse,
CommunityResponse,
Expand Down Expand Up @@ -116,7 +118,9 @@ def _custom_openapi():
description=params.docs.description,
routes=app.routes,
)
openapi_schema["info"]["x-logo"] = {"url": "/" + str(params.web.logo.dark)}
openapi_schema["info"]["x-logo"] = {
"url": "/images/light" + params.web.logo.light.suffix
}

query_samples = []
queries_samples = []
Expand Down Expand Up @@ -177,6 +181,17 @@ def _custom_openapi():
allow_headers=["*"],
)

app.add_api_route(
path="/api/info",
endpoint=info,
methods=["GET"],
response_model=InfoResponse,
response_class=JSONResponse,
summary=params.docs.info.summary,
description=params.docs.info.description,
tags=[params.docs.info.title],
)

app.add_api_route(
path="/api/devices",
endpoint=routers,
Expand Down
26 changes: 25 additions & 1 deletion hyperglass/api/models/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ class RoutersResponse(BaseModel):

name: StrictStr
network: Network
location: StrictStr
display_name: StrictStr
vrfs: List[Vrf]

Expand Down Expand Up @@ -225,3 +224,28 @@ class Config:
{"name": "bgp_route", "display_name": "BGP Route", "enable": True}
]
}


class InfoResponse(BaseModel):
"""Response model for /api/info endpoint."""

name: StrictStr
organization: StrictStr
primary_asn: StrictInt
version: StrictStr

class Config:
"""Pydantic model configuration."""

title = "System Information"
description = "General information about this looking glass."
schema_extra = {
"examples": [
{
"name": "hyperglass",
"organization": "Company Name",
"primary_asn": 65000,
"version": "hyperglass 1.0.0-beta.52",
}
]
}
14 changes: 12 additions & 2 deletions hyperglass/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from hyperglass.cache import AsyncCache
from hyperglass.encode import jwt_decode
from hyperglass.external import Webhook, bgptools
from hyperglass.constants import __version__
from hyperglass.exceptions import HyperglassError
from hyperglass.configuration import REDIS_CONFIG, params, devices
from hyperglass.api.models.query import Query
Expand Down Expand Up @@ -211,7 +212,6 @@ async def routers():
include={
"name": ...,
"network": ...,
"location": ...,
"display_name": ...,
"vrfs": {-1: {"name", "display_name"}},
}
Expand All @@ -233,4 +233,14 @@ async def queries():
return params.queries.list


endpoints = [query, docs, routers]
async def info():
"""Serve general information about this instance of hyperglass."""
return {
"name": params.site_title,
"organization": params.org_name,
"primary_asn": int(params.primary_asn),
"version": f"hyperglass {__version__}",
}


endpoints = [query, docs, routers, info]
5 changes: 5 additions & 0 deletions hyperglass/configuration/models/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class Docs(HyperglassModel):
description="List of BGP communities.",
summary="BGP Communities List",
)
info: EndpointConfig = EndpointConfig(
title="System Information",
description="General information about this looking glass.",
summary="System Information",
)

class Config:
"""Pydantic model configuration."""
Expand Down

0 comments on commit 24cb5ab

Please sign in to comment.