Added a timeout param to prevent infinite loop while waiting for

completion of a task.
reviewable/pr18780/r1
pascalheraud 9 years ago committed by Toshio Kuratomi
parent 37900daf79
commit f7a5013083

@ -56,6 +56,12 @@ options:
required: true required: true
description: description:
- The consumer key to use - The consumer key to use
timeout:
required: false
type: "int"
default: "120
descriptin:
- The timeout in seconds used to wait for a task to be completed. Default is 120 seconds.
''' '''
@ -88,22 +94,28 @@ def getOvhClient(ansibleModule):
consumer_key=consumer_key consumer_key=consumer_key
) )
def waitForNoTask(client, name): def waitForNoTask(client, name, timeout):
currentTimeout = timeout;
while len(client.get('/ip/loadBalancing/{}/task'.format(name)))>0: while len(client.get('/ip/loadBalancing/{}/task'.format(name)))>0:
time.sleep(1) # Delay for 1 sec time.sleep(1) # Delay for 1 sec
currentTimeout-=1
if currentTimeout < 0:
return False
return True
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
name = dict(required=True), name = dict(required=True),
backend = dict(required=True), backend = dict(required=True),
weight = dict(default='8'), weight = dict(default='8', type='int'),
probe = dict(default='none', choices =['none', 'http', 'icmp' , 'oco']), probe = dict(default='none', choices =['none', 'http', 'icmp' , 'oco']),
state = dict(default='present', choices=['present', 'absent']), state = dict(default='present', choices=['present', 'absent']),
endpoint = dict(required=True), endpoint = dict(required=True),
application_key = dict(required=True), application_key = dict(required=True),
application_secret = dict(required=True), application_secret = dict(required=True),
consumer_key = dict(required=True), consumer_key = dict(required=True),
timeout = dict(default=120, type='int')
) )
) )
@ -116,6 +128,7 @@ def main():
backend = module.params.get('backend') backend = module.params.get('backend')
weight = long(module.params.get('weight')) weight = long(module.params.get('weight'))
probe = module.params.get('probe') probe = module.params.get('probe')
timeout = module.params.get('timeout')
# Connect to OVH API # Connect to OVH API
client = getOvhClient(module) client = getOvhClient(module)
@ -130,7 +143,8 @@ def main():
module.fail_json(msg='IP LoadBalancing {} does not exist'.format(name)) module.fail_json(msg='IP LoadBalancing {} does not exist'.format(name))
# Check that no task is pending before going on # Check that no task is pending before going on
waitForNoTask(client, name) if not waitForNoTask(client, name, timeout):
module.fail_json(msg='Timeout of {} seconds while waiting for no pending tasks before executing the module '.format(timeout))
try : try :
backends = client.get('/ip/loadBalancing/{}/backend'.format(name)) backends = client.get('/ip/loadBalancing/{}/backend'.format(name))
@ -144,8 +158,9 @@ def main():
# Remove backend # Remove backend
try : try :
client.delete('/ip/loadBalancing/{}/backend/{}'.format(name, backend)) client.delete('/ip/loadBalancing/{}/backend/{}'.format(name, backend))
waitForNoTask(client, name) if not waitForNoTask(client, name, timeout):
except : module.fail_json(msg='Timeout of {} seconds while waiting for completion of removing backend task'.format(timeout))
except APIError as apiError:
module.fail_json(msg='Unable to call OVH api for deleting the backend, check application key, secret, consumerkey and parameters. Error returned by OVH api was : {}'.format(apiError)) module.fail_json(msg='Unable to call OVH api for deleting the backend, check application key, secret, consumerkey and parameters. Error returned by OVH api was : {}'.format(apiError))
moduleChanged = True moduleChanged = True
else : else :
@ -159,7 +174,8 @@ def main():
# Change weight # Change weight
try : try :
client.post('/ip/loadBalancing/{}/backend/{}/setWeight'.format(name, backend), weight=weight) client.post('/ip/loadBalancing/{}/backend/{}/setWeight'.format(name, backend), weight=weight)
waitForNoTask(client, name) if not waitForNoTask(client, name, timeout):
module.fail_json(msg='Timeout of {} seconds while waiting for completion of setWeight to backend task'.format(timeout))
except APIError as apiError: except APIError as apiError:
module.fail_json(msg='Unable to call OVH api for updating the weight of the backend, check application key, secret, consumerkey and parameters. Error returned by OVH api was : {}'.format(apiError)) module.fail_json(msg='Unable to call OVH api for updating the weight of the backend, check application key, secret, consumerkey and parameters. Error returned by OVH api was : {}'.format(apiError))
moduleChanged = True moduleChanged = True
@ -168,7 +184,8 @@ def main():
backendProperties['probe'] = probe backendProperties['probe'] = probe
try: try:
client.put('/ip/loadBalancing/{}/backend/{}'.format(name, backend), probe=probe ) client.put('/ip/loadBalancing/{}/backend/{}'.format(name, backend), probe=probe )
waitForNoTask(client, name) if not waitForNoTask(client, name, timeout):
module.fail_json(msg='Timeout of {} seconds while waiting for completion of setProbe to backend task'.format(timeout))
except APIError as apiError: except APIError as apiError:
module.fail_json(msg='Unable to call OVH api for updating the propbe of the backend, check application key, secret, consumerkey and parameters. Error returned by OVH api was : {}'.format(apiError)) module.fail_json(msg='Unable to call OVH api for updating the propbe of the backend, check application key, secret, consumerkey and parameters. Error returned by OVH api was : {}'.format(apiError))
moduleChanged = True moduleChanged = True
@ -177,7 +194,8 @@ def main():
# Creates backend # Creates backend
try: try:
client.post('/ip/loadBalancing/{}/backend'.format(name), ipBackend=backend, probe=probe, weight=weight) client.post('/ip/loadBalancing/{}/backend'.format(name), ipBackend=backend, probe=probe, weight=weight)
waitForNoTask(client, name) if not waitForNoTask(client, name, timeout):
module.fail_json(msg='Timeout of {} seconds while waiting for completion of backend creation task'.format(timeout))
except APIError as apiError: except APIError as apiError:
module.fail_json(msg='Unable to call OVH api for creating the backend, check application key, secret, consumerkey and parameters. Error returned by OVH api was : {}'.format(apiError)) module.fail_json(msg='Unable to call OVH api for creating the backend, check application key, secret, consumerkey and parameters. Error returned by OVH api was : {}'.format(apiError))
moduleChanged = True moduleChanged = True

Loading…
Cancel
Save