Adds coding conventions for the bigip-virtual-server module (#2473)

A number of coding conventions have been adopted for new F5 modules
that are in development. To ensure common usage across the modules,
this module needed to be updated to reflect those conventions.
This commit is contained in:
Tim Rupp 2016-06-29 00:09:37 -07:00 committed by Matt Clay
parent a369096f88
commit 3a4bfc731d

View file

@ -49,101 +49,95 @@ options:
description:
- BIG-IP username
required: true
default: null
choices: []
aliases: []
password:
description:
- BIG-IP password
required: true
default: null
choices: []
aliases: []
validate_certs:
description:
- If C(no), SSL certificates will not be validated. This should only be used
on personally controlled sites using self-signed certificates.
required: false
default: 'yes'
choices: ['yes', 'no']
version_added: 2.0
choices:
- yes
- no
state:
description:
- Virtual Server state
- Absent, delete the VS if present
- present (and its synonym enabled), create if needed the VS and set state to enabled
- disabled, create if needed the VS and set state to disabled
- C(present) (and its synonym enabled), create if needed the VS and set
state to enabled
- C(disabled), create if needed the VS and set state to disabled
required: false
default: present
choices: ['present', 'absent', 'enabled', 'disabled']
choices:
- present
- absent
- enabled
- disabled
aliases: []
partition:
description:
- Partition
required: false
default: 'Common'
choices: []
aliases: []
name:
description:
- "Virtual server name."
- Virtual server name
required: true
aliases: ['vs']
aliases:
- vs
destination:
description:
- "Destination IP of the virtual server (only host is currently supported) . Required when state=present and vs does not exist."
- Destination IP of the virtual server (only host is currently supported).
Required when state=present and vs does not exist.
required: true
default: null
choices: []
aliases: ['address', 'ip']
aliases:
- address
- ip
port:
description:
- "Port of the virtual server . Required when state=present and vs does not exist"
- Port of the virtual server . Required when state=present and vs does not exist
required: false
default: None
all_profiles:
description:
- "List of all Profiles (HTTP,ClientSSL,ServerSSL,etc) that must be used by the virtual server"
- List of all Profiles (HTTP,ClientSSL,ServerSSL,etc) that must be used
by the virtual server
required: false
default: None
all_rules:
version_added: "2.2"
description:
- "List of rules to be applied in priority order"
- List of rules to be applied in priority order
required: false
default: None
pool:
description:
- "Default pool for the virtual server"
- Default pool for the virtual server
required: false
default: None
snat:
description:
- "Source network address policy"
- Source network address policy
required: false
default: None
default_persistence_profile:
description:
- "Default Profile which manages the session persistence"
- Default Profile which manages the session persistence
required: false
default: None
description:
description:
- "Virtual server description."
- Virtual server description
required: false
default: None
'''
EXAMPLES = '''
## playbook task examples:
---
# file bigip-test.yml
# ...
- name: Add VS
local_action:
module: bigip_virtual_server
- name: Add virtual server
bigip_virtual_server:
server: lb.mydomain.net
user: admin
password: secret
@ -158,10 +152,10 @@ EXAMPLES = '''
all_profiles:
- http
- clientssl
delegate_to: localhost
- name: Modify Port of the Virtual Server
local_action:
module: bigip_virtual_server
bigip_virtual_server:
server: lb.mydomain.net
user: admin
password: secret
@ -169,37 +163,41 @@ EXAMPLES = '''
partition: MyPartition
name: myvirtualserver
port: 8080
delegate_to: localhost
- name: Delete virtual server
local_action:
module: bigip_virtual_server
bigip_virtual_server:
server: lb.mydomain.net
user: admin
password: secret
state: absent
partition: MyPartition
name: myvirtualserver
delegate_to: localhost
'''
RETURN = '''
---
deleted:
description: Name of a virtual server that was deleted
returned: virtual server was successfully deleted on state=absent
returned: changed
type: string
sample: "my-virtual-server"
'''
# ==========================
# bigip_virtual_server module specific
#
# map of state values
STATES={'enabled': 'STATE_ENABLED',
'disabled': 'STATE_DISABLED'}
STATUSES={'enabled': 'SESSION_STATUS_ENABLED',
STATES = {
'enabled': 'STATE_ENABLED',
'disabled': 'STATE_DISABLED'
}
STATUSES = {
'enabled': 'SESSION_STATUS_ENABLED',
'disabled': 'SESSION_STATUS_DISABLED',
'offline': 'SESSION_STATUS_FORCED_DISABLED'}
'offline': 'SESSION_STATUS_FORCED_DISABLED'
}
def vs_exists(api, vs):
# hack to determine if pool exists
@ -207,7 +205,7 @@ def vs_exists(api, vs):
try:
api.LocalLB.VirtualServer.get_object_status(virtual_servers=[vs])
result = True
except bigsuds.OperationFailed, e:
except bigsuds.OperationFailed as e:
if "was not found" in str(e):
result = False
else:
@ -215,6 +213,7 @@ def vs_exists(api, vs):
raise
return result
def vs_create(api, name, destination, port, pool):
_profiles = [[{'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': 'tcp'}]]
created = False
@ -231,16 +230,21 @@ def vs_create(api,name,destination,port,pool):
profiles=_profiles)
created = True
return created
except bigsuds.OperationFailed, e:
except bigsuds.OperationFailed as e:
if "already exists" not in str(e):
raise Exception('Error on creating Virtual Server : %s' % e)
def vs_remove(api, name):
api.LocalLB.VirtualServer.delete_virtual_server(virtual_servers = [name ])
api.LocalLB.VirtualServer.delete_virtual_server(
virtual_servers=[name]
)
def get_rules(api, name):
return api.LocalLB.VirtualServer.get_rule(virtual_servers = [name])[0]
return api.LocalLB.VirtualServer.get_rule(
virtual_servers=[name]
)[0]
def set_rules(api, name, rules_list):
@ -259,17 +263,26 @@ def set_rules(api,name,rules_list):
if (i, x) not in rules_list:
to_del_rules.append({'priority': i, 'rule_name': x})
if len(to_del_rules) > 0:
api.LocalLB.VirtualServer.remove_rule(virtual_servers = [name],rules = [to_del_rules])
api.LocalLB.VirtualServer.remove_rule(
virtual_servers=[name],
rules=[to_del_rules]
)
updated = True
if len(to_add_rules) > 0:
api.LocalLB.VirtualServer.add_rule(virtual_servers = [name],rules= [to_add_rules])
api.LocalLB.VirtualServer.add_rule(
virtual_servers=[name],
rules=[to_add_rules]
)
updated = True
return updated
except bigsuds.OperationFailed, e:
except bigsuds.OperationFailed as e:
raise Exception('Error on setting profiles : %s' % e)
def get_profiles(api, name):
return api.LocalLB.VirtualServer.get_profile(virtual_servers = [name])[0]
return api.LocalLB.VirtualServer.get_profile(
virtual_servers=[name]
)[0]
def set_profiles(api, name, profiles_list):
@ -287,15 +300,22 @@ def set_profiles(api,name,profiles_list):
if (x not in profiles_list) and (x != "/Common/tcp"):
to_del_profiles.append({'profile_context': 'PROFILE_CONTEXT_TYPE_ALL', 'profile_name': x})
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]
)
updated = True
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]
)
updated = True
return updated
except bigsuds.OperationFailed, e:
except bigsuds.OperationFailed as e:
raise Exception('Error on setting profiles : %s' % e)
def set_snat(api, name, snat):
updated = False
try:
@ -303,46 +323,65 @@ def set_snat(api,name,snat):
if snat is None:
return updated
elif 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]
)
updated = True
elif 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]
)
updated = True
return updated
except bigsuds.OperationFailed, e:
except bigsuds.OperationFailed as e:
raise Exception('Error on setting snat : %s' % e)
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]
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):
updated = False
try:
current_pool = get_pool(api, name)
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
return updated
except bigsuds.OperationFailed, e:
except bigsuds.OperationFailed as e:
raise Exception('Error on setting pool : %s' % e)
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):
updated = False
try:
current_destination = get_destination(api, name)
if destination is not None and destination != current_destination['address']:
api.LocalLB.VirtualServer.set_destination_v2(virtual_servers = [name],destinations=[{'address': destination, 'port': current_destination['port']}])
api.LocalLB.VirtualServer.set_destination_v2(
virtual_servers=[name],
destinations=[{'address': destination, 'port': current_destination['port']}]
)
updated = True
return updated
except bigsuds.OperationFailed, e:
except bigsuds.OperationFailed as e:
raise Exception('Error on setting destination : %s' % e)
@ -351,14 +390,21 @@ def set_port(api,name,port):
try:
current_destination = get_destination(api, name)
if port is not None and port != current_destination['port']:
api.LocalLB.VirtualServer.set_destination_v2(virtual_servers = [name],destinations=[{'address': current_destination['address'], 'port': port}])
api.LocalLB.VirtualServer.set_destination_v2(
virtual_servers=[name],
destinations=[{'address': current_destination['address'], 'port': port}]
)
updated = True
return updated
except bigsuds.OperationFailed, e:
except bigsuds.OperationFailed as e:
raise Exception('Error on setting port : %s' % e)
def get_state(api, name):
return api.LocalLB.VirtualServer.get_enabled_state(virtual_servers = [name])[0]
return api.LocalLB.VirtualServer.get_enabled_state(
virtual_servers=[name]
)[0]
def set_state(api, name, state):
updated = False
@ -368,28 +414,42 @@ def set_state(api,name,state):
if state == 'present':
state = 'enabled'
if STATES[state] != current_state:
api.LocalLB.VirtualServer.set_enabled_state(virtual_servers=[name],states=[STATES[state]])
api.LocalLB.VirtualServer.set_enabled_state(
virtual_servers=[name],
states=[STATES[state]]
)
updated = True
return updated
except bigsuds.OperationFailed, e:
except bigsuds.OperationFailed as e:
raise Exception('Error on setting state : %s' % e)
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):
updated = False
try:
current_description = get_description(api, name)
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
return updated
except bigsuds.OperationFailed, e:
except bigsuds.OperationFailed as e:
raise Exception('Error on setting description : %s ' % e)
def get_persistence_profiles(api, name):
return api.LocalLB.VirtualServer.get_persistence_profile(virtual_servers = [name])[0]
return api.LocalLB.VirtualServer.get_persistence_profile(
virtual_servers=[name]
)[0]
def set_default_persistence_profiles(api, name, persistence_profile):
updated = False
@ -403,14 +463,21 @@ def set_default_persistence_profiles(api,name,persistence_profile):
default = profile['profile_name']
break
if default is not None and default != persistence_profile:
api.LocalLB.VirtualServer.remove_persistence_profile(virtual_servers=[name],profiles=[[{'profile_name':default,'default_profile' : True}]])
api.LocalLB.VirtualServer.remove_persistence_profile(
virtual_servers=[name],
profiles=[[{'profile_name': default, 'default_profile': True}]]
)
if default != persistence_profile:
api.LocalLB.VirtualServer.add_persistence_profile(virtual_servers=[name],profiles=[[{'profile_name':persistence_profile,'default_profile' : True}]])
api.LocalLB.VirtualServer.add_persistence_profile(
virtual_servers=[name],
profiles=[[{'profile_name': persistence_profile, 'default_profile': True}]]
)
updated = True
return updated
except bigsuds.OperationFailed, e:
except bigsuds.OperationFailed as e:
raise Exception('Error on setting default persistence profile : %s' % e)
def main():
argument_spec = f5_argument_spec()
argument_spec.update(dict(
@ -425,8 +492,7 @@ def main():
description=dict(type='str'),
snat=dict(type='str'),
default_persistence_profile=dict(type='str')
)
)
))
module = AnsibleModule(
argument_spec=argument_spec,
@ -474,7 +540,7 @@ def main():
try:
vs_remove(api, name)
result = {'changed': True, 'deleted': name}
except bigsuds.OperationFailed, e:
except bigsuds.OperationFailed as e:
if "was not found" in str(e):
result['changed'] = False
else:
@ -503,7 +569,7 @@ def main():
set_default_persistence_profiles(api, name, default_persistence_profile)
set_state(api, name, state)
result = {'changed': True}
except bigsuds.OperationFailed, e:
except bigsuds.OperationFailed as e:
raise Exception('Error on creating Virtual Server : %s' % e)
else:
# check-mode return value
@ -526,13 +592,13 @@ def main():
result['changed'] |= set_default_persistence_profiles(api, name, default_persistence_profile)
result['changed'] |= set_state(api, name, state)
api.System.Session.submit_transaction()
except Exception,e:
except Exception as e:
raise Exception("Error on updating Virtual Server : %s" % e)
else:
# check-mode return value
result = {'changed': True}
except Exception, e:
except Exception as e:
module.fail_json(msg="received exception: %s" % e)
module.exit_json(**result)