Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit b3e73c5

Browse files
committed
Implement being able to iterate on self.conditions
1 parent 71caf67 commit b3e73c5

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

crossplane/pythonic/composite.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,27 @@ def __getattr__(self, type):
449449
def __getitem__(self, type):
450450
return Condition(self, type)
451451

452+
def __bool__(self):
453+
return bool(self._types())
454+
455+
def __len__(self):
456+
return len(self._types())
457+
458+
def __iter__(self):
459+
for type in self._types():
460+
yield self[type]
461+
462+
def _types(self):
463+
types = set()
464+
if self._response is not None:
465+
for condition in self._response.conditions:
466+
if condition.type:
467+
types.add(str(condition.type))
468+
for condition in self._observed.resource.status.conditions:
469+
if condition.type:
470+
types.add(str(condition.type))
471+
return sorted(types)
472+
452473

453474
class Condition(protobuf.ProtobufValue):
454475
def __init__(self, conditions, type):

crossplane/pythonic/protobuf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __getitem__(self, key):
8181
value = getattr(self._message, key)
8282
if value is _Unknown and field.has_default_value:
8383
value = field.default_value
84-
if field.label == field.LABEL_REPEATED:
84+
if field.is_repeated:
8585
if field.type == field.TYPE_MESSAGE and field.message_type.GetOptions().map_entry:
8686
value = MapMessage(self, key, field.message_type.fields_by_name['value'], value, self._readOnly)
8787
else:

tests/fn_cases/conditions.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ request:
2424
composite: |
2525
class ConditionsComposite(BaseComposite):
2626
def compose(self):
27+
assert bool(self.conditions)
28+
assert len(self.conditions) == 1
29+
for condition in self.conditions:
30+
assert condition.type == 'Ready'
31+
2732
self.status.compositeCondition = self.conditions.Ready
2833
self.status.compositeNotFound = self.conditions.Other
2934
self.status.composedCondition = self.resources.project.conditions.Ready
@@ -36,6 +41,10 @@ request:
3641
self.conditions.ConditionTrue('TrueCondition', 'we are true', True, True)
3742
self.conditions.DatabaseReady('Ready', 'Database is ready', True)
3843
44+
assert bool(self.conditions)
45+
assert len(self.conditions) == 4
46+
assert len(list(self.conditions)) == 4
47+
3948
response:
4049
desired:
4150
composite:

tests/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def message_merge(message, values):
3030
if isinstance(current, Struct):
3131
map_merge(current, value)
3232
else:
33-
if descriptor.label == descriptor.LABEL_REPEATED:
33+
if descriptor.is_repeated:
3434
if descriptor.message_type.GetOptions().map_entry:
3535
message_map_merge(descriptor, current, value)
3636
else:
@@ -42,7 +42,7 @@ def message_merge(message, values):
4242
list_merge(current, value)
4343
else:
4444
descriptor = message.DESCRIPTOR.fields_by_name.get(field)
45-
if descriptor.label == descriptor.LABEL_REPEATED:
45+
if descriptor.is_repeated:
4646
if descriptor.message_type.GetOptions().map_entry:
4747
message_map_merge(descriptor, current, value)
4848
else:
@@ -115,7 +115,7 @@ def message_dict(message):
115115
value = map_dict(value)
116116
elif field.message_type.name == 'ListValue':
117117
value = list_list(value)
118-
elif field.label == field.LABEL_REPEATED:
118+
elif field.is_repeated:
119119
if field.message_type.GetOptions().map_entry:
120120
value = message_map_dict(field, value)
121121
else:

0 commit comments

Comments
 (0)