mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 04:40:22 -07:00
Fixes and additions to bigip_pool (#34205)
Removes deprecated host/port params. Adds metadata param. Adds new classes to better support refactoring and maintenance.
This commit is contained in:
parent
554ffd70f5
commit
75fbfb9e36
2 changed files with 276 additions and 461 deletions
|
@ -22,14 +22,16 @@ from ansible.module_utils.f5_utils import AnsibleF5Client
|
|||
from ansible.module_utils.f5_utils import F5ModuleError
|
||||
|
||||
try:
|
||||
from library.bigip_pool import Parameters
|
||||
from library.bigip_pool import ApiParameters
|
||||
from library.bigip_pool import ModuleParameters
|
||||
from library.bigip_pool import ModuleManager
|
||||
from library.bigip_pool import ArgumentSpec
|
||||
from ansible.module_utils.f5_utils import iControlUnexpectedHTTPError
|
||||
from test.unit.modules.utils import set_module_args
|
||||
except ImportError:
|
||||
try:
|
||||
from ansible.modules.network.f5.bigip_pool import Parameters
|
||||
from ansible.modules.network.f5.bigip_pool import ApiParameters
|
||||
from ansible.modules.network.f5.bigip_pool import ModuleParameters
|
||||
from ansible.modules.network.f5.bigip_pool import ModuleManager
|
||||
from ansible.modules.network.f5.bigip_pool import ArgumentSpec
|
||||
from ansible.module_utils.f5_utils import iControlUnexpectedHTTPError
|
||||
|
@ -59,11 +61,6 @@ def load_fixture(name):
|
|||
return data
|
||||
|
||||
|
||||
class BigIpObj(object):
|
||||
def __init__(self, **kwargs):
|
||||
self.__dict__.update(kwargs)
|
||||
|
||||
|
||||
class TestParameters(unittest.TestCase):
|
||||
def test_module_parameters(self):
|
||||
args = dict(
|
||||
|
@ -72,33 +69,26 @@ class TestParameters(unittest.TestCase):
|
|||
quorum=1,
|
||||
slow_ramp_time=200,
|
||||
reselect_tries=5,
|
||||
service_down_action='drop',
|
||||
host='192.168.1.1',
|
||||
port=8080
|
||||
service_down_action='drop'
|
||||
)
|
||||
|
||||
p = Parameters(args)
|
||||
p = ModuleParameters(args)
|
||||
assert p.monitor_type == 'm_of_n'
|
||||
assert p.quorum == 1
|
||||
assert p.monitors == 'min 1 of { /Common/Fake /Common/Fake2 }'
|
||||
assert p.host == '192.168.1.1'
|
||||
assert p.port == 8080
|
||||
assert p.member_name == '192.168.1.1:8080'
|
||||
assert p.slow_ramp_time == 200
|
||||
assert p.reselect_tries == 5
|
||||
assert p.service_down_action == 'drop'
|
||||
|
||||
def test_api_parameters(self):
|
||||
m = ['/Common/Fake', '/Common/Fake2']
|
||||
args = dict(
|
||||
monitor_type='and_list',
|
||||
monitors=m,
|
||||
monitor="/Common/Fake and /Common/Fake2 ",
|
||||
slowRampTime=200,
|
||||
reselectTries=5,
|
||||
serviceDownAction='drop'
|
||||
)
|
||||
|
||||
p = Parameters(args)
|
||||
p = ApiParameters(args)
|
||||
assert p.monitors == '/Common/Fake and /Common/Fake2'
|
||||
assert p.slow_ramp_time == 200
|
||||
assert p.reselect_tries == 5
|
||||
|
@ -109,7 +99,7 @@ class TestParameters(unittest.TestCase):
|
|||
lb_method='obscure_hyphenated_fake_method',
|
||||
)
|
||||
with pytest.raises(F5ModuleError):
|
||||
p = Parameters(args)
|
||||
p = ModuleParameters(args)
|
||||
assert p.lb_method == 'foo'
|
||||
|
||||
def test_unknown_api_lb_method(self):
|
||||
|
@ -117,7 +107,7 @@ class TestParameters(unittest.TestCase):
|
|||
loadBalancingMode='obscure_hypenated_fake_method'
|
||||
)
|
||||
with pytest.raises(F5ModuleError):
|
||||
p = Parameters(args)
|
||||
p = ApiParameters(args)
|
||||
assert p.lb_method == 'foo'
|
||||
|
||||
|
||||
|
@ -127,17 +117,13 @@ class TestManager(unittest.TestCase):
|
|||
|
||||
def setUp(self):
|
||||
self.spec = ArgumentSpec()
|
||||
self.loaded_members = []
|
||||
members = load_fixture('pool_members_subcollection.json')
|
||||
for item in members:
|
||||
self.loaded_members.append(BigIpObj(**item))
|
||||
|
||||
def test_create_pool(self, *args):
|
||||
set_module_args(dict(
|
||||
pool='fake_pool',
|
||||
description='fakepool',
|
||||
service_down_action='drop',
|
||||
lb_method='round_robin',
|
||||
lb_method='round-robin',
|
||||
partition='Common',
|
||||
slow_ramp_time=10,
|
||||
reselect_tries=1,
|
||||
|
@ -166,102 +152,10 @@ class TestManager(unittest.TestCase):
|
|||
assert results['slow_ramp_time'] == 10
|
||||
assert results['reselect_tries'] == 1
|
||||
|
||||
def test_create_pool_with_pool_member(self, *args):
|
||||
set_module_args(dict(
|
||||
pool='fake_pool',
|
||||
partition='Common',
|
||||
host='192.168.1.1',
|
||||
port=8080,
|
||||
server='localhost',
|
||||
password='password',
|
||||
user='admin'
|
||||
))
|
||||
|
||||
client = AnsibleF5Client(
|
||||
argument_spec=self.spec.argument_spec,
|
||||
supports_check_mode=self.spec.supports_check_mode,
|
||||
f5_product_name=self.spec.f5_product_name
|
||||
)
|
||||
|
||||
current = (
|
||||
Parameters(
|
||||
load_fixture('load_ltm_pool.json')
|
||||
),
|
||||
self.loaded_members,
|
||||
{},
|
||||
)
|
||||
|
||||
mm = ModuleManager(client)
|
||||
mm.create_on_device = Mock(return_value=True)
|
||||
mm.exists = Mock(return_value=False)
|
||||
mm.read_current_from_device = Mock(return_value=current)
|
||||
mm.create_member_on_device = Mock(return_value=True)
|
||||
|
||||
results = mm.exec_module()
|
||||
|
||||
assert results['changed'] is True
|
||||
assert results['host'] == '192.168.1.1'
|
||||
assert results['port'] == 8080
|
||||
assert results['member_name'] == '192.168.1.1:8080'
|
||||
|
||||
def test_create_pool_invalid_host(self, *args):
|
||||
set_module_args(dict(
|
||||
pool='fake_pool',
|
||||
partition='Common',
|
||||
host='this.will.fail',
|
||||
port=8080,
|
||||
server='localhost',
|
||||
password='password',
|
||||
user='admin'
|
||||
))
|
||||
|
||||
client = AnsibleF5Client(
|
||||
argument_spec=self.spec.argument_spec,
|
||||
supports_check_mode=self.spec.supports_check_mode,
|
||||
f5_product_name=self.spec.f5_product_name
|
||||
)
|
||||
|
||||
mm = ModuleManager(client)
|
||||
mm.create_on_device = Mock(return_value=True)
|
||||
mm.exists = Mock(return_value=False)
|
||||
|
||||
msg = "'this.will.fail' is not a valid IP address"
|
||||
|
||||
with pytest.raises(F5ModuleError) as err:
|
||||
mm.exec_module()
|
||||
assert str(err.value) == msg
|
||||
|
||||
def test_create_pool_invalid_port(self, *args):
|
||||
set_module_args(dict(
|
||||
pool='fake_pool',
|
||||
partition='Common',
|
||||
host='192.168.1.1',
|
||||
port=98741,
|
||||
server='localhost',
|
||||
password='password',
|
||||
user='admin'
|
||||
))
|
||||
|
||||
client = AnsibleF5Client(
|
||||
argument_spec=self.spec.argument_spec,
|
||||
supports_check_mode=self.spec.supports_check_mode,
|
||||
f5_product_name=self.spec.f5_product_name
|
||||
)
|
||||
|
||||
mm = ModuleManager(client)
|
||||
mm.create_on_device = Mock(return_value=True)
|
||||
mm.exists = Mock(return_value=False)
|
||||
|
||||
msg = "The provided port '98741' must be between 0 and 65535"
|
||||
|
||||
with pytest.raises(F5ModuleError) as err:
|
||||
mm.exec_module()
|
||||
assert str(err.value) == msg
|
||||
|
||||
def test_create_pool_monitor_type_missing(self, *args):
|
||||
set_module_args(dict(
|
||||
pool='fake_pool',
|
||||
lb_method='round_robin',
|
||||
lb_method='round-robin',
|
||||
partition='Common',
|
||||
monitors=['/Common/tcp', '/Common/http'],
|
||||
server='localhost',
|
||||
|
@ -283,13 +177,13 @@ class TestManager(unittest.TestCase):
|
|||
|
||||
assert results['changed'] is True
|
||||
assert results['name'] == 'fake_pool'
|
||||
assert results['monitors'] == '/Common/tcp and /Common/http'
|
||||
assert results['monitors'] == ['/Common/http', '/Common/tcp']
|
||||
assert results['monitor_type'] == 'and_list'
|
||||
|
||||
def test_create_pool_monitors_missing(self, *args):
|
||||
set_module_args(dict(
|
||||
pool='fake_pool',
|
||||
lb_method='round_robin',
|
||||
lb_method='round-robin',
|
||||
partition='Common',
|
||||
monitor_type='and_list',
|
||||
server='localhost',
|
||||
|
@ -317,7 +211,7 @@ class TestManager(unittest.TestCase):
|
|||
def test_create_pool_quorum_missing(self, *args):
|
||||
set_module_args(dict(
|
||||
pool='fake_pool',
|
||||
lb_method='round_robin',
|
||||
lb_method='round-robin',
|
||||
partition='Common',
|
||||
monitor_type='m_of_n',
|
||||
monitors=['/Common/tcp', '/Common/http'],
|
||||
|
@ -367,7 +261,7 @@ class TestManager(unittest.TestCase):
|
|||
|
||||
assert results['changed'] is True
|
||||
assert results['name'] == 'fake_pool'
|
||||
assert results['monitors'] == '/Common/tcp and /Common/http'
|
||||
assert results['monitors'] == ['/Common/http', '/Common/tcp']
|
||||
assert results['monitor_type'] == 'and_list'
|
||||
|
||||
def test_create_pool_monitor_m_of_n(self, *args):
|
||||
|
@ -396,7 +290,7 @@ class TestManager(unittest.TestCase):
|
|||
|
||||
assert results['changed'] is True
|
||||
assert results['name'] == 'fake_pool'
|
||||
assert results['monitors'] == 'min 1 of { /Common/tcp /Common/http }'
|
||||
assert results['monitors'] == ['/Common/http', '/Common/tcp']
|
||||
assert results['monitor_type'] == 'm_of_n'
|
||||
|
||||
def test_update_monitors(self, *args):
|
||||
|
@ -417,13 +311,7 @@ class TestManager(unittest.TestCase):
|
|||
)
|
||||
mm = ModuleManager(client)
|
||||
|
||||
current = (
|
||||
Parameters(
|
||||
load_fixture('load_ltm_pool.json')
|
||||
),
|
||||
[],
|
||||
{},
|
||||
)
|
||||
current = ApiParameters(load_fixture('load_ltm_pool.json'))
|
||||
|
||||
mm.update_on_device = Mock(return_value=True)
|
||||
mm.exists = Mock(return_value=True)
|
||||
|
@ -433,79 +321,6 @@ class TestManager(unittest.TestCase):
|
|||
|
||||
assert results['changed'] is True
|
||||
assert results['monitor_type'] == 'and_list'
|
||||
assert results['monitors'] == '/Common/http and /Common/tcp'
|
||||
|
||||
def test_update_pool_new_member(self, *args):
|
||||
set_module_args(dict(
|
||||
name='test_pool',
|
||||
partition='Common',
|
||||
host='192.168.1.1',
|
||||
port=8080,
|
||||
server='localhost',
|
||||
password='password',
|
||||
user='admin'
|
||||
))
|
||||
|
||||
client = AnsibleF5Client(
|
||||
argument_spec=self.spec.argument_spec,
|
||||
supports_check_mode=self.spec.supports_check_mode,
|
||||
f5_product_name=self.spec.f5_product_name
|
||||
)
|
||||
mm = ModuleManager(client)
|
||||
|
||||
current = (
|
||||
Parameters(
|
||||
load_fixture('load_ltm_pool.json')
|
||||
),
|
||||
self.loaded_members,
|
||||
{},
|
||||
)
|
||||
|
||||
mm.update_on_device = Mock(return_value=True)
|
||||
mm.exists = Mock(return_value=True)
|
||||
mm.read_current_from_device = Mock(return_value=current)
|
||||
mm.create_member_on_device = Mock(return_value=True)
|
||||
|
||||
results = mm.exec_module()
|
||||
|
||||
assert results['changed'] is True
|
||||
assert results['host'] == '192.168.1.1'
|
||||
assert results['port'] == 8080
|
||||
|
||||
def test_update_pool_member_exists(self, *args):
|
||||
set_module_args(dict(
|
||||
name='test_pool',
|
||||
partition='Common',
|
||||
host='1.1.1.1',
|
||||
port=80,
|
||||
server='localhost',
|
||||
password='password',
|
||||
user='admin'
|
||||
))
|
||||
|
||||
client = AnsibleF5Client(
|
||||
argument_spec=self.spec.argument_spec,
|
||||
supports_check_mode=self.spec.supports_check_mode,
|
||||
f5_product_name=self.spec.f5_product_name
|
||||
)
|
||||
mm = ModuleManager(client)
|
||||
|
||||
current = (
|
||||
Parameters(
|
||||
load_fixture('load_ltm_pool.json')
|
||||
),
|
||||
self.loaded_members,
|
||||
{},
|
||||
)
|
||||
|
||||
mm.update_on_device = Mock(return_value=True)
|
||||
mm.exists = Mock(return_value=True)
|
||||
mm.read_current_from_device = Mock(return_value=current)
|
||||
mm.create_member_on_device = Mock(return_value=True)
|
||||
|
||||
results = mm.exec_module()
|
||||
|
||||
assert results['changed'] is False
|
||||
|
||||
def test_create_pool_monitor_and_list_no_partition(self, *args):
|
||||
set_module_args(dict(
|
||||
|
@ -531,7 +346,7 @@ class TestManager(unittest.TestCase):
|
|||
|
||||
assert results['changed'] is True
|
||||
assert results['name'] == 'fake_pool'
|
||||
assert results['monitors'] == '/Common/tcp and /Common/http'
|
||||
assert results['monitors'] == ['/Common/http', '/Common/tcp']
|
||||
assert results['monitor_type'] == 'and_list'
|
||||
|
||||
def test_create_pool_monitor_m_of_n_no_partition(self, *args):
|
||||
|
@ -559,7 +374,7 @@ class TestManager(unittest.TestCase):
|
|||
|
||||
assert results['changed'] is True
|
||||
assert results['name'] == 'fake_pool'
|
||||
assert results['monitors'] == 'min 1 of { /Common/tcp /Common/http }'
|
||||
assert results['monitors'] == ['/Common/http', '/Common/tcp']
|
||||
assert results['monitor_type'] == 'm_of_n'
|
||||
|
||||
def test_create_pool_monitor_and_list_custom_partition(self, *args):
|
||||
|
@ -587,7 +402,7 @@ class TestManager(unittest.TestCase):
|
|||
|
||||
assert results['changed'] is True
|
||||
assert results['name'] == 'fake_pool'
|
||||
assert results['monitors'] == '/Testing/tcp and /Testing/http'
|
||||
assert results['monitors'] == ['/Testing/http', '/Testing/tcp']
|
||||
assert results['monitor_type'] == 'and_list'
|
||||
|
||||
def test_create_pool_monitor_m_of_n_custom_partition(self, *args):
|
||||
|
@ -616,5 +431,32 @@ class TestManager(unittest.TestCase):
|
|||
|
||||
assert results['changed'] is True
|
||||
assert results['name'] == 'fake_pool'
|
||||
assert results['monitors'] == 'min 1 of { /Testing/tcp /Testing/http }'
|
||||
assert results['monitors'] == ['/Testing/http', '/Testing/tcp']
|
||||
assert results['monitor_type'] == 'm_of_n'
|
||||
|
||||
def test_create_pool_with_metadata(self, *args):
|
||||
set_module_args(dict(
|
||||
pool='fake_pool',
|
||||
metadata=dict(ansible='2.4'),
|
||||
server='localhost',
|
||||
password='password',
|
||||
user='admin'
|
||||
))
|
||||
|
||||
client = AnsibleF5Client(
|
||||
argument_spec=self.spec.argument_spec,
|
||||
supports_check_mode=self.spec.supports_check_mode,
|
||||
f5_product_name=self.spec.f5_product_name
|
||||
)
|
||||
|
||||
mm = ModuleManager(client)
|
||||
mm.create_on_device = Mock(return_value=True)
|
||||
mm.exists = Mock(return_value=False)
|
||||
|
||||
results = mm.exec_module()
|
||||
|
||||
assert results['changed'] is True
|
||||
assert results['name'] == 'fake_pool'
|
||||
assert 'metadata' in results
|
||||
assert 'ansible' in results['metadata']
|
||||
assert results['metadata']['ansible'] == '2.4'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue