Fixed the module to be compliant with pep8
This commit is contained in:
parent
1972df5a71
commit
7c9fdba5ad
1 changed files with 154 additions and 90 deletions
|
@ -1,5 +1,16 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
import sys
|
||||||
|
try:
|
||||||
|
import ovh
|
||||||
|
import ovh.exceptions
|
||||||
|
HAS_OVH = True
|
||||||
|
except ImportError:
|
||||||
|
HAS_OVH = False
|
||||||
|
|
||||||
|
# import module snippets
|
||||||
|
from ansible.module_utils.basic import *
|
||||||
|
# and APIError from ovh api
|
||||||
from ovh.exceptions import APIError
|
from ovh.exceptions import APIError
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
|
@ -11,8 +22,10 @@ description:
|
||||||
version_added: "2.0"
|
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). \
|
||||||
requirements:
|
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:
|
||||||
- ovh > 0.3.5
|
- ovh > 0.3.5
|
||||||
options:
|
options:
|
||||||
name:
|
name:
|
||||||
|
@ -28,7 +41,8 @@ options:
|
||||||
default: present
|
default: present
|
||||||
choices: ['present', 'absent']
|
choices: ['present', 'absent']
|
||||||
description:
|
description:
|
||||||
- Determines wether the backend is to be created/modified or deleted
|
- Determines wether the backend is to be created/modified \
|
||||||
|
or deleted
|
||||||
probe:
|
probe:
|
||||||
required: false
|
required: false
|
||||||
default: none
|
default: none
|
||||||
|
@ -59,33 +73,32 @@ options:
|
||||||
timeout:
|
timeout:
|
||||||
required: false
|
required: false
|
||||||
type: "int"
|
type: "int"
|
||||||
default: "120
|
default: 120
|
||||||
descriptin:
|
description:
|
||||||
- The timeout in seconds used to wait for a task to be completed. Default is 120 seconds.
|
- The timeout in seconds used to wait for a task to be \
|
||||||
|
completed. Default is 120 seconds.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
# Adds or modify a backend to a loadbalancing
|
# Adds or modify a backend to a loadbalancing
|
||||||
- ovh_ip_loadbalancing name=ip-1.1.1.1 ip=212.1.1.1 state=present probe=none weight=8 endpoint=ovh-eu application_key=yourkey application_secret=yoursecret consumer_key=yourconsumerkey
|
- ovh_ip_loadbalancing name=ip-1.1.1.1 ip=212.1.1.1 state=present \
|
||||||
|
probe=none weight=8 \
|
||||||
|
endpoint=ovh-eu application_key=yourkey \
|
||||||
|
application_secret=yoursecret consumer_key=yourconsumerkey
|
||||||
|
|
||||||
# Removes a backend from a loadbalancing
|
# Removes a backend from a loadbalancing
|
||||||
- ovh_ip_loadbalancing name=ip-1.1.1.1 ip=212.1.1.1 state=absent endpoint=ovh-eu application_key=yourkey application_secret=yoursecret consumer_key=yourconsumerkey
|
- ovh_ip_loadbalancing name=ip-1.1.1.1 ip=212.1.1.1 state=absent \
|
||||||
|
endpoint=ovh-eu application_key=yourkey \
|
||||||
|
application_secret=yoursecret consumer_key=yourconsumerkey
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import sys
|
|
||||||
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')
|
||||||
application_key = ansibleModule.params.get('application_key')
|
application_key = ansibleModule.params.get('application_key')
|
||||||
application_secret = ansibleModule.params.get('application_secret')
|
application_secret = ansibleModule.params.get('application_secret')
|
||||||
consumer_key = ansibleModule.params.get('consumer_key')
|
consumer_key = ansibleModule.params.get('consumer_key')
|
||||||
|
|
||||||
return ovh.Client(
|
return ovh.Client(
|
||||||
endpoint=endpoint,
|
endpoint=endpoint,
|
||||||
|
@ -94,128 +107,179 @@ def getOvhClient(ansibleModule):
|
||||||
consumer_key=consumer_key
|
consumer_key=consumer_key
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def waitForNoTask(client, name, timeout):
|
def waitForNoTask(client, name, timeout):
|
||||||
currentTimeout = 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
|
currentTimeout -= 1
|
||||||
if currentTimeout < 0:
|
if currentTimeout < 0:
|
||||||
return False
|
return False
|
||||||
return True
|
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', type='int'),
|
weight=dict(default='8', type='int'),
|
||||||
probe = dict(default='none', choices =['none', 'http', 'icmp' , 'oco']),
|
probe=dict(default='none',
|
||||||
state = dict(default='present', choices=['present', 'absent']),
|
choices=['none', 'http', 'icmp', 'oco']),
|
||||||
endpoint = dict(required=True),
|
state=dict(default='present', choices=['present', 'absent']),
|
||||||
application_key = dict(required=True),
|
endpoint=dict(required=True),
|
||||||
application_secret = dict(required=True),
|
application_key=dict(required=True),
|
||||||
consumer_key = dict(required=True),
|
application_secret=dict(required=True),
|
||||||
timeout = dict(default=120, type='int')
|
consumer_key=dict(required=True),
|
||||||
|
timeout=dict(default=120, type='int')
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if not HAS_OVH:
|
if not HAS_OVH:
|
||||||
module.fail_json(msg='ovh-api python module is required to run this module ')
|
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')
|
||||||
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')
|
timeout = module.params.get('timeout')
|
||||||
|
|
||||||
# Connect to OVH API
|
# Connect to OVH API
|
||||||
client = getOvhClient(module)
|
client = getOvhClient(module)
|
||||||
|
|
||||||
# Check that the load balancing exists
|
# Check that the load balancing exists
|
||||||
try :
|
try:
|
||||||
loadBalancings = client.get('/ip/loadBalancing')
|
loadBalancings = client.get('/ip/loadBalancing')
|
||||||
except APIError as apiError:
|
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))
|
module.fail_json(
|
||||||
|
msg='Unable to call OVH api for getting the list of loadBalancing, \
|
||||||
if not name in loadBalancings:
|
check application key, secret, consumerkey and parameters. \
|
||||||
|
Error returned by OVH api was : {}'.format(apiError))
|
||||||
|
|
||||||
|
if name not 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
|
||||||
try :
|
try:
|
||||||
if not waitForNoTask(client, name, timeout):
|
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))
|
module.fail_json(
|
||||||
|
msg='Timeout of {} seconds while waiting for no pending \
|
||||||
|
tasks before executing the module '.format(timeout))
|
||||||
except APIError as apiError:
|
except APIError as apiError:
|
||||||
module.fail_json(msg='Unable to call OVH api for getting the list of pending tasks of the loadBalancing, 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 getting the list of pending tasks \
|
||||||
|
of the loadBalancing, check application key, secret, consumerkey \
|
||||||
|
and parameters. Error returned by OVH api was : {}\
|
||||||
|
'.format(apiError))
|
||||||
|
|
||||||
try :
|
try:
|
||||||
backends = client.get('/ip/loadBalancing/{}/backend'.format(name))
|
backends = client.get('/ip/loadBalancing/{}/backend'.format(name))
|
||||||
except APIError as apiError:
|
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))
|
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 :
|
try:
|
||||||
client.delete('/ip/loadBalancing/{}/backend/{}'.format(name, backend))
|
client.delete(
|
||||||
if not waitForNoTask(client, name, timeout):
|
'/ip/loadBalancing/{}/backend/{}'.format(name, backend))
|
||||||
module.fail_json(msg='Timeout of {} seconds while waiting for completion of removing backend task'.format(timeout))
|
if not waitForNoTask(client, name, timeout):
|
||||||
|
module.fail_json(
|
||||||
|
msg='Timeout of {} seconds while waiting for completion \
|
||||||
|
of removing backend task'.format(timeout))
|
||||||
except APIError as apiError:
|
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:
|
||||||
if backendExists :
|
if backendExists:
|
||||||
# Get properties
|
# Get properties
|
||||||
try :
|
try:
|
||||||
backendProperties = client.get('/ip/loadBalancing/{}/backend/{}'.format(name, backend))
|
backendProperties = client.get(
|
||||||
|
'/ip/loadBalancing/{}/backend/{}'.format(name, backend))
|
||||||
except APIError as apiError:
|
except APIError as apiError:
|
||||||
module.fail_json(msg='Unable to call OVH api for getting the backend properties, 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 getting the backend properties, \
|
||||||
|
check application key, secret, consumerkey and \
|
||||||
|
parameters. Error returned by OVH api was : {}\
|
||||||
|
'.format(apiError))
|
||||||
|
|
||||||
if (backendProperties['weight'] != weight):
|
if (backendProperties['weight'] != weight):
|
||||||
# Change weight
|
# Change weight
|
||||||
try :
|
try:
|
||||||
client.post('/ip/loadBalancing/{}/backend/{}/setWeight'.format(name, backend), weight=weight)
|
client.post(
|
||||||
if not waitForNoTask(client, name, timeout):
|
'/ip/loadBalancing/{}/backend/{}/setWeight\
|
||||||
module.fail_json(msg='Timeout of {} seconds while waiting for completion of setWeight to backend task'.format(timeout))
|
'.format(name, backend), weight=weight)
|
||||||
|
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
|
||||||
|
|
||||||
if (backendProperties['probe'] != probe):
|
if (backendProperties['probe'] != probe):
|
||||||
# Change probe
|
# Change probe
|
||||||
backendProperties['probe'] = probe
|
backendProperties['probe'] = probe
|
||||||
try:
|
try:
|
||||||
client.put('/ip/loadBalancing/{}/backend/{}'.format(name, backend), probe=probe )
|
client.put(
|
||||||
if not waitForNoTask(client, name, timeout):
|
'/ip/loadBalancing/{}/backend/{}\
|
||||||
module.fail_json(msg='Timeout of {} seconds while waiting for completion of setProbe to backend task'.format(timeout))
|
'.format(name, backend), probe=probe)
|
||||||
|
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
|
||||||
|
|
||||||
else :
|
else:
|
||||||
# Creates backend
|
# Creates backend
|
||||||
try:
|
try:
|
||||||
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)
|
||||||
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 \
|
||||||
if not waitForNoTask(client, name, timeout):
|
application key, secret, consumerkey and parameters. \
|
||||||
module.fail_json(msg='Timeout of {} seconds while waiting for completion of backend creation task'.format(timeout))
|
Error returned by OVH api was : {}'.format(apiError))
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
module.exit_json(changed=moduleChanged)
|
module.exit_json(changed=moduleChanged)
|
||||||
|
|
||||||
# We should never reach here
|
# We should never reach here
|
||||||
module.fail_json(msg='Internal ovh_ip_loadbalancing_backend module error')
|
module.fail_json(msg='Internal ovh_ip_loadbalancing_backend module error')
|
||||||
|
|
||||||
|
|
||||||
# import module snippets
|
|
||||||
from ansible.module_utils.basic import *
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue