diff --git a/testsuite/gateway/__init__.py b/testsuite/gateway/__init__.py index 4412188b..bc27428b 100644 --- a/testsuite/gateway/__init__.py +++ b/testsuite/gateway/__init__.py @@ -9,7 +9,7 @@ from testsuite.certificates import Certificate from testsuite.lifecycle import LifecycleObject -from testsuite.utils import asdict, _asdict_recurse +from testsuite.utils import asdict if TYPE_CHECKING: from testsuite.openshift.client import OpenShiftClient @@ -76,10 +76,6 @@ class PathMatch: type: Optional[MatchType] = None value: Optional[str] = None - # def asdict(self): - # """Custom dict due to nested structure of matchers.""" - # return {"path": _asdict_recurse(self, False)} - @dataclass class HeadersMatch: @@ -89,10 +85,6 @@ class HeadersMatch: value: str type: Optional[Literal[MatchType.EXACT, MatchType.REGULAR_EXPRESSION]] = None - # def asdict(self): - # """Custom dict due to nested structure of matchers.""" - # return {"headers": [_asdict_recurse(self, False)]} - @dataclass class QueryParamsMatch: @@ -102,23 +94,6 @@ class QueryParamsMatch: value: str type: Optional[Literal[MatchType.EXACT, MatchType.REGULAR_EXPRESSION]] = None - # def asdict(self): - # """Custom dict due to nested structure of matchers.""" - # return {"queryParams": [_asdict_recurse(self, False)]} - - -@dataclass -class MethodMatch: - """ - HTTPMethod describes how to select a HTTP route by matching the HTTP method. The value is expected in upper case. - """ - - value: HTTPMethod = None - - def asdict(self): - """Custom dict due to nested structure of matchers.""" - return {"method": self.value.value} - @dataclass class RouteMatch: @@ -132,7 +107,7 @@ class RouteMatch: path: Optional[PathMatch] = None headers: Optional[List[HeadersMatch]] = None query_params: Optional[List[QueryParamsMatch]] = None - method: HTTPMethod = None + method: Optional[HTTPMethod] = None class Gateway(LifecycleObject, Referencable): diff --git a/testsuite/policy/rate_limit_policy.py b/testsuite/policy/rate_limit_policy.py index 9343fa0a..1e547bb2 100644 --- a/testsuite/policy/rate_limit_policy.py +++ b/testsuite/policy/rate_limit_policy.py @@ -1,13 +1,13 @@ """RateLimitPolicy related objects""" from dataclasses import dataclass from time import sleep -from typing import Iterable, Literal, Optional +from typing import Iterable, Literal, Optional, List import openshift as oc from testsuite.policy.authorization import Rule from testsuite.utils import asdict -from testsuite.gateway import Referencable, HTTPMatcher +from testsuite.gateway import Referencable, RouteMatch from testsuite.openshift.client import OpenShiftClient from testsuite.openshift import OpenShiftObject, modify @@ -22,15 +22,21 @@ class Limit: @dataclass -class RouteSelect: +class RouteSelector: """ - HTRTPPathMatch, HTTPHeaderMatch, HTTPQueryParamMatch, HTTPMethodMatch + RouteSelector is an object composed of a set of HTTPRouteMatch objects (from Gateway API - + HTRTPPathMatch, HTTPHeaderMatch, HTTPQueryParamMatch, HTTPMethodMatch), + and an additional hostnames field. https://docs.kuadrant.io/kuadrant-operator/doc/reference/route-selectors/#routeselector """ - matches: Optional[list[HTTPMatcher]] = None + matches: Optional[list[RouteMatch]] = None hostnames: Optional[list[str]] = None + def __init__(self, *matches: RouteMatch, hostnames: Optional[List[str]] = None): + self.matches = list(matches) if matches else [] + self.hostnames = hostnames + class RateLimitPolicy(OpenShiftObject): """RateLimitPolicy (or RLP for short) object, used for applying rate limiting rules to a Gateway/HTTPRoute""" @@ -57,7 +63,7 @@ def add_limit( limits: Iterable[Limit], when: Iterable[Rule] = None, counters: list[str] = None, - route_selectors: Iterable[RouteSelect] = None, + route_selectors: Iterable[RouteSelector] = None, ): """Add another limit""" limit: dict = { diff --git a/testsuite/utils.py b/testsuite/utils.py index e9116fa6..e11042fd 100644 --- a/testsuite/utils.py +++ b/testsuite/utils.py @@ -138,6 +138,9 @@ def asdict(obj) -> dict[str, JSONValues]: def _asdict_recurse(obj): + if hasattr(obj, "asdict"): + return obj.asdict() + if not is_dataclass(obj): return deepcopy(obj)