From 2832686014d148f5f0126257c97433611147a743 Mon Sep 17 00:00:00 2001 From: Oleksandr Savchenko Date: Sun, 23 Sep 2018 00:07:54 +0200 Subject: [PATCH 1/3] Various performance optimizations --- ansible-modules/netscaler_cs_policy.py | 23 +++++----- ansible-modules/netscaler_lb_monitor.py | 50 +++++++++++----------- ansible-modules/netscaler_lb_vserver.py | 18 +++++--- ansible-modules/netscaler_nitro_request.py | 30 +++++++++++-- ansible-modules/netscaler_server.py | 12 ++++-- ansible-modules/netscaler_servicegroup.py | 25 +++++++---- 6 files changed, 100 insertions(+), 58 deletions(-) diff --git a/ansible-modules/netscaler_cs_policy.py b/ansible-modules/netscaler_cs_policy.py index fa2365d94..5b0f18afa 100644 --- a/ansible-modules/netscaler_cs_policy.py +++ b/ansible-modules/netscaler_cs_policy.py @@ -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 @@ -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: @@ -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(): @@ -256,7 +258,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') diff --git a/ansible-modules/netscaler_lb_monitor.py b/ansible-modules/netscaler_lb_monitor.py index e9a686efd..a5d374968 100644 --- a/ansible-modules/netscaler_lb_monitor.py +++ b/ansible-modules/netscaler_lb_monitor.py @@ -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 = [ @@ -879,9 +874,8 @@ 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(): @@ -1312,19 +1306,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) @@ -1341,18 +1336,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']: @@ -1364,7 +1361,8 @@ 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') diff --git a/ansible-modules/netscaler_lb_vserver.py b/ansible-modules/netscaler_lb_vserver.py index 773ff2d30..28afaba09 100644 --- a/ansible-modules/netscaler_lb_vserver.py +++ b/ansible-modules/netscaler_lb_vserver.py @@ -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, @@ -999,24 +998,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): diff --git a/ansible-modules/netscaler_nitro_request.py b/ansible-modules/netscaler_nitro_request.py index ac637b498..00689fc5f 100644 --- a/ansible-modules/netscaler_nitro_request.py +++ b/ansible-modules/netscaler_nitro_request.py @@ -365,6 +365,7 @@ class NitroAPICaller(object): instance_ip=dict(type='str'), instance_name=dict(type='str'), instance_id=dict(type='str'), + timeout=dict(type='int', default=45), ) def __init__(self): @@ -525,13 +526,14 @@ def add(self): ) data = self._module.jsonify({self._module.params['resource']: self._module.params['attributes']}) - + timeout = self._module.params['timeout'] r, info = fetch_url( self._module, url=url, headers=self._headers, data=data, method='POST', + timeout=timeout, ) result = {} @@ -563,13 +565,14 @@ def update(self): ) data = self._module.jsonify({self._module.params['resource']: self._module.params['attributes']}) - + timeout = self._module.params['timeout'] r, info = fetch_url( self._module, url=url, headers=self._headers, data=data, method='PUT', + timeout=timeout, ) result = {} @@ -595,11 +598,13 @@ def get(self): self._module.params['name'], ) + timeout = self._module.params['timeout'] r, info = fetch_url( self._module, url=url, headers=self._headers, method='GET', + timeout=timeout, ) result = {} @@ -630,11 +635,13 @@ def get_by_args(self): url = '?'.join([url, args]) + timeout = self._module.params['timeout'] r, info = fetch_url( self._module, url=url, headers=self._headers, method='GET', + timeout=timeout, ) result = {} self.edit_response_data(r, info, result, success_status=200) @@ -663,11 +670,13 @@ def get_filtered(self): filter_str, ) + timeout = self._module.params['timeout'] r, info = fetch_url( self._module, url=url, headers=self._headers, method='GET', + timeout=timeout, ) result = {} @@ -688,11 +697,13 @@ def get_all(self): ) print('headers %s' % self._headers) + timeout = self._module.params['timeout'] r, info = fetch_url( self._module, url=url, headers=self._headers, method='GET', + timeout=timeout, ) result = {} @@ -717,12 +728,14 @@ def delete(self): self._module.params['resource'], self._module.params['name'], ) + timeout = self._module.params['timeout'] r, info = fetch_url( self._module, url=url, headers=self._headers, method='DELETE', + timeout=timeout, ) result = {} @@ -754,11 +767,14 @@ def delete_by_args(self): args = 'args=' + args url = '?'.join([url, args]) + + timeout = self._module.params['timeout'] r, info = fetch_url( self._module, url=url, headers=self._headers, method='DELETE', + timeout=timeout, ) result = {} self.edit_response_data(r, info, result, success_status=200) @@ -780,11 +796,13 @@ def count(self): self._module.params['resource'], ) + timeout = self._module.params['timeout'] r, info = fetch_url( self._module, url=url, headers=self._headers, method='GET', + timeout=timeout, ) result = {} @@ -821,12 +839,14 @@ def action(self): data = self._module.jsonify({self._module.params['resource']: self._module.params['attributes']}) + timeout = self._module.params['timeout'] r, info = fetch_url( self._module, url=url, headers=self._headers, data=data, method='POST', + timeout=timeout, ) result = {} @@ -856,12 +876,14 @@ def mas_login(self): data = 'object=\n%s' % self._module.jsonify(login_credentials) + timeout = self._module.params['timeout'] r, info = fetch_url( self._module, url=url, headers=self._headers, data=data, method='POST', + timeout=timeout, ) print(r, info) @@ -888,12 +910,15 @@ def save_config(self): 'nsconfig': {}, } ) + + timeout = self._module.params['timeout'] r, info = fetch_url( self._module, url=url, headers=self._headers, data=data, method='POST', + timeout=timeout, ) result = {} @@ -905,7 +930,6 @@ def save_config(self): def main(): - nitro_api_caller = NitroAPICaller() nitro_api_caller.main() diff --git a/ansible-modules/netscaler_server.py b/ansible-modules/netscaler_server.py index 744b748ba..8b040bd04 100644 --- a/ansible-modules/netscaler_server.py +++ b/ansible-modules/netscaler_server.py @@ -154,6 +154,7 @@ try: from nssrc.com.citrix.netscaler.nitro.resource.config.basic.server import server from nssrc.com.citrix.netscaler.nitro.exception.nitro_exception import nitro_exception + PYTHON_SDK_IMPORTED = True except ImportError as e: PYTHON_SDK_IMPORTED = False @@ -165,15 +166,18 @@ def server_exists(client, module): log('Checking if server exists') - if server.count_filtered(client, 'name:%s' % module.params['name']) > 0: + try: + server.get(client, module.params['name']) return True - else: + except nitro_exception: return False def server_identical(client, module, server_proxy): log('Checking if configured server is identical') - if server.count_filtered(client, 'name:%s' % module.params['name']) == 0: + try: + server.get(client, module.params['name']) + except nitro_exception: return False diff = diff_list(client, module, server_proxy) @@ -190,7 +194,7 @@ def server_identical(client, module, server_proxy): def diff_list(client, module, server_proxy): - ret_val = server_proxy.diff_object(server.get_filtered(client, 'name:%s' % module.params['name'])[0]), + ret_val = server_proxy.diff_object(server.get(client, module.params['name'])), return ret_val[0] diff --git a/ansible-modules/netscaler_servicegroup.py b/ansible-modules/netscaler_servicegroup.py index 36e677957..55d56fe5c 100644 --- a/ansible-modules/netscaler_servicegroup.py +++ b/ansible-modules/netscaler_servicegroup.py @@ -410,18 +410,23 @@ def servicegroup_exists(client, module): log('Checking if service group exists') - count = servicegroup.count_filtered(client, 'servicegroupname:%s' % module.params['servicegroupname']) - log('count is %s' % count) - if count > 0: + try: + servicegroup.get(client, module.params['servicegroupname']) + log('Servicegroup exists') return True - else: + except nitro_exception: + log('Servicegroup doesn not exist') return False def servicegroup_identical(client, module, servicegroup_proxy): log('Checking if service group is identical') - servicegroups = servicegroup.get_filtered(client, 'servicegroupname:%s' % module.params['servicegroupname']) - if servicegroup_proxy.has_equal_attributes(servicegroups[0]): + try: + servicegroup_inst = servicegroup.get(client, module.params['servicegroupname']) + except nitro_exception: + return False + + if servicegroup_proxy.has_equal_attributes(servicegroup_inst): return True else: return False @@ -675,8 +680,12 @@ def sync_monitor_bindings(client, module): def diff(client, module, servicegroup_proxy): - servicegroup_list = servicegroup.get_filtered(client, 'servicegroupname:%s' % module.params['servicegroupname']) - diff_object = servicegroup_proxy.diff_object(servicegroup_list[0]) + try: + servicegroup_inst = servicegroup.get(client, module.params['servicegroupname']) + except nitro_exception: + return False + + diff_object = servicegroup_proxy.diff_object(servicegroup_inst) return diff_object From daf532ec778a2ccf61e472cd51fdaa3274ba37f0 Mon Sep 17 00:00:00 2001 From: Oleksandr Savchenko Date: Sun, 23 Sep 2018 00:53:55 +0200 Subject: [PATCH 2/3] added 'mas_proxy_call=dict' module definitions otherwise Ansible 2.5 fails --- ansible-modules/netscaler_cs_policy.py | 4 ++++ ansible-modules/netscaler_lb_monitor.py | 5 ++++- ansible-modules/netscaler_lb_vserver.py | 4 ++++ ansible-modules/netscaler_server.py | 5 ++++- ansible-modules/netscaler_servicegroup.py | 4 ++++ 5 files changed, 20 insertions(+), 2 deletions(-) diff --git a/ansible-modules/netscaler_cs_policy.py b/ansible-modules/netscaler_cs_policy.py index 5b0f18afa..d7653f655 100644 --- a/ansible-modules/netscaler_cs_policy.py +++ b/ansible-modules/netscaler_cs_policy.py @@ -158,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( diff --git a/ansible-modules/netscaler_lb_monitor.py b/ansible-modules/netscaler_lb_monitor.py index a5d374968..950c512da 100644 --- a/ansible-modules/netscaler_lb_monitor.py +++ b/ansible-modules/netscaler_lb_monitor.py @@ -883,7 +883,10 @@ def main(): module_specific_arguments = dict( monitorname=dict(type='str'), - + mas_proxy_call=dict( + default=False, + type='bool' + ), type=dict( type='str', choices=[ diff --git a/ansible-modules/netscaler_lb_vserver.py b/ansible-modules/netscaler_lb_vserver.py index 28afaba09..fd008a8ea 100644 --- a/ansible-modules/netscaler_lb_vserver.py +++ b/ansible-modules/netscaler_lb_vserver.py @@ -1297,6 +1297,10 @@ def main(): module_specific_arguments = dict( name=dict(type='str'), + mas_proxy_call=dict( + default=False, + type='bool' + ), servicetype=dict( type='str', choices=[ diff --git a/ansible-modules/netscaler_server.py b/ansible-modules/netscaler_server.py index 8b040bd04..347ec8cb1 100644 --- a/ansible-modules/netscaler_server.py +++ b/ansible-modules/netscaler_server.py @@ -212,7 +212,10 @@ def main(): module_specific_arguments = dict( name=dict(type='str'), - ipaddress=dict(type='str'), + mas_proxy_call=dict( + default=False, + type='bool' + ), ipaddress=dict(type='str'), domain=dict(type='str'), translationip=dict(type='str'), translationmask=dict(type='str'), diff --git a/ansible-modules/netscaler_servicegroup.py b/ansible-modules/netscaler_servicegroup.py index 55d56fe5c..0b2ba3eb3 100644 --- a/ansible-modules/netscaler_servicegroup.py +++ b/ansible-modules/netscaler_servicegroup.py @@ -703,6 +703,10 @@ def main(): module_specific_arguments = dict( servicegroupname=dict(type='str'), + mas_proxy_call=dict( + default=False, + type='bool' + ), servicetype=dict( type='str', choices=[ From 9df9401a21a5aec78a76c14f3a3d30416d5df1c0 Mon Sep 17 00:00:00 2001 From: Oleksandr Savchenko Date: Thu, 27 Sep 2018 21:03:27 +0200 Subject: [PATCH 3/3] netscaler_lb_monitor.py: removed unnnecessary task which impacted performance --- ansible-modules/netscaler_lb_monitor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ansible-modules/netscaler_lb_monitor.py b/ansible-modules/netscaler_lb_monitor.py index 950c512da..58bd85268 100644 --- a/ansible-modules/netscaler_lb_monitor.py +++ b/ansible-modules/netscaler_lb_monitor.py @@ -1368,7 +1368,6 @@ def main(): 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)