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

Complete directives implementation, refactor exceptions, deprecate VRFs, bump minimum Python version

This commit is contained in:
thatmattlove
2021-09-07 22:58:39 -07:00
parent b05e544e40
commit 5ccfe50792
58 changed files with 1222 additions and 1010 deletions

View File

@@ -60,7 +60,7 @@ class AnyUri(str):
@classmethod
def __get_validators__(cls):
"""Pydantic custim field method."""
"""Pydantic custom field method."""
yield cls.validate
@classmethod
@@ -79,3 +79,35 @@ class AnyUri(str):
def __repr__(self):
"""Stringify custom field representation."""
return f"AnyUri({super().__repr__()})"
class Action(str):
"""Custom field type for policy actions."""
permits = ("permit", "allow", "accept")
denies = ("deny", "block", "reject")
@classmethod
def __get_validators__(cls):
"""Pydantic custom field method."""
yield cls.validate
@classmethod
def validate(cls, value: str):
"""Ensure action is an allowed value or acceptable alias."""
if not isinstance(value, str):
raise TypeError("Action type must be a string")
value = value.strip().lower()
if value in cls.permits:
return cls("permit")
elif value in cls.denies:
return cls("deny")
raise ValueError(
"Action must be one of '{}'".format(", ".join((*cls.permits, *cls.denies)))
)
def __repr__(self):
"""Stringify custom field representation."""
return f"Action({super().__repr__()})"