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

one_host: Fix ID logic #8907

Merged
merged 4 commits into from
Oct 10, 2024

Conversation

abakanovskii
Copy link
Contributor

SUMMARY

Fix if statements when ID equals to 0

Fixes #1199

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

one_host

ADDITIONAL INFORMATION

So I tested all of the one. commands and It seem to be returning ID (integer) of created/updated host or cluster
So using simple if some_variable: will not work if ID=0 because bool(0) = False and since and since OpenNebula starts ID counter from zero thats a big problem

# Now this works if ID is 0
    - name: Add host to OpenNebula
      community.general.one_host:
        api_url: "{{ one_api_url }}"
        api_username: "{{ one_api_username }}"
        api_password: "{{ one_api_password }}"
        name: "{{ one_host }}"
        im_mad_name: kvm
        vmm_mad_name: kvm
        template:
          TYPE: "testing"
        state: present

@ansibullbot
Copy link
Collaborator

@ansibullbot ansibullbot added bug This issue/PR relates to a bug module module plugins plugin (any type) labels Sep 24, 2024
plugins/modules/one_host.py Outdated Show resolved Hide resolved
@felixfontein felixfontein added check-before-release PR will be looked at again shortly before release and merged if possible. backport-8 Automatically create a backport for the stable-8 branch backport-9 Automatically create a backport for the stable-9 branch labels Sep 24, 2024
self.result['changed'] = True
except Exception as e:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just to be on the safe side, have you checked whether the other if statements you are modifying do not fall under the same situation?
Methods:

  • one.host.status()
  • one.host.delete()
  • one.host.update()
  • one.host.addhost()
    Don't they also trigger exceptions when parameters are invalid?

Copy link
Contributor Author

@abakanovskii abakanovskii Sep 30, 2024

Choose a reason for hiding this comment

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

they are all almost the same in terms of error handling but it seems to me that other if statements look right

here are 2 examples for one.host.status(), 1 when running with no parameters, 2 when running with wrong parameter
image

Copy link
Collaborator

Choose a reason for hiding this comment

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

So, in that case, wouldn't it be good to add the try-except around them below as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you please show which if statemets are wrong as well? From my perspective other if statements look right

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would not go as far as saying they are wrong, but the same way that you transformed the if statement into a try-except, maybe those could benefit from that as well - line numbers on your branch (right hand side):

  • line 229
  • line 245
  • line 259
  • line 282
  • line 291

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops, I completely missed them, thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Beware that by using this, we are assuming that those calls will raise exceptions when failing (which makes sense, but not all developers write code that way). I would suggest you confirm that if you haven't yet.

Other than that, LGTM.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, it's too much IMO, no need to use it for every call :)
I simply do not use this module that much, so if one day I would need to use this module directly then might as well check if these are necessary

@felixfontein felixfontein removed the backport-8 Automatically create a backport for the stable-8 branch label Oct 7, 2024
Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

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

LGTM

@russoz
Copy link
Collaborator

russoz commented Oct 10, 2024

I did hit the button twice by mistake. 🤦

@felixfontein felixfontein merged commit 8df9d0d into ansible-collections:main Oct 10, 2024
141 checks passed
Copy link

patchback bot commented Oct 10, 2024

Backport to stable-9: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-9/8df9d0d7dee12391a65b868d390b2dcd3e3803c6/pr-8907

Backported as #9014

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

@felixfontein felixfontein removed the check-before-release PR will be looked at again shortly before release and merged if possible. label Oct 10, 2024
patchback bot pushed a commit that referenced this pull request Oct 10, 2024
* Fix one_host module

* Add CHANGELOG fragment

* PR Fixes

* Update exceptions

(cherry picked from commit 8df9d0d)
@felixfontein
Copy link
Collaborator

@abakanovskii thanks for your contribution!
@russoz thanks for reviewing!

felixfontein pushed a commit that referenced this pull request Oct 10, 2024
one_host: Fix ID logic (#8907)

* Fix one_host module

* Add CHANGELOG fragment

* PR Fixes

* Update exceptions

(cherry picked from commit 8df9d0d)

Co-authored-by: alexander <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-9 Automatically create a backport for the stable-9 branch bug This issue/PR relates to a bug module module plugins plugin (any type)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

one_host fails when allocating or updating host with ID of 0
4 participants