Removes f5 sdk from bigiq modules and fixes unit tests (#47720)

pull/47721/head
Tim Rupp 6 years ago committed by GitHub
parent b7a937cb0d
commit d5d18a995a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -63,20 +63,21 @@ EXAMPLES = r'''
regkey_pool: foo-pool regkey_pool: foo-pool
license_key: XXXXX-XXXXX-XXXXX-XXXXX-XXXXX license_key: XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
accept_eula: yes accept_eula: yes
password: secret provider:
server: lb.mydomain.com password: secret
state: present server: lb.mydomain.com
user: admin user: admin
delegate_to: localhost delegate_to: localhost
- name: Remove a registration key license from a pool - name: Remove a registration key license from a pool
bigiq_regkey_license: bigiq_regkey_license:
regkey_pool: foo-pool regkey_pool: foo-pool
license_key: XXXXX-XXXXX-XXXXX-XXXXX-XXXXX license_key: XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
password: secret
server: lb.mydomain.com
state: absent state: absent
user: admin provider:
password: secret
server: lb.mydomain.com
user: admin
delegate_to: localhost delegate_to: localhost
''' '''
@ -93,25 +94,21 @@ import time
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
try: try:
from library.module_utils.network.f5.bigiq import HAS_F5SDK from library.module_utils.network.f5.bigiq import F5RestClient
from library.module_utils.network.f5.bigiq 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 f5_argument_spec from library.module_utils.network.f5.common import f5_argument_spec
try: from library.module_utils.network.f5.common import cleanup_tokens
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError from library.module_utils.network.f5.common import exit_json
except ImportError: from library.module_utils.network.f5.common import fail_json
HAS_F5SDK = False
except ImportError: except ImportError:
from ansible.module_utils.network.f5.bigiq import HAS_F5SDK from ansible.module_utils.network.f5.bigiq import F5RestClient
from ansible.module_utils.network.f5.bigiq 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 f5_argument_spec from ansible.module_utils.network.f5.common import f5_argument_spec
try: from ansible.module_utils.network.f5.common import cleanup_tokens
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError from ansible.module_utils.network.f5.common import exit_json
except ImportError: from ansible.module_utils.network.f5.common import fail_json
HAS_F5SDK = False
class Parameters(AnsibleF5Parameters): class Parameters(AnsibleF5Parameters):
@ -151,13 +148,34 @@ class ModuleParameters(Parameters):
def regkey_pool_uuid(self): def regkey_pool_uuid(self):
if self._values['regkey_pool_uuid']: if self._values['regkey_pool_uuid']:
return self._values['regkey_pool_uuid'] return self._values['regkey_pool_uuid']
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.regkey_pool), None) resource = next((x for x in collection if x.name == self.regkey_pool), None)
if resource is None: if resource is None:
raise F5ModuleError("Could not find the specified regkey pool.") raise F5ModuleError("Could not find the specified regkey pool.")
self._values['regkey_pool_uuid'] = resource.id self._values['regkey_pool_uuid'] = resource.id
return resource.id return resource.id
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 Changes(Parameters): class Changes(Parameters):
def to_return(self): def to_return(self):
@ -246,13 +264,10 @@ class ModuleManager(object):
result = dict() result = dict()
state = self.want.state state = self.want.state
try: if state == "present":
if state == "present": changed = self.present()
changed = self.present() elif state == "absent":
elif state == "absent": changed = self.absent()
changed = self.absent()
except iControlUnexpectedHTTPError as e:
raise F5ModuleError(str(e))
reportable = ReportableChanges(params=self.changes.to_return()) reportable = ReportableChanges(params=self.changes.to_return())
changes = reportable.to_return() changes = reportable.to_return()
@ -276,11 +291,18 @@ class ModuleManager(object):
return self.create() return self.create()
def exists(self): def exists(self):
collection = self.client.api.cm.device.licensing.pool.regkey.licenses_s uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses/{2}/offerings/{3}".format(
pool = collection.licenses.load(id=self.want.regkey_pool_uuid) self.client.provider['server'],
collection = pool.offerings_s.get_collection() self.client.provider['server_port'],
resource = next((x for x in collection if x.regKey == self.want.license_key), None) self.want.regkey_pool_uuid,
if resource is None: self.want.license_key
)
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 False
return True return True
@ -314,41 +336,74 @@ class ModuleManager(object):
def create_on_device(self): def create_on_device(self):
params = self.want.api_params() params = self.want.api_params()
collection = self.client.api.cm.device.licensing.pool.regkey.licenses_s params['name'] = self.want.name
pool = collection.licenses.load(id=self.want.regkey_pool_uuid) params['status'] = 'ACTIVATING_AUTOMATIC'
resource = pool.offerings_s.offerings.create( uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses/{2}/offerings".format(
status='ACTIVATING_AUTOMATIC', self.client.provider['server'],
**params self.client.provider['server_port'],
self.want.regkey_pool_uuid,
) )
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)
for x in range(60): for x in range(60):
resource.refresh() resource = self.read_current_from_device()
if resource.status == 'READY': if resource.status == 'READY':
break break
elif resource.status == 'ACTIVATING_AUTOMATIC_NEED_EULA_ACCEPT': elif resource.status == 'ACTIVATING_AUTOMATIC_NEED_EULA_ACCEPT':
resource.modify( params = dict(
status='ACTIVATING_AUTOMATIC_EULA_ACCEPTED', status='ACTIVATING_AUTOMATIC_EULA_ACCEPTED',
eulaText=resource.eulaText eulaText=resource.eulaText
) )
uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses/{2}/offerings/{3}".format(
self.client.provider['server'],
self.client.provider['server_port'],
self.want.regkey_pool_uuid,
self.want.license_key
)
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)
elif resource.status == 'ACTIVATION_FAILED': elif resource.status == 'ACTIVATION_FAILED':
raise F5ModuleError(str(resource.message)) raise F5ModuleError(str(resource.message))
time.sleep(1) time.sleep(1)
def wait_for_status(self, resource, status):
for x in range(60):
resource.refresh()
if resource.status == status:
return
time.sleep(1)
def update_on_device(self): def update_on_device(self):
params = self.changes.api_params() params = self.changes.api_params()
collection = self.client.api.cm.device.licensing.pool.regkey.licenses_s uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses/{2}/offerings/{3}".format(
pool = collection.licenses.load(id=self.want.regkey_pool_uuid) self.client.provider['server'],
collection = pool.offerings_s.get_collection() self.client.provider['server_port'],
resource = next((x for x in collection if x.regKey == self.want.license_key), None) self.want.regkey_pool_uuid,
if resource is None: self.want.license_key
return False )
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): def absent(self):
if self.exists(): if self.exists():
@ -356,24 +411,35 @@ class ModuleManager(object):
return False return False
def remove_from_device(self): def remove_from_device(self):
collection = self.client.api.cm.device.licensing.pool.regkey.licenses_s uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses/{2}/offerings/{3}".format(
pool = collection.licenses.load(id=self.want.regkey_pool_uuid) self.client.provider['server'],
collection = pool.offerings_s.get_collection() self.client.provider['server_port'],
resource = next((x for x in collection if x.regKey == self.want.license_key), None) self.want.regkey_pool_uuid,
if resource is None: self.want.license_key
return False )
if resource: resp = self.client.api.delete(uri)
resource.delete() if resp.status == 200:
return True
def read_current_from_device(self): def read_current_from_device(self):
collection = self.client.api.cm.device.licensing.pool.regkey.licenses_s uri = "https://{0}:{1}/mgmt/cm/device/licensing/pool/regkey/licenses/{2}/offerings/{3}".format(
pool = collection.licenses.load(id=self.want.regkey_pool_uuid) self.client.provider['server'],
collection = pool.offerings_s.get_collection() self.client.provider['server_port'],
resource = next((x for x in collection if x.regKey == self.want.license_key), None) self.want.regkey_pool_uuid,
if resource is None: self.want.license_key
return False )
result = resource.attrs resp = self.client.api.get(uri)
return ApiParameters(params=result) 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): class ArgumentSpec(object):
@ -403,18 +469,18 @@ def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=spec.argument_spec, argument_spec=spec.argument_spec,
supports_check_mode=spec.supports_check_mode, supports_check_mode=spec.supports_check_mode,
required_if=spec.required_if required_if=spec.required_if,
) )
if not HAS_F5SDK: client = F5RestClient(**module.params)
module.fail_json(msg="The python f5-sdk module is required")
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()
module.exit_json(**results) cleanup_tokens(client)
except F5ModuleError as e: exit_json(module, results, client)
module.fail_json(msg=str(e)) except F5ModuleError as ex:
cleanup_tokens(client)
fail_json(module, ex, client)
if __name__ == '__main__': if __name__ == '__main__':

@ -8,16 +8,12 @@ __metaclass__ = type
import os import os
import json import json
import pytest
import sys import sys
from nose.plugins.skip import SkipTest 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:
@ -25,17 +21,25 @@ try:
from library.modules.bigiq_regkey_license import ApiParameters from library.modules.bigiq_regkey_license import ApiParameters
from library.modules.bigiq_regkey_license import ModuleManager from library.modules.bigiq_regkey_license import ModuleManager
from library.modules.bigiq_regkey_license import ArgumentSpec from library.modules.bigiq_regkey_license 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.bigiq_regkey_license import ModuleParameters from ansible.modules.network.f5.bigiq_regkey_license import ModuleParameters
from ansible.modules.network.f5.bigiq_regkey_license import ApiParameters from ansible.modules.network.f5.bigiq_regkey_license import ApiParameters
from ansible.modules.network.f5.bigiq_regkey_license import ModuleManager from ansible.modules.network.f5.bigiq_regkey_license import ModuleManager
from ansible.modules.network.f5.bigiq_regkey_license import ArgumentSpec from ansible.modules.network.f5.bigiq_regkey_license 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…
Cancel
Save