Fixed module from review inputs : - Caught the exception from import ovh to provide a proper message to the user - Removed unuseful brackets - Added a else to check the state instead of two if - Changed the module to be added to 2.0 - Added exceptions handling for all APIs calls with a clear message including the return from the API.

And :
- Fixed dependency of OVH api to 0.3.5
pull/18777/head
pascalheraud 9 years ago committed by Matt Clay
parent 926c80328d
commit f05f74d9f6

@ -1,5 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from ovh.exceptions import APIError
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
@ -7,12 +8,12 @@ module: ovh_ip_loadbalancing_backend
short_description: Manage OVH IP LoadBalancing backends short_description: Manage OVH IP LoadBalancing backends
description: description:
- Manage OVH (French European hosting provider) LoadBalancing IP backends - Manage OVH (French European hosting provider) LoadBalancing IP backends
version_added: "1.9" version_added: "2.0"
author: Pascal HERAUD @pascalheraud author: Pascal HERAUD @pascalheraud
notes: notes:
- Uses the python OVH Api U(https://github.com/ovh/python-ovh). You have to create an application (a key and secret) with a consummer key as described into U(https://eu.api.ovh.com/g934.first_step_with_api) - Uses the python OVH Api U(https://github.com/ovh/python-ovh). You have to create an application (a key and secret) with a consummer key as described into U(https://eu.api.ovh.com/g934.first_step_with_api)
requirements: requirements:
- ovh > 0.35 - ovh > 0.3.5
options: options:
name: name:
required: true required: true
@ -67,7 +68,12 @@ EXAMPLES = '''
''' '''
import sys import sys
import ovh try:
import ovh
import ovh.exceptions
HAS_OVH = True
except ImportError:
HAS_OVH = False
def getOvhClient(ansibleModule): def getOvhClient(ansibleModule):
endpoint = ansibleModule.params.get('endpoint') endpoint = ansibleModule.params.get('endpoint')
@ -100,6 +106,9 @@ def main():
consumer_key = dict(required=True), consumer_key = dict(required=True),
) )
) )
if not HAS_OVH:
module.fail_json(msg='ovh-api python module is required to run this module ')
# Get parameters # Get parameters
name = module.params.get('name') name = module.params.get('name')
@ -112,46 +121,65 @@ def main():
client = getOvhClient(module) client = getOvhClient(module)
# Check that the load balancing exists # Check that the load balancing exists
loadBalancings = client.get('/ip/loadBalancing') try :
loadBalancings = client.get('/ip/loadBalancing')
except APIError as apiError:
module.fail_json(msg='Unable to call OVH api for getting the list of loadBalancing, check application key, secret, consumerkey and parameters. Error returned by OVH api was : {}'.format(apiError))
if not name in loadBalancings: if not name in loadBalancings:
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) waitForNoTask(client, name)
backends = client.get('/ip/loadBalancing/{}/backend'.format(name)) try :
backends = client.get('/ip/loadBalancing/{}/backend'.format(name))
except APIError as apiError:
module.fail_json(msg='Unable to call OVH api for getting the list of backends of the loadBalancing, check application key, secret, consumerkey and parameters. Error returned by OVH api was : {}'.format(apiError))
backendExists = backend in backends backendExists = backend in backends
moduleChanged = False moduleChanged = False
if (state=="absent") : if state=="absent" :
if (backendExists) : if backendExists :
# Remove backend # Remove backend
client.delete('/ip/loadBalancing/{}/backend/{}'.format(name, backend)) try :
waitForNoTask(client, name) client.delete('/ip/loadBalancing/{}/backend/{}'.format(name, backend))
waitForNoTask(client, name)
except :
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 :
moduleChanged = False moduleChanged = False
if (state=="present") : else :
if (backendExists) : if backendExists :
moduleChanged = False moduleChanged = False
# Get properties # Get properties
backendProperties = client.get('/ip/loadBalancing/{}/backend/{}'.format(name, backend)) backendProperties = client.get('/ip/loadBalancing/{}/backend/{}'.format(name, backend))
if (backendProperties['weight'] != weight): if (backendProperties['weight'] != weight):
# Change weight # Change weight
client.post('/ip/loadBalancing/{}/backend/{}/setWeight'.format(name, backend), weight=weight) try :
waitForNoTask(client, name) client.post('/ip/loadBalancing/{}/backend/{}/setWeight'.format(name, backend), weight=weight)
waitForNoTask(client, name)
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))
moduleChanged = True moduleChanged = True
if (backendProperties['probe'] != probe): if (backendProperties['probe'] != probe):
# Change probe # Change probe
backendProperties['probe'] = probe backendProperties['probe'] = probe
client.put('/ip/loadBalancing/{}/backend/{}'.format(name, backend), probe=probe ) try:
waitForNoTask(client, name) client.put('/ip/loadBalancing/{}/backend/{}'.format(name, backend), probe=probe )
waitForNoTask(client, name)
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))
moduleChanged = True moduleChanged = True
else : else :
# Creates backend # Creates backend
client.post('/ip/loadBalancing/{}/backend'.format(name), ipBackend=backend, probe=probe, weight=weight) try:
waitForNoTask(client, name) client.post('/ip/loadBalancing/{}/backend'.format(name), ipBackend=backend, probe=probe, weight=weight)
waitForNoTask(client, name)
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))
moduleChanged = True moduleChanged = True
module.exit_json(changed=moduleChanged) module.exit_json(changed=moduleChanged)

Loading…
Cancel
Save