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

Fix idempotency for relative rule location #517

Merged
merged 2 commits into from
Jan 18, 2024

Conversation

meni2029
Copy link
Contributor

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no API changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

Rule module: the location options rule_id and folder are mutually exclusive. The problem comes when creating a rule with a relative location to an existing rule_id (before/after) and an expected folder other than Main. The expected folder for the rule is not known by the module, therefore it is not capable to recognise an already existing rule. It always compares with rules in default folder Main. Idempotency is not achieved for rules outside of Main folder and duplicates are created at each Ansible run.

What is the new behavior?

  • new function get_rule_by_id
  • modified function get_existing_rule : when location option rule_id is set, the expected folder is retrieved from the neighbour rule
  • now the function get_existing_rule is able to acurately check if the rule already exists in the expected folder

Other information

@github-actions github-actions bot added the module:rule This affects the rule module label Dec 27, 2023
@robin-checkmk robin-checkmk added the bug Something isn't working label Dec 28, 2023
@robin-checkmk robin-checkmk mentioned this pull request Dec 28, 2023
7 tasks
@robin-checkmk
Copy link
Member

Thank you for this contribution @meni2029! 🙏
Our code-savvy people are on well-deserved holiday, but as soon as they are back, they will take a look at your changes.
Have you seen #508? 👀 I am not sure, if these two PRs somehow correlate, but @lgetwan should be able to distinguish that, once he is back.

@lgetwan
Copy link
Contributor

lgetwan commented Jan 5, 2024

Thanks for detecting this incorrect behavior, @meni2029!
If I understood your code correctly, it improves the situation already, but considering this case

  • The desired rule is already in the correct folder but not located before/after its intended neighbor rule

the task will reply a "changed" instead of an "ok". Fixing this would bring us one step closer to idempotency (which is really challenging for this module... 🤯).

@meni2029
Copy link
Contributor Author

meni2029 commented Jan 8, 2024

Thanks @lgetwan for looking at my PR.

About:

* The desired rule is already in the correct folder but not located before/after its intended neighbor rule

If the rule already exists it will anyway ignore it (create_rule function returns not changed)

def create_rule(module, base_url, headers, ruleset, rule):
    api_endpoint = "/domain-types/rule/collections/all"
    changed = True
    e = get_existing_rule(module, base_url, headers, ruleset, rule)
    if e:
        return (e["id"], not changed)

it will not try to move it, the task will return "ok".
It is a pity that already existing rules cannot be moved in subsequent runs, but this is another story.

This PR allows the module to recognise the existing rule in the expected folder (when other than default Main) and avoid creating duplicates (when using before/after rule_id). We're running it for our deployments now and it looks good.

Copy link
Contributor

@lgetwan lgetwan left a comment

Choose a reason for hiding this comment

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

You're right! The module is still not perfect, but your PR brings an improvement. Thanks for your contribution!

@robin-checkmk robin-checkmk added the release:4.2.0 Affects the mentioned release. label Jan 11, 2024
@robin-checkmk robin-checkmk merged commit ec37b6d into Checkmk:devel Jan 18, 2024
21 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jan 18, 2024
@meni2029 meni2029 deleted the fix/module_rule_relative_id branch January 19, 2024 08:57
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working module:rule This affects the rule module release:4.2.0 Affects the mentioned release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants