Localize exceptions for F5 LTM virtual server module

This commit is contained in:
Etienne CARRIERE 2015-07-01 22:26:32 +02:00
parent ca366059d3
commit 3d9fd24d62

View file

@ -196,39 +196,44 @@ def get_profiles(api,name):
def set_profiles(api,name,profiles_list): def set_profiles(api,name,profiles_list):
if profiles_list is None: updated=False
return False try:
current_profiles=map(lambda x:x['profile_name'], get_profiles(api,name)) if profiles_list is None:
to_add_profiles=[] return False
for x in profiles_list: current_profiles=map(lambda x:x['profile_name'], get_profiles(api,name))
if x not in current_profiles: to_add_profiles=[]
to_add_profiles.append({'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': x}) for x in profiles_list:
to_del_profiles=[] if x not in current_profiles:
for x in current_profiles: to_add_profiles.append({'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': x})
if (x not in profiles_list) and (x!= "/Common/tcp"): to_del_profiles=[]
to_del_profiles.append({'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': x}) for x in current_profiles:
changed=False if (x not in profiles_list) and (x!= "/Common/tcp"):
if len(to_del_profiles)>0: to_del_profiles.append({'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': x})
api.LocalLB.VirtualServer.remove_profile(virtual_servers = [name],profiles = [to_del_profiles]) if len(to_del_profiles)>0:
changed=True api.LocalLB.VirtualServer.remove_profile(virtual_servers = [name],profiles = [to_del_profiles])
if len(to_add_profiles)>0: updated=True
api.LocalLB.VirtualServer.add_profile(virtual_servers = [name],profiles= [to_add_profiles]) if len(to_add_profiles)>0:
changed=True api.LocalLB.VirtualServer.add_profile(virtual_servers = [name],profiles= [to_add_profiles])
return changed updated=True
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):
current_state=get_snat_type(api,name) updated = False
update = False try:
if snat is None: current_state=get_snat_type(api,name)
return update if snat is None:
if snat == 'None' and current_state != 'SRC_TRANS_NONE': return update
api.LocalLB.VirtualServer.set_source_address_translation_none(virtual_servers = [name]) if snat == 'None' and current_state != 'SRC_TRANS_NONE':
update = True api.LocalLB.VirtualServer.set_source_address_translation_none(virtual_servers = [name])
if snat == 'Automap' and current_state != 'SRC_TRANS_AUTOMAP': updated = True
api.LocalLB.VirtualServer.set_source_address_translation_automap(virtual_servers = [name]) if snat == 'Automap' and current_state != 'SRC_TRANS_AUTOMAP':
update = True api.LocalLB.VirtualServer.set_source_address_translation_automap(virtual_servers = [name])
return update updated = True
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]
@ -238,37 +243,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
if pool is not None and (pool != current_pool): try:
api.LocalLB.VirtualServer.set_default_pool_name(virtual_servers = [name],default_pools = [pool]) current_pool = get_pool (api,name)
updated=True if pool is not None and (pool != current_pool):
return updated api.LocalLB.VirtualServer.set_default_pool_name(virtual_servers = [name],default_pools = [pool])
updated=True
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
if (destination is not None and port is not None) and (destination != current_destination['address'] or port != current_destination['port']): try:
api.LocalLB.VirtualServer.set_destination_v2(virtual_servers = [name],destinations=[{'address': destination, 'port':port}]) current_destination = get_destination(api,name)
updated=True if (destination is not None and port is not None) and (destination != current_destination['address'] or port != current_destination['port']):
return updated api.LocalLB.VirtualServer.set_destination_v2(virtual_servers = [name],destinations=[{'address': destination, 'port':port}])
updated=True
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
if description is not None and current_description != description: try:
api.LocalLB.VirtualServer.set_description(virtual_servers =[name],descriptions=[description]) current_description = get_description(api,name)
updated=True if description is not None and current_description != description:
return updated api.LocalLB.VirtualServer.set_description(virtual_servers =[name],descriptions=[description])
updated=True
return updated
except bigsuds.OperationFailed, e:
raise Exception('Error on setting description : %s ' % e)
def main(): def main():
@ -342,7 +356,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)
@ -356,13 +370,16 @@ 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
api.System.Session.start_transaction() try:
result['changed']|=set_destination(api,name,fq_name(partition,destination),port) api.System.Session.start_transaction()
result['changed']|=set_pool(api,name,pool) result['changed']|=set_destination(api,name,fq_name(partition,destination),port)
result['changed']|=set_description(api,name,description) result['changed']|=set_pool(api,name,pool)
result['changed']|=set_snat(api,name,snat) result['changed']|=set_description(api,name,description)
result['changed']|=set_profiles(api,name,all_profiles) result['changed']|=set_snat(api,name,snat)
api.System.Session.submit_transaction() result['changed']|=set_profiles(api,name,all_profiles)
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}