Skip to content

Commit

Permalink
Add rudimentary wait for os_zone
Browse files Browse the repository at this point in the history
This adds a rudimentary wait functionality for the os_zone module.

It will wait for:
* Zone not in OpenStack API when choosing `absent`
* Zone `status` being `AVAILABLE` in OpenStack when choosing `present`
  • Loading branch information
ederst committed Mar 26, 2019
1 parent 34868f0 commit ca0d0f7
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/ansible/modules/cloud/openstack/os_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,25 @@ def _system_state_change(state, email, description, ttl, masters, zone):
return False


def _wait(timeout, cloud, zone, state, module, sdk):
"""Wait for a zone to reach the desired state for the given state."""

for count in sdk.utils.iterate_timeout(
timeout,
"Timeout waiting for zone to be %s" % state):

if (state == 'absent' and zone is None) or (state == 'present' and zone and zone.status == 'ACTIVE'):
return

try:
zone = cloud.get_zone(zone.id)
except Exception:
continue

if zone and zone.status == 'ERROR':
module.fail_json(msg="Zone reached ERROR state while waiting for it to be %s" % state)


def main():
argument_spec = openstack_full_argument_spec(
name=dict(required=True),
Expand All @@ -156,6 +175,8 @@ def main():

name = module.params.get('name')
state = module.params.get('state')
wait = module.params.get('wait')
timeout = module.params.get('timeout')

sdk, cloud = openstack_cloud_from_module(module)
try:
Expand Down Expand Up @@ -191,6 +212,10 @@ def main():
name, email=email,
description=description,
ttl=ttl, masters=masters)

if wait:
_wait(timeout, cloud, zone, state, module, sdk)

module.exit_json(changed=changed, zone=zone)

elif state == 'absent':
Expand All @@ -204,6 +229,10 @@ def main():
else:
cloud.delete_zone(name)
changed = True

if wait:
_wait(timeout, cloud, zone, state, module, sdk)

module.exit_json(changed=changed)

except sdk.exceptions.OpenStackCloudException as e:
Expand Down

0 comments on commit ca0d0f7

Please sign in to comment.