Skip to content
Merged
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
9 changes: 6 additions & 3 deletions crossplane/function/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,16 @@ def require_resources( # noqa: PLR0913
namespace: The namespace to search in (optional).

Raises:
ValueError: If both match_name and match_labels are provided, or neither.
ValueError: If both match_name and match_labels are provided.

This tells Crossplane to fetch the specified resources and include them
in the next call to the function in req.required_resources[name].

If neither match_name nor match_labels is provided, all resources of the
given api_version and kind are matched.
"""
if (match_name is None) == (match_labels is None):
msg = "Exactly one of match_name or match_labels must be provided"
if match_name is not None and match_labels is not None:
msg = "match_name and match_labels are mutually exclusive"
raise ValueError(msg)

selector = fnv1.ResourceSelector(
Expand Down
26 changes: 15 additions & 11 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,21 @@ class TestCase:
match_name="worker-1",
),
),
TestCase(
reason="Should match all resources of a kind with no match field.",
rsp=fnv1.RunFunctionResponse(),
name="all-pods",
api_version="v1",
kind="Pod",
match_name=None,
match_labels=None,
namespace="default",
want_selector=fnv1.ResourceSelector(
api_version="v1",
kind="Pod",
namespace="default",
),
),
]

for case in cases:
Expand Down Expand Up @@ -270,17 +285,6 @@ def test_require_resources_invalid_args(self) -> None:
match_labels={"app": "test"},
)

# Should raise ValueError if neither match_name nor match_labels are provided
with self.assertRaises(ValueError):
response.require_resources(
rsp,
"test",
"v1",
"Pod",
match_name=None,
match_labels=None,
)

def test_require_schema(self) -> None:
@dataclasses.dataclass
class TestCase:
Expand Down