Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Use Literals instead (str, Enum) in models #476

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions catalystwan/api/configuration_groups/parcel.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from enum import Enum
from typing import Any, Dict, Generic, Optional, TypeVar
from typing import Any, Dict, Generic, Literal, Optional, TypeVar, get_origin

from pydantic import AliasPath, BaseModel, ConfigDict, Field, PrivateAttr, model_serializer

Expand Down Expand Up @@ -49,10 +49,7 @@ class OptionType(str, Enum):


class ParcelAttribute(BaseModel):
model_config = ConfigDict(
extra="forbid",
)

model_config = ConfigDict(extra="forbid")
option_type: OptionType = Field(serialization_alias="optionType", validation_alias="optionType")


Expand Down Expand Up @@ -94,13 +91,47 @@ class Default(ParcelAttribute, Generic[T]):
value: Any


def as_global(value: Any):
return Global[type(value)](value=value) # type: ignore
def as_global(value: Any, generic_alias: Any = None):
"""Produces Global object given only value (type is induced from value)

Args:
value (Any): value of Global object to be produced
generic_alias (Any, optional): specify alias type like Literal. Defaults to None.

Returns:
Global[Any]: global option type object
"""
if generic_alias is None:
return Global[type(value)](value=value) # type: ignore
elif get_origin(generic_alias) is Literal:
return Global[generic_alias](value=value) # type: ignore
TypeError("Inappropriate type for argument generic_alias")


def as_variable(value: str):
"""Produces Variable object from variable name string

Args:
value (str): value of Variable object to be produced

Returns:
Variable: variable option type object
"""
return Variable(value=value)


def as_default(value: Any):
return Default[type(value)](value=value) # type: ignore
def as_default(value: Any, generic_alias: Any = None):
"""Produces Default object given only value (type is induced from value)

Args:
value (Any): value of Default object to be produced
generic_alias (Any, optional): specify alias type like Literal. Defaults to None.

Returns:
Default[Any]: default option type object
"""
if generic_alias is None:
return Default[type(value)](value=value) # type: ignore
elif get_origin(generic_alias) is Literal:
return Default[generic_alias](value=value) # type: ignore
TypeError("Inappropriate type for argument generic_alias")
121 changes: 69 additions & 52 deletions catalystwan/models/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from enum import Enum
from typing import Dict, List, Set, Tuple
from typing import Dict, List, Literal, Set, Tuple


def check_fields_exclusive(values: Dict, field_names: Set[str], at_least_one: bool = False) -> bool:
Expand Down Expand Up @@ -47,53 +46,71 @@ def check_any_of_exclusive_field_sets(values: Dict, field_sets: List[Tuple[Set[s
raise ValueError(f"One of {all_sets_field_names} must be assigned")


class InterfaceTypeEnum(str, Enum):
ETHERNET = "Ethernet"
FAST_ETHERNET = "FastEthernet"
FIVE_GIGABIT_ETHERNET = "FiveGigabitEthernet"
FORTY_GIGABIT_ETHERNET = "FortyGigabitEthernet"
GIGABIT_ETHERNET = "GigabitEthernet"
HUNDRED_GIG_ETHERNET = "HundredGigE"
LOOPBACK = "Loopback"
TEN_GIGABIT_ETHERNET = "TenGigabitEthernet"
TUNNEL = "Tunnel"
TWENTY_FIVEGIGABIT_ETHERNET = "TwentyFiveGigabitEthernet"
TWENTY_FIVE_GIG_ETHERNET = "TwentyFiveGigE"
TWO_GIGABIT_ETHERNET = "TwoGigabitEthernet"
VIRTUAL_PORT_GROUP = "VirtualPortGroup"
VLAN = "Vlan"


class TLOCColorEnum(str, Enum):
DEFAULT = "default"
MPLS = "mpls"
METRO_ETHERNET = "metro-ethernet"
BIZ_INTERNET = "biz-internet"
PUBLIC_INTERNET = "public-internet"
LTE = "lte"
THREEG = "3g"
RED = "red"
GREEN = "green"
BLUE = "blue"
GOLD = "gold"
SILVER = "silver"
BRONZE = "bronze"
CUSTOM1 = "custom1"
CUSTOM2 = "custom2"
CUSTOM3 = "custom3"
PRIVATE1 = "private1"
PRIVATE2 = "private2"
PRIVATE3 = "private3"
PRIVATE4 = "private4"
PRIVATE5 = "private5"
PRIVATE6 = "private6"


class WellKnownBGPCommunitiesEnum(str, Enum):
INTERNET = "internet"
LOCAL_AS = "local-AS"
NO_ADVERTISE = "no-advertise"
NO_EXPORT = "no-export"


UUID = str
InterfaceType = Literal[
"Ethernet",
"FastEthernet",
"FiveGigabitEthernet",
"FortyGigabitEthernet",
"GigabitEthernet",
"HundredGigE",
"Loopback",
"TenGigabitEthernet",
"Tunnel",
"TwentyFiveGigabitEthernet",
"TwentyFiveGigE",
"TwoGigabitEthernet",
"VirtualPortGroup",
"Vlan",
]

TLOCColor = Literal[
"default",
"mpls",
"metro-ethernet",
"biz-internet",
"public-internet",
"lte",
"3g",
"red",
"green",
"blue",
"gold",
"silver",
"bronze",
"custom1",
"custom2",
"custom3",
"private1",
"private2",
"private3",
"private4",
"private5",
"private6",
]


WellKnownBGPCommunities = Literal[
"internet",
"local-AS",
"no-advertise",
"no-export",
]

ServiceChainNumber = Literal[
"SC1",
"SC2",
"SC3",
"SC4",
"SC5",
"SC6",
"SC7",
"SC8",
"SC9",
"SC10",
"SC11",
"SC12",
"SC13",
"SC14",
"SC15",
"SC16",
]
28 changes: 8 additions & 20 deletions catalystwan/models/configuration/common.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
from enum import Enum

from pydantic import BaseModel, Extra, Field

from catalystwan.models.profileparcel.traffic_policy import GlobalOptionTypeDef, UuidDef


class Solution(str, Enum):
MOBILITY = "mobility"
SDWAN = "sdwan"
NFVIRTUAL = "nfvirtual"
SDROUTING = "sd-routing"


class RefId(BaseModel):
class Config:
extra = Extra.forbid

option_type: GlobalOptionTypeDef = Field(..., alias="optionType")
value: UuidDef
from typing import Literal

Solution = Literal[
"mobility",
"sdwan",
"nfvirtual",
"sd-routing",
]
8 changes: 4 additions & 4 deletions catalystwan/models/configuration/feature_profile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,10 @@ auto_sim: Union[Variable, Global[bool], Default[None]]

### 3. Literals vs Enums

Use `Enums` over the `Literals`. Justification:
- `Enum` provides IntelliSense.
- Both provides static typing safety.
- `Enum` can be treated as a `str`.
Use `Literal` over the `Enums`. Justification:
- Both provides static typing safety ant intellisense (type-hints).
- `(str, Enum)` can be problematic as `__str__` != `__format__` for Enum
- When instantiating pydantic Field defined as `Literal` no explicit import of specific `Enum` is needed

### 4. Naming Conventions

Expand Down
119 changes: 59 additions & 60 deletions catalystwan/models/configuration/feature_profile/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from datetime import datetime
from enum import Enum
from typing import Generic, List, Literal, Optional, TypeVar, Union
from uuid import UUID

Expand All @@ -14,15 +13,65 @@
IPV4Address = str
IPv6Address = str


class ProfileType(str, Enum):
TRANSPORT = "transport"
SYSTEM = "system"
CLI = "cli"
SERVICE = "service"
APPLICATION_PRIORITY = "application-priority"
POLICY_OBJECT = "policy-object"
EMBEDDED_SECURITY = "embedded-security"
ParcelType = Literal[
"appqoe",
"lan/vpn",
"lan/vpn/interface/ethernet",
"lan/vpn/interface/gre",
"lan/vpn/interface/ipsec",
"lan/vpn/interface/svi",
"dhcp-server",
"tracker",
"trackergroup",
"routing/bgp",
"routing/eigrp",
"routing/multicast",
"routing/ospf",
"routing/ospfv3/ipv4",
"routing/ospfv3/ipv6",
"wirelesslan",
"switchport",
"app-probe",
"app-list",
"color",
"data-prefix",
"expanded-community",
"class",
"data-ipv6-prefix",
"ipv6-prefix",
"prefix",
"policer",
"preferred-color-group",
"sla-class",
"tloc",
"standard-community",
"security-localdomain",
"security-fqdn",
"security-ipssignature",
"security-urllist",
"security-urllist",
"security-port",
"security-protocolname",
"security-geolocation",
"security-zone",
"security-localapp",
"security-data-ip-prefix",
]

ProfileType = Literal[
"transport",
"system",
"cli",
"service",
"application-priority",
"policy-object",
"embedded-security",
]

SchemaType = Literal[
"post",
"put",
]


class FeatureProfileInfo(BaseModel):
Expand Down Expand Up @@ -78,51 +127,6 @@ class ParcelCreationResponse(BaseModel):
id: UUID = Field(serialization_alias="parcelId", validation_alias="parcelId")


class ParcelType(str, Enum):
APPQOE = "appqoe"
LAN_VPN = "lan/vpn"
LAN_VPN_INTERFACE_ETHERNET = "lan/vpn/interface/ethernet"
LAN_VPN_INTERFACE_GRE = "lan/vpn/interface/gre"
LAN_VPN_INTERFACE_IPSEC = "lan/vpn/interface/ipsec"
LAN_VPN_INTERFACE_SVI = "lan/vpn/interface/svi"
DHCP_SERVER = "dhcp-server"
TRACKER = "tracker"
TRACKER_GROUP = "trackergroup"
ROUTING_BGP = "routing/bgp"
ROUTING_EIGRP = "routing/eigrp"
ROUTING_MULTICAST = "routing/multicast"
ROUTING_OSPF = "routing/ospf"
ROUTING_OSPFV3_IPV4 = "routing/ospfv3/ipv4"
ROUTING_OSPFV3_IPV6 = "routing/ospfv3/ipv6"
WIRELESSLAN = "wirelesslan"
SWITCHPORT = "switchport"
APP_PROBE = "app-probe"
APPLICATION_LIST = "app-list"
COLOR = "color"
DATA_PREFIX = "data-prefix"
EXPANDED_COMMUNITY = "expanded-community"
FOWARDING_CLASS = "class"
IPV6_DATA_PREFIX = "data-ipv6-prefix"
IPV6_PREFIX_LIST = "ipv6-prefix"
PREFIX_LIST = "prefix"
POLICIER = "policer"
PREFERRED_COLOR_GROUP = "preferred-color-group"
SLA_CLASS = "sla-class"
TLOC = "tloc"
STANDARD_COMMUNITY = "standard-community"
LOCAL_DOMAIN = "security-localdomain"
FQDN_DOMAIN = "security-fqdn"
IPS_SIGNATURE = "security-ipssignature"
URL_ALLOW = "security-urllist"
URL_BLOCK = "security-urllist"
SECURITY_PORT = "security-port"
PROTOCOL_LIST = "security-protocolname"
GEO_LOCATION_LIST = "security-geolocation"
SECURITY_ZONE_LIST = "security-zone"
SECURITY_APPLICATION_LIST = "security-localapp"
SECURITY_DATA_PREFIX = "security-data-ip-prefix"


class Parcel(BaseModel, Generic[T]):
parcel_id: str = Field(alias="parcelId")
parcel_type: ParcelType = Field(alias="parcelType")
Expand Down Expand Up @@ -157,11 +161,6 @@ class Prefix(BaseModel):
mask: Union[Variable, Global[str]]


class SchemaType(str, Enum):
POST = "post"
PUT = "put"


class SchemaTypeQuery(BaseModel):
model_config = ConfigDict(populate_by_name=True)

Expand Down
Loading
Loading