Skip to content

Commit fb39682

Browse files
committed
Make set_condition variadic and rename to set_conditions
This commit renames set_condition to set_conditions and changes the signature to accept variadic resource.Condition arguments. Functions that set multiple conditions can now pass them all in one call. Signed-off-by: Nic Cope <nicc@rk0n.org>
1 parent c8bdf85 commit fb39682

2 files changed

Lines changed: 72 additions & 64 deletions

File tree

crossplane/function/response.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,40 +81,42 @@ def fatal(rsp: fnv1.RunFunctionResponse, message: str) -> None:
8181
)
8282

8383

84-
def set_condition(
84+
_STATUS_MAP = {
85+
"True": fnv1.STATUS_CONDITION_TRUE,
86+
"False": fnv1.STATUS_CONDITION_FALSE,
87+
"Unknown": fnv1.STATUS_CONDITION_UNKNOWN,
88+
}
89+
90+
91+
def set_conditions(
8592
rsp: fnv1.RunFunctionResponse,
86-
condition: resource.Condition,
93+
*conditions: resource.Condition,
8794
) -> None:
88-
"""Set a condition on the composite resource (XR).
95+
"""Set one or more conditions on the composite resource (XR).
8996
9097
Args:
9198
rsp: The RunFunctionResponse to update.
92-
condition: The condition to set.
99+
*conditions: The conditions to set.
93100
94-
The condition is appended to ``rsp.conditions``. Crossplane uses the
101+
Each condition is appended to ``rsp.conditions``. Crossplane uses the
95102
conditions returned by a function to set custom status conditions on
96103
the composite resource.
97104
98-
The ``last_transition_time`` field of the condition is ignored.
105+
The ``last_transition_time`` field of each condition is ignored.
99106
Crossplane sets the transition time itself.
100107
101108
Do not set the ``Ready`` condition type. Crossplane manages it based
102109
on resource readiness.
103110
"""
104-
status_map = {
105-
"True": fnv1.STATUS_CONDITION_TRUE,
106-
"False": fnv1.STATUS_CONDITION_FALSE,
107-
"Unknown": fnv1.STATUS_CONDITION_UNKNOWN,
108-
}
109-
110-
rsp.conditions.append(
111-
fnv1.Condition(
112-
type=condition.typ,
113-
status=status_map.get(condition.status, fnv1.STATUS_CONDITION_UNKNOWN),
114-
reason=condition.reason or "",
115-
message=condition.message or "",
111+
for condition in conditions:
112+
rsp.conditions.append(
113+
fnv1.Condition(
114+
type=condition.typ,
115+
status=_STATUS_MAP.get(condition.status, fnv1.STATUS_CONDITION_UNKNOWN),
116+
reason=condition.reason or "",
117+
message=condition.message or "",
118+
)
116119
)
117-
)
118120

119121

120122
def set_output(rsp: fnv1.RunFunctionResponse, output: dict | structpb.Struct) -> None:

tests/test_response.py

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -71,65 +71,71 @@ class TestCase:
7171
"-want, +got",
7272
)
7373

74-
def test_set_condition(self) -> None:
74+
def test_set_conditions(self) -> None:
7575
@dataclasses.dataclass
7676
class TestCase:
7777
reason: str
78-
condition: resource.Condition
79-
want_type: str
80-
want_status: fnv1.Status.ValueType
81-
want_reason: str
82-
want_message: str
78+
conditions: list[resource.Condition]
79+
want_types: list[str]
80+
want_statuses: list[fnv1.Status.ValueType]
81+
want_reasons: list[str]
82+
want_messages: list[str]
8383

8484
cases = [
8585
TestCase(
86-
reason="A True condition should use STATUS_CONDITION_TRUE.",
87-
condition=resource.Condition(
88-
typ="DatabaseReady",
89-
status="True",
90-
reason="Available",
91-
message="The database is ready",
92-
),
93-
want_type="DatabaseReady",
94-
want_status=fnv1.STATUS_CONDITION_TRUE,
95-
want_reason="Available",
96-
want_message="The database is ready",
97-
),
98-
TestCase(
99-
reason="A False condition should use STATUS_CONDITION_FALSE.",
100-
condition=resource.Condition(
101-
typ="DatabaseReady",
102-
status="False",
103-
reason="Creating",
104-
),
105-
want_type="DatabaseReady",
106-
want_status=fnv1.STATUS_CONDITION_FALSE,
107-
want_reason="Creating",
108-
want_message="",
86+
reason="A single True condition should work.",
87+
conditions=[
88+
resource.Condition(
89+
typ="DatabaseReady",
90+
status="True",
91+
reason="Available",
92+
message="The database is ready",
93+
),
94+
],
95+
want_types=["DatabaseReady"],
96+
want_statuses=[fnv1.STATUS_CONDITION_TRUE],
97+
want_reasons=["Available"],
98+
want_messages=["The database is ready"],
10999
),
110100
TestCase(
111-
reason="An Unknown condition should use STATUS_CONDITION_UNKNOWN.",
112-
condition=resource.Condition(
113-
typ="DatabaseReady",
114-
status="Unknown",
115-
),
116-
want_type="DatabaseReady",
117-
want_status=fnv1.STATUS_CONDITION_UNKNOWN,
118-
want_reason="",
119-
want_message="",
101+
reason="Multiple conditions should all be appended.",
102+
conditions=[
103+
resource.Condition(
104+
typ="DatabaseReady",
105+
status="True",
106+
reason="Available",
107+
),
108+
resource.Condition(
109+
typ="CacheReady",
110+
status="False",
111+
reason="Creating",
112+
),
113+
resource.Condition(
114+
typ="NetworkReady",
115+
status="Unknown",
116+
),
117+
],
118+
want_types=["DatabaseReady", "CacheReady", "NetworkReady"],
119+
want_statuses=[
120+
fnv1.STATUS_CONDITION_TRUE,
121+
fnv1.STATUS_CONDITION_FALSE,
122+
fnv1.STATUS_CONDITION_UNKNOWN,
123+
],
124+
want_reasons=["Available", "Creating", ""],
125+
want_messages=["", "", ""],
120126
),
121127
]
122128

123129
for case in cases:
124130
rsp = fnv1.RunFunctionResponse()
125-
response.set_condition(rsp, case.condition)
131+
response.set_conditions(rsp, *case.conditions)
126132

127-
self.assertEqual(1, len(rsp.conditions), case.reason)
128-
got = rsp.conditions[0]
129-
self.assertEqual(case.want_type, got.type, case.reason)
130-
self.assertEqual(case.want_status, got.status, case.reason)
131-
self.assertEqual(case.want_reason, got.reason, case.reason)
132-
self.assertEqual(case.want_message, got.message, case.reason)
133+
self.assertEqual(len(case.conditions), len(rsp.conditions), case.reason)
134+
for i, got in enumerate(rsp.conditions):
135+
self.assertEqual(case.want_types[i], got.type, case.reason)
136+
self.assertEqual(case.want_statuses[i], got.status, case.reason)
137+
self.assertEqual(case.want_reasons[i], got.reason, case.reason)
138+
self.assertEqual(case.want_messages[i], got.message, case.reason)
133139

134140
def test_set_output(self) -> None:
135141
@dataclasses.dataclass

0 commit comments

Comments
 (0)