2016-08-24 03:51:13 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2018-11-11 04:09:05 +00:00
|
|
|
# Copyright: (c) 2017, F5 Networks Inc.
|
2017-10-04 19:16:17 +00:00
|
|
|
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2016-08-24 03:51:13 +00:00
|
|
|
|
2017-11-02 18:38:39 +00:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2018-06-20 19:16:43 +00:00
|
|
|
'status': ['stableinterface'],
|
2018-10-12 15:57:28 +00:00
|
|
|
'supported_by': 'certified'}
|
2016-12-06 10:35:25 +00:00
|
|
|
|
2017-11-02 18:38:39 +00:00
|
|
|
DOCUMENTATION = r'''
|
2016-08-24 03:51:13 +00:00
|
|
|
---
|
|
|
|
module: bigip_irule
|
2017-11-02 18:38:39 +00:00
|
|
|
short_description: Manage iRules across different modules on a BIG-IP
|
2016-08-24 03:51:13 +00:00
|
|
|
description:
|
|
|
|
- Manage iRules across different modules on a BIG-IP.
|
2018-04-30 16:48:41 +00:00
|
|
|
version_added: 2.2
|
2016-08-24 03:51:13 +00:00
|
|
|
options:
|
|
|
|
content:
|
|
|
|
description:
|
|
|
|
- When used instead of 'src', sets the contents of an iRule directly to
|
|
|
|
the specified value. This is for simple values, but can be used with
|
|
|
|
lookup plugins for anything complex or with formatting. Either one
|
|
|
|
of C(src) or C(content) must be provided.
|
|
|
|
module:
|
|
|
|
description:
|
|
|
|
- The BIG-IP module to add the iRule to.
|
2017-06-14 18:03:17 +00:00
|
|
|
required: True
|
2016-08-24 03:51:13 +00:00
|
|
|
choices:
|
|
|
|
- ltm
|
|
|
|
- gtm
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The name of the iRule.
|
2017-06-14 18:03:17 +00:00
|
|
|
required: True
|
2016-08-24 03:51:13 +00:00
|
|
|
src:
|
|
|
|
description:
|
|
|
|
- The iRule file to interpret and upload to the BIG-IP. Either one
|
|
|
|
of C(src) or C(content) must be provided.
|
2017-06-14 18:03:17 +00:00
|
|
|
required: True
|
2016-08-24 03:51:13 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Whether the iRule should exist or not.
|
|
|
|
default: present
|
|
|
|
choices:
|
|
|
|
- present
|
|
|
|
- absent
|
2017-11-02 18:38:39 +00:00
|
|
|
partition:
|
|
|
|
description:
|
|
|
|
- Device partition to manage resources on.
|
|
|
|
default: Common
|
|
|
|
version_added: 2.5
|
2016-08-24 03:51:13 +00:00
|
|
|
extends_documentation_fragment: f5
|
|
|
|
author:
|
|
|
|
- Tim Rupp (@caphrim007)
|
2018-11-11 04:09:05 +00:00
|
|
|
- Wojciech Wypior (@wojtek0806)
|
2016-08-24 03:51:13 +00:00
|
|
|
'''
|
|
|
|
|
2017-11-02 18:38:39 +00:00
|
|
|
EXAMPLES = r'''
|
2017-06-14 18:03:17 +00:00
|
|
|
- name: Add the iRule contained in template irule.tcl to the LTM module
|
2016-08-24 03:51:13 +00:00
|
|
|
bigip_irule:
|
2017-11-02 18:38:39 +00:00
|
|
|
content: "{{ lookup('template', 'irule.tcl') }}"
|
|
|
|
module: ltm
|
|
|
|
name: MyiRule
|
|
|
|
state: present
|
2018-12-04 19:15:49 +00:00
|
|
|
provider:
|
|
|
|
user: admin
|
|
|
|
password: secret
|
|
|
|
server: lb.mydomain.com
|
2016-08-24 03:51:13 +00:00
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
- name: Add the iRule contained in static file irule.tcl to the LTM module
|
|
|
|
bigip_irule:
|
2017-11-02 18:38:39 +00:00
|
|
|
module: ltm
|
|
|
|
name: MyiRule
|
|
|
|
src: irule.tcl
|
|
|
|
state: present
|
2018-12-04 19:15:49 +00:00
|
|
|
provider:
|
|
|
|
user: admin
|
|
|
|
password: secret
|
|
|
|
server: lb.mydomain.com
|
2016-08-24 03:51:13 +00:00
|
|
|
delegate_to: localhost
|
|
|
|
'''
|
|
|
|
|
2017-11-02 18:38:39 +00:00
|
|
|
RETURN = r'''
|
2016-08-29 16:29:47 +00:00
|
|
|
module:
|
2017-11-02 18:38:39 +00:00
|
|
|
description: The module that the iRule was added to
|
|
|
|
returned: changed and success
|
2018-12-18 21:25:30 +00:00
|
|
|
type: str
|
2017-11-02 18:38:39 +00:00
|
|
|
sample: gtm
|
2016-08-29 16:29:47 +00:00
|
|
|
src:
|
2017-11-02 18:38:39 +00:00
|
|
|
description: The filename that included the iRule source
|
|
|
|
returned: changed and success, when provided
|
2018-12-18 21:25:30 +00:00
|
|
|
type: str
|
2017-11-02 18:38:39 +00:00
|
|
|
sample: /opt/src/irules/example1.tcl
|
2016-08-29 16:29:47 +00:00
|
|
|
content:
|
2017-11-02 18:38:39 +00:00
|
|
|
description: The content of the iRule that was managed
|
|
|
|
returned: changed and success
|
2018-12-18 21:25:30 +00:00
|
|
|
type: str
|
2017-11-02 18:38:39 +00:00
|
|
|
sample: "when LB_FAILED { set wipHost [LB::server addr] }"
|
2016-08-24 03:51:13 +00:00
|
|
|
'''
|
|
|
|
|
2017-11-02 18:38:39 +00:00
|
|
|
import os
|
|
|
|
|
2018-01-12 21:23:19 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.basic import env_fallback
|
|
|
|
|
2017-11-02 18:38:39 +00:00
|
|
|
try:
|
2018-11-11 04:09:05 +00:00
|
|
|
from library.module_utils.network.f5.bigip import F5RestClient
|
2018-01-12 21:23:19 +00:00
|
|
|
from library.module_utils.network.f5.common import F5ModuleError
|
|
|
|
from library.module_utils.network.f5.common import AnsibleF5Parameters
|
|
|
|
from library.module_utils.network.f5.common import cleanup_tokens
|
|
|
|
from library.module_utils.network.f5.common import f5_argument_spec
|
2018-11-11 04:09:05 +00:00
|
|
|
from library.module_utils.network.f5.common import transform_name
|
|
|
|
from library.module_utils.network.f5.common import exit_json
|
|
|
|
from library.module_utils.network.f5.common import fail_json
|
2017-11-02 18:38:39 +00:00
|
|
|
except ImportError:
|
2018-11-11 04:09:05 +00:00
|
|
|
from ansible.module_utils.network.f5.bigip import F5RestClient
|
2018-01-12 21:23:19 +00:00
|
|
|
from ansible.module_utils.network.f5.common import F5ModuleError
|
|
|
|
from ansible.module_utils.network.f5.common import AnsibleF5Parameters
|
|
|
|
from ansible.module_utils.network.f5.common import cleanup_tokens
|
|
|
|
from ansible.module_utils.network.f5.common import f5_argument_spec
|
2018-11-11 04:09:05 +00:00
|
|
|
from ansible.module_utils.network.f5.common import transform_name
|
|
|
|
from ansible.module_utils.network.f5.common import exit_json
|
|
|
|
from ansible.module_utils.network.f5.common import fail_json
|
2016-08-24 03:51:13 +00:00
|
|
|
|
|
|
|
|
2017-06-14 18:03:17 +00:00
|
|
|
class Parameters(AnsibleF5Parameters):
|
|
|
|
api_map = {
|
2018-11-11 04:09:05 +00:00
|
|
|
'apiAnonymous': 'content',
|
2017-06-14 18:03:17 +00:00
|
|
|
}
|
2016-08-24 03:51:13 +00:00
|
|
|
|
2017-06-14 18:03:17 +00:00
|
|
|
updatables = [
|
2018-11-11 04:09:05 +00:00
|
|
|
'content',
|
2017-06-14 18:03:17 +00:00
|
|
|
]
|
2016-08-24 03:51:13 +00:00
|
|
|
|
2017-06-14 18:03:17 +00:00
|
|
|
api_attributes = [
|
2018-11-11 04:09:05 +00:00
|
|
|
'apiAnonymous',
|
2017-06-14 18:03:17 +00:00
|
|
|
]
|
2016-08-24 03:51:13 +00:00
|
|
|
|
2017-06-14 18:03:17 +00:00
|
|
|
returnables = [
|
2018-11-11 04:09:05 +00:00
|
|
|
'content', 'src', 'module',
|
2017-06-14 18:03:17 +00:00
|
|
|
]
|
2016-08-24 03:51:13 +00:00
|
|
|
|
|
|
|
|
2018-11-11 04:09:05 +00:00
|
|
|
class ApiParameters(Parameters):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ModuleParameters(Parameters):
|
2017-06-14 18:03:17 +00:00
|
|
|
@property
|
|
|
|
def content(self):
|
|
|
|
if self._values['content'] is None:
|
2017-11-02 18:38:39 +00:00
|
|
|
result = self.src_content
|
|
|
|
else:
|
|
|
|
result = self._values['content']
|
|
|
|
|
|
|
|
return str(result).strip()
|
2017-06-14 18:03:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def src(self):
|
|
|
|
if self._values['src'] is None:
|
|
|
|
return None
|
|
|
|
return self._values['src']
|
|
|
|
|
2017-11-02 18:38:39 +00:00
|
|
|
@property
|
|
|
|
def src_content(self):
|
|
|
|
if not os.path.exists(self._values['src']):
|
|
|
|
raise F5ModuleError(
|
|
|
|
"The specified 'src' was not found."
|
|
|
|
)
|
|
|
|
with open(self._values['src']) as f:
|
|
|
|
result = f.read()
|
|
|
|
return result
|
2017-06-14 18:03:17 +00:00
|
|
|
|
|
|
|
|
2018-11-11 04:09:05 +00:00
|
|
|
class Changes(Parameters):
|
|
|
|
def to_return(self):
|
|
|
|
result = {}
|
|
|
|
for returnable in self.returnables:
|
|
|
|
result[returnable] = getattr(self, returnable)
|
|
|
|
result = self._filter_params(result)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
class UsableChanges(Changes):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ReportableChanges(Changes):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Difference(object):
|
|
|
|
def __init__(self, want, have=None):
|
|
|
|
self.want = want
|
|
|
|
self.have = have
|
|
|
|
|
|
|
|
def compare(self, param):
|
|
|
|
try:
|
|
|
|
result = getattr(self, param)
|
|
|
|
return result
|
|
|
|
except AttributeError:
|
|
|
|
return self.__default(param)
|
|
|
|
|
|
|
|
def __default(self, param):
|
|
|
|
attr1 = getattr(self.want, param)
|
|
|
|
try:
|
|
|
|
attr2 = getattr(self.have, param)
|
|
|
|
if attr1 != attr2:
|
|
|
|
return attr1
|
|
|
|
except AttributeError:
|
|
|
|
return attr1
|
|
|
|
|
|
|
|
|
2017-06-14 18:03:17 +00:00
|
|
|
class ModuleManager(object):
|
2018-01-12 21:23:19 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.client = kwargs.get('client', None)
|
|
|
|
self.module = kwargs.get('module', None)
|
|
|
|
self.kwargs = kwargs
|
2017-06-14 18:03:17 +00:00
|
|
|
|
|
|
|
def exec_module(self):
|
2018-01-12 21:23:19 +00:00
|
|
|
if self.module.params['module'] == 'ltm':
|
2017-06-14 18:03:17 +00:00
|
|
|
manager = self.get_manager('ltm')
|
2018-01-12 21:23:19 +00:00
|
|
|
elif self.module.params['module'] == 'gtm':
|
2017-06-14 18:03:17 +00:00
|
|
|
manager = self.get_manager('gtm')
|
|
|
|
else:
|
|
|
|
raise F5ModuleError(
|
|
|
|
"An unknown iRule module type was specified"
|
|
|
|
)
|
|
|
|
return manager.exec_module()
|
|
|
|
|
|
|
|
def get_manager(self, type):
|
|
|
|
if type == 'ltm':
|
2018-01-12 21:23:19 +00:00
|
|
|
return LtmManager(**self.kwargs)
|
2017-06-14 18:03:17 +00:00
|
|
|
elif type == 'gtm':
|
2018-01-12 21:23:19 +00:00
|
|
|
return GtmManager(**self.kwargs)
|
2017-06-14 18:03:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BaseManager(object):
|
2018-01-12 21:23:19 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.module = kwargs.get('module', None)
|
|
|
|
self.client = kwargs.get('client', None)
|
2018-11-11 04:09:05 +00:00
|
|
|
self.have = None
|
|
|
|
self.want = ModuleParameters(params=self.module.params)
|
|
|
|
self.changes = UsableChanges()
|
2016-08-24 03:51:13 +00:00
|
|
|
|
2017-06-14 18:03:17 +00:00
|
|
|
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:
|
2018-11-11 04:09:05 +00:00
|
|
|
self.changes = UsableChanges(params=changed)
|
2017-06-14 18:03:17 +00:00
|
|
|
|
|
|
|
def _update_changed_options(self):
|
2018-11-11 04:09:05 +00:00
|
|
|
diff = Difference(self.want, self.have)
|
|
|
|
updatables = Parameters.updatables
|
|
|
|
changed = dict()
|
|
|
|
for k in updatables:
|
|
|
|
change = diff.compare(k)
|
|
|
|
if change is None:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
if isinstance(change, dict):
|
|
|
|
changed.update(change)
|
|
|
|
else:
|
|
|
|
changed[k] = change
|
2017-06-14 18:03:17 +00:00
|
|
|
if changed:
|
2018-11-11 04:09:05 +00:00
|
|
|
self.changes = UsableChanges(params=changed)
|
2016-08-24 03:51:13 +00:00
|
|
|
return True
|
2017-06-14 18:03:17 +00:00
|
|
|
return False
|
2016-08-24 03:51:13 +00:00
|
|
|
|
2018-11-11 04:09:05 +00:00
|
|
|
def _announce_deprecations(self, result):
|
|
|
|
warnings = result.pop('__warnings', [])
|
|
|
|
for warning in warnings:
|
|
|
|
self.module.deprecate(
|
|
|
|
msg=warning['msg'],
|
|
|
|
version=warning['version']
|
|
|
|
)
|
|
|
|
|
|
|
|
def exec_module(self):
|
|
|
|
changed = False
|
|
|
|
result = dict()
|
|
|
|
state = self.want.state
|
|
|
|
|
|
|
|
if state in ["present"]:
|
|
|
|
changed = self.present()
|
|
|
|
elif state == "absent":
|
|
|
|
changed = self.absent()
|
|
|
|
|
|
|
|
reportable = ReportableChanges(params=self.changes.to_return())
|
|
|
|
changes = reportable.to_return()
|
|
|
|
result.update(**changes)
|
|
|
|
result.update(dict(changed=changed))
|
|
|
|
self._announce_deprecations(result)
|
|
|
|
return result
|
|
|
|
|
2016-08-24 03:51:13 +00:00
|
|
|
def present(self):
|
2017-06-14 18:03:17 +00:00
|
|
|
if not self.want.content and not self.want.src:
|
|
|
|
raise F5ModuleError(
|
|
|
|
"Either 'content' or 'src' must be provided"
|
|
|
|
)
|
2016-08-24 03:51:13 +00:00
|
|
|
if self.exists():
|
2016-10-25 11:50:42 +00:00
|
|
|
return self.update()
|
2016-08-24 03:51:13 +00:00
|
|
|
else:
|
2016-10-25 11:50:42 +00:00
|
|
|
return self.create()
|
2016-08-24 03:51:13 +00:00
|
|
|
|
|
|
|
def create(self):
|
2017-06-14 18:03:17 +00:00
|
|
|
self._set_changed_options()
|
2018-01-12 21:23:19 +00:00
|
|
|
if self.module.check_mode:
|
2016-08-24 03:51:13 +00:00
|
|
|
return True
|
2017-06-14 18:03:17 +00:00
|
|
|
self.create_on_device()
|
|
|
|
if not self.exists():
|
|
|
|
raise F5ModuleError("Failed to create the iRule")
|
|
|
|
return True
|
2016-08-24 03:51:13 +00:00
|
|
|
|
2017-06-14 18:03:17 +00:00
|
|
|
def should_update(self):
|
|
|
|
result = self._update_changed_options()
|
|
|
|
if result:
|
2016-08-24 03:51:13 +00:00
|
|
|
return True
|
2017-06-14 18:03:17 +00:00
|
|
|
return False
|
2016-08-24 03:51:13 +00:00
|
|
|
|
2017-06-14 18:03:17 +00:00
|
|
|
def update(self):
|
|
|
|
self.have = self.read_current_from_device()
|
|
|
|
if not self.should_update():
|
|
|
|
return False
|
2018-01-12 21:23:19 +00:00
|
|
|
if self.module.check_mode:
|
2017-06-14 18:03:17 +00:00
|
|
|
return True
|
|
|
|
self.update_on_device()
|
2016-08-24 03:51:13 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def absent(self):
|
2017-06-14 18:03:17 +00:00
|
|
|
if self.exists():
|
|
|
|
return self.remove()
|
|
|
|
return False
|
2016-08-24 03:51:13 +00:00
|
|
|
|
2017-06-14 18:03:17 +00:00
|
|
|
def remove(self):
|
2018-01-12 21:23:19 +00:00
|
|
|
if self.module.check_mode:
|
2017-06-14 18:03:17 +00:00
|
|
|
return True
|
|
|
|
self.remove_from_device()
|
2016-08-24 03:51:13 +00:00
|
|
|
if self.exists():
|
2017-06-14 18:03:17 +00:00
|
|
|
raise F5ModuleError("Failed to delete the iRule")
|
|
|
|
return True
|
2016-08-24 03:51:13 +00:00
|
|
|
|
|
|
|
|
2017-06-14 18:03:17 +00:00
|
|
|
class LtmManager(BaseManager):
|
|
|
|
def exists(self):
|
2018-11-11 04:09:05 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/ltm/rule/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, self.want.name)
|
2017-06-14 18:03:17 +00:00
|
|
|
)
|
2018-11-11 04:09:05 +00:00
|
|
|
resp = self.client.api.get(uri)
|
|
|
|
try:
|
|
|
|
response = resp.json()
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
if resp.status == 404 or 'code' in response and response['code'] == 404:
|
|
|
|
return False
|
|
|
|
return True
|
2016-08-24 03:51:13 +00:00
|
|
|
|
2017-06-14 18:03:17 +00:00
|
|
|
def update_on_device(self):
|
2017-11-02 18:38:39 +00:00
|
|
|
params = self.changes.api_params()
|
2018-11-11 04:09:05 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/ltm/rule/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, self.want.name)
|
2017-06-14 18:03:17 +00:00
|
|
|
)
|
2018-11-11 04:09:05 +00:00
|
|
|
resp = self.client.api.patch(uri, json=params)
|
|
|
|
try:
|
|
|
|
response = resp.json()
|
|
|
|
except ValueError as ex:
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] == 400:
|
|
|
|
if 'message' in response:
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
else:
|
|
|
|
raise F5ModuleError(resp.content)
|
2017-06-14 18:03:17 +00:00
|
|
|
|
|
|
|
def create_on_device(self):
|
2018-11-11 04:09:05 +00:00
|
|
|
params = self.changes.api_params()
|
|
|
|
params['name'] = self.want.name
|
|
|
|
params['partition'] = self.want.partition
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/ltm/rule/".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
2017-06-14 18:03:17 +00:00
|
|
|
)
|
2018-11-11 04:09:05 +00:00
|
|
|
resp = self.client.api.post(uri, json=params)
|
|
|
|
try:
|
|
|
|
response = resp.json()
|
|
|
|
except ValueError as ex:
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] in [400, 403]:
|
|
|
|
if 'message' in response:
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
else:
|
|
|
|
raise F5ModuleError(resp.content)
|
|
|
|
return response['selfLink']
|
2017-06-14 18:03:17 +00:00
|
|
|
|
|
|
|
def read_current_from_device(self):
|
2018-11-11 04:09:05 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/ltm/rule/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, self.want.name)
|
2017-06-14 18:03:17 +00:00
|
|
|
)
|
|
|
|
|
2018-11-11 04:09:05 +00:00
|
|
|
resp = self.client.api.get(uri)
|
2017-06-14 18:03:17 +00:00
|
|
|
|
2018-11-11 04:09:05 +00:00
|
|
|
try:
|
|
|
|
response = resp.json()
|
|
|
|
except ValueError as ex:
|
|
|
|
raise F5ModuleError(str(ex))
|
2017-06-14 18:03:17 +00:00
|
|
|
|
2018-11-11 04:09:05 +00:00
|
|
|
if 'code' in response and response['code'] == 400:
|
|
|
|
if 'message' in response:
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
else:
|
|
|
|
raise F5ModuleError(resp.content)
|
|
|
|
return ApiParameters(params=response)
|
2017-06-14 18:03:17 +00:00
|
|
|
|
|
|
|
def remove_from_device(self):
|
2018-11-11 04:09:05 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/ltm/rule/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, self.want.name)
|
2017-06-14 18:03:17 +00:00
|
|
|
)
|
2018-11-11 04:09:05 +00:00
|
|
|
response = self.client.api.delete(uri)
|
|
|
|
if response.status == 200:
|
|
|
|
return True
|
|
|
|
raise F5ModuleError(response.content)
|
2016-08-24 03:51:13 +00:00
|
|
|
|
2018-11-11 04:09:05 +00:00
|
|
|
|
|
|
|
class GtmManager(BaseManager):
|
2017-06-14 18:03:17 +00:00
|
|
|
def exists(self):
|
2018-11-11 04:09:05 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/gtm/rule/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, self.want.name)
|
2017-06-14 18:03:17 +00:00
|
|
|
)
|
2018-11-11 04:09:05 +00:00
|
|
|
resp = self.client.api.get(uri)
|
|
|
|
try:
|
|
|
|
response = resp.json()
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
if resp.status == 404 or 'code' in response and response['code'] == 404:
|
|
|
|
return False
|
|
|
|
return True
|
2016-08-24 03:51:13 +00:00
|
|
|
|
2017-06-14 18:03:17 +00:00
|
|
|
def update_on_device(self):
|
2017-11-02 18:38:39 +00:00
|
|
|
params = self.changes.api_params()
|
2018-11-11 04:09:05 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/gtm/rule/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, self.want.name)
|
2017-06-14 18:03:17 +00:00
|
|
|
)
|
2018-11-11 04:09:05 +00:00
|
|
|
resp = self.client.api.patch(uri, json=params)
|
|
|
|
try:
|
|
|
|
response = resp.json()
|
|
|
|
except ValueError as ex:
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] == 400:
|
|
|
|
if 'message' in response:
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
else:
|
|
|
|
raise F5ModuleError(resp.content)
|
2017-06-14 18:03:17 +00:00
|
|
|
|
|
|
|
def create_on_device(self):
|
2018-11-11 04:09:05 +00:00
|
|
|
params = self.changes.api_params()
|
|
|
|
params['name'] = self.want.name
|
|
|
|
params['partition'] = self.want.partition
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/gtm/rule/".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
)
|
|
|
|
resp = self.client.api.post(uri, json=params)
|
|
|
|
try:
|
|
|
|
response = resp.json()
|
|
|
|
except ValueError as ex:
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] in [400, 403]:
|
|
|
|
if 'message' in response:
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
else:
|
|
|
|
raise F5ModuleError(resp.content)
|
|
|
|
return response['selfLink']
|
|
|
|
|
|
|
|
def read_current_from_device(self):
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/gtm/rule/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, self.want.name)
|
2017-06-14 18:03:17 +00:00
|
|
|
)
|
|
|
|
|
2018-11-11 04:09:05 +00:00
|
|
|
resp = self.client.api.get(uri)
|
|
|
|
|
|
|
|
try:
|
|
|
|
response = resp.json()
|
|
|
|
except ValueError as ex:
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] == 400:
|
|
|
|
if 'message' in response:
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
else:
|
|
|
|
raise F5ModuleError(resp.content)
|
|
|
|
return ApiParameters(params=response)
|
|
|
|
|
|
|
|
def remove_from_device(self):
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/gtm/rule/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, self.want.name)
|
|
|
|
)
|
|
|
|
response = self.client.api.delete(uri)
|
|
|
|
if response.status == 200:
|
|
|
|
return True
|
|
|
|
raise F5ModuleError(response.content)
|
|
|
|
|
2017-06-14 18:03:17 +00:00
|
|
|
|
|
|
|
class ArgumentSpec(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.supports_check_mode = True
|
2018-01-12 21:23:19 +00:00
|
|
|
argument_spec = dict(
|
2018-12-04 19:15:49 +00:00
|
|
|
content=dict(),
|
2017-06-14 18:03:17 +00:00
|
|
|
src=dict(
|
2018-12-04 19:15:49 +00:00
|
|
|
type='path',
|
2017-06-14 18:03:17 +00:00
|
|
|
),
|
|
|
|
name=dict(required=True),
|
|
|
|
module=dict(
|
|
|
|
required=True,
|
|
|
|
choices=['gtm', 'ltm']
|
2018-01-12 21:23:19 +00:00
|
|
|
),
|
|
|
|
state=dict(
|
|
|
|
default='present',
|
|
|
|
choices=['present', 'absent']
|
|
|
|
),
|
|
|
|
partition=dict(
|
|
|
|
default='Common',
|
|
|
|
fallback=(env_fallback, ['F5_PARTITION'])
|
2017-06-14 18:03:17 +00:00
|
|
|
)
|
|
|
|
)
|
2018-01-12 21:23:19 +00:00
|
|
|
self.argument_spec = {}
|
|
|
|
self.argument_spec.update(f5_argument_spec)
|
|
|
|
self.argument_spec.update(argument_spec)
|
2017-06-14 18:03:17 +00:00
|
|
|
self.mutually_exclusive = [
|
2016-08-24 03:51:13 +00:00
|
|
|
['content', 'src']
|
|
|
|
]
|
2017-12-22 19:40:20 +00:00
|
|
|
|
2017-06-14 18:03:17 +00:00
|
|
|
|
2017-12-22 19:40:20 +00:00
|
|
|
def main():
|
2017-06-14 18:03:17 +00:00
|
|
|
spec = ArgumentSpec()
|
|
|
|
|
2018-01-12 21:23:19 +00:00
|
|
|
module = AnsibleModule(
|
2017-06-14 18:03:17 +00:00
|
|
|
argument_spec=spec.argument_spec,
|
|
|
|
supports_check_mode=spec.supports_check_mode,
|
2018-01-12 21:23:19 +00:00
|
|
|
mutually_exclusive=spec.mutually_exclusive
|
2016-08-24 03:51:13 +00:00
|
|
|
)
|
2018-11-11 04:09:05 +00:00
|
|
|
|
|
|
|
client = F5RestClient(**module.params)
|
2016-08-24 03:51:13 +00:00
|
|
|
|
|
|
|
try:
|
2018-01-12 21:23:19 +00:00
|
|
|
mm = ModuleManager(module=module, client=client)
|
2017-06-14 18:03:17 +00:00
|
|
|
results = mm.exec_module()
|
2017-12-22 19:40:20 +00:00
|
|
|
cleanup_tokens(client)
|
2018-11-11 04:09:05 +00:00
|
|
|
exit_json(module, results, client)
|
|
|
|
except F5ModuleError as ex:
|
2017-12-22 19:40:20 +00:00
|
|
|
cleanup_tokens(client)
|
2018-11-11 04:09:05 +00:00
|
|
|
fail_json(module, ex, client)
|
2016-08-24 03:51:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|