2017-05-24 16:57:30 +00:00
|
|
|
#!/usr/bin/python
|
2017-10-04 19:16:17 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-05-24 16:57:30 +00:00
|
|
|
#
|
2018-11-03 05:20:39 +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)
|
2017-05-24 16:57:30 +00:00
|
|
|
|
2017-11-01 22:20:28 +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'}
|
2017-05-24 16:57:30 +00:00
|
|
|
|
2017-11-01 22:20:28 +00:00
|
|
|
DOCUMENTATION = r'''
|
2018-11-03 05:20:39 +00:00
|
|
|
---
|
2017-05-24 16:57:30 +00:00
|
|
|
module: bigip_snmp_trap
|
2017-11-01 22:20:28 +00:00
|
|
|
short_description: Manipulate SNMP trap information on a BIG-IP
|
2017-05-24 16:57:30 +00:00
|
|
|
description:
|
|
|
|
- Manipulate SNMP trap information on a BIG-IP.
|
|
|
|
version_added: 2.4
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name of the SNMP configuration endpoint.
|
|
|
|
required: True
|
|
|
|
snmp_version:
|
|
|
|
description:
|
|
|
|
- Specifies to which Simple Network Management Protocol (SNMP) version
|
|
|
|
the trap destination applies.
|
2018-04-24 05:06:33 +00:00
|
|
|
choices: ['1', '2c']
|
2017-05-24 16:57:30 +00:00
|
|
|
community:
|
|
|
|
description:
|
|
|
|
- Specifies the community name for the trap destination.
|
|
|
|
destination:
|
|
|
|
description:
|
|
|
|
- Specifies the address for the trap destination. This can be either an
|
|
|
|
IP address or a hostname.
|
|
|
|
port:
|
|
|
|
description:
|
|
|
|
- Specifies the port for the trap destination.
|
|
|
|
network:
|
|
|
|
description:
|
|
|
|
- Specifies the name of the trap network. This option is not supported in
|
|
|
|
versions of BIG-IP < 12.1.0. If used on versions < 12.1.0, it will simply
|
|
|
|
be ignored.
|
2018-04-24 05:06:33 +00:00
|
|
|
- The value C(default) was removed in BIG-IP version 13.1.0. Specifying this
|
|
|
|
value when configuring a BIG-IP will cause the module to stop and report
|
|
|
|
an error. The usual remedy is to choose one of the other options, such as
|
|
|
|
C(management).
|
2017-05-24 16:57:30 +00:00
|
|
|
choices:
|
|
|
|
- other
|
|
|
|
- management
|
|
|
|
- default
|
|
|
|
state:
|
|
|
|
description:
|
2018-04-24 05:06:33 +00:00
|
|
|
- When C(present), ensures that the resource exists.
|
|
|
|
- When C(absent), ensures that the resource does not exist.
|
2017-05-24 16:57:30 +00:00
|
|
|
default: present
|
|
|
|
choices:
|
|
|
|
- present
|
|
|
|
- absent
|
2018-01-13 05:49:12 +00:00
|
|
|
partition:
|
|
|
|
description:
|
|
|
|
- Device partition to manage resources on.
|
|
|
|
default: Common
|
|
|
|
version_added: 2.5
|
2017-05-24 16:57:30 +00:00
|
|
|
notes:
|
|
|
|
- This module only supports version v1 and v2c of SNMP.
|
|
|
|
- The C(network) option is not supported on versions of BIG-IP < 12.1.0 because
|
|
|
|
the platform did not support that option until 12.1.0. If used on versions
|
|
|
|
< 12.1.0, it will simply be ignored.
|
|
|
|
extends_documentation_fragment: f5
|
|
|
|
author:
|
|
|
|
- Tim Rupp (@caphrim007)
|
2018-11-03 05:20:39 +00:00
|
|
|
- Wojciech Wypior (@wojtek0806)
|
2017-05-24 16:57:30 +00:00
|
|
|
'''
|
|
|
|
|
2017-11-01 22:20:28 +00:00
|
|
|
EXAMPLES = r'''
|
2017-05-24 16:57:30 +00:00
|
|
|
- name: Create snmp v1 trap
|
|
|
|
bigip_snmp_trap:
|
2017-11-01 22:20:28 +00:00
|
|
|
community: general
|
|
|
|
destination: 1.2.3.4
|
|
|
|
name: my-trap1
|
|
|
|
network: management
|
|
|
|
port: 9000
|
|
|
|
snmp_version: 1
|
2018-12-04 18:31:25 +00:00
|
|
|
provider:
|
|
|
|
server: lb.mydomain.com
|
|
|
|
user: admin
|
|
|
|
password: secret
|
2017-05-24 16:57:30 +00:00
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
- name: Create snmp v2 trap
|
|
|
|
bigip_snmp_trap:
|
2017-11-01 22:20:28 +00:00
|
|
|
community: general
|
|
|
|
destination: 5.6.7.8
|
|
|
|
name: my-trap2
|
|
|
|
network: default
|
|
|
|
port: 7000
|
|
|
|
snmp_version: 2c
|
2018-12-04 18:31:25 +00:00
|
|
|
provider:
|
|
|
|
server: lb.mydomain.com
|
|
|
|
user: admin
|
|
|
|
password: secret
|
2017-05-24 16:57:30 +00:00
|
|
|
delegate_to: localhost
|
|
|
|
'''
|
|
|
|
|
2017-11-01 22:20:28 +00:00
|
|
|
RETURN = r'''
|
2017-05-24 16:57:30 +00:00
|
|
|
snmp_version:
|
2017-11-01 22:20:28 +00:00
|
|
|
description: The new C(snmp_version) configured on the remote device.
|
|
|
|
returned: changed and success
|
|
|
|
type: string
|
|
|
|
sample: 2c
|
2017-05-24 16:57:30 +00:00
|
|
|
community:
|
2017-11-01 22:20:28 +00:00
|
|
|
description: The new C(community) name for the trap destination.
|
|
|
|
returned: changed and success
|
|
|
|
type: list
|
|
|
|
sample: secret
|
2017-05-24 16:57:30 +00:00
|
|
|
destination:
|
2017-11-01 22:20:28 +00:00
|
|
|
description: The new address for the trap destination in either IP or hostname form.
|
|
|
|
returned: changed and success
|
|
|
|
type: string
|
|
|
|
sample: 1.2.3.4
|
2017-05-24 16:57:30 +00:00
|
|
|
port:
|
2017-11-01 22:20:28 +00:00
|
|
|
description: The new C(port) of the trap destination.
|
|
|
|
returned: changed and success
|
|
|
|
type: string
|
|
|
|
sample: 900
|
2017-05-24 16:57:30 +00:00
|
|
|
network:
|
2017-11-01 22:20:28 +00:00
|
|
|
description: The new name of the network the SNMP trap is on.
|
|
|
|
returned: changed and success
|
|
|
|
type: string
|
|
|
|
sample: management
|
2017-05-24 16:57:30 +00:00
|
|
|
'''
|
|
|
|
|
2018-01-13 05:49:12 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.basic import env_fallback
|
2017-05-24 16:57:30 +00:00
|
|
|
from distutils.version import LooseVersion
|
2017-11-01 22:20:28 +00:00
|
|
|
|
|
|
|
try:
|
2018-11-03 05:20:39 +00:00
|
|
|
from library.module_utils.network.f5.bigip import F5RestClient
|
2018-01-13 05:49:12 +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-03 05:20:39 +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
|
|
|
|
from library.module_utils.network.f5.icontrol import tmos_version
|
2017-11-01 22:20:28 +00:00
|
|
|
except ImportError:
|
2018-11-03 05:20:39 +00:00
|
|
|
from ansible.module_utils.network.f5.bigip import F5RestClient
|
2018-01-13 05:49:12 +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-03 05:20:39 +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
|
|
|
|
from ansible.module_utils.network.f5.icontrol import tmos_version
|
2017-05-24 16:57:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Parameters(AnsibleF5Parameters):
|
|
|
|
api_map = {
|
|
|
|
'version': 'snmp_version',
|
|
|
|
'community': 'community',
|
2018-11-03 05:20:39 +00:00
|
|
|
'host': 'destination',
|
2017-05-24 16:57:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def snmp_version(self):
|
|
|
|
if self._values['snmp_version'] is None:
|
|
|
|
return None
|
|
|
|
return str(self._values['snmp_version'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def port(self):
|
|
|
|
if self._values['port'] is None:
|
|
|
|
return None
|
|
|
|
return int(self._values['port'])
|
|
|
|
|
|
|
|
def to_return(self):
|
|
|
|
result = {}
|
|
|
|
for returnable in self.returnables:
|
|
|
|
result[returnable] = getattr(self, returnable)
|
|
|
|
result = self._filter_params(result)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2018-04-24 05:06:33 +00:00
|
|
|
class V3Parameters(Parameters):
|
|
|
|
updatables = [
|
2018-11-03 05:20:39 +00:00
|
|
|
'snmp_version',
|
|
|
|
'community',
|
|
|
|
'destination',
|
|
|
|
'port',
|
|
|
|
'network',
|
2018-04-24 05:06:33 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
returnables = [
|
2018-11-03 05:20:39 +00:00
|
|
|
'snmp_version',
|
|
|
|
'community',
|
|
|
|
'destination',
|
|
|
|
'port',
|
|
|
|
'network',
|
2018-04-24 05:06:33 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
api_attributes = [
|
2018-11-03 05:20:39 +00:00
|
|
|
'version',
|
|
|
|
'community',
|
|
|
|
'host',
|
|
|
|
'port',
|
|
|
|
'network',
|
2018-04-24 05:06:33 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def network(self):
|
|
|
|
if self._values['network'] is None:
|
|
|
|
return None
|
|
|
|
network = str(self._values['network'])
|
|
|
|
if network == 'management':
|
|
|
|
return 'mgmt'
|
|
|
|
elif network == 'default':
|
|
|
|
raise F5ModuleError(
|
|
|
|
"'default' is not a valid option for this version of BIG-IP. "
|
|
|
|
"Use either 'management', 'or 'other' instead."
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return network
|
|
|
|
|
|
|
|
|
|
|
|
class V2Parameters(Parameters):
|
2017-05-24 16:57:30 +00:00
|
|
|
updatables = [
|
2018-11-03 05:20:39 +00:00
|
|
|
'snmp_version',
|
|
|
|
'community',
|
|
|
|
'destination',
|
|
|
|
'port',
|
|
|
|
'network',
|
2017-05-24 16:57:30 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
returnables = [
|
2018-11-03 05:20:39 +00:00
|
|
|
'snmp_version',
|
|
|
|
'community',
|
|
|
|
'destination',
|
|
|
|
'port',
|
|
|
|
'network',
|
2017-05-24 16:57:30 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
api_attributes = [
|
2018-11-03 05:20:39 +00:00
|
|
|
'version',
|
|
|
|
'community',
|
|
|
|
'host',
|
|
|
|
'port',
|
|
|
|
'network',
|
2017-05-24 16:57:30 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def network(self):
|
|
|
|
if self._values['network'] is None:
|
|
|
|
return None
|
|
|
|
network = str(self._values['network'])
|
|
|
|
if network == 'management':
|
|
|
|
return 'mgmt'
|
|
|
|
elif network == 'default':
|
|
|
|
return ''
|
|
|
|
else:
|
|
|
|
return network
|
|
|
|
|
|
|
|
|
2018-04-24 05:06:33 +00:00
|
|
|
class V1Parameters(Parameters):
|
2017-05-24 16:57:30 +00:00
|
|
|
updatables = [
|
2018-11-03 05:20:39 +00:00
|
|
|
'snmp_version',
|
|
|
|
'community',
|
|
|
|
'destination',
|
|
|
|
'port',
|
2017-05-24 16:57:30 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
returnables = [
|
2018-11-03 05:20:39 +00:00
|
|
|
'snmp_version',
|
|
|
|
'community',
|
|
|
|
'destination',
|
|
|
|
'port',
|
2017-05-24 16:57:30 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
api_attributes = [
|
2018-11-03 05:20:39 +00:00
|
|
|
'version',
|
|
|
|
'community',
|
|
|
|
'host',
|
|
|
|
'port',
|
2017-05-24 16:57:30 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def network(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
class ModuleManager(object):
|
2018-01-13 05:49:12 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.module = kwargs.get('module', None)
|
|
|
|
self.client = kwargs.get('client', None)
|
|
|
|
self.kwargs = kwargs
|
2017-05-24 16:57:30 +00:00
|
|
|
|
|
|
|
def exec_module(self):
|
2018-04-24 05:06:33 +00:00
|
|
|
if self.is_version_without_network():
|
|
|
|
manager = V1Manager(**self.kwargs)
|
|
|
|
elif self.is_version_with_default_network():
|
|
|
|
manager = V2Manager(**self.kwargs)
|
2017-05-24 16:57:30 +00:00
|
|
|
else:
|
2018-04-24 05:06:33 +00:00
|
|
|
manager = V3Manager(**self.kwargs)
|
2017-05-24 16:57:30 +00:00
|
|
|
|
|
|
|
return manager.exec_module()
|
|
|
|
|
2018-04-24 05:06:33 +00:00
|
|
|
def is_version_without_network(self):
|
|
|
|
"""Is current BIG-IP version missing "network" value support
|
2017-05-24 16:57:30 +00:00
|
|
|
|
2018-04-24 05:06:33 +00:00
|
|
|
Returns:
|
|
|
|
bool: True when it is missing. False otherwise.
|
2017-05-24 16:57:30 +00:00
|
|
|
"""
|
2018-11-03 05:20:39 +00:00
|
|
|
version = tmos_version(self.client)
|
2017-05-24 16:57:30 +00:00
|
|
|
if LooseVersion(version) < LooseVersion('12.1.0'):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2018-04-24 05:06:33 +00:00
|
|
|
def is_version_with_default_network(self):
|
|
|
|
"""Is current BIG-IP version missing "default" network value support
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True when it is missing. False otherwise.
|
|
|
|
"""
|
2018-11-03 05:20:39 +00:00
|
|
|
version = tmos_version(self.client)
|
2018-04-24 05:06:33 +00:00
|
|
|
if LooseVersion(version) < LooseVersion('13.1.0'):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2017-05-24 16:57:30 +00:00
|
|
|
|
|
|
|
class BaseManager(object):
|
2018-01-13 05:49:12 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.module = kwargs.get('module', None)
|
|
|
|
self.client = kwargs.get('client', None)
|
2017-05-24 16:57:30 +00:00
|
|
|
self.have = None
|
|
|
|
|
|
|
|
def exec_module(self):
|
|
|
|
changed = False
|
|
|
|
result = dict()
|
|
|
|
state = self.want.state
|
|
|
|
|
2018-11-03 05:20:39 +00:00
|
|
|
if state == "present":
|
|
|
|
changed = self.present()
|
|
|
|
elif state == "absent":
|
|
|
|
changed = self.absent()
|
2017-05-24 16:57:30 +00:00
|
|
|
|
|
|
|
changes = self.changes.to_return()
|
|
|
|
result.update(**changes)
|
|
|
|
result.update(dict(changed=changed))
|
|
|
|
return result
|
|
|
|
|
|
|
|
def present(self):
|
|
|
|
if self.exists():
|
|
|
|
return self.update()
|
|
|
|
else:
|
|
|
|
return self.create()
|
|
|
|
|
2018-11-03 05:20:39 +00:00
|
|
|
def absent(self):
|
|
|
|
if self.exists():
|
|
|
|
return self.remove()
|
|
|
|
return False
|
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
if self.module.check_mode:
|
|
|
|
return True
|
|
|
|
self.remove_from_device()
|
|
|
|
if self.exists():
|
|
|
|
raise F5ModuleError("Failed to delete the snmp trap")
|
|
|
|
return True
|
|
|
|
|
2017-05-24 16:57:30 +00:00
|
|
|
def create(self):
|
|
|
|
self._set_changed_options()
|
2018-01-13 05:49:12 +00:00
|
|
|
if self.module.check_mode:
|
2017-05-24 16:57:30 +00:00
|
|
|
return True
|
|
|
|
if all(getattr(self.want, v) is None for v in self.required_resources):
|
|
|
|
raise F5ModuleError(
|
|
|
|
"You must specify at least one of "
|
|
|
|
', '.join(self.required_resources)
|
|
|
|
)
|
|
|
|
self.create_on_device()
|
|
|
|
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
|
2018-01-13 05:49:12 +00:00
|
|
|
if self.module.check_mode:
|
2017-05-24 16:57:30 +00:00
|
|
|
return True
|
|
|
|
self.update_on_device()
|
|
|
|
return True
|
|
|
|
|
2018-11-03 05:20:39 +00:00
|
|
|
def exists(self):
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/traps/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, self.want.name)
|
|
|
|
)
|
|
|
|
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
|
|
|
|
|
2017-05-24 16:57:30 +00:00
|
|
|
def update_on_device(self):
|
|
|
|
params = self.want.api_params()
|
2018-11-03 05:20:39 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/traps/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, self.want.name)
|
2017-05-24 16:57:30 +00:00
|
|
|
)
|
2018-11-03 05:20:39 +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-05-24 16:57:30 +00:00
|
|
|
|
|
|
|
def create_on_device(self):
|
|
|
|
params = self.want.api_params()
|
2018-11-03 05:20:39 +00:00
|
|
|
params['name'] = self.want.name
|
|
|
|
params['partition'] = self.want.partition
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/traps/".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
2017-05-24 16:57:30 +00:00
|
|
|
)
|
2018-11-03 05:20:39 +00:00
|
|
|
resp = self.client.api.post(uri, json=params)
|
|
|
|
try:
|
|
|
|
response = resp.json()
|
|
|
|
except ValueError as ex:
|
|
|
|
raise F5ModuleError(str(ex))
|
2017-05-24 16:57:30 +00:00
|
|
|
|
2018-11-03 05:20:39 +00:00
|
|
|
if 'code' in response and response['code'] in [400, 403]:
|
|
|
|
if 'message' in response:
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
else:
|
|
|
|
raise F5ModuleError(resp.content)
|
2017-05-24 16:57:30 +00:00
|
|
|
|
|
|
|
def remove_from_device(self):
|
2018-11-03 05:20:39 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/traps/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, self.want.name)
|
2017-05-24 16:57:30 +00:00
|
|
|
)
|
2018-11-03 05:20:39 +00:00
|
|
|
resp = self.client.api.delete(uri)
|
|
|
|
if resp.status == 200:
|
|
|
|
return True
|
2017-05-24 16:57:30 +00:00
|
|
|
|
|
|
|
|
2018-04-24 05:06:33 +00:00
|
|
|
class V3Manager(BaseManager):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(V3Manager, self).__init__(**kwargs)
|
|
|
|
self.required_resources = [
|
|
|
|
'version', 'community', 'destination', 'port', 'network'
|
|
|
|
]
|
|
|
|
self.want = V3Parameters(params=self.module.params)
|
|
|
|
self.changes = V3Parameters()
|
|
|
|
|
|
|
|
def _set_changed_options(self):
|
|
|
|
changed = {}
|
|
|
|
for key in V3Parameters.returnables:
|
|
|
|
if getattr(self.want, key) is not None:
|
|
|
|
changed[key] = getattr(self.want, key)
|
|
|
|
if changed:
|
|
|
|
self.changes = V3Parameters(params=changed)
|
|
|
|
|
|
|
|
def _update_changed_options(self):
|
|
|
|
changed = {}
|
|
|
|
for key in V3Parameters.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 = V3Parameters(params=changed)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def read_current_from_device(self):
|
2018-11-03 05:20:39 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/traps/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, self.want.name)
|
2018-04-24 05:06:33 +00:00
|
|
|
)
|
2018-11-03 05:20:39 +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 V3Parameters(params=response)
|
2018-04-24 05:06:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class V2Manager(BaseManager):
|
2018-01-13 05:49:12 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2018-04-24 05:06:33 +00:00
|
|
|
super(V2Manager, self).__init__(**kwargs)
|
2017-05-24 16:57:30 +00:00
|
|
|
self.required_resources = [
|
|
|
|
'version', 'community', 'destination', 'port', 'network'
|
|
|
|
]
|
2018-04-24 05:06:33 +00:00
|
|
|
self.want = V2Parameters(params=self.module.params)
|
|
|
|
self.changes = V2Parameters()
|
2017-05-24 16:57:30 +00:00
|
|
|
|
|
|
|
def _set_changed_options(self):
|
|
|
|
changed = {}
|
2018-04-24 05:06:33 +00:00
|
|
|
for key in V2Parameters.returnables:
|
2017-05-24 16:57:30 +00:00
|
|
|
if getattr(self.want, key) is not None:
|
|
|
|
changed[key] = getattr(self.want, key)
|
|
|
|
if changed:
|
2018-04-24 05:06:33 +00:00
|
|
|
self.changes = V2Parameters(params=changed)
|
2017-05-24 16:57:30 +00:00
|
|
|
|
|
|
|
def _update_changed_options(self):
|
|
|
|
changed = {}
|
2018-04-24 05:06:33 +00:00
|
|
|
for key in V2Parameters.updatables:
|
2017-05-24 16:57:30 +00:00
|
|
|
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:
|
2018-04-24 05:06:33 +00:00
|
|
|
self.changes = V2Parameters(params=changed)
|
2017-05-24 16:57:30 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def read_current_from_device(self):
|
2018-11-03 05:20:39 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/traps/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, self.want.name)
|
2017-05-24 16:57:30 +00:00
|
|
|
)
|
2018-11-03 05:20:39 +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)
|
|
|
|
self._ensure_network(response)
|
|
|
|
return V2Parameters(params=response)
|
2017-05-24 16:57:30 +00:00
|
|
|
|
|
|
|
def _ensure_network(self, result):
|
|
|
|
# BIG-IP's value for "default" is that the key does not
|
|
|
|
# exist. This conflicts with our purpose of having a key
|
|
|
|
# not exist (which we equate to "i dont want to change that"
|
|
|
|
# therefore, if we load the information from BIG-IP and
|
|
|
|
# find that there is no 'network' key, that is BIG-IP's
|
|
|
|
# way of saying that the network value is "default"
|
|
|
|
if 'network' not in result:
|
|
|
|
result['network'] = 'default'
|
|
|
|
|
|
|
|
|
2018-04-24 05:06:33 +00:00
|
|
|
class V1Manager(BaseManager):
|
2018-01-13 05:49:12 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2018-04-24 05:06:33 +00:00
|
|
|
super(V1Manager, self).__init__(**kwargs)
|
2017-05-24 16:57:30 +00:00
|
|
|
self.required_resources = [
|
|
|
|
'version', 'community', 'destination', 'port'
|
|
|
|
]
|
2018-04-24 05:06:33 +00:00
|
|
|
self.want = V1Parameters(params=self.module.params)
|
|
|
|
self.changes = V1Parameters()
|
2017-05-24 16:57:30 +00:00
|
|
|
|
|
|
|
def _set_changed_options(self):
|
|
|
|
changed = {}
|
2018-04-24 05:06:33 +00:00
|
|
|
for key in V1Parameters.returnables:
|
2017-05-24 16:57:30 +00:00
|
|
|
if getattr(self.want, key) is not None:
|
|
|
|
changed[key] = getattr(self.want, key)
|
|
|
|
if changed:
|
2018-04-24 05:06:33 +00:00
|
|
|
self.changes = V1Parameters(params=changed)
|
2017-05-24 16:57:30 +00:00
|
|
|
|
|
|
|
def _update_changed_options(self):
|
|
|
|
changed = {}
|
2018-04-24 05:06:33 +00:00
|
|
|
for key in V1Parameters.updatables:
|
2017-05-24 16:57:30 +00:00
|
|
|
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:
|
2018-04-24 05:06:33 +00:00
|
|
|
self.changes = V1Parameters(params=changed)
|
2017-05-24 16:57:30 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def read_current_from_device(self):
|
2018-11-03 05:20:39 +00:00
|
|
|
uri = "https://{0}:{1}/mgmt/tm/sys/snmp/traps/{2}".format(
|
|
|
|
self.client.provider['server'],
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
transform_name(self.want.partition, self.want.name)
|
2017-05-24 16:57:30 +00:00
|
|
|
)
|
2018-11-03 05:20:39 +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 V1Parameters(params=response)
|
2017-05-24 16:57:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ArgumentSpec(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.supports_check_mode = True
|
2018-01-13 05:49:12 +00:00
|
|
|
argument_spec = dict(
|
2017-05-24 16:57:30 +00:00
|
|
|
name=dict(
|
|
|
|
required=True
|
|
|
|
),
|
|
|
|
snmp_version=dict(
|
|
|
|
choices=['1', '2c']
|
|
|
|
),
|
2018-04-24 05:06:33 +00:00
|
|
|
community=dict(no_log=True),
|
2017-05-24 16:57:30 +00:00
|
|
|
destination=dict(),
|
|
|
|
port=dict(),
|
|
|
|
network=dict(
|
|
|
|
choices=['other', 'management', 'default']
|
|
|
|
),
|
|
|
|
state=dict(
|
|
|
|
default='present',
|
|
|
|
choices=['absent', 'present']
|
2018-01-13 05:49:12 +00:00
|
|
|
),
|
|
|
|
partition=dict(
|
|
|
|
default='Common',
|
|
|
|
fallback=(env_fallback, ['F5_PARTITION'])
|
2017-05-24 16:57:30 +00:00
|
|
|
)
|
|
|
|
)
|
2018-01-13 05:49:12 +00:00
|
|
|
self.argument_spec = {}
|
|
|
|
self.argument_spec.update(f5_argument_spec)
|
|
|
|
self.argument_spec.update(argument_spec)
|
2017-05-24 16:57:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
spec = ArgumentSpec()
|
|
|
|
|
2018-01-13 05:49:12 +00:00
|
|
|
module = AnsibleModule(
|
2017-05-24 16:57:30 +00:00
|
|
|
argument_spec=spec.argument_spec,
|
2018-01-13 05:49:12 +00:00
|
|
|
supports_check_mode=spec.supports_check_mode
|
2017-05-24 16:57:30 +00:00
|
|
|
)
|
2018-11-03 05:20:39 +00:00
|
|
|
|
|
|
|
client = F5RestClient(**module.params)
|
2018-01-13 05:49:12 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
mm = ModuleManager(module=module, client=client)
|
|
|
|
results = mm.exec_module()
|
|
|
|
cleanup_tokens(client)
|
2018-11-03 05:20:39 +00:00
|
|
|
exit_json(module, results, client)
|
2018-01-13 05:49:12 +00:00
|
|
|
except F5ModuleError as ex:
|
|
|
|
cleanup_tokens(client)
|
2018-11-03 05:20:39 +00:00
|
|
|
fail_json(module, ex, client)
|
2017-05-24 16:57:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|