Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to patch multiple object in patch path by a single template #4513

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
47 changes: 27 additions & 20 deletions reconcile/templating/lib/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ def render_output(self) -> str:

def get_identifier(data: dict[str, Any]) -> Any:
assert self.template.patch is not None # mypy
if not self.template.patch.identifier:
raise ValueError(
f"Expected identifier in patch for list at {self.template}"
)
if self.template.patch.identifier.startswith(
"$"
) and not self.template.patch.identifier.startswith("$ref"):
Expand All @@ -144,24 +140,35 @@ 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(data_to_add: dict[str, Any], matched_value: list) -> None:
assert self.template.patch is not None # mypy
if not self.template.patch.identifier:
raise ValueError(
f"Expected identifier in patch for list at {self.template}"
)
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(d, matched_value)
else:
matched_value[index] = data_to_add
update_data(data_to_add, matched_value)
else:
matched_value.update(data_to_add)

Expand Down