Skip to content

Commit

Permalink
Added validators for required fields
Browse files Browse the repository at this point in the history
  • Loading branch information
vitthalmagadum committed Jan 20, 2025
1 parent d57b53e commit 59b92e1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
4 changes: 2 additions & 2 deletions anta/input_models/routing/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ class BgpRoute(BaseModel):
"""The IPv4 network address."""
vrf: str = "default"
"""Optional VRF for the BGP peer. Defaults to `default`."""
paths: list[BgpRoutePath] | None = None
"""A list of paths for the BGP route. Required field in the `VerifyBGPRouteOrigin` test."""
paths: list[BgpRoutePath]
"""A list of paths for the BGP route."""

def __str__(self) -> str:
"""Return a human-readable string representation of the BgpRoute for reporting.
Expand Down
4 changes: 2 additions & 2 deletions anta/input_models/routing/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class IPv4Routes(BaseModel):
"""The IPV4 network to validate the route type."""
vrf: str = "default"
"""VRF context. Defaults to `default` VRF."""
route_type: IPv4RouteType
"""List of IPV4 Route type to validate the valid rout type."""
route_type: IPv4RouteType | None = None
"""List of IPV4 Route type to validate the valid rout type. Required field in the `VerifyIPv4RouteType` test."""

def __str__(self) -> str:
"""Return a human-readable string representation of the IPv4RouteType for reporting."""
Expand Down
27 changes: 25 additions & 2 deletions anta/tests/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

import re
from ipaddress import IPv4Interface
from typing import Any, ClassVar
from typing import Any, ClassVar, TypeVar

from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, field_validator
from pydantic_extra_types.mac_address import MacAddress

from anta import GITHUB_SUGGESTION
Expand All @@ -23,6 +23,9 @@

BPS_GBPS_CONVERSIONS = 1000000000

# Using a TypeVar for the InterfaceState model since mypy thinks it's a ClassVar and not a valid type when used in field validators
T = TypeVar("T", bound=InterfaceState)


class VerifyInterfaceUtilization(AntaTest):
"""Verifies that the utilization of interfaces is below a certain threshold.
Expand Down Expand Up @@ -226,6 +229,16 @@ class Input(AntaTest.Input):
"""List of interfaces with their expected state."""
InterfaceState: ClassVar[type[InterfaceState]] = InterfaceState

@field_validator("interfaces")
@classmethod
def validate_interfaces(cls, interfaces: list[T]) -> list[T]:
"""Validate that 'status' field is provided in each interface."""
for interface in interfaces:
if interface.status is None:
msg = f"{interface} 'status' field missing in the input"
raise ValueError(msg)
return interfaces

@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifyInterfacesStatus."""
Expand Down Expand Up @@ -891,6 +904,16 @@ class Input(AntaTest.Input):
"""List of interfaces with their expected state."""
InterfaceState: ClassVar[type[InterfaceState]] = InterfaceState

@field_validator("interfaces")
@classmethod
def validate_interfaces(cls, interfaces: list[T]) -> list[T]:
"""Validate that 'portchannel' field is provided in each interface."""
for interface in interfaces:
if interface.portchannel is None:
msg = f"{interface} 'portchannel' field missing in the input"
raise ValueError(msg)
return interfaces

@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifyLACPInterfacesStatus."""
Expand Down
20 changes: 16 additions & 4 deletions anta/tests/routing/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ipaddress import IPv4Address, IPv4Interface
from typing import TYPE_CHECKING, ClassVar, Literal

from pydantic import model_validator
from pydantic import field_validator, model_validator

from anta.custom_types import PositiveInteger
from anta.input_models.routing.generic import IPv4Routes
Expand Down Expand Up @@ -189,9 +189,10 @@ class VerifyIPv4RouteType(AntaTest):
"""Verifies the route-type of the IPv4 prefixes.
This test performs the following checks for each IPv4 route:
1. Verifies that the specified VRF is configured.
2. Verifies that the specified IPv4 route is exists in the configuration.
3. Verifies that the the specified IPv4 route is of the expected type.
1. Verifies that the specified VRF is configured.
2. Verifies that the specified IPv4 route is exists in the configuration.
3. Verifies that the the specified IPv4 route is of the expected type.
Expected Results
----------------
Expand Down Expand Up @@ -230,6 +231,17 @@ class Input(AntaTest.Input):
"""Input model for the VerifyIPv4RouteType test."""

routes_entries: list[IPv4Routes]
"""List of IPv4 route(s)."""

@field_validator("routes_entries")
@classmethod
def validate_routes_entries(cls, routes_entries: list[IPv4Routes]) -> list[IPv4Routes]:
"""Validate that 'route_type' field is provided in each BGP route entry."""
for entry in routes_entries:
if entry.route_type is None:
msg = f"{entry} 'route_type' field missing in the input"
raise ValueError(msg)
return routes_entries

@AntaTest.anta_test
def test(self) -> None:
Expand Down

0 comments on commit 59b92e1

Please sign in to comment.