1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Refactors irule module to conform to recent standards (#25709)

Also includes unit tests for the code
This commit is contained in:
Tim Rupp 2017-06-14 11:03:17 -07:00 committed by John R Barker
parent ba12ce64b0
commit b85785bf07
6 changed files with 767 additions and 234 deletions

View file

@ -18,10 +18,11 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
ANSIBLE_METADATA = {'metadata_version': '1.0',
ANSIBLE_METADATA = {
'status': ['preview'],
'supported_by': 'community'}
'supported_by': 'community',
'metadata_version': '1.0'
}
DOCUMENTATION = '''
---
@ -40,28 +41,22 @@ options:
module:
description:
- The BIG-IP module to add the iRule to.
required: true
required: True
choices:
- ltm
- gtm
partition:
description:
- The partition to create the iRule on.
required: false
default: Common
name:
description:
- The name of the iRule.
required: true
required: True
src:
description:
- The iRule file to interpret and upload to the BIG-IP. Either one
of C(src) or C(content) must be provided.
required: true
required: True
state:
description:
- Whether the iRule should exist or not.
required: false
default: present
choices:
- present
@ -77,9 +72,9 @@ author:
'''
EXAMPLES = '''
- name: Add the iRule contained in templated irule.tcl to the LTM module
- name: Add the iRule contained in template irule.tcl to the LTM module
bigip_irule:
content: "{{ lookup('template', 'irule-template.tcl') }}"
content: "{{ lookup('template', 'irule.tcl') }}"
module: "ltm"
name: "MyiRule"
password: "secret"
@ -94,7 +89,7 @@ EXAMPLES = '''
name: "MyiRule"
password: "secret"
server: "lb.mydomain.com"
src: "irule-static.tcl"
src: "irule.tcl"
state: "present"
user: "admin"
delegate_to: localhost
@ -111,62 +106,112 @@ src:
returned: changed and success, when provided
type: string
sample: "/opt/src/irules/example1.tcl"
name:
description: The name of the iRule that was managed
returned: changed and success
type: string
sample: "my-irule"
content:
description: The content of the iRule that was managed
returned: changed and success
type: string
sample: "when LB_FAILED { set wipHost [LB::server addr] }"
partition:
description: The partition in which the iRule was managed
returned: changed and success
type: string
sample: "Common"
'''
try:
from f5.bigip import ManagementRoot
from icontrol.session import iControlUnexpectedHTTPError
HAS_F5SDK = True
except ImportError:
HAS_F5SDK = False
MODULES = ['gtm', 'ltm']
from ansible.module_utils.f5_utils import (
AnsibleF5Client,
AnsibleF5Parameters,
HAS_F5SDK,
F5ModuleError,
iControlUnexpectedHTTPError
)
class BigIpiRule(object):
def __init__(self, *args, **kwargs):
if not HAS_F5SDK:
raise F5ModuleError("The python f5-sdk module is required")
class Parameters(AnsibleF5Parameters):
api_map = {
'apiAnonymous': 'content'
}
if kwargs['state'] != 'absent':
if not kwargs['content'] and not kwargs['src']:
updatables = [
'content'
]
api_attributes = [
'apiAnonymous'
]
returnables = [
'content', 'src', 'module'
]
def to_return(self):
result = {}
try:
for returnable in self.returnables:
result[returnable] = getattr(self, returnable)
result = self._filter_params(result)
except Exception:
pass
return result
def api_params(self):
result = {}
for api_attribute in self.api_attributes:
if self.api_map is not None and api_attribute in self.api_map:
result[api_attribute] = getattr(self, self.api_map[api_attribute])
else:
result[api_attribute] = getattr(self, api_attribute)
result = self._filter_params(result)
return result
@property
def content(self):
if self._values['content'] is None:
return None
return str(self._values['content']).strip()
@property
def src(self):
if self._values['src'] is None:
return None
return self._values['src']
@src.setter
def src(self, value):
if value:
self._values['src'] = value
with open(value) as f:
result = f.read()
self._values['content'] = result
class ModuleManager(object):
def __init__(self, client):
self.client = client
def exec_module(self):
if self.client.module.params['module'] == 'ltm':
manager = self.get_manager('ltm')
elif self.client.module.params['module'] == 'gtm':
manager = self.get_manager('gtm')
else:
raise F5ModuleError(
"Either 'content' or 'src' must be provided"
"An unknown iRule module type was specified"
)
return manager.exec_module()
source = kwargs['src']
if source:
with open(source) as f:
kwargs['content'] = f.read()
def get_manager(self, type):
if type == 'ltm':
return LtmManager(self.client)
elif type == 'gtm':
return GtmManager(self.client)
# The params that change in the module
self.cparams = dict()
# Stores the params that are sent to the module
self.params = kwargs
self.api = ManagementRoot(kwargs['server'],
kwargs['user'],
kwargs['password'],
port=kwargs['server_port'])
class BaseManager(object):
def __init__(self, client):
self.client = client
self.want = Parameters(self.client.module.params)
self.changes = Parameters()
def flush(self):
def exec_module(self):
changed = False
result = dict()
state = self.params['state']
state = self.want.state
try:
if state == "present":
@ -176,214 +221,206 @@ class BigIpiRule(object):
except iControlUnexpectedHTTPError as e:
raise F5ModuleError(str(e))
result.update(**self.cparams)
changes = self.changes.to_return()
result.update(**changes)
result.update(dict(changed=changed))
return result
def read(self):
"""Read information and transform it
def _set_changed_options(self):
changed = {}
for key in Parameters.returnables:
if getattr(self.want, key) is not None:
changed[key] = getattr(self.want, key)
if changed:
self.changes = Parameters(changed)
The values that are returned by BIG-IP in the f5-sdk can have encoding
attached to them as well as be completely missing in some cases.
Therefore, this method will transform the data from the BIG-IP into a
format that is more easily consumable by the rest of the class and the
parameters that are supported by the module.
"""
p = dict()
name = self.params['name']
partition = self.params['partition']
module = self.params['module']
if module == 'ltm':
r = self.api.tm.ltm.rules.rule.load(
name=name,
partition=partition
)
elif module == 'gtm':
r = self.api.tm.gtm.rules.rule.load(
name=name,
partition=partition
)
if hasattr(r, 'apiAnonymous'):
p['content'] = str(r.apiAnonymous.strip())
p['name'] = name
return p
def delete(self):
params = dict()
check_mode = self.params['check_mode']
module = self.params['module']
params['name'] = self.params['name']
params['partition'] = self.params['partition']
self.cparams = camel_dict_to_snake_dict(params)
if check_mode:
def _update_changed_options(self):
changed = {}
for key in Parameters.updatables:
if getattr(self.want, key) is not None:
attr1 = getattr(self.want, key)
attr2 = getattr(self.have, key)
if attr1 != attr2:
changed[key] = attr1
if changed:
self.changes = Parameters(changed)
return True
if module == 'ltm':
r = self.api.tm.ltm.rules.rule.load(**params)
r.delete()
elif module == 'gtm':
r = self.api.tm.gtm.rules.rule.load(**params)
r.delete()
if self.exists():
raise F5ModuleError("Failed to delete the iRule")
return True
def exists(self):
name = self.params['name']
partition = self.params['partition']
module = self.params['module']
if module == 'ltm':
return self.api.tm.ltm.rules.rule.exists(
name=name,
partition=partition
)
elif module == 'gtm':
return self.api.tm.gtm.rules.rule.exists(
name=name,
partition=partition
)
return False
def present(self):
if not self.want.content and not self.want.src:
raise F5ModuleError(
"Either 'content' or 'src' must be provided"
)
if self.exists():
return self.update()
else:
return self.create()
def update(self):
params = dict()
current = self.read()
changed = False
check_mode = self.params['check_mode']
content = self.params['content']
name = self.params['name']
partition = self.params['partition']
module = self.params['module']
if content is not None:
content = content.strip()
if 'content' in current:
if content != current['content']:
params['apiAnonymous'] = content
else:
params['apiAnonymous'] = content
if params:
changed = True
params['name'] = name
params['partition'] = partition
self.cparams = camel_dict_to_snake_dict(params)
if 'api_anonymous' in self.cparams:
self.cparams['content'] = self.cparams.pop('api_anonymous')
if self.params['src']:
self.cparams['src'] = self.params['src']
if check_mode:
return changed
else:
return changed
if module == 'ltm':
d = self.api.tm.ltm.rules.rule.load(
name=name,
partition=partition
)
d.update(**params)
d.refresh()
elif module == 'gtm':
d = self.api.tm.gtm.rules.rule.load(
name=name,
partition=partition
)
d.update(**params)
d.refresh()
return True
def create(self):
params = dict()
check_mode = self.params['check_mode']
content = self.params['content']
name = self.params['name']
partition = self.params['partition']
module = self.params['module']
if check_mode:
self._set_changed_options()
if self.client.check_mode:
return True
if content is not None:
params['apiAnonymous'] = content.strip()
params['name'] = name
params['partition'] = partition
self.cparams = camel_dict_to_snake_dict(params)
if 'api_anonymous' in self.cparams:
self.cparams['content'] = self.cparams.pop('api_anonymous')
if self.params['src']:
self.cparams['src'] = self.params['src']
if check_mode:
return True
if module == 'ltm':
d = self.api.tm.ltm.rules.rule
d.create(**params)
elif module == 'gtm':
d = self.api.tm.gtm.rules.rule
d.create(**params)
self.create_on_device()
if not self.exists():
raise F5ModuleError("Failed to create the iRule")
return True
def should_update(self):
result = self._update_changed_options()
if result:
return True
return False
def update(self):
self.have = self.read_current_from_device()
if not self.should_update():
return False
if self.client.check_mode:
return True
self.update_on_device()
return True
def absent(self):
changed = False
if self.exists():
changed = self.delete()
return self.remove()
return False
return changed
def remove(self):
if self.client.check_mode:
return True
self.remove_from_device()
if self.exists():
raise F5ModuleError("Failed to delete the iRule")
return True
class LtmManager(BaseManager):
def exists(self):
result = self.client.api.tm.ltm.rules.rule.exists(
name=self.want.name,
partition=self.want.partition
)
return result
def update_on_device(self):
params = self.want.api_params()
resource = self.client.api.tm.ltm.rules.rule.load(
name=self.want.name,
partition=self.want.partition
)
resource.update(**params)
def create_on_device(self):
params = self.want.api_params()
resource = self.client.api.tm.ltm.rules.rule
resource.create(
name=self.want.name,
partition=self.want.partition,
**params
)
def read_current_from_device(self):
resource = self.client.api.tm.ltm.rules.rule.load(
name=self.want.name,
partition=self.want.partition
)
result = resource.attrs
return Parameters(result)
def remove_from_device(self):
resource = self.client.api.tm.ltm.rules.rule.load(
name=self.want.name,
partition=self.want.partition
)
resource.delete()
class GtmManager(BaseManager):
def read_current_from_device(self):
resource = self.client.api.tm.gtm.rules.rule.load(
name=self.want.name,
partition=self.want.partition
)
result = resource.attrs
return Parameters(result)
def remove_from_device(self):
resource = self.client.api.tm.gtm.rules.rule.load(
name=self.want.name,
partition=self.want.partition
)
resource.delete()
def exists(self):
result = self.client.api.tm.gtm.rules.rule.exists(
name=self.want.name,
partition=self.want.partition
)
return result
def update_on_device(self):
params = self.want.api_params()
resource = self.client.api.tm.gtm.rules.rule.load(
name=self.want.name,
partition=self.want.partition
)
resource.update(**params)
def create_on_device(self):
params = self.want.api_params()
resource = self.client.api.tm.gtm.rules.rule
resource.create(
name=self.want.name,
partition=self.want.partition,
**params
)
class ArgumentSpec(object):
def __init__(self):
self.supports_check_mode = True
self.argument_spec = dict(
content=dict(
required=False,
default=None
),
src=dict(
required=False,
default=None
),
name=dict(required=True),
module=dict(
required=True,
choices=['gtm', 'ltm']
)
)
self.mutually_exclusive = [
['content', 'src']
]
self.f5_product_name = 'bigip'
def main():
argument_spec = f5_argument_spec()
if not HAS_F5SDK:
raise F5ModuleError("The python f5-sdk module is required")
meta_args = dict(
content=dict(required=False, default=None),
src=dict(required=False, default=None),
name=dict(required=True),
module=dict(required=True, choices=MODULES)
)
argument_spec.update(meta_args)
spec = ArgumentSpec()
module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True,
mutually_exclusive=[
['content', 'src']
]
client = AnsibleF5Client(
argument_spec=spec.argument_spec,
mutually_exclusive=spec.mutually_exclusive,
supports_check_mode=spec.supports_check_mode,
f5_product_name=spec.f5_product_name
)
try:
obj = BigIpiRule(check_mode=module.check_mode, **module.params)
result = obj.flush()
module.exit_json(**result)
mm = ModuleManager(client)
results = mm.exec_module()
client.module.exit_json(**results)
except F5ModuleError as e:
module.fail_json(msg=str(e))
client.module.fail_json(msg=str(e))
from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import camel_dict_to_snake_dict
from ansible.module_utils.f5_utils import *
if __name__ == '__main__':
main()

View file

@ -0,0 +1,8 @@
when LB_SELECTED {
# Capture IP address chosen by WIP load balancing
set wipHost [LB::server addr]
}
when LB_FAILED {
set wipHost [LB::server addr]
}

View file

@ -0,0 +1,18 @@
when RULE_INIT {
set static::FormBaseURL "/sp-ofba-form"
set static::FormReturnURL "/sp-ofba-completed"
set static::HeadAuthReq "X-FORMS_BASED_AUTH_REQUIRED"
set static::HeadAuthRet "X-FORMS_BASED_AUTH_RETURN_URL"
set static::HeadAuthSize "X-FORMS_BASED_AUTH_DIALOG_SIZE"
set static::HeadAuthSizeVal "800x600"
set static::ckname "MRHSession_SP"
set static::Basic_Realm_Text "SharePoint Authentication"
}
when HTTP_REQUEST {
set apmsessionid [HTTP::cookie value MRHSession]
}
when HTTP_RESPONSE {
# Insert persistent cookie for html content type and private session
}

View file

@ -0,0 +1,20 @@
[
{
"kind": "tm:gtm:rule:rulestate",
"name": "asdf",
"partition": "Common",
"fullPath": "/Common/asdf",
"generation": 92,
"selfLink": "https://localhost/mgmt/tm/gtm/rule/~Common~asdf?ver=12.1.2",
"apiAnonymous": "when DNS_REQUEST {\n if { [IP::addr [IP::remote_addr] equals 10.254.254.0/24] } {\n cname test.affilate.example.com\n\n } elseif { [IP::addr [IP::remote_addr] equals 10.0.0.0/8] } {\n cname test.internal.example.com\n\n }\n #everything else will be handled by the default pools in the main WIP\n}"
},
{
"kind": "tm:gtm:rule:rulestate",
"name": "foo",
"partition": "Common",
"fullPath": "/Common/foo",
"generation": 93,
"selfLink": "https://localhost/mgmt/tm/gtm/rule/~Common~foo?ver=12.1.2",
"apiAnonymous": "when LB_SELECTED {\n # Capture IP address chosen by WIP load balancing\n set wipHost [LB::server addr]\n}\n\nwhen LB_FAILED {\n set wipHost [LB::server addr]\n}"
}
]

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,271 @@
# -*- 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 <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import sys
if sys.version_info < (2, 7):
from nose.plugins.skip import SkipTest
raise SkipTest("F5 Ansible modules require Python >= 2.7")
import os
import json
from ansible.compat.tests import unittest
from ansible.compat.tests.mock import patch, mock_open, Mock
from ansible.module_utils import basic
from ansible.module_utils._text import to_bytes
from ansible.module_utils.f5_utils import AnsibleF5Client
from ansible.module_utils.six import PY3
try:
from library.bigip_irule import Parameters
from library.bigip_irule import ModuleManager
from library.bigip_irule import ArgumentSpec
from library.bigip_irule import GtmManager
from library.bigip_irule import LtmManager
except ImportError:
from ansible.modules.network.f5.bigip_irule import Parameters
from ansible.modules.network.f5.bigip_irule import ModuleManager
from ansible.modules.network.f5.bigip_irule import ArgumentSpec
from ansible.modules.network.f5.bigip_irule import GtmManager
from ansible.modules.network.f5.bigip_irule import LtmManager
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
fixture_data = {}
def set_module_args(args):
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
basic._ANSIBLE_ARGS = to_bytes(args)
def load_fixture(name):
path = os.path.join(fixture_path, name)
if path in fixture_data:
return fixture_data[path]
with open(path) as f:
data = f.read()
try:
data = json.loads(data)
except Exception:
pass
fixture_data[path] = data
return data
class BigIpObj(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
class TestParameters(unittest.TestCase):
def test_module_parameters_ltm(self):
content = load_fixture('create_ltm_irule.tcl')
args = dict(
content=content,
module='ltm',
name='foo',
state='present'
)
p = Parameters(args)
assert p.content == content.strip()
def test_module_parameters_gtm(self):
content = load_fixture('create_gtm_irule.tcl')
args = dict(
content=content,
module='gtm',
name='foo',
state='present'
)
p = Parameters(args)
assert p.content == content.strip()
def test_api_parameters_ltm(self):
content = load_fixture('create_ltm_irule.tcl')
args = dict(
apiAnonymous=content
)
p = Parameters(args)
assert p.content == content.strip()
def test_return_api_params(self):
content = load_fixture('create_ltm_irule.tcl')
args = dict(
content=content,
module='ltm',
name='foo',
state='present'
)
p = Parameters(args)
params = p.api_params()
assert 'apiAnonymous' in params
@patch('ansible.module_utils.f5_utils.AnsibleF5Client._get_mgmt_root',
return_value=True)
class TestManager(unittest.TestCase):
def setUp(self):
self.spec = ArgumentSpec()
self.ltm_irules = []
self.gtm_irules = []
members = load_fixture('load_ltm_irules.json')
for item in members:
self.ltm_irules.append(BigIpObj(**item))
members = load_fixture('load_gtm_irules.json')
for item in members:
self.gtm_irules.append(BigIpObj(**item))
def test_create_ltm_irule(self, *args):
set_module_args(dict(
name='foo',
module='ltm',
content='this is my content',
partition='Common',
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,
mutually_exclusive=self.spec.mutually_exclusive,
)
# Override methods in the specific type of manager
tm = LtmManager(client)
tm.exists = Mock(side_effect=[False, True])
tm.create_on_device = Mock(return_value=True)
# Override methods to force specific logic in the module to happen
mm = ModuleManager(client)
mm.get_manager = Mock(return_value=tm)
results = mm.exec_module()
assert results['changed'] is True
assert results['content'] == 'this is my content'
def test_create_gtm_irule(self, *args):
set_module_args(dict(
name='foo',
module='gtm',
content='this is my content',
partition='Common',
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,
mutually_exclusive=self.spec.mutually_exclusive,
)
# Override methods in the specific type of manager
tm = GtmManager(client)
tm.exists = Mock(side_effect=[False, True])
tm.create_on_device = Mock(return_value=True)
# Override methods to force specific logic in the module to happen
mm = ModuleManager(client)
mm.get_manager = Mock(return_value=tm)
results = mm.exec_module()
assert results['changed'] is True
assert results['content'] == 'this is my content'
def test_create_gtm_irule_src(self, *args):
set_module_args(dict(
name='foo',
module='gtm',
src='/path/to/irules/foo.tcl',
partition='Common',
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,
mutually_exclusive=self.spec.mutually_exclusive,
)
if PY3:
builtins_name = 'builtins'
else:
builtins_name = '__builtin__'
with patch(builtins_name + '.open', mock_open(read_data='this is my content'), create=True):
# Override methods in the specific type of manager
tm = GtmManager(client)
tm.exists = Mock(side_effect=[False, True])
tm.create_on_device = Mock(return_value=True)
# Override methods to force specific logic in the module to happen
mm = ModuleManager(client)
mm.get_manager = Mock(return_value=tm)
results = mm.exec_module()
assert results['changed'] is True
assert results['content'] == 'this is my content'
assert results['module'] == 'gtm'
assert results['src'] == '/path/to/irules/foo.tcl'
assert len(results.keys()) == 4
def test_module_mutual_exclusion(self, *args):
set_module_args(dict(
content='foo',
module='ltm',
name='foo',
state='present',
src='/path/to/irules/foo.tcl',
partition='Common',
server='localhost',
password='password',
user='admin'
))
with patch('ansible.module_utils.basic.AnsibleModule.fail_json', unsafe=True) as mo:
AnsibleF5Client(
argument_spec=self.spec.argument_spec,
supports_check_mode=self.spec.supports_check_mode,
f5_product_name=self.spec.f5_product_name,
mutually_exclusive=self.spec.mutually_exclusive,
)
mo.assert_called_once()