Skip to content

Add --diff support to some IPA modules - #1415

Open
NavidSassan wants to merge 2 commits into
freeipa:masterfrom
Linuxfabrik:diff_support
Open

Add --diff support to some IPA modules#1415
NavidSassan wants to merge 2 commits into
freeipa:masterfrom
Linuxfabrik:diff_support

Conversation

@NavidSassan

@NavidSassan NavidSassan commented Mar 26, 2026

Copy link
Copy Markdown

This PR adds --diff support to the following 8 modules:

  • ipagroup
  • ipahbacrule
  • ipahostgroup
  • ipapwpolicy
  • ipasudocmd
  • ipasudocmdgroup
  • ipasudorule
  • ipauser

(since these are the ones that we are using for our LFOps Ansible Collection right now).

This partly fixes #483.

Summary by Sourcery

Add structured diff reporting for IPA modules to support Ansible --diff output when managing IPA objects and their memberships.

New Features:

  • Expose before/after state in Ansible --diff output for ipagroup, ipahbacrule, ipahostgroup, ipapwpolicy, ipasudocmd, ipasudocmdgroup, ipasudorule, and ipauser modules, including creation, deletion, attribute updates, membership changes, and enable/disable operations.

Enhancements:

  • Introduce a shared IPADiffTracker utility and helper functions to compute and merge attribute and membership diffs consistently across IPA modules, masking sensitive values like user passwords in diff output.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The diff tracking logic for present/absent/member actions in several modules (e.g. ipagroup, ipahostgroup, ipahbacrule, ipasudorule) is quite repetitive; consider extracting common patterns into small helpers (e.g. a function that takes _orig, add/del lists, and the resource name) to reduce duplication and make future changes less error‑prone.
  • In ipauser, the member diff handling is asymmetric between state: present and state: absent (e.g. manager_add/manager_del, principal_add/principal_del), which makes the code harder to reason about; consider harmonizing the use of add/del lists in both directions so the diff generation follows a consistent pattern.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The diff tracking logic for present/absent/member actions in several modules (e.g. ipagroup, ipahostgroup, ipahbacrule, ipasudorule) is quite repetitive; consider extracting common patterns into small helpers (e.g. a function that takes `_orig`, add/del lists, and the resource name) to reduce duplication and make future changes less error‑prone.
- In ipauser, the member diff handling is asymmetric between `state: present` and `state: absent` (e.g. `manager_add`/`manager_del`, `principal_add`/`principal_del`), which makes the code harder to reason about; consider harmonizing the use of add/del lists in both directions so the diff generation follows a consistent pattern.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

…rs_diff

Address sourcery-ai feedback on PR freeipa#1415:

* Extract the per-resource member-list diff loop, which was duplicated
  between state=present action=member and state=absent action=member,
  into a single specs-driven helper `gen_members_diff(res_find, specs)`
  in module_utils. Each spec is a tuple of
  `(member_key, add_list, del_list, current_source)`, where
  `current_source` may be a single attribute name in the IPA response,
  a list of attribute names (concatenated; for IPA fields that split
  one logical list, e.g. memberhost_host + externalhost), or a
  precomputed list.

* Use the new helper in ipagroup, ipahbacrule, ipahostgroup,
  ipasudorule and ipasudocmdgroup. Each module now declares its member
  specs once and reuses them across the present/<resource>,
  present/member and absent/member branches.

* In ipauser, harmonise the member-diff handling between state=present
  action=user, state=present action=member and state=absent action=member:
  all *_add / *_del lists are now initialised to [] at the top of the
  loop, each branch only fills the lists it actually touches, and a
  single post-state-branch block calls gen_members_diff() with the same
  member_specs in all three cases. The diff generation no longer flips
  between two patterns.

No behaviour change. Net diff: +228 / -351 lines across 7 files.
@markuslf

Copy link
Copy Markdown

@t-woerner @rjeffman would appreciate a review when you have a slot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RFE: Support Ansible diff mode

2 participants