Rework on Exception management

This commit is contained in:
Etienne CARRIERE 2015-07-02 07:35:19 +02:00 committed by Matt Clay
parent 37cb6519af
commit 3c06b968bc

View file

@ -202,16 +202,23 @@ def vs_exists(api, vs):
def vs_create(api,name,destination,port,pool): def vs_create(api,name,destination,port,pool):
_profiles=[[{'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': 'tcp'}]] _profiles=[[{'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': 'tcp'}]]
created = False
# a bit of a hack to handle concurrent runs of this module.
# even though we've checked the vs doesn't exist,
# it may exist by the time we run create_vs().
# this catches the exception and does something smart
# about it!
try: try:
api.LocalLB.VirtualServer.create( api.LocalLB.VirtualServer.create(
definitions = [{'name': [name], 'address': [destination], 'port': port, 'protocol': 'PROTOCOL_TCP'}], definitions = [{'name': [name], 'address': [destination], 'port': port, 'protocol': 'PROTOCOL_TCP'}],
wildmasks = ['255.255.255.255'], wildmasks = ['255.255.255.255'],
resources = [{'type': 'RESOURCE_TYPE_POOL', 'default_pool_name': pool}], resources = [{'type': 'RESOURCE_TYPE_POOL', 'default_pool_name': pool}],
profiles = _profiles) profiles = _profiles)
result = True created = True
desc = 0 return created
except Exception, e : except bigsudsOperationFailed, e :
print e.args if "already exists" not in str(e):
raise Exception('Error on creating Virtual Server : %s' % e)
def vs_remove(api,name): def vs_remove(api,name):
api.LocalLB.VirtualServer.delete_virtual_server(virtual_servers = [name ]) api.LocalLB.VirtualServer.delete_virtual_server(virtual_servers = [name ])
@ -377,16 +384,12 @@ def main():
# about it! # about it!
try: try:
vs_create(api,name,destination,port,pool) vs_create(api,name,destination,port,pool)
result = {'changed': True}
except bigsuds.OperationFailed, e:
if "already exists" in str(e):
update = True
else:
raise Exception('Error on creating Virtual Server : %s' % e)
else:
set_profiles(api,name,all_profiles) set_profiles(api,name,all_profiles)
set_snat(api,name,snat) set_snat(api,name,snat)
set_description(api,name,description) set_description(api,name,description)
result = {'changed': True}
except bigsuds.OperationFailed, e:
raise Exception('Error on creating Virtual Server : %s' % e)
else: else:
# check-mode return value # check-mode return value
result = {'changed': True} result = {'changed': True}