mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Remove f5-sdk from bigip_device_ntp (#48477)
This commit is contained in:
parent
ccb6349e70
commit
90857004c9
2 changed files with 215 additions and 80 deletions
|
@ -1,8 +1,9 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
#
|
#
|
||||||
# Copyright (c) 2017 F5 Networks Inc.
|
# Copyright: (c) 2017, F5 Networks Inc.
|
||||||
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# 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
|
from __future__ import absolute_import, division, print_function
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
|
@ -47,19 +48,19 @@ EXAMPLES = r'''
|
||||||
bigip_device_ntp:
|
bigip_device_ntp:
|
||||||
ntp_servers:
|
ntp_servers:
|
||||||
- 192.0.2.23
|
- 192.0.2.23
|
||||||
password: secret
|
provider:
|
||||||
server: lb.mydomain.com
|
password: secret
|
||||||
user: admin
|
server: lb.mydomain.com
|
||||||
validate_certs: no
|
user: admin
|
||||||
delegate_to: localhost
|
delegate_to: localhost
|
||||||
|
|
||||||
- name: Set timezone
|
- name: Set timezone
|
||||||
bigip_device_ntp:
|
bigip_device_ntp:
|
||||||
password: secret
|
|
||||||
server: lb.mydomain.com
|
|
||||||
timezone: America/Los_Angeles
|
timezone: America/Los_Angeles
|
||||||
user: admin
|
provider:
|
||||||
validate_certs: no
|
password: secret
|
||||||
|
server: lb.mydomain.com
|
||||||
|
user: admin
|
||||||
delegate_to: localhost
|
delegate_to: localhost
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
@ -79,32 +80,30 @@ timezone:
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from library.module_utils.network.f5.bigip import HAS_F5SDK
|
from library.module_utils.network.f5.bigip import F5RestClient
|
||||||
from library.module_utils.network.f5.bigip import F5Client
|
|
||||||
from library.module_utils.network.f5.common import F5ModuleError
|
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 AnsibleF5Parameters
|
||||||
from library.module_utils.network.f5.common import cleanup_tokens
|
from library.module_utils.network.f5.common import cleanup_tokens
|
||||||
|
from library.module_utils.network.f5.common import fq_name
|
||||||
from library.module_utils.network.f5.common import f5_argument_spec
|
from library.module_utils.network.f5.common import f5_argument_spec
|
||||||
try:
|
from library.module_utils.network.f5.common import exit_json
|
||||||
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
|
from library.module_utils.network.f5.common import fail_json
|
||||||
except ImportError:
|
from library.module_utils.network.f5.common import is_empty_list
|
||||||
HAS_F5SDK = False
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from ansible.module_utils.network.f5.bigip import HAS_F5SDK
|
from ansible.module_utils.network.f5.bigip import F5RestClient
|
||||||
from ansible.module_utils.network.f5.bigip import F5Client
|
|
||||||
from ansible.module_utils.network.f5.common import F5ModuleError
|
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 AnsibleF5Parameters
|
||||||
from ansible.module_utils.network.f5.common import cleanup_tokens
|
from ansible.module_utils.network.f5.common import cleanup_tokens
|
||||||
|
from ansible.module_utils.network.f5.common import fq_name
|
||||||
from ansible.module_utils.network.f5.common import f5_argument_spec
|
from ansible.module_utils.network.f5.common import f5_argument_spec
|
||||||
try:
|
from ansible.module_utils.network.f5.common import exit_json
|
||||||
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
|
from ansible.module_utils.network.f5.common import fail_json
|
||||||
except ImportError:
|
from ansible.module_utils.network.f5.common import is_empty_list
|
||||||
HAS_F5SDK = False
|
|
||||||
|
|
||||||
|
|
||||||
class Parameters(AnsibleF5Parameters):
|
class Parameters(AnsibleF5Parameters):
|
||||||
api_map = {
|
api_map = {
|
||||||
'servers': 'ntp_servers'
|
'servers': 'ntp_servers',
|
||||||
}
|
}
|
||||||
|
|
||||||
api_attributes = [
|
api_attributes = [
|
||||||
|
@ -112,75 +111,168 @@ class Parameters(AnsibleF5Parameters):
|
||||||
]
|
]
|
||||||
|
|
||||||
updatables = [
|
updatables = [
|
||||||
'ntp_servers', 'timezone'
|
'ntp_servers', 'timezone',
|
||||||
]
|
]
|
||||||
|
|
||||||
returnables = [
|
returnables = [
|
||||||
'ntp_servers', 'timezone'
|
'ntp_servers', 'timezone',
|
||||||
]
|
]
|
||||||
|
|
||||||
absentables = [
|
absentables = [
|
||||||
'ntp_servers'
|
'ntp_servers',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class ApiParameters(Parameters):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ModuleParameters(Parameters):
|
||||||
|
@property
|
||||||
|
def ntp_servers(self):
|
||||||
|
ntp_servers = self._values['ntp_servers']
|
||||||
|
if ntp_servers is None:
|
||||||
|
return None
|
||||||
|
if is_empty_list(ntp_servers):
|
||||||
|
return []
|
||||||
|
return ntp_servers
|
||||||
|
|
||||||
|
|
||||||
|
class Changes(Parameters):
|
||||||
def to_return(self):
|
def to_return(self):
|
||||||
result = {}
|
result = {}
|
||||||
for returnable in self.returnables:
|
try:
|
||||||
result[returnable] = getattr(self, returnable)
|
for returnable in self.returnables:
|
||||||
result = self._filter_params(result)
|
change = getattr(self, returnable)
|
||||||
|
if isinstance(change, dict):
|
||||||
|
result.update(change)
|
||||||
|
else:
|
||||||
|
result[returnable] = change
|
||||||
|
result = self._filter_params(result)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
return 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
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ntp_servers(self):
|
||||||
|
state = self.want.state
|
||||||
|
if self.want.ntp_servers is None:
|
||||||
|
return None
|
||||||
|
if state == 'absent':
|
||||||
|
if self.have.ntp_servers is None and self.want.ntp_servers:
|
||||||
|
return None
|
||||||
|
if set(self.want.ntp_servers) == set(self.have.ntp_servers):
|
||||||
|
return []
|
||||||
|
if set(self.want.ntp_servers) != set(self.have.ntp_servers):
|
||||||
|
return list(set(self.want.ntp_servers).difference(self.have.ntp_servers))
|
||||||
|
if not self.want.ntp_servers:
|
||||||
|
if self.have.ntp_servers is None:
|
||||||
|
return None
|
||||||
|
if self.have.ntp_servers is not None:
|
||||||
|
return self.want.ntp_servers
|
||||||
|
if self.have.ntp_servers is None:
|
||||||
|
return self.want.ntp_servers
|
||||||
|
if set(self.want.ntp_servers) != set(self.have.ntp_servers):
|
||||||
|
return self.want.ntp_servers
|
||||||
|
|
||||||
|
|
||||||
class ModuleManager(object):
|
class ModuleManager(object):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
self.module = kwargs.get('module', None)
|
self.module = kwargs.pop('module', None)
|
||||||
self.client = kwargs.get('client', None)
|
self.client = kwargs.pop('client', None)
|
||||||
self.have = None
|
self.want = ModuleParameters(params=self.module.params)
|
||||||
self.want = Parameters(params=self.module.params)
|
self.have = ApiParameters()
|
||||||
self.changes = Parameters()
|
self.changes = UsableChanges()
|
||||||
|
|
||||||
def _update_changed_options(self): # lgtm [py/similar-function]
|
def _announce_deprecations(self, result):
|
||||||
changed = {}
|
warnings = result.pop('__warnings', [])
|
||||||
for key in Parameters.updatables:
|
for warning in warnings:
|
||||||
if getattr(self.want, key) is not None:
|
self.module.deprecate(
|
||||||
attr1 = getattr(self.want, key)
|
msg=warning['msg'],
|
||||||
attr2 = getattr(self.have, key)
|
version=warning['version']
|
||||||
if attr1 != attr2:
|
)
|
||||||
changed[key] = attr1
|
|
||||||
|
def _update_changed_options(self):
|
||||||
|
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
|
||||||
if changed:
|
if changed:
|
||||||
self.changes = Parameters(params=changed)
|
self.changes = UsableChanges(params=changed)
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _absent_changed_options(self):
|
def _absent_changed_options(self):
|
||||||
changed = {}
|
diff = Difference(self.want, self.have)
|
||||||
for key in Parameters.absentables:
|
absentables = Parameters.absentables
|
||||||
if getattr(self.want, key) is not None:
|
changed = dict()
|
||||||
set_want = set(getattr(self.want, key))
|
for k in absentables:
|
||||||
set_have = set(getattr(self.have, key))
|
change = diff.compare(k)
|
||||||
if set_want != set_have:
|
if change is None:
|
||||||
changed[key] = list(set_want)
|
continue
|
||||||
|
else:
|
||||||
|
if isinstance(change, dict):
|
||||||
|
changed.update(change)
|
||||||
|
else:
|
||||||
|
changed[k] = change
|
||||||
if changed:
|
if changed:
|
||||||
self.changes = Parameters(params=changed)
|
self.changes = UsableChanges(params=changed)
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def exec_module(self): # lgtm [py/similar-function]
|
def exec_module(self):
|
||||||
changed = False
|
changed = False
|
||||||
result = dict()
|
result = dict()
|
||||||
state = self.want.state
|
state = self.want.state
|
||||||
|
|
||||||
try:
|
if state == "present":
|
||||||
if state == "present":
|
changed = self.update()
|
||||||
changed = self.update()
|
elif state == "absent":
|
||||||
elif state == "absent":
|
changed = self.absent()
|
||||||
changed = self.absent()
|
|
||||||
except iControlUnexpectedHTTPError as e:
|
|
||||||
raise F5ModuleError(str(e))
|
|
||||||
|
|
||||||
changes = self.changes.to_return()
|
reportable = ReportableChanges(params=self.changes.to_return())
|
||||||
|
changes = reportable.to_return()
|
||||||
result.update(**changes)
|
result.update(**changes)
|
||||||
result.update(dict(changed=changed))
|
result.update(dict(changed=changed))
|
||||||
|
self._announce_deprecations(result)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
|
@ -214,19 +306,58 @@ class ModuleManager(object):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def update_on_device(self):
|
def update_on_device(self):
|
||||||
params = self.want.api_params()
|
params = self.changes.api_params()
|
||||||
resource = self.client.api.tm.sys.ntp.load()
|
uri = "https://{0}:{1}/mgmt/tm/sys/ntp/".format(
|
||||||
resource.update(**params)
|
self.client.provider['server'],
|
||||||
|
self.client.provider['server_port'],
|
||||||
|
)
|
||||||
|
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)
|
||||||
|
|
||||||
def read_current_from_device(self):
|
def read_current_from_device(self):
|
||||||
resource = self.client.api.tm.sys.ntp.load()
|
uri = "https://{0}:{1}/mgmt/tm/sys/ntp/".format(
|
||||||
result = resource.attrs
|
self.client.provider['server'],
|
||||||
return Parameters(params=result)
|
self.client.provider['server_port'],
|
||||||
|
)
|
||||||
|
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 absent_on_device(self):
|
def absent_on_device(self):
|
||||||
params = self.changes.api_params()
|
params = self.changes.api_params()
|
||||||
resource = self.client.api.tm.sys.ntp.load()
|
uri = "https://{0}:{1}/mgmt/tm/sys/ntp/".format(
|
||||||
resource.update(**params)
|
self.client.provider['server'],
|
||||||
|
self.client.provider['server_port'],
|
||||||
|
)
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
class ArgumentSpec(object):
|
class ArgumentSpec(object):
|
||||||
|
@ -259,18 +390,17 @@ def main():
|
||||||
supports_check_mode=spec.supports_check_mode,
|
supports_check_mode=spec.supports_check_mode,
|
||||||
required_one_of=spec.required_one_of
|
required_one_of=spec.required_one_of
|
||||||
)
|
)
|
||||||
if not HAS_F5SDK:
|
|
||||||
module.fail_json(msg="The python f5-sdk module is required")
|
client = F5RestClient(**module.params)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
client = F5Client(**module.params)
|
|
||||||
mm = ModuleManager(module=module, client=client)
|
mm = ModuleManager(module=module, client=client)
|
||||||
results = mm.exec_module()
|
results = mm.exec_module()
|
||||||
cleanup_tokens(client)
|
cleanup_tokens(client)
|
||||||
module.exit_json(**results)
|
exit_json(module, results, client)
|
||||||
except F5ModuleError as ex:
|
except F5ModuleError as ex:
|
||||||
cleanup_tokens(client)
|
cleanup_tokens(client)
|
||||||
module.fail_json(msg=str(ex))
|
fail_json(module, ex, client)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -14,25 +14,30 @@ from nose.plugins.skip import SkipTest
|
||||||
if sys.version_info < (2, 7):
|
if sys.version_info < (2, 7):
|
||||||
raise SkipTest("F5 Ansible modules require Python >= 2.7")
|
raise SkipTest("F5 Ansible modules require Python >= 2.7")
|
||||||
|
|
||||||
from units.compat import unittest
|
|
||||||
from units.compat.mock import Mock
|
|
||||||
from units.compat.mock import patch
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from library.modules.bigip_device_ntp import Parameters
|
from library.modules.bigip_device_ntp import Parameters
|
||||||
from library.modules.bigip_device_ntp import ModuleManager
|
from library.modules.bigip_device_ntp import ModuleManager
|
||||||
from library.modules.bigip_device_ntp import ArgumentSpec
|
from library.modules.bigip_device_ntp import ArgumentSpec
|
||||||
from library.module_utils.network.f5.common import F5ModuleError
|
|
||||||
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
|
# In Ansible 2.8, Ansible changed import paths.
|
||||||
from test.unit.modules.utils import set_module_args
|
from test.units.compat import unittest
|
||||||
|
from test.units.compat.mock import Mock
|
||||||
|
from test.units.compat.mock import patch
|
||||||
|
|
||||||
|
from test.units.modules.utils import set_module_args
|
||||||
except ImportError:
|
except ImportError:
|
||||||
try:
|
try:
|
||||||
from ansible.modules.network.f5.bigip_device_ntp import Parameters
|
from ansible.modules.network.f5.bigip_device_ntp import Parameters
|
||||||
from ansible.modules.network.f5.bigip_device_ntp import ModuleManager
|
from ansible.modules.network.f5.bigip_device_ntp import ModuleManager
|
||||||
from ansible.modules.network.f5.bigip_device_ntp import ArgumentSpec
|
from ansible.modules.network.f5.bigip_device_ntp import ArgumentSpec
|
||||||
from ansible.module_utils.network.f5.common import F5ModuleError
|
|
||||||
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
|
# Ansible 2.8 imports
|
||||||
|
from units.compat import unittest
|
||||||
|
from units.compat.mock import Mock
|
||||||
|
from units.compat.mock import patch
|
||||||
|
|
||||||
from units.modules.utils import set_module_args
|
from units.modules.utils import set_module_args
|
||||||
except ImportError:
|
except ImportError:
|
||||||
raise SkipTest("F5 Ansible modules require the f5-sdk Python library")
|
raise SkipTest("F5 Ansible modules require the f5-sdk Python library")
|
||||||
|
|
Loading…
Reference in a new issue