mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 05:10:22 -07:00
Remove the f5-sdk from more bigiq modules (#47721)
This commit is contained in:
parent
e7c83d6aa9
commit
6e28b7d572
7 changed files with 210 additions and 120 deletions
|
@ -72,25 +72,15 @@ description:
|
|||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
try:
|
||||
from library.module_utils.network.f5.bigiq import HAS_F5SDK
|
||||
from library.module_utils.network.f5.bigiq import F5Client
|
||||
from library.module_utils.network.f5.bigiq import F5RestClient
|
||||
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 f5_argument_spec
|
||||
try:
|
||||
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
|
||||
except ImportError:
|
||||
HAS_F5SDK = False
|
||||
except ImportError:
|
||||
from ansible.module_utils.network.f5.bigiq import HAS_F5SDK
|
||||
from ansible.module_utils.network.f5.bigiq import F5Client
|
||||
from ansible.module_utils.network.f5.bigiq import F5RestClient
|
||||
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 f5_argument_spec
|
||||
try:
|
||||
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
|
||||
except ImportError:
|
||||
HAS_F5SDK = False
|
||||
|
||||
|
||||
class Parameters(AnsibleF5Parameters):
|
||||
|
@ -135,13 +125,34 @@ class ModuleParameters(Parameters):
|
|||
|
||||
:return:
|
||||
"""
|
||||
collection = self.client.api.cm.device.licensing.pool.regkey.licenses_s.get_collection()
|
||||
collection = self.read_current_from_device()
|
||||
resource = next((x for x in collection if x.name == self._values['name']), None)
|
||||
if resource:
|
||||
return resource.id
|
||||
else:
|
||||
return "none"
|
||||
|
||||
def read_current_from_device(self):
|
||||
uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses".format(
|
||||
self.client.provider['server'],
|
||||
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)
|
||||
if 'items' not in response:
|
||||
return []
|
||||
result = [ApiParameters(params=r) for r in response['items']]
|
||||
return result
|
||||
|
||||
|
||||
class ApiParameters(Parameters):
|
||||
@property
|
||||
|
@ -187,6 +198,7 @@ class ModuleManager(object):
|
|||
def __init__(self, *args, **kwargs):
|
||||
self.module = kwargs.get('module', None)
|
||||
self.client = kwargs.get('client', None)
|
||||
self.client = F5RestClient(**self.module.params)
|
||||
self.want = ModuleParameters(client=self.client, params=self.module.params)
|
||||
self.have = ApiParameters()
|
||||
self.changes = UsableChanges()
|
||||
|
@ -228,13 +240,10 @@ class ModuleManager(object):
|
|||
result = dict()
|
||||
state = self.want.state
|
||||
|
||||
try:
|
||||
if state == "present":
|
||||
changed = self.present()
|
||||
elif state == "absent":
|
||||
changed = self.absent()
|
||||
except iControlUnexpectedHTTPError as e:
|
||||
raise F5ModuleError(str(e))
|
||||
if state == "present":
|
||||
changed = self.present()
|
||||
elif state == "absent":
|
||||
changed = self.absent()
|
||||
|
||||
reportable = ReportableChanges(params=self.changes.to_return())
|
||||
changes = reportable.to_return()
|
||||
|
@ -258,10 +267,19 @@ class ModuleManager(object):
|
|||
return self.create()
|
||||
|
||||
def exists(self):
|
||||
result = self.client.api.cm.device.licensing.pool.regkey.licenses_s.licenses.exists(
|
||||
id=self.want.uuid
|
||||
uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses/{2}".format(
|
||||
self.client.provider['server'],
|
||||
self.client.provider['server_port'],
|
||||
self.want.uuid,
|
||||
)
|
||||
return result
|
||||
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
|
||||
|
||||
def update(self):
|
||||
self.have = self.read_current_from_device()
|
||||
|
@ -289,17 +307,41 @@ class ModuleManager(object):
|
|||
|
||||
def create_on_device(self):
|
||||
params = self.want.api_params()
|
||||
self.client.api.cm.device.licensing.pool.regkey.licenses_s.licenses.create(
|
||||
name=self.want.name,
|
||||
**params
|
||||
params['name'] = self.want.name
|
||||
uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses/".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)
|
||||
|
||||
def update_on_device(self):
|
||||
params = self.changes.api_params()
|
||||
resource = self.client.api.cm.device.licensing.pool.regkey.licenses_s.licenses.load(
|
||||
id=self.want.uuid
|
||||
uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses/{2}".format(
|
||||
self.client.provider['server'],
|
||||
self.client.provider['server_port'],
|
||||
self.want.uuid
|
||||
)
|
||||
resource.modify(**params)
|
||||
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 absent(self):
|
||||
if self.exists():
|
||||
|
@ -307,18 +349,33 @@ class ModuleManager(object):
|
|||
return False
|
||||
|
||||
def remove_from_device(self):
|
||||
resource = self.client.api.cm.device.licensing.pool.regkey.licenses_s.licenses.load(
|
||||
id=self.want.uuid
|
||||
uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses/{2}".format(
|
||||
self.client.provider['server'],
|
||||
self.client.provider['server_port'],
|
||||
self.want.uuid
|
||||
)
|
||||
if resource:
|
||||
resource.delete()
|
||||
resp = self.client.api.delete(uri)
|
||||
if resp.status == 200:
|
||||
return True
|
||||
|
||||
def read_current_from_device(self):
|
||||
resource = self.client.api.cm.device.licensing.pool.regkey.licenses_s.licenses.load(
|
||||
id=self.want.uuid
|
||||
uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses/{2}".format(
|
||||
self.client.provider['server'],
|
||||
self.client.provider['server_port'],
|
||||
self.want.uuid
|
||||
)
|
||||
result = resource.attrs
|
||||
return ApiParameters(params=result)
|
||||
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)
|
||||
|
||||
|
||||
class ArgumentSpec(object):
|
||||
|
@ -342,18 +399,15 @@ def main():
|
|||
|
||||
module = AnsibleModule(
|
||||
argument_spec=spec.argument_spec,
|
||||
supports_check_mode=spec.supports_check_mode
|
||||
supports_check_mode=spec.supports_check_mode,
|
||||
)
|
||||
if not HAS_F5SDK:
|
||||
module.fail_json(msg="The python f5-sdk module is required")
|
||||
|
||||
try:
|
||||
client = F5Client(**module.params)
|
||||
mm = ModuleManager(module=module, client=client)
|
||||
mm = ModuleManager(module=module)
|
||||
results = mm.exec_module()
|
||||
module.exit_json(**results)
|
||||
except F5ModuleError as e:
|
||||
module.fail_json(msg=str(e))
|
||||
except F5ModuleError as ex:
|
||||
module.fail_json(msg=str(ex))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue