Skip to content

Commit

Permalink
one_host: Fix ID logic (#8907)
Browse files Browse the repository at this point in the history
* Fix one_host module

* Add CHANGELOG fragment

* PR Fixes

* Update exceptions

(cherry picked from commit 8df9d0d)
  • Loading branch information
abakanovskii authored and patchback[bot] committed Oct 10, 2024
1 parent 6f98adf commit 038ab1c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8907-fix-one-host-id.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- one_host - fix if statements for cases when ``ID=0`` (https://github.com/ansible-collections/community.general/issues/1199, https://github.com/ansible-collections/community.general/pull/8907).
59 changes: 36 additions & 23 deletions plugins/modules/one_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,19 @@ def __init__(self):
def allocate_host(self):
"""
Creates a host entry in OpenNebula
self.one.host.allocate returns ID of a host
Returns: True on success, fails otherwise.
"""
if not self.one.host.allocate(self.get_parameter('name'),
self.get_parameter('vmm_mad_name'),
self.get_parameter('im_mad_name'),
self.get_parameter('cluster_id')):
self.fail(msg="could not allocate host")
else:
try:
self.one.host.allocate(self.get_parameter('name'),
self.get_parameter('vmm_mad_name'),
self.get_parameter('im_mad_name'),
self.get_parameter('cluster_id'))
self.result['changed'] = True
except Exception as e:
self.fail(msg="Could not allocate host, ERROR: " + str(e))

return True

def wait_for_host_state(self, host, target_states):
Expand Down Expand Up @@ -221,11 +224,13 @@ def run(self, one, module, result):
if current_state == HOST_ABSENT:
self.fail(msg='absent host cannot be put in disabled state')
elif current_state in [HOST_STATES.MONITORED, HOST_STATES.OFFLINE]:
if one.host.status(host.ID, HOST_STATUS.DISABLED):
self.wait_for_host_state(host, [HOST_STATES.DISABLED])
# returns host ID integer
try:
one.host.status(host.ID, HOST_STATUS.DISABLED)
result['changed'] = True
else:
self.fail(msg="could not disable host")
except Exception as e:
self.fail(msg="Could not disable host, ERROR: " + str(e))
self.wait_for_host_state(host, [HOST_STATES.DISABLED])
elif current_state in [HOST_STATES.DISABLED]:
pass
else:
Expand All @@ -235,22 +240,26 @@ def run(self, one, module, result):
if current_state == HOST_ABSENT:
self.fail(msg='absent host cannot be placed in offline state')
elif current_state in [HOST_STATES.MONITORED, HOST_STATES.DISABLED]:
if one.host.status(host.ID, HOST_STATUS.OFFLINE):
self.wait_for_host_state(host, [HOST_STATES.OFFLINE])
# returns host ID integer
try:
one.host.status(host.ID, HOST_STATUS.OFFLINE)
result['changed'] = True
else:
self.fail(msg="could not set host offline")
except Exception as e:
self.fail(msg="Could not set host offline, ERROR: " + str(e))
self.wait_for_host_state(host, [HOST_STATES.OFFLINE])
elif current_state in [HOST_STATES.OFFLINE]:
pass
else:
self.fail(msg="unknown host state %s, cowardly refusing to change state to offline" % current_state_name)

elif desired_state == 'absent':
if current_state != HOST_ABSENT:
if one.host.delete(host.ID):
# returns host ID integer
try:
one.host.delete(host.ID)
result['changed'] = True
else:
self.fail(msg="could not delete host from cluster")
except Exception as e:
self.fail(msg="Could not delete host from cluster, ERROR: " + str(e))

# if we reach this point we can assume that the host was taken to the desired state

Expand All @@ -268,17 +277,21 @@ def run(self, one, module, result):
if self.requires_template_update(host.TEMPLATE, desired_template_changes):
# setup the root element so that pyone will generate XML instead of attribute vector
desired_template_changes = {"TEMPLATE": desired_template_changes}
if one.host.update(host.ID, desired_template_changes, 1): # merge the template
# merge the template, returns host ID integer
try:
one.host.update(host.ID, desired_template_changes, 1)
result['changed'] = True
else:
self.fail(msg="failed to update the host template")
except Exception as e:
self.fail(msg="Failed to update the host template, ERROR: " + str(e))

# the cluster
if host.CLUSTER_ID != self.get_parameter('cluster_id'):
if one.cluster.addhost(self.get_parameter('cluster_id'), host.ID):
# returns cluster id in int
try:
one.cluster.addhost(self.get_parameter('cluster_id'), host.ID)
result['changed'] = True
else:
self.fail(msg="failed to update the host cluster")
except Exception as e:
self.fail(msg="Failed to update the host cluster, ERROR: " + str(e))

# return
self.exit()
Expand Down

0 comments on commit 038ab1c

Please sign in to comment.