From 6df5f89763a8ba1fd62c401ff56c9cf374ffbaa8 Mon Sep 17 00:00:00 2001 From: Gaurav Rastogi Date: Tue, 28 Feb 2017 11:21:20 -0800 Subject: [PATCH] Added check for avi SDK version as suggested in review. (#21927) * Added check for avi SDK version as suggested in review. * Fixed documentation based on review. * Renamed module_utils.avi to module_utils.avi_ansible_utils as import of avi.sdk would fail due to name collisions. Moved the code to check for AVI version into the ansible modules. * Updated the module with note about reason for name change. --- .../{avi.py => avi_ansible_utils.py} | 19 +++++++++++++++++++ .../modules/network/avi/avi_api_session.py | 4 ++-- .../modules/network/avi/avi_healthmonitor.py | 8 +++----- lib/ansible/modules/network/avi/avi_pool.py | 8 +++----- .../modules/network/avi/avi_poolgroup.py | 10 ++++------ .../network/avi/avi_sslkeyandcertificate.py | 8 +++----- .../modules/network/avi/avi_virtualservice.py | 8 +++----- 7 files changed, 37 insertions(+), 28 deletions(-) rename lib/ansible/module_utils/{avi.py => avi_ansible_utils.py} (78%) diff --git a/lib/ansible/module_utils/avi.py b/lib/ansible/module_utils/avi_ansible_utils.py similarity index 78% rename from lib/ansible/module_utils/avi.py rename to lib/ansible/module_utils/avi_ansible_utils.py index 8aaa73bb73..1bbb3e9c00 100644 --- a/lib/ansible/module_utils/avi.py +++ b/lib/ansible/module_utils/avi_ansible_utils.py @@ -27,7 +27,26 @@ # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # +# This module initially matched the namespace of network module avi. However, +# that causes namespace import error when other modules from avi namespaces +# are imported. In order to avoid the import collisions this was renamed to +# avi_ansible_utils to allow this module to be ceterpiece of all integration +# with external avi modules. + import os +from pkg_resources import parse_version + +HAS_AVI = True +try: + import avi.sdk + sdk_version = getattr(avi.sdk, '__version__', None) + if ((sdk_version is None) or (sdk_version and + (parse_version(sdk_version) < parse_version('16.3.5.post1')))): + # It allows the __version__ to be '' as that value is used in development builds + raise ImportError + from avi.sdk.utils.ansible_utils import avi_ansible_api +except ImportError: + HAS_AVI = False def avi_common_argument_spec(): diff --git a/lib/ansible/modules/network/avi/avi_api_session.py b/lib/ansible/modules/network/avi/avi_api_session.py index 043c542158..3024c7eacd 100644 --- a/lib/ansible/modules/network/avi/avi_api_session.py +++ b/lib/ansible/modules/network/avi/avi_api_session.py @@ -116,11 +116,11 @@ obj: import json import time from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.avi import avi_common_argument_spec, ansible_return from copy import deepcopy -HAS_AVI = True try: + from ansible.module_utils.avi_ansible_utils import ( + avi_common_argument_spec, ansible_return, HAS_AVI) from avi.sdk.avi_api import ApiSession from avi.sdk.utils.ansible_utils import avi_obj_cmp, cleanup_absent_fields except ImportError: diff --git a/lib/ansible/modules/network/avi/avi_healthmonitor.py b/lib/ansible/modules/network/avi/avi_healthmonitor.py index 5bc0f0a181..4327be3a58 100644 --- a/lib/ansible/modules/network/avi/avi_healthmonitor.py +++ b/lib/ansible/modules/network/avi/avi_healthmonitor.py @@ -133,12 +133,10 @@ obj: ''' from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.avi import avi_common_argument_spec - -HAS_AVI = True try: - from avi.sdk.utils.ansible_utils import avi_ansible_api + from ansible.module_utils.avi_ansible_utils import ( + avi_common_argument_spec, HAS_AVI, avi_ansible_api) except ImportError: HAS_AVI = False @@ -170,7 +168,7 @@ def main(): argument_spec=argument_specs, supports_check_mode=True) if not HAS_AVI: return module.fail_json(msg=( - 'Avi python API SDK (avisdk) is not installed. ' + 'Avi python API SDK (avisdk>=16.3.5.post1) is not installed. ' 'For more details visit https://github.com/avinetworks/sdk.')) return avi_ansible_api(module, 'healthmonitor', set([])) diff --git a/lib/ansible/modules/network/avi/avi_pool.py b/lib/ansible/modules/network/avi/avi_pool.py index 801b1af336..2116506f9f 100644 --- a/lib/ansible/modules/network/avi/avi_pool.py +++ b/lib/ansible/modules/network/avi/avi_pool.py @@ -289,12 +289,10 @@ obj: ''' from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.avi import avi_common_argument_spec - -HAS_AVI = True try: - from avi.sdk.utils.ansible_utils import avi_ansible_api + from ansible.module_utils.avi_ansible_utils import ( + avi_common_argument_spec, HAS_AVI, avi_ansible_api) except ImportError: HAS_AVI = False @@ -362,7 +360,7 @@ def main(): argument_spec=argument_specs, supports_check_mode=True) if not HAS_AVI: return module.fail_json(msg=( - 'Avi python API SDK (avisdk) is not installed. ' + 'Avi python API SDK (avisdk>=16.3.5.post1) is not installed. ' 'For more details visit https://github.com/avinetworks/sdk.')) return avi_ansible_api(module, 'pool', set([])) diff --git a/lib/ansible/modules/network/avi/avi_poolgroup.py b/lib/ansible/modules/network/avi/avi_poolgroup.py index c84280fd41..48a5b5aa3c 100644 --- a/lib/ansible/modules/network/avi/avi_poolgroup.py +++ b/lib/ansible/modules/network/avi/avi_poolgroup.py @@ -93,7 +93,7 @@ extends_documentation_fragment: ''' EXAMPLES = """ -- name: Example Adds/Deletes PoolGroup configuration from Avi Controller +- name: Example to create PoolGroup object avi_poolgroup: controller: 10.10.25.42 username: admin @@ -110,12 +110,10 @@ obj: ''' from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.avi import avi_common_argument_spec - -HAS_AVI = True try: - from avi.sdk.utils.ansible_utils import avi_ansible_api + from ansible.module_utils.avi_ansible_utils import ( + avi_common_argument_spec, HAS_AVI, avi_ansible_api) except ImportError: HAS_AVI = False @@ -143,7 +141,7 @@ def main(): argument_spec=argument_specs, supports_check_mode=True) if not HAS_AVI: return module.fail_json(msg=( - 'Avi python API SDK (avisdk) is not installed. ' + 'Avi python API SDK (avisdk>=16.3.5.post1) is not installed. ' 'For more details visit https://github.com/avinetworks/sdk.')) return avi_ansible_api(module, 'poolgroup', set([])) diff --git a/lib/ansible/modules/network/avi/avi_sslkeyandcertificate.py b/lib/ansible/modules/network/avi/avi_sslkeyandcertificate.py index aa1ac5039f..090226c80d 100644 --- a/lib/ansible/modules/network/avi/avi_sslkeyandcertificate.py +++ b/lib/ansible/modules/network/avi/avi_sslkeyandcertificate.py @@ -128,12 +128,10 @@ obj: ''' from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.avi import avi_common_argument_spec - -HAS_AVI = True try: - from avi.sdk.utils.ansible_utils import avi_ansible_api + from ansible.module_utils.avi_ansible_utils import ( + avi_common_argument_spec, HAS_AVI, avi_ansible_api) except ImportError: HAS_AVI = False @@ -164,7 +162,7 @@ def main(): argument_spec=argument_specs, supports_check_mode=True) if not HAS_AVI: return module.fail_json(msg=( - 'Avi python API SDK (avisdk) is not installed. ' + 'Avi python API SDK (avisdk>=16.3.5.post1) is not installed. ' 'For more details visit https://github.com/avinetworks/sdk.')) return avi_ansible_api(module, 'sslkeyandcertificate', set(['key'])) diff --git a/lib/ansible/modules/network/avi/avi_virtualservice.py b/lib/ansible/modules/network/avi/avi_virtualservice.py index 926750bab4..94b47da84e 100644 --- a/lib/ansible/modules/network/avi/avi_virtualservice.py +++ b/lib/ansible/modules/network/avi/avi_virtualservice.py @@ -353,12 +353,10 @@ obj: ''' from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.avi import avi_common_argument_spec - -HAS_AVI = True try: - from avi.sdk.utils.ansible_utils import avi_ansible_api + from ansible.module_utils.avi_ansible_utils import ( + avi_common_argument_spec, HAS_AVI, avi_ansible_api) except ImportError: HAS_AVI = False @@ -445,7 +443,7 @@ def main(): argument_spec=argument_specs, supports_check_mode=True) if not HAS_AVI: return module.fail_json(msg=( - 'Avi python API SDK (avisdk) is not installed. ' + 'Avi python API SDK (avisdk>=16.3.5.post1) is not installed. ' 'For more details visit https://github.com/avinetworks/sdk.')) return avi_ansible_api(module, 'virtualservice', set([]))