1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00
Files
checktheroads-hyperglass/hyperglass/api/models/response.py

252 lines
7.4 KiB
Python
Raw Normal View History

2020-01-21 02:38:04 -07:00
"""Response model."""
2020-05-29 17:47:53 -07:00
2020-02-03 02:34:50 -07:00
# Standard Library
2020-07-13 02:17:56 -07:00
from typing import Dict, List, Union, Optional
2020-01-21 02:38:04 -07:00
2020-02-03 02:34:50 -07:00
# Third Party
2020-04-16 23:43:02 -07:00
from pydantic import BaseModel, StrictInt, StrictStr, StrictBool, constr
2020-02-03 02:34:50 -07:00
# Project
from hyperglass.configuration import params
2020-07-13 02:17:56 -07:00
ErrorName = constr(regex=r"(success|warning|error|danger)")
ResponseLevel = constr(regex=r"success")
ResponseFormat = constr(regex=r"(application\/json|text\/plain)")
2020-01-21 02:38:04 -07:00
2020-01-21 17:30:14 -07:00
class QueryError(BaseModel):
2020-01-21 02:38:04 -07:00
"""Query response model."""
2020-02-03 02:34:50 -07:00
output: StrictStr = params.messages.general
2020-07-13 02:17:56 -07:00
level: ErrorName = "danger"
2020-04-16 23:43:02 -07:00
id: Optional[StrictStr]
2020-02-03 02:34:50 -07:00
keywords: List[StrictStr] = []
2020-01-21 17:30:14 -07:00
2020-02-01 12:50:12 -10:00
class Config:
"""Pydantic model configuration."""
title = "Query Error"
description = (
"Response received when there is an error executing the requested query."
)
2020-02-03 02:34:50 -07:00
fields = {
"output": {
"title": "Output",
"description": "Error Details",
"example": "192.0.2.1/32 is not allowed.",
},
"level": {
"title": "Level",
"description": "Error Severity",
"example": "danger",
},
"keywords": {
"title": "Keywords",
"description": "Relevant keyword values contained in the `output` field, which can be used for formatting.",
"example": ["192.0.2.1/32"],
},
}
2020-02-01 12:50:12 -10:00
schema_extra = {
"examples": [
{
"output": "192.0.2.1/32 is not allowed.",
"level": "danger",
"keywords": ["192.0.2.1/32"],
}
]
}
2020-01-21 17:30:14 -07:00
class QueryResponse(BaseModel):
"""Query response model."""
2020-07-13 02:17:56 -07:00
output: Union[Dict, StrictStr]
level: ResponseLevel = "success"
2020-04-18 07:58:46 -07:00
random: StrictStr
2020-04-16 23:43:02 -07:00
cached: StrictBool
runtime: StrictInt
2020-01-21 17:30:14 -07:00
keywords: List[StrictStr] = []
2020-04-18 07:58:46 -07:00
timestamp: StrictStr
2020-07-13 02:17:56 -07:00
format: ResponseFormat = "text/plain"
2020-02-01 12:50:12 -10:00
class Config:
"""Pydantic model configuration."""
title = "Query Response"
description = "Looking glass response"
2020-02-03 02:34:50 -07:00
fields = {
"level": {"title": "Level", "description": "Severity"},
2020-04-18 07:58:46 -07:00
"cached": {
"title": "Cached",
"description": "`true` if the response is from a previously cached query.",
},
"random": {
"title": "Random",
"description": "Random string to prevent client or intermediate caching.",
"example": "504cbdb47eb8310ca237bf512c3e10b44b0a3d85868c4b64a20037dc1c3ef857",
},
"runtime": {
"title": "Runtime",
"description": "Time it took to run the query in seconds.",
"example": 6,
},
"timestamp": {
"title": "Timestamp",
"description": "UTC Time at which the backend application received the query.",
"example": "2020-04-18 14:45:37",
},
"format": {
"title": "Format",
"description": "Response [MIME Type](http://www.iana.org/assignments/media-types/media-types.xhtml). Supported values: `text/plain` and `application/json`.",
"example": "text/plain",
},
2020-02-03 02:34:50 -07:00
"keywords": {
"title": "Keywords",
"description": "Relevant keyword values contained in the `output` field, which can be used for formatting.",
"example": ["1.1.1.0/24", "best #1"],
},
"output": {
"title": "Output",
"description": "Looking Glass Response",
"example": """
BGP routing table entry for 1.1.1.0/24, version 224184946
BGP Bestpath: deterministic-med
Paths: (12 available, best #1, table default)
Advertised to update-groups:
1 40
13335, (aggregated by 13335 172.68.129.1), (received & used)
192.0.2.1 (metric 51) from 192.0.2.1 (192.0.2.1)
Origin IGP, metric 0, localpref 250, valid, internal
Community: 65000:1 65000:2
""",
},
}
2020-02-01 12:50:12 -10:00
schema_extra = {
"examples": [
{
"output": """
BGP routing table entry for 1.1.1.0/24, version 224184946
BGP Bestpath: deterministic-med
2020-02-03 02:34:50 -07:00
Paths: (12 available, best #1, table default)
2020-02-01 12:50:12 -10:00
Advertised to update-groups:
1 40
13335, (aggregated by 13335 172.68.129.1), (received & used)
2020-02-03 02:34:50 -07:00
192.0.2.1 (metric 51) from 192.0.2.1 (192.0.2.1)
2020-02-01 12:50:12 -10:00
Origin IGP, metric 0, localpref 250, valid, internal
2020-02-03 02:34:50 -07:00
Community: 65000:1 65000:2
2020-02-01 12:50:12 -10:00
""",
"level": "success",
2020-02-03 02:34:50 -07:00
"keywords": ["1.1.1.0/24", "best #1"],
2020-02-01 12:50:12 -10:00
}
]
}
class Vrf(BaseModel):
"""Response model for /api/devices VRFs."""
name: StrictStr
display_name: StrictStr
class Config:
"""Pydantic model configuration."""
title = "VRF"
description = "VRF attributes"
schema_extra = {
"examples": [
{"name": "default", "display_name": "Global Routing Table"},
{"name": "customer_vrf_name", "display_name": "Customer Name"},
]
}
class Network(BaseModel):
"""Response model for /api/devices networks."""
name: StrictStr
display_name: StrictStr
class Config:
"""Pydantic model configuration."""
title = "Network"
description = "Network/ASN attributes"
schema_extra = {"examples": [{"name": "primary", "display_name": "AS65000"}]}
2020-02-03 02:34:50 -07:00
class RoutersResponse(BaseModel):
2020-02-01 12:50:12 -10:00
"""Response model for /api/devices list items."""
name: StrictStr
network: Network
display_name: StrictStr
vrfs: List[Vrf]
class Config:
"""Pydantic model configuration."""
title = "Device"
description = "Per-device attributes"
schema_extra = {
"examples": [
{
"name": "router01-nyc01",
"location": "nyc01",
"display_name": "New York City, NY",
}
]
}
2020-04-18 11:34:23 -07:00
class CommunityResponse(BaseModel):
"""Response model for /api/communities."""
community: StrictStr
display_name: StrictStr
description: StrictStr
2020-02-03 02:34:50 -07:00
class SupportedQueryResponse(BaseModel):
2020-02-01 12:50:12 -10:00
"""Response model for /api/queries list items."""
name: StrictStr
display_name: StrictStr
enable: StrictBool
class Config:
"""Pydantic model configuration."""
title = "Query Type"
description = "If enabled is `true`, the `name` field may be used to specify the query type."
schema_extra = {
"examples": [
{"name": "bgp_route", "display_name": "BGP Route", "enable": True}
]
}
2020-07-17 01:06:19 -07:00
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",
}
]
}