diff --git a/testsuite/kuadrant/policy/__init__.py b/testsuite/kuadrant/policy/__init__.py index d4ba2ad8..2666af52 100644 --- a/testsuite/kuadrant/policy/__init__.py +++ b/testsuite/kuadrant/policy/__init__.py @@ -1,11 +1,19 @@ """Contains Base class for policies""" from dataclasses import dataclass +from enum import Enum from testsuite.kubernetes import KubernetesObject from testsuite.utils import check_condition +class Strategy(Enum): + """Class for merge strategies of defaults and overrides.""" + + ATOMIC = "atomic" + MERGE = "merge" + + @dataclass class CelPredicate: """Dataclass that references CEL predicate e.g. auth.identity.anonymous == 'true'""" diff --git a/testsuite/kuadrant/policy/authorization/auth_policy.py b/testsuite/kuadrant/policy/authorization/auth_policy.py index 62d2292f..4830a2fb 100644 --- a/testsuite/kuadrant/policy/authorization/auth_policy.py +++ b/testsuite/kuadrant/policy/authorization/auth_policy.py @@ -7,10 +7,11 @@ from testsuite.kubernetes import modify from testsuite.kubernetes.client import KubernetesClient from testsuite.utils import asdict + +from .. import CelPredicate, Policy, Strategy +from . import Pattern from .auth_config import AuthConfig from .sections import ResponseSection -from .. import Policy, CelPredicate -from . import Pattern class AuthPolicy(Policy, AuthConfig): @@ -49,6 +50,21 @@ def add_rule(self, when: list[CelPredicate]): self.model.spec.setdefault("when", []) self.model.spec["when"].extend([asdict(x) for x in when]) + # TODO: need to check if the typing is set up correctlly. + @modify + def strategy(self, strategy: Strategy) -> None: + """Add strategy type to default or overrides spec""" + if self.spec_section is None: + if "defaults" in self.model.spec: + self.spec_section = self.model.spec["default"] + elif "overrides" in self.model.spec: + self.spec_section = self.model.spec["overrides"] + else: + raise TypeError("no default or override section found in spec") + + self.spec_section["strategy"] = strategy.value + self.spec_section = None + @property def auth_section(self): if self.spec_section is None: diff --git a/testsuite/kuadrant/policy/rate_limit.py b/testsuite/kuadrant/policy/rate_limit.py index 7332f751..95fd48cc 100644 --- a/testsuite/kuadrant/policy/rate_limit.py +++ b/testsuite/kuadrant/policy/rate_limit.py @@ -5,9 +5,9 @@ from typing import Iterable from testsuite.gateway import Referencable +from testsuite.kuadrant.policy import CelExpression, CelPredicate, Policy, Strategy from testsuite.kubernetes import modify from testsuite.kubernetes.client import KubernetesClient -from testsuite.kuadrant.policy import Policy, CelPredicate, CelExpression from testsuite.utils import asdict @@ -72,6 +72,21 @@ def add_limit( self.spec_section.setdefault("limits", {})[name] = limit self.spec_section = None + # TODO: need to check if the typing is set up correctlly. + @modify + def strategy(self, strategy: Strategy) -> None: + """Add strategy type to default or overrides spec""" + if self.spec_section is None: + if "defaults" in self.model.spec: + self.spec_section = self.model.spec["defaults"] + elif "overrides" in self.model.spec: + self.spec_section = self.model.spec["overrides"] + else: + raise TypeError("no default or override section found in spec") + + self.spec_section["strategy"] = strategy.value + self.spec_section = None + @property def defaults(self): """Add new rule into the `defaults` RateLimitPolicy section"""