Skip to content

Commit

Permalink
builder method example (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbasan authored Dec 18, 2024
1 parent 453a05d commit 5176bec
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
7 changes: 7 additions & 0 deletions catalystwan/api/configuration_groups/parcel.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,10 @@ def as_default(value: Any, generic_alias: Any = None):
raise TypeError(
f"Inappropriate type origin: {generic_alias} {get_origin(generic_alias)} for argument generic_alias"
)


def as_optional_global_or_variable(value: Any, generic_alias: Any = None):
if isinstance(value, str) and value.startswith("{{") and value.endswith("}}"):
return as_variable(value)
else:
return as_optional_global(value, generic_alias)
22 changes: 21 additions & 1 deletion catalystwan/models/configuration/feature_profile/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
from pydantic import BaseModel, ConfigDict, Field, model_validator
from typing_extensions import Self

from catalystwan.api.configuration_groups.parcel import Default, Global, Variable, as_default, as_global
from catalystwan.api.configuration_groups.parcel import (
Default,
Global,
Variable,
as_default,
as_global,
as_optional_global,
as_optional_global_or_variable,
)
from catalystwan.models.common import (
CableLengthLongValue,
CableLengthShortValue,
Expand Down Expand Up @@ -485,6 +493,18 @@ class Encapsulation(BaseModel):
preference: Optional[Union[Global[int], Variable, Default[None]]] = Field(default=None)
weight: Optional[Union[Global[int], Variable, Default[int]]] = Field(default=None)

@staticmethod
def from_params(
encap: Optional[EncapType] = None,
preference: Union[None, int, str] = None,
weight: Union[None, int, str] = None,
) -> "Encapsulation":
return Encapsulation(
encap=as_optional_global(encap, EncapType),
preference=as_optional_global_or_variable(preference),
weight=as_optional_global_or_variable(weight),
)


class AllowService(BaseModel):
model_config = ConfigDict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
from pydantic import AliasPath, BaseModel, ConfigDict, Field

from catalystwan.api.configuration_groups.parcel import Default, Global, Variable, _ParcelBase, as_default
from catalystwan.models.common import CarrierType, EthernetDuplexMode, MediaType, Speed, TLOCColor, TunnelMode
from catalystwan.models.common import (
CarrierType,
EncapType,
EthernetDuplexMode,
MediaType,
Speed,
TLOCColor,
TunnelMode,
)
from catalystwan.models.configuration.feature_profile.common import (
AclQos,
AllowService,
Expand Down Expand Up @@ -196,7 +204,7 @@ class InterfaceEthernetParcel(_ParcelBase):
model_config = ConfigDict(arbitrary_types_allowed=True, populate_by_name=True, extra="forbid")
type_: Literal["wan/vpn/interface/ethernet"] = Field(default="wan/vpn/interface/ethernet", exclude=True)
encapsulation: List[Encapsulation] = Field(
validation_alias=AliasPath("data", "encapsulation"), description="Encapsulation for TLOC"
default_factory=list, validation_alias=AliasPath("data", "encapsulation"), description="Encapsulation for TLOC"
)
interface_name: Union[Variable, Global[str]] = Field(validation_alias=AliasPath("data", "interfaceName"))
interface_ip_address: Union[InterfaceDynamicIPv4Address, InterfaceStaticIPv4Address] = Field(
Expand Down Expand Up @@ -263,3 +271,11 @@ class InterfaceEthernetParcel(_ParcelBase):
tunnel: Optional[Tunnel] = Field(
default=None, validation_alias=AliasPath("data", "tunnel"), description="Tunnel Interface Attributes"
)

def add_encapsulation(
self,
encap: Optional[EncapType] = None,
preference: Union[None, int, str] = None,
weight: Union[None, int, str] = None,
) -> None:
self.encapsulation.append(Encapsulation.from_params(encap, preference, weight))

0 comments on commit 5176bec

Please sign in to comment.