Skip to content

Commit

Permalink
allow to patch multiple object in patch path by a single template
Browse files Browse the repository at this point in the history
Signed-off-by: Feng Huang <fehuang@redhat.com>
  • Loading branch information
BumbleFeng committed Jun 28, 2024
1 parent 2864f14 commit 3e2da93
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions reconcile/templating/lib/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,24 +144,30 @@ def get_identifier(data: dict[str, Any]) -> Any:
return None
return data.get(self.template.patch.identifier)

dta_identifier = get_identifier(data_to_add)
if not dta_identifier:
raise ValueError(
f"Expected identifier {self.template.patch.identifier} in data to add"
def update_data(matched_value: list, data_to_add: dict[str, Any]) -> Any:
dta_identifier = get_identifier(data_to_add)
if not dta_identifier:
raise ValueError(
f"Expected identifier {self.template.patch.identifier} in data to add"
)
index = next(
(
index
for index, data in enumerate(matched_value)
if get_identifier(data) == dta_identifier
),
None,
)

index = next(
(
index
for index, data in enumerate(matched_value)
if get_identifier(data) == dta_identifier
),
None,
)
if index is None:
matched_value.append(data_to_add)
if index is None:
matched_value.append(data_to_add)
else:
matched_value[index] = data_to_add

if isinstance(data_to_add, list):
for d in data_to_add:
update_data(matched_value, d)
else:
matched_value[index] = data_to_add
update_data(matched_value, data_to_add)
else:
matched_value.update(data_to_add)

Expand Down

0 comments on commit 3e2da93

Please sign in to comment.