forked from mirrors/thatmattlove-hyperglass
improve validation of listen_address
This commit is contained in:
parent
c469e6576c
commit
3d04bbdd47
1 changed files with 28 additions and 1 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
# Standard Library Imports
|
# Standard Library Imports
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from ipaddress import ip_address
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List
|
from typing import List
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
@ -50,10 +51,36 @@ class General(HyperglassModel):
|
||||||
redis_port: StrictInt = 6379
|
redis_port: StrictInt = 6379
|
||||||
requires_ipv6_cidr: List[StrictStr] = ["cisco_ios", "cisco_nxos"]
|
requires_ipv6_cidr: List[StrictStr] = ["cisco_ios", "cisco_nxos"]
|
||||||
request_timeout: StrictInt = 30
|
request_timeout: StrictInt = 30
|
||||||
listen_address: Union[IPvAnyAddress, StrictStr] = "localhost"
|
listen_address: Optional[Union[IPvAnyAddress, StrictStr]]
|
||||||
listen_port: StrictInt = 8001
|
listen_port: StrictInt = 8001
|
||||||
log_file: Optional[FilePath]
|
log_file: Optional[FilePath]
|
||||||
|
|
||||||
|
@validator("listen_address", pre=True, always=True)
|
||||||
|
def validate_listen_address(cls, value, values):
|
||||||
|
"""Set default listen_address based on debug mode.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
value {str|IPvAnyAddress|None} -- listen_address
|
||||||
|
values {dict} -- already-validated entries before listen_address
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
{str} -- Validated listen_address
|
||||||
|
"""
|
||||||
|
if value is None and not values["debug"]:
|
||||||
|
listen_address = "localhost"
|
||||||
|
elif value is None and values["debug"]:
|
||||||
|
listen_address = ip_address("0.0.0.0") # noqa: S104
|
||||||
|
elif isinstance(value, str) and value != "localhost":
|
||||||
|
try:
|
||||||
|
listen_address = ip_address(value)
|
||||||
|
except ValueError:
|
||||||
|
raise ValueError(str(value))
|
||||||
|
elif isinstance(value, str) and value == "localhost":
|
||||||
|
listen_address = value
|
||||||
|
else:
|
||||||
|
raise ValueError(str(value))
|
||||||
|
return listen_address
|
||||||
|
|
||||||
@validator("site_description")
|
@validator("site_description")
|
||||||
def validate_site_description(cls, value, values):
|
def validate_site_description(cls, value, values):
|
||||||
"""Format the site descripion with the org_name field.
|
"""Format the site descripion with the org_name field.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue