From cc4bbb2929c66a344642ad7571b4b33079476a9b Mon Sep 17 00:00:00 2001 From: Tim Rupp Date: Wed, 25 Oct 2017 12:57:21 -0700 Subject: [PATCH] Various bigip_pool fixes (#32161) * corrects copyrights and mocks in unit tests * fixes module code to include code to cleanup tokens --- lib/ansible/modules/network/f5/bigip_pool.py | 17 ++++++++++---- .../modules/network/f5/test_bigip_pool.py | 22 ++++--------------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/lib/ansible/modules/network/f5/bigip_pool.py b/lib/ansible/modules/network/f5/bigip_pool.py index b87ba1e0f38..60f74bd5419 100644 --- a/lib/ansible/modules/network/f5/bigip_pool.py +++ b/lib/ansible/modules/network/f5/bigip_pool.py @@ -289,7 +289,6 @@ reselect_tries: ''' import re -import os from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import AnsibleF5Parameters @@ -392,9 +391,7 @@ class Parameters(AnsibleF5Parameters): ) ) lb_method = lb_map.get(lb_method, lb_method.replace('_', '-')) - try: - assert lb_method in spec.lb_choice - except AssertionError: + if lb_method not in spec.lb_choice: raise F5ModuleError('Provided lb_method is unknown') return lb_method @@ -893,6 +890,16 @@ class ArgumentSpec(object): self.f5_product_name = 'bigip' +def cleanup_tokens(client): + try: + resource = client.api.shared.authz.tokens_s.token.load( + name=client.api.icrs.token + ) + resource.delete() + except Exception: + pass + + def main(): if not HAS_F5SDK: raise F5ModuleError("The python f5-sdk module is required") @@ -908,8 +915,10 @@ def main(): try: mm = ModuleManager(client) results = mm.exec_module() + cleanup_tokens(client) client.module.exit_json(**results) except F5ModuleError as e: + cleanup_tokens(client) client.module.fail_json(msg=str(e)) diff --git a/test/units/modules/network/f5/test_bigip_pool.py b/test/units/modules/network/f5/test_bigip_pool.py index bb7fdbc6b07..a6b4277ba62 100644 --- a/test/units/modules/network/f5/test_bigip_pool.py +++ b/test/units/modules/network/f5/test_bigip_pool.py @@ -1,21 +1,7 @@ # -*- coding: utf-8 -*- # -# Copyright 2017 F5 Networks Inc. -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public Liccense for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright (c) 2017 F5 Networks Inc. +# 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 @@ -171,8 +157,8 @@ class TestManager(unittest.TestCase): ) mm = ModuleManager(client) - mm.create_on_device = lambda: True - mm.exists = lambda: False + mm.create_on_device = Mock(return_value=True) + mm.exists = Mock(return_value=False) results = mm.exec_module()