1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

PEP8 Compliant except needed long lines and better exception handling

This commit is contained in:
Mischa Peters 2014-08-10 11:25:19 +02:00
parent a0ae43b311
commit 5b9b42056e
3 changed files with 264 additions and 97 deletions

View file

@ -113,29 +113,40 @@ ansible host -m a10_server -a "host=a10adc.example.com username=axapiuser passwo
import urllib2 import urllib2
def axapi_call(url, post=None): def axapi_call(url, post=None):
result = urllib2.urlopen(url, post).read() result = urllib2.urlopen(url, post).read()
return result return result
def axapi_authenticate(base_url, user, pwd): def axapi_authenticate(base_url, user, pwd):
url = base_url + '&method=authenticate&username=' + user + '&password=' + pwd url = base_url + '&method=authenticate&username=' + user + \
sessid = json.loads(axapi_call(url))['session_id'] '&password=' + pwd
result = base_url + '&session_id=' + sessid result = json.loads(axapi_call(url))
return result if 'response' in result:
return module.fail_json(msg=result['response']['err']['msg'])
sessid = result['session_id']
return base_url + '&session_id=' + sessid
def main(): def main():
global module global module
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
host = dict(type='str', required=True), host=dict(type='str', required=True),
username = dict(type='str', aliases=['user', 'admin'], required=True), username=dict(type='str', aliases=['user', 'admin'],
password = dict(type='str', aliases=['pass', 'pwd'], required=True), required=True),
server_name = dict(type='str', aliases=['server'], required=True), password=dict(type='str', aliases=['pass', 'pwd'], required=True),
server_ip = dict(type='str', aliases=['ip', 'address']), server_name=dict(type='str', aliases=['server'], required=True),
server_port = dict(type='int', aliases=['port']), server_ip=dict(type='str', aliases=['ip', 'address']),
server_protocol = dict(type='str', aliases=['proto', 'protocol'], choices=['tcp', 'udp']), server_port=dict(type='int', aliases=['port']),
server_status = dict(type='str', default='enable', aliases=['status'], choices=['enable', 'disable']), server_protocol=dict(type='str', aliases=['proto', 'protocol'],
state = dict(type='str', default='present', choices=['present', 'absent']), choices=['tcp', 'udp']),
server_status=dict(type='str', default='enable',
aliases=['status'],
choices=['enable', 'disable']),
state=dict(type='str', default='present',
choices=['present', 'absent']),
), ),
supports_check_mode=False supports_check_mode=False
) )
@ -152,7 +163,8 @@ def main():
axapi_base_url = 'https://' + host + '/services/rest/V2.1/?format=json' axapi_base_url = 'https://' + host + '/services/rest/V2.1/?format=json'
if slb_server_proto == 'tcp' or slb_server_proto == 'TCP' or slb_server_proto is None: if slb_server_proto == 'tcp' or slb_server_proto == 'TCP' or \
slb_server_proto is None:
protocol = '2' protocol = '2'
else: else:
protocol = '3' protocol = '3'
@ -166,26 +178,37 @@ def main():
module.fail_json(msg='server_name is required') module.fail_json(msg='server_name is required')
if slb_server_port is None: if slb_server_port is None:
json_post = {'server': { 'name': slb_server, 'host': slb_server_ip, 'status': status }} json_post = {'server': {'name': slb_server,
'host': slb_server_ip, 'status': status}}
else: else:
json_post = {'server': { 'name': slb_server, 'host': slb_server_ip, 'status': status, 'port_list': [{ 'port_num': slb_server_port, 'protocol': protocol }]}} json_post = {'server': {'name': slb_server, 'host': slb_server_ip,
'status': status, 'port_list':
[{'port_num': slb_server_port,
'protocol': protocol}]}}
try: try:
session_url = axapi_authenticate(axapi_base_url, user, pwd) session_url = axapi_authenticate(axapi_base_url, user, pwd)
if state == 'present': if state == 'present':
response = axapi_call(session_url + '&method=slb.server.search', json.dumps({ 'name': slb_server })) response = axapi_call(session_url + '&method=slb.server.search',
json.dumps({'name': slb_server}))
slb_server_exist = re.search(slb_server, response, re.I) slb_server_exist = re.search(slb_server, response, re.I)
if slb_server_exist is None: if slb_server_exist is None:
if slb_server_ip is None: if slb_server_ip is None:
module.fail_json(msg='IP address is required') module.fail_json(msg='IP address is required')
response = axapi_call(session_url + '&method=slb.server.create', json.dumps(json_post)) response = axapi_call(session_url +
'&method=slb.server.create',
json.dumps(json_post))
else: else:
response = axapi_call(session_url + '&method=slb.server.update', json.dumps(json_post)) response = axapi_call(session_url +
'&method=slb.server.update',
json.dumps(json_post))
if state == 'absent': if state == 'absent':
response = axapi_call(session_url + '&method=slb.server.delete', json.dumps({ 'name': slb_server })) response = axapi_call(session_url +
'&method=slb.server.delete',
json.dumps({'name': slb_server}))
result = json.loads(response) result = json.loads(response)
axapi_call(session_url + '&method=session.close') axapi_call(session_url + '&method=session.close')
@ -193,6 +216,9 @@ def main():
except Exception, e: except Exception, e:
return module.fail_json(msg='received exception: %s' % e) return module.fail_json(msg='received exception: %s' % e)
if 'respone' in result and 'err' in result['response']:
return module.fail_json(msg=result['response']['err']['msg'])
module.exit_json(changed=True, content=result) module.exit_json(changed=True, content=result)
from ansible.module_utils.basic import * from ansible.module_utils.basic import *

View file

@ -121,30 +121,56 @@ ansible host -m a10_service_group -a "host=a10adc.example.com username=axapiuser
import urllib2 import urllib2
def axapi_call(url, post=None): def axapi_call(url, post=None):
result = urllib2.urlopen(url, post).read() result = urllib2.urlopen(url, post).read()
return result return result
def axapi_authenticate(base_url, user, pwd): def axapi_authenticate(base_url, user, pwd):
url = base_url + '&method=authenticate&username=' + user + '&password=' + pwd url = base_url + '&method=authenticate&username=' + user + \
sessid = json.loads(axapi_call(url))['session_id'] '&password=' + pwd
result = base_url + '&session_id=' + sessid result = json.loads(axapi_call(url))
return result if 'response' in result:
return module.fail_json(msg=result['response']['err']['msg'])
sessid = result['session_id']
return base_url + '&session_id=' + sessid
def main(): def main():
global module global module
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
host = dict(type='str', required=True), host=dict(type='str', required=True),
username = dict(type='str', aliases=['user', 'admin'], required=True), username=dict(type='str', aliases=['user', 'admin'],
password = dict(type='str', aliases=['pass', 'pwd'], required=True), required=True),
service_group = dict(type='str', aliases=['service', 'pool', 'group'], required=True), password=dict(type='str', aliases=['pass', 'pwd'], required=True),
service_group_protocol = dict(type='str', default='tcp', aliases=['proto', 'protocol'], choices=['tcp', 'udp']), service_group=dict(type='str',
service_group_method = dict(type='str', default='round-robin', aliases=['method'], choices=['round-robin', 'weighted-rr', 'least-connection', 'weighted-least-connection', 'service-least-connection', 'service-weighted-least-connection', 'fastest-response', 'least-request', 'round-robin-strict', 'src-ip-only-hash', 'src-ip-hash']), aliases=['service', 'pool', 'group'],
server_name = dict(type='str', aliases=['server', 'member']), required=True),
server_port = dict(type='int', aliases=['port']), service_group_protocol=dict(type='str', default='tcp',
server_status = dict(type='str', default='enable', aliases=['status'], choices=['enable', 'disable']), aliases=['proto', 'protocol'],
state = dict(type='str', default='present', choices=['present', 'absent']), choices=['tcp', 'udp']),
service_group_method=dict(type='str', default='round-robin',
aliases=['method'],
choices=['round-robin',
'weighted-rr',
'least-connection',
'weighted-least-connection',
'service-least-connection',
'service-weighted-least-connection',
'fastest-response',
'least-request',
'round-robin-strict',
'src-ip-only-hash',
'src-ip-hash']),
server_name=dict(type='str', aliases=['server', 'member']),
server_port=dict(type='int', aliases=['port']),
server_status=dict(type='str', default='enable',
aliases=['status'],
choices=['enable', 'disable']),
state=dict(type='str', default='present',
choices=['present', 'absent']),
), ),
supports_check_mode=False supports_check_mode=False
) )
@ -161,17 +187,17 @@ def main():
state = module.params['state'] state = module.params['state']
axapi_base_url = 'https://' + host + '/services/rest/V2.1/?format=json' axapi_base_url = 'https://' + host + '/services/rest/V2.1/?format=json'
load_balancing_methods = { 'round-robin': 0, load_balancing_methods = {'round-robin': 0,
'weighted-rr': 1, 'weighted-rr': 1,
'least-connection': 2, 'least-connection': 2,
'weighted-least-connection': 3, 'weighted-least-connection': 3,
'service-least-connection': 4, 'service-least-connection': 4,
'service-weighted-least-connection': 5, 'service-weighted-least-connection': 5,
'fastest-response': 6, 'fastest-response': 6,
'least-request': 7, 'least-request': 7,
'round-robin-strict': 8, 'round-robin-strict': 8,
'src-ip-only-hash': 14, 'src-ip-only-hash': 14,
'src-ip-hash': 15} 'src-ip-hash': 15}
if slb_service_group_proto == 'tcp' or slb_service_group_proto == 'TCP': if slb_service_group_proto == 'tcp' or slb_service_group_proto == 'TCP':
protocol = '2' protocol = '2'
@ -187,26 +213,44 @@ def main():
module.fail_json(msg='service_group is required') module.fail_json(msg='service_group is required')
if slb_server is None and slb_server_port is None: if slb_server is None and slb_server_port is None:
json_post = {'service_group': { 'name': slb_service_group, 'protocol': protocol, 'lb_method': load_balancing_methods[slb_service_group_method] }} json_post = {'service_group': {'name': slb_service_group,
'protocol': protocol,
'lb_method': load_balancing_methods[slb_service_group_method]}}
elif slb_server is not None and slb_server_port is not None: elif slb_server is not None and slb_server_port is not None:
json_post = {'service_group': { 'name': slb_service_group, 'protocol': protocol, 'lb_method': load_balancing_methods[slb_service_group_method], 'member_list': [{ 'server': slb_server, 'port': slb_server_port, 'status': status }]}} json_post = {'service_group': {'name': slb_service_group,
'protocol': protocol,
'lb_method': load_balancing_methods[slb_service_group_method],
'member_list':
[{'server': slb_server,
'port': slb_server_port,
'status': status}]}}
else: else:
module.fail_json(msg='server_name and server_name_port are required to add to the service-group') module.fail_json(msg='server_name and server_name_port are \
required to add to the service-group')
try: try:
session_url = axapi_authenticate(axapi_base_url, user, pwd) session_url = axapi_authenticate(axapi_base_url, user, pwd)
if state == 'present': if state == 'present':
response = axapi_call(session_url + '&method=slb.service_group.search', json.dumps({ 'name': slb_service_group })) response = axapi_call(session_url +
slb_service_group_exist = re.search(slb_service_group, response, re.I) '&method=slb.service_group.search',
json.dumps({'name': slb_service_group}))
slb_service_group_exist = re.search(slb_service_group,
response, re.I)
if slb_service_group_exist is None: if slb_service_group_exist is None:
response = axapi_call(session_url + '&method=slb.service_group.create', json.dumps(json_post)) response = axapi_call(session_url +
'&method=slb.service_group.create',
json.dumps(json_post))
else: else:
response = axapi_call(session_url + '&method=slb.service_group.update', json.dumps(json_post)) response = axapi_call(session_url +
'&method=slb.service_group.update',
json.dumps(json_post))
if state == 'absent': if state == 'absent':
response = axapi_call(session_url + '&method=slb.service_group.delete', json.dumps({ 'name': slb_service_group })) response = axapi_call(session_url +
'&method=slb.service_group.delete',
json.dumps({'name': slb_service_group}))
result = json.loads(response) result = json.loads(response)
axapi_call(session_url + '&method=session.close') axapi_call(session_url + '&method=session.close')
@ -214,6 +258,9 @@ def main():
except Exception, e: except Exception, e:
return module.fail_json(msg='received exception: %s' % e) return module.fail_json(msg='received exception: %s' % e)
if 'respone' in result and 'err' in result['response']:
return module.fail_json(msg=result['response']['err']['msg'])
module.exit_json(changed=True, content=result) module.exit_json(changed=True, content=result)
from ansible.module_utils.basic import * from ansible.module_utils.basic import *

View file

@ -130,31 +130,51 @@ ansible host -m a10_virtual -a "host=a10adc.example.com username=axapiuser passw
import urllib2 import urllib2
def axapi_call(url, post=None): def axapi_call(url, post=None):
result = urllib2.urlopen(url, post).read() result = urllib2.urlopen(url, post).read()
return result return result
def axapi_authenticate(base_url, user, pwd): def axapi_authenticate(base_url, user, pwd):
url = base_url + '&method=authenticate&username=' + user + '&password=' + pwd url = base_url + '&method=authenticate&username=' + user + \
sessid = json.loads(axapi_call(url))['session_id'] '&password=' + pwd
result = base_url + '&session_id=' + sessid result = json.loads(axapi_call(url))
return result if 'response' in result:
return module.fail_json(msg=result['response']['err']['msg'])
sessid = result['session_id']
return base_url + '&session_id=' + sessid
def main(): def main():
global module global module
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
host = dict(type='str', required=True), host=dict(type='str', required=True),
username = dict(type='str', aliases=['user', 'admin'], required=True), username=dict(type='str', aliases=['user', 'admin'],
password = dict(type='str', aliases=['pass', 'pwd'], required=True), required=True),
virtual_server = dict(type='str', aliases=['vip', 'virtual'], required=True), password=dict(type='str', aliases=['pass', 'pwd'],
virtual_server_ip = dict(type='str', aliases=['ip', 'address']), required=True),
virtual_server_status = dict(type='str', default='enabled', aliases=['status'], choices=['enabled', 'disabled']), virtual_server=dict(type='str', aliases=['vip', 'virtual'],
virtual_server_port = dict(type='int', aliases=['port', 'vport']), required=True),
virtual_server_port_type = dict(type='str', aliases=['proto', 'protocol'], choices=['tcp', 'udp', 'fast-http', 'http', 'https']), virtual_server_ip=dict(type='str',
virtual_server_port_status = dict(type='str', default='enabled', aliases=['portstatus', 'port_status'], choices=['enabled', 'disabled']), aliases=['ip', 'address']),
service_group = dict(type='str', aliases=['pool', 'group']), virtual_server_status=dict(type='str', default='enabled',
state = dict(type='str', default='present', choices=['present', 'absent']), aliases=['status'],
choices=['enabled', 'disabled']),
virtual_server_port=dict(type='int',
aliases=['port', 'vport']),
virtual_server_port_type=dict(type='str',
aliases=['proto', 'protocol'],
choices=['tcp', 'udp', 'fast-http',
'http', 'https']),
virtual_server_port_status=dict(type='str', default='enabled',
aliases=['portstatus',
'port_status'],
choices=['enabled', 'disabled']),
service_group=dict(type='str', aliases=['pool', 'group']),
state=dict(type='str', default='present',
choices=['present', 'absent']),
), ),
supports_check_mode=False supports_check_mode=False
) )
@ -172,11 +192,11 @@ def main():
state = module.params['state'] state = module.params['state']
axapi_base_url = 'https://' + host + '/services/rest/V2.1/?format=json' axapi_base_url = 'https://' + host + '/services/rest/V2.1/?format=json'
vport_types = { 'tcp': 2, vport_types = {'tcp': 2,
'udp': 3, 'udp': 3,
'fast-http': 9, 'fast-http': 9,
'http': 11, 'http': 11,
'https': 12} 'https': 12}
if slb_virtual_status == 'enabled': if slb_virtual_status == 'enabled':
status = '1' status = '1'
@ -195,23 +215,55 @@ def main():
session_url = axapi_authenticate(axapi_base_url, user, pwd) session_url = axapi_authenticate(axapi_base_url, user, pwd)
if state == 'present': if state == 'present':
find_slb_virtual = axapi_call(session_url + '&method=slb.virtual_server.search', json.dumps({ 'name': slb_virtual })) find_slb_virtual = axapi_call(session_url +
slb_virtual_fail = re.search('status": "fail', find_slb_virtual, re.I) '&method=slb.virtual_server.search',
json.dumps({'name': slb_virtual}))
slb_virtual_fail = re.search('status": "fail',
find_slb_virtual, re.I)
if slb_virtual_fail: if slb_virtual_fail:
if slb_virtual_port is None and slb_virtual_port_type is None and slb_service_group is None: if slb_virtual_port is None and slb_virtual_port_type is None \
json_post = { 'virtual_server': { 'name': slb_virtual, 'address': slb_virtual_ip, 'status': status }} and slb_service_group is None:
elif slb_virtual_port is not None and slb_virtual_port_type is not None and slb_service_group is None: json_post = {'virtual_server': {'name': slb_virtual,
json_post = { 'virtual_server': { 'name': slb_virtual, 'address': slb_virtual_ip, 'status': status, 'vport_list': [{ 'protocol': vport_types[slb_virtual_port_type], 'port': slb_virtual_port }]}} 'address': slb_virtual_ip,
elif slb_virtual_port is not None and slb_virtual_port_type is not None and slb_service_group is not None: 'status': status}}
json_post = { 'virtual_server': { 'name': slb_virtual, 'address': slb_virtual_ip, 'status': status, 'vport_list': [{ 'protocol': vport_types[slb_virtual_port_type], 'port': slb_virtual_port, 'service_group': slb_service_group }]}} elif slb_virtual_port is not None and \
slb_virtual_port_type is not None and \
slb_service_group is None:
json_post = {'virtual_server':
{'name': slb_virtual,
'address': slb_virtual_ip,
'status': status,
'vport_list':
[{'protocol':
vport_types[slb_virtual_port_type],
'port': slb_virtual_port}]}}
elif slb_virtual_port is not None and \
slb_virtual_port_type is not None and \
slb_service_group is not None:
json_post = {'virtual_server':
{'name': slb_virtual,
'address': slb_virtual_ip,
'status': status, 'vport_list':
[{'protocol':
vport_types[slb_virtual_port_type],
'port': slb_virtual_port,
'service_group': slb_service_group}]}}
else: else:
module.fail_json(msg='virtual_server_port and virtual_server_type are required to create the virtual port') module.fail_json(msg='virtual_server_port and
virtual_server_type are required to
create the virtual port')
response = axapi_call(session_url + '&method=slb.virtual_server.create', json.dumps(json_post)) response = axapi_call(session_url +
'&method=slb.virtual_server.create',
json.dumps(json_post))
else: else:
response = axapi_call(session_url + '&method=slb.virtual_server.search', json.dumps({ 'name': slb_virtual })) response = axapi_call(session_url +
slb_virtual_port_exist = re.search('"port":' + str(slb_virtual_port), response, re.I) '&method=slb.virtual_server.search',
json.dumps({'name': slb_virtual}))
slb_virtual_port_exist = re.search('"port":' +
str(slb_virtual_port)
response, re.I)
current_status = json.loads(response)['virtual_server']['status'] current_status = json.loads(response)['virtual_server']['status']
current_port_status = 1 current_port_status = 1
@ -222,29 +274,68 @@ def main():
if slb_virtual_port == str(vport_list[port]['port']): if slb_virtual_port == str(vport_list[port]['port']):
current_port_status = vport_list[port]['port'] current_port_status = vport_list[port]['port']
json_post = { 'address': slb_virtual_ip, 'vport': { 'protocol': vport_types[slb_virtual_port_type], 'port': slb_virtual_port, 'service_group': slb_service_group }, 'status': port_status } json_post = {'address': slb_virtual_ip,
response = axapi_call(session_url + '&method=slb.virtual_server.vport.update', json.dumps(json_post)) 'vport':
{'protocol':
vport_types[slb_virtual_port_type],
'port': slb_virtual_port,
'service_group': slb_service_group},
'status': port_status}
response = axapi_call(session_url +
'&method=slb.virtual_server.\
vport.update', json.dumps(json_post))
else: else:
if slb_service_group is None: if slb_service_group is None:
module.fail_json(msg='service_group is required') module.fail_json(msg='service_group is required')
json_post = { 'name': slb_virtual, 'vport': { 'protocol': vport_types[slb_virtual_port_type], 'port': slb_virtual_port, 'service_group': slb_service_group }, 'status': port_status } json_post = {'name': slb_virtual,
response = axapi_call(session_url + '&method=slb.virtual_server.vport.create', json.dumps(json_post)) 'vport':
{'protocol':
vport_types[slb_virtual_port_type],
'port': slb_virtual_port,
'service_group': slb_service_group},
'status': port_status}
response = axapi_call(session_url +
'&method=slb.virtual_server.\
vport.create', json.dumps(json_post))
if current_status != status: if current_status != status:
json_post = { 'virtual_server': { 'name': slb_virtual, 'address': slb_virtual_ip, 'status': status }} json_post = {'virtual_server':
response = axapi_call(session_url + '&method=slb.virtual_server.update', json.dumps(json_post)) {'name': slb_virtual,
'address': slb_virtual_ip,
'status': status}}
response = axapi_call(session_url +
'&method=slb.virtual_server.update',
json.dumps(json_post))
if current_port_status != port_status: if current_port_status != port_status:
json_post = { 'address': slb_virtual_ip, 'vport': { 'protocol': vport_types[slb_virtual_port_type], 'port': slb_virtual_port }, 'status': port_status } json_post = {'address': slb_virtual_ip, 'vport':
response = axapi_call(session_url + '&method=slb.virtual_server.vport.update', json.dumps(json_post)) {'protocol':
vport_types[slb_virtual_port_type],
'port': slb_virtual_port},
'status': port_status}
response = axapi_call(session_url +
'&method=slb.virtual_server.\
vport.update', json.dumps(json_post))
if state == 'absent': if state == 'absent':
if slb_virtual_port is not None and slb_virtual_port_type is not None: if slb_virtual_port is not None and \
response = axapi_call(session_url + '&method=slb.virtual_server.vport.delete', json.dumps({ 'name': slb_virtual, 'vport': { 'protocol': vport_types[slb_virtual_port_type], 'port': slb_virtual_port }})) slb_virtual_port_type is not None:
response = axapi_call(session_url +
'&method=slb.virtual_server.\
vport.delete',
json.dumps({'name': slb_virtual,
'vport':
{'protocol':
vport_types[slb_virtual_port_type],
'port': slb_virtual_port}}))
elif slb_virtual_port is None and slb_virtual_port_type is None: elif slb_virtual_port is None and slb_virtual_port_type is None:
response = axapi_call(session_url + '&method=slb.virtual_server.delete', json.dumps({ 'name': slb_virtual })) response = axapi_call(session_url +
'&method=slb.virtual_server.delete',
json.dumps({'name': slb_virtual}))
else: else:
module.fail_json(msg='virtual_server_port and virtual_server_type are required to remove the virtual port') module.fail_json(msg='virtual_server_port and \
virtual_server_type are required to remove \
the virtual port')
result = json.loads(response) result = json.loads(response)
axapi_call(session_url + '&method=session.close') axapi_call(session_url + '&method=session.close')
@ -252,6 +343,9 @@ def main():
except Exception, e: except Exception, e:
return module.fail_json(msg='received exception: %s' % e) return module.fail_json(msg='received exception: %s' % e)
if 'respone' in result and 'err' in result['response']:
return module.fail_json(msg=result['response']['err']['msg'])
module.exit_json(changed=True, content=result) module.exit_json(changed=True, content=result)
from ansible.module_utils.basic import * from ansible.module_utils.basic import *