Localize exceptions for F5 LTM virtual server module

This commit is contained in:
Etienne CARRIERE 2015-07-01 22:26:32 +02:00 committed by Matt Clay
parent ed3a2ca136
commit 37cb6519af

View file

@ -222,6 +222,8 @@ def get_profiles(api,name):
def set_profiles(api,name,profiles_list): def set_profiles(api,name,profiles_list):
updated=False
try:
if profiles_list is None: if profiles_list is None:
return False return False
current_profiles=map(lambda x:x['profile_name'], get_profiles(api,name)) current_profiles=map(lambda x:x['profile_name'], get_profiles(api,name))
@ -233,28 +235,31 @@ def set_profiles(api,name,profiles_list):
for x in current_profiles: for x in current_profiles:
if (x not in profiles_list) and (x!= "/Common/tcp"): if (x not in profiles_list) and (x!= "/Common/tcp"):
to_del_profiles.append({'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': x}) to_del_profiles.append({'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': x})
changed=False
if len(to_del_profiles)>0: if len(to_del_profiles)>0:
api.LocalLB.VirtualServer.remove_profile(virtual_servers = [name],profiles = [to_del_profiles]) api.LocalLB.VirtualServer.remove_profile(virtual_servers = [name],profiles = [to_del_profiles])
changed=True updated=True
if len(to_add_profiles)>0: if len(to_add_profiles)>0:
api.LocalLB.VirtualServer.add_profile(virtual_servers = [name],profiles= [to_add_profiles]) api.LocalLB.VirtualServer.add_profile(virtual_servers = [name],profiles= [to_add_profiles])
changed=True updated=True
return changed return updated
except bigsuds.OperationFailed, e:
raise Exception('Error on setting profiles : %s' % e)
def set_snat(api,name,snat): def set_snat(api,name,snat):
updated = False
try:
current_state=get_snat_type(api,name) current_state=get_snat_type(api,name)
update = False
if snat is None: if snat is None:
return update return update
if snat == 'None' and current_state != 'SRC_TRANS_NONE': if snat == 'None' and current_state != 'SRC_TRANS_NONE':
api.LocalLB.VirtualServer.set_source_address_translation_none(virtual_servers = [name]) api.LocalLB.VirtualServer.set_source_address_translation_none(virtual_servers = [name])
update = True updated = True
if snat == 'Automap' and current_state != 'SRC_TRANS_AUTOMAP': if snat == 'Automap' and current_state != 'SRC_TRANS_AUTOMAP':
api.LocalLB.VirtualServer.set_source_address_translation_automap(virtual_servers = [name]) api.LocalLB.VirtualServer.set_source_address_translation_automap(virtual_servers = [name])
update = True updated = True
return update return updated
except bigsuds.OperationFailed, e:
raise Exception('Error on setting snat : %s' % e)
def get_snat_type(api,name): def get_snat_type(api,name):
return api.LocalLB.VirtualServer.get_source_address_translation_type(virtual_servers = [name])[0] return api.LocalLB.VirtualServer.get_source_address_translation_type(virtual_servers = [name])[0]
@ -264,37 +269,46 @@ def get_pool(api,name):
return api.LocalLB.VirtualServer.get_default_pool_name(virtual_servers = [name])[0] return api.LocalLB.VirtualServer.get_default_pool_name(virtual_servers = [name])[0]
def set_pool(api,name,pool): def set_pool(api,name,pool):
current_pool = get_pool (api,name)
updated=False updated=False
try:
current_pool = get_pool (api,name)
if pool is not None and (pool != current_pool): if pool is not None and (pool != current_pool):
api.LocalLB.VirtualServer.set_default_pool_name(virtual_servers = [name],default_pools = [pool]) api.LocalLB.VirtualServer.set_default_pool_name(virtual_servers = [name],default_pools = [pool])
updated=True updated=True
return updated return updated
except bigsuds.OperationFailed, e:
raise Exception('Error on setting pool : %s' % e)
def get_destination(api,name): def get_destination(api,name):
return api.LocalLB.VirtualServer.get_destination_v2(virtual_servers = [name])[0] return api.LocalLB.VirtualServer.get_destination_v2(virtual_servers = [name])[0]
def set_destination(api,name,destination,port): def set_destination(api,name,destination,port):
current_destination = get_destination(api,name)
updated=False updated=False
try:
current_destination = get_destination(api,name)
if (destination is not None and port is not None) and (destination != current_destination['address'] or port != current_destination['port']): if (destination is not None and port is not None) and (destination != current_destination['address'] or port != current_destination['port']):
api.LocalLB.VirtualServer.set_destination_v2(virtual_servers = [name],destinations=[{'address': destination, 'port':port}]) api.LocalLB.VirtualServer.set_destination_v2(virtual_servers = [name],destinations=[{'address': destination, 'port':port}])
updated=True updated=True
return updated return updated
except bigsuds.OperationFailed, e:
raise Exception('Error on setting destination : %s'% e )
def get_description(api,name): def get_description(api,name):
return api.LocalLB.VirtualServer.get_description(virtual_servers = [name])[0] return api.LocalLB.VirtualServer.get_description(virtual_servers = [name])[0]
def set_description(api,name,description): def set_description(api,name,description):
current_description = get_description(api,name)
updated=False updated=False
try:
current_description = get_description(api,name)
if description is not None and current_description != description: if description is not None and current_description != description:
api.LocalLB.VirtualServer.set_description(virtual_servers =[name],descriptions=[description]) api.LocalLB.VirtualServer.set_description(virtual_servers =[name],descriptions=[description])
updated=True updated=True
return updated return updated
except bigsuds.OperationFailed, e:
raise Exception('Error on setting description : %s ' % e)
def main(): def main():
@ -368,7 +382,7 @@ def main():
if "already exists" in str(e): if "already exists" in str(e):
update = True update = True
else: else:
raise raise Exception('Error on creating Virtual Server : %s' % e)
else: else:
set_profiles(api,name,all_profiles) set_profiles(api,name,all_profiles)
set_snat(api,name,snat) set_snat(api,name,snat)
@ -382,6 +396,7 @@ def main():
# VS exists # VS exists
if not module.check_mode: if not module.check_mode:
# Have a transaction for all the changes # Have a transaction for all the changes
try:
api.System.Session.start_transaction() api.System.Session.start_transaction()
result['changed']|=set_destination(api,name,fq_name(partition,destination),port) result['changed']|=set_destination(api,name,fq_name(partition,destination),port)
result['changed']|=set_pool(api,name,pool) result['changed']|=set_pool(api,name,pool)
@ -389,6 +404,8 @@ def main():
result['changed']|=set_snat(api,name,snat) result['changed']|=set_snat(api,name,snat)
result['changed']|=set_profiles(api,name,all_profiles) result['changed']|=set_profiles(api,name,all_profiles)
api.System.Session.submit_transaction() api.System.Session.submit_transaction()
except Exception,e:
raise Exception("Error on updating Virtual Server : %s" % e)
else: else:
# check-mode return value # check-mode return value
result = {'changed': True} result = {'changed': True}