Skip to content

Commit

Permalink
Merge pull request #59 from savealive/modules_perf_optimization
Browse files Browse the repository at this point in the history
Modules perf optimization
  • Loading branch information
George Nikolopoulos authored Oct 19, 2018
2 parents d628114 + 9df9401 commit 5572546
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 61 deletions.
27 changes: 17 additions & 10 deletions ansible-modules/netscaler_cs_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}


DOCUMENTATION = '''
---
module: netscaler_cs_policy
Expand Down Expand Up @@ -123,18 +122,21 @@

def policy_exists(client, module):
log('Checking if policy exists')
if cspolicy.count_filtered(client, 'policyname:%s' % module.params['policyname']) > 0:
try:
cspolicy.get(client, module.params['policyname'])
return True
else:
except:
return False


def policy_identical(client, module, cspolicy_proxy):
log('Checking if defined policy is identical to configured')
if cspolicy.count_filtered(client, 'policyname:%s' % module.params['policyname']) == 0:
try:
policy_list = cspolicy.get(client, module.params['policyname'])
except:
return False
policy_list = cspolicy.get_filtered(client, 'policyname:%s' % module.params['policyname'])
diff_dict = cspolicy_proxy.diff_object(policy_list[0])

diff_dict = cspolicy_proxy.diff_object(policy_list)
if 'ip' in diff_dict:
del diff_dict['ip']
if len(diff_dict) == 0:
Expand All @@ -144,8 +146,8 @@ def policy_identical(client, module, cspolicy_proxy):


def diff_list(client, module, cspolicy_proxy):
policy_list = cspolicy.get_filtered(client, 'policyname:%s' % module.params['policyname'])
return cspolicy_proxy.diff_object(policy_list[0])
policy_list = cspolicy.get(client, module.params['policyname'])
return cspolicy_proxy.diff_object(policy_list)


def main():
Expand All @@ -156,6 +158,10 @@ def main():
rule=dict(type='str'),
domain=dict(type='str'),
action=dict(type='str'),
mas_proxy_call=dict(
default=False,
type='bool'
),
)

hand_inserted_arguments = dict(
Expand Down Expand Up @@ -256,7 +262,8 @@ def main():
if not policy_exists(client, module):
module.fail_json(msg='Policy does not exist', **module_result)
if not policy_identical(client, module, cspolicy_proxy):
module.fail_json(msg='Policy differs from configured', diff=diff_list(client, module, cspolicy_proxy), **module_result)
module.fail_json(msg='Policy differs from configured',
diff=diff_list(client, module, cspolicy_proxy), **module_result)

elif module.params['state'] == 'absent':
log('Applying actions for state absent')
Expand Down
56 changes: 28 additions & 28 deletions ansible-modules/netscaler_lb_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,23 +845,18 @@
PYTHON_SDK_IMPORTED = False


def lbmonitor_exists(client, module):
log('Checking if monitor exists')
if lbmonitor.count_filtered(client, 'monitorname:%s' % module.params['monitorname']) > 0:
return True
else:
return False

def lbmonitor_get(client, module):
log('Getting lb monitor')
try:
lbmonitor_inst = lbmonitor.get(client, module.params['monitorname'])
return True, lbmonitor_inst
except nitro_exception:
return False, None

def lbmonitor_identical(client, module, lbmonitor_proxy):
def lbmonitor_identical(lbmonitor_inst, lbmonitor_proxy):
log('Checking if monitor is identical')

count = lbmonitor.count_filtered(client, 'monitorname:%s' % module.params['monitorname'])
if count == 0:
return False

lbmonitor_list = lbmonitor.get_filtered(client, 'monitorname:%s' % module.params['monitorname'])
diff_dict = lbmonitor_proxy.diff_object(lbmonitor_list[0])
diff_dict = lbmonitor_proxy.diff_object(lbmonitor_inst)

# Skipping hashed fields since the cannot be compared directly
hashed_fields = [
Expand All @@ -879,17 +874,19 @@ def lbmonitor_identical(client, module, lbmonitor_proxy):
return False


def diff_list(client, module, lbmonitor_proxy):
monitor_list = lbmonitor.get_filtered(client, 'monitorname:%s' % module.params['monitorname'])
return lbmonitor_proxy.diff_object(monitor_list[0])
def diff_list(lbmonitor_inst, lbmonitor_proxy):
return lbmonitor_proxy.diff_object(lbmonitor_inst)


def main():

module_specific_arguments = dict(

monitorname=dict(type='str'),

mas_proxy_call=dict(
default=False,
type='bool'
),
type=dict(
type='str',
choices=[
Expand Down Expand Up @@ -1312,19 +1309,20 @@ def main():

if module.params['state'] == 'present':
log('Applying actions for state present')
if not lbmonitor_exists(client, module):
lbmonitor_exists, lbmonitor_inst = lbmonitor_get(client, module)
if not lbmonitor_exists:
if not module.check_mode:
log('Adding monitor')
lbmonitor_proxy.add()
if module.params['save_config']:
client.save_config()
module_result['changed'] = True
elif not lbmonitor_identical(client, module, lbmonitor_proxy):
elif not lbmonitor_identical(lbmonitor_inst, lbmonitor_proxy):

# Check if we try to change value of immutable attributes
immutables_changed = get_immutables_intersection(lbmonitor_proxy, diff_list(client, module, lbmonitor_proxy).keys())
immutables_changed = get_immutables_intersection(lbmonitor_proxy, diff_list(lbmonitor_inst, lbmonitor_proxy).keys())
if immutables_changed != []:
diff = diff_list(client, module, lbmonitor_proxy)
diff = diff_list(lbmonitor_inst, lbmonitor_proxy)
msg = 'Cannot update immutable attributes %s' % (immutables_changed,)
module.fail_json(msg=msg, diff=diff, **module_result)

Expand All @@ -1341,18 +1339,20 @@ def main():
# Sanity check for result
log('Sanity checks for state present')
if not module.check_mode:
if not lbmonitor_exists(client, module):
lbmonitor_exists, lbmonitor_inst = lbmonitor_get(client, module)
if not lbmonitor_exists:
module.fail_json(msg='lb monitor does not exist', **module_result)
if not lbmonitor_identical(client, module, lbmonitor_proxy):
if not lbmonitor_identical(lbmonitor_inst, lbmonitor_proxy):
module.fail_json(
msg='lb monitor is not configured correctly',
diff=diff_list(client, module, lbmonitor_proxy),
diff=diff_list(lbmonitor_inst, lbmonitor_proxy),
**module_result
)

elif module.params['state'] == 'absent':
log('Applying actions for state absent')
if lbmonitor_exists(client, module):
lbmonitor_exists, lbmonitor_inst = lbmonitor_get(client, module)
if lbmonitor_exists:
if not module.check_mode:
lbmonitor_proxy.delete()
if module.params['save_config']:
Expand All @@ -1364,10 +1364,10 @@ def main():
# Sanity check for result
log('Sanity checks for state absent')
if not module.check_mode:
if lbmonitor_exists(client, module):
lbmonitor_exists, lbmonitor_inst = lbmonitor_get(client, module)
if lbmonitor_exists:
module.fail_json(msg='lb monitor still exists', **module_result)

module_result['actual_attributes'] = lbmonitor_proxy.get_actual_rw_attributes(filter='monitorname')
except nitro_exception as e:
msg = "nitro exception errorcode=%s, message=%s" % (str(e.errorcode), e.message)
module.fail_json(msg=msg, **module_result)
Expand Down
22 changes: 15 additions & 7 deletions ansible-modules/netscaler_lb_vserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,6 @@
type: dict
sample: { 'clttimeout': 'difference. ours: (float) 10.0 other: (float) 20.0' }
'''

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network.netscaler.netscaler import (
ConfigProxy,
Expand Down Expand Up @@ -1000,24 +999,29 @@

def lb_vserver_exists(client, module):
log('Checking if lb vserver exists')
if lbvserver.count_filtered(client, 'name:%s' % module.params['name']) > 0:
try:
lbvserver.get(client, module.params['name'])
return True
else:
except nitro_exception:
return False


def lb_vserver_identical(client, module, lbvserver_proxy):
log('Checking if configured lb vserver is identical')
lbvserver_list = lbvserver.get_filtered(client, 'name:%s' % module.params['name'])
if lbvserver_proxy.has_equal_attributes(lbvserver_list[0]):
try:
lbvserver_inst = lbvserver.get(client, module.params['name'])
except nitro_exception:
return False

if lbvserver_proxy.has_equal_attributes(lbvserver_inst):
return True
else:
return False


def lb_vserver_diff(client, module, lbvserver_proxy):
lbvserver_list = lbvserver.get_filtered(client, 'name:%s' % module.params['name'])
return lbvserver_proxy.diff_object(lbvserver_list[0])
lbvserver_inst = lbvserver.get(client, module.params['name'])
return lbvserver_proxy.diff_object(lbvserver_inst)


def get_configured_service_bindings(client, module):
Expand Down Expand Up @@ -1389,6 +1393,10 @@ def main():

module_specific_arguments = dict(
name=dict(type='str'),
mas_proxy_call=dict(
default=False,
type='bool'
),
servicetype=dict(
type='str',
choices=[
Expand Down
Loading

0 comments on commit 5572546

Please sign in to comment.