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
This commit is contained in:
parent
a767da1139
commit
37900daf79
1 changed files with 45 additions and 17 deletions
|
@ -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')
|
||||||
|
@ -101,6 +107,9 @@ def main():
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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')
|
||||||
state = module.params.get('state')
|
state = module.params.get('state')
|
||||||
|
@ -112,46 +121,65 @@ def main():
|
||||||
client = getOvhClient(module)
|
client = getOvhClient(module)
|
||||||
|
|
||||||
# Check that the load balancing exists
|
# Check that the load balancing exists
|
||||||
|
try :
|
||||||
loadBalancings = client.get('/ip/loadBalancing')
|
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)
|
||||||
|
|
||||||
|
try :
|
||||||
backends = client.get('/ip/loadBalancing/{}/backend'.format(name))
|
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
|
||||||
|
try :
|
||||||
client.delete('/ip/loadBalancing/{}/backend/{}'.format(name, backend))
|
client.delete('/ip/loadBalancing/{}/backend/{}'.format(name, backend))
|
||||||
waitForNoTask(client, name)
|
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
|
||||||
|
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)
|
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
|
||||||
|
try:
|
||||||
client.put('/ip/loadBalancing/{}/backend/{}'.format(name, backend), probe=probe )
|
client.put('/ip/loadBalancing/{}/backend/{}'.format(name, backend), probe=probe )
|
||||||
waitForNoTask(client, name)
|
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
|
||||||
|
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)
|
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…
Reference in a new issue