Skip to content
Open
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
27 changes: 22 additions & 5 deletions testsuite/kuadrant/policy/authorization/auth_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,28 @@ def strategy(self, strategy: Strategy) -> None:
if self.spec_section is None:
raise TypeError("Strategy can only be set on defaults or overrides")

self.spec_section["strategy"] = strategy.value
if isinstance(self.spec_section, str):
# String marker - create the section now
section = self.model.spec.setdefault(self.spec_section, {})
else:
section = self.spec_section
Comment on lines +58 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if isinstance(self.spec_section, str):
# String marker - create the section now
section = self.model.spec.setdefault(self.spec_section, {})
else:
section = self.spec_section
section = self.spec_section
if isinstance(self.spec_section, str):
# String marker - create the section now
section = self.model.spec.setdefault(self.spec_section, {})

better code flow 👍


section["strategy"] = strategy.value
self.spec_section = None

@property
def auth_section(self):
"""Returns the rules section for adding auth configuration"""
if self.spec_section is None:
self.spec_section = self.model.spec
# Implicit mode - use model.spec directly
spec_section = self.model.spec
elif isinstance(self.spec_section, str):
# String marker ("defaults" or "overrides") - create the section now
spec_section = self.model.spec.setdefault(self.spec_section, {})
else:
# Already a dict (shouldn't happen with new code but keep for compatibility)
spec_section = self.spec_section

spec_section = self.spec_section
self.spec_section = None
return spec_section.setdefault("rules", {})

Expand All @@ -75,13 +88,17 @@ def responses(self) -> ResponseSection:
@property
def defaults(self):
"""Add new rule into the `defaults` AuthPolicy section"""
self.spec_section = self.model.spec.setdefault("defaults", {})
# Don't create the dict yet - only mark which section to use
# The dict will be created when auth_section is called
self.spec_section = "defaults"
return self

@property
def overrides(self):
"""Add new rule into the `overrides` AuthPolicy section"""
self.spec_section = self.model.spec.setdefault("overrides", {})
# Don't create the dict yet - only mark which section to use
# The dict will be created when auth_section is called
self.spec_section = "overrides"
return self

@modify
Expand Down
29 changes: 23 additions & 6 deletions testsuite/kuadrant/policy/rate_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,16 @@ def add_limit(
limit["counters"] = [asdict(rule) for rule in counters]

if self.spec_section is None:
self.spec_section = self.model.spec

self.spec_section.setdefault("limits", {})[name] = limit
# Implicit mode - use model.spec directly
spec_section = self.model.spec
elif isinstance(self.spec_section, str):
# String marker ("defaults" or "overrides") - create the section now
spec_section = self.model.spec.setdefault(self.spec_section, {})
else:
# Already a dict (shouldn't happen with new code but keep for compatibility)
spec_section = self.spec_section

spec_section.setdefault("limits", {})[name] = limit
self.spec_section = None

@modify
Expand All @@ -78,19 +85,29 @@ def strategy(self, strategy: Strategy) -> None:
if self.spec_section is None:
raise TypeError("Strategy can only be set on defaults or overrides")

self.spec_section["strategy"] = strategy.value
if isinstance(self.spec_section, str):
# String marker - create the section now
section = self.model.spec.setdefault(self.spec_section, {})
else:
section = self.spec_section

section["strategy"] = strategy.value
self.spec_section = None

@property
def defaults(self):
"""Add new rule into the `defaults` RateLimitPolicy section"""
self.spec_section = self.model.spec.setdefault("defaults", {})
# Don't create the dict yet - only mark which section to use
# The dict will be created when add_limit or strategy is called
self.spec_section = "defaults"
return self

@property
def overrides(self):
"""Add new rule into the `overrides` RateLimitPolicy section"""
self.spec_section = self.model.spec.setdefault("overrides", {})
# Don't create the dict yet - only mark which section to use
# The dict will be created when add_limit or strategy is called
self.spec_section = "overrides"
return self

def wait_for_ready(self):
Expand Down
Loading