1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Remove f5-sdk from bigip_vcmp_guest (#48541)

This commit is contained in:
Tim Rupp 2018-11-11 13:58:11 -08:00 committed by GitHub
parent baaa142248
commit 5e7a02d574
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 309 additions and 134 deletions

View file

@ -157,6 +157,7 @@ notes:
extends_documentation_fragment: f5 extends_documentation_fragment: f5
author: author:
- Tim Rupp (@caphrim007) - Tim Rupp (@caphrim007)
- Wojciech Wypior (@wojtek0806)
''' '''
EXAMPLES = r''' EXAMPLES = r'''
@ -208,35 +209,29 @@ from ansible.module_utils.basic import env_fallback
from collections import namedtuple from collections import namedtuple
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 fq_name
from library.module_utils.network.f5.common import f5_argument_spec from library.module_utils.network.f5.common import f5_argument_spec
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.urls import parseStats
from library.module_utils.network.f5.ipaddress import is_valid_ip from library.module_utils.network.f5.ipaddress import is_valid_ip
from library.module_utils.compat.ipaddress import ip_interface from library.module_utils.compat.ipaddress import ip_interface
try:
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
from f5.utils.responses.handlers import Stats
except ImportError:
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 fq_name
from ansible.module_utils.network.f5.common import f5_argument_spec from ansible.module_utils.network.f5.common import f5_argument_spec
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.urls import parseStats
from ansible.module_utils.network.f5.ipaddress import is_valid_ip from ansible.module_utils.network.f5.ipaddress import is_valid_ip
from ansible.module_utils.compat.ipaddress import ip_interface from ansible.module_utils.compat.ipaddress import ip_interface
try:
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
from f5.utils.responses.handlers import Stats
except ImportError:
HAS_F5SDK = False
class Parameters(AnsibleF5Parameters): class Parameters(AnsibleF5Parameters):
@ -291,16 +286,12 @@ class Parameters(AnsibleF5Parameters):
'allowed_slots', 'allowed_slots',
] ]
def to_return(self):
result = {}
try:
for returnable in self.returnables:
result[returnable] = getattr(self, returnable)
result = self._filter_params(result)
except Exception:
pass
return result
class ApiParameters(Parameters):
pass
class ModuleParameters(Parameters):
@property @property
def mgmt_route(self): def mgmt_route(self):
if self._values['mgmt_route'] is None: if self._values['mgmt_route'] is None:
@ -328,7 +319,6 @@ class Parameters(AnsibleF5Parameters):
@property @property
def mgmt_tuple(self): def mgmt_tuple(self):
result = None
Destination = namedtuple('Destination', ['ip', 'subnet']) Destination = namedtuple('Destination', ['ip', 'subnet'])
try: try:
parts = self._values['mgmt_address'].split('/') parts = self._values['mgmt_address'].split('/')
@ -371,9 +361,19 @@ class Parameters(AnsibleF5Parameters):
) )
def initial_image_exists(self, image): def initial_image_exists(self, image):
collection = self.client.api.tm.sys.software.images.get_collection() uri = "https://{0}:{1}/mgmt/tm/sys/software/images/".format(
for resource in collection: self.client.provider['server'],
if resource.name.startswith(image): self.client.provider['server_port'],
)
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
for resource in response['items']:
if resource['name'].startswith(image):
return True return True
return False return False
@ -386,23 +386,27 @@ class Parameters(AnsibleF5Parameters):
return result return result
class ApiParameters(Parameters):
pass
class ModuleParameters(Parameters):
pass
class Changes(Parameters): class Changes(Parameters):
def to_return(self):
result = {}
try:
for returnable in self.returnables:
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
class UsableChanges(Changes):
pass pass
class UsableChanges(Parameters): class ReportableChanges(Changes):
pass
class ReportableChanges(Parameters):
pass pass
@ -452,7 +456,8 @@ class ModuleManager(object):
self.module = kwargs.get('module', None) self.module = kwargs.get('module', None)
self.client = kwargs.get('client', None) self.client = kwargs.get('client', None)
self.want = ModuleParameters(client=self.client, params=self.module.params) self.want = ModuleParameters(client=self.client, params=self.module.params)
self.changes = Changes() self.have = None
self.changes = ReportableChanges()
def _set_changed_options(self): def _set_changed_options(self):
changed = {} changed = {}
@ -462,7 +467,7 @@ class ModuleManager(object):
if changed: if changed:
self.changes = UsableChanges(params=changed) self.changes = UsableChanges(params=changed)
def _update_changed_options(self): # lgtm [py/similar-function] def _update_changed_options(self):
diff = Difference(self.want, self.have) diff = Difference(self.want, self.have)
updatables = Parameters.updatables updatables = Parameters.updatables
changed = dict() changed = dict()
@ -477,6 +482,14 @@ class ModuleManager(object):
return True return True
return False return False
def _announce_deprecations(self, result):
warnings = result.pop('__warnings', [])
for warning in warnings:
self.module.deprecate(
msg=warning['msg'],
version=warning['version']
)
def should_update(self): def should_update(self):
result = self._update_changed_options() result = self._update_changed_options()
if result: if result:
@ -488,39 +501,28 @@ class ModuleManager(object):
result = dict() result = dict()
state = self.want.state state = self.want.state
try: if state in ['configured', 'provisioned', 'deployed']:
if state in ['configured', 'provisioned', 'deployed']: 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))
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) self._announce_deprecations(result)
return result return result
def _announce_deprecations(self, result):
warnings = result.pop('__warnings', [])
for warning in warnings:
self.module.deprecate(
msg=warning['msg'],
version=warning['version']
)
def present(self): def present(self):
if self.exists(): if self.exists():
return self.update() return self.update()
else: else:
return self.create() return self.create()
def exists(self): def absent(self):
result = self.client.api.tm.vcmp.guests.guest.exists( if self.exists():
name=self.want.name return self.remove()
) return False
return result
def update(self): def update(self):
self.have = self.read_current_from_device() self.have = self.read_current_from_device()
@ -569,45 +571,96 @@ class ModuleManager(object):
self.configure() self.configure()
return True return True
def create_on_device(self): def exists(self):
params = self.want.api_params() uri = "https://{0}:{1}/mgmt/tm/vcmp/guest/{2}".format(
self.client.api.tm.vcmp.guests.guest.create( self.client.provider['server'],
name=self.want.name, self.client.provider['server_port'],
**params 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
def create_on_device(self):
params = self.changes.api_params()
params['name'] = self.want.name
uri = "https://{0}:{1}/mgmt/tm/vcmp/guest/".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 update_on_device(self): def update_on_device(self):
params = self.changes.api_params() params = self.changes.api_params()
resource = self.client.api.tm.vcmp.guests.guest.load( uri = "https://{0}:{1}/mgmt/tm/vcmp/guest/{2}".format(
name=self.want.name self.client.provider['server'],
self.client.provider['server_port'],
self.want.name
) )
resource.modify(**params) resp = self.client.api.patch(uri, json=params)
try:
response = resp.json()
except ValueError as ex:
raise F5ModuleError(str(ex))
def absent(self): if 'code' in response and response['code'] == 400:
if self.exists(): if 'message' in response:
return self.remove() raise F5ModuleError(response['message'])
return False else:
raise F5ModuleError(resp.content)
def remove_from_device(self): def remove_from_device(self):
resource = self.client.api.tm.vcmp.guests.guest.load( uri = "https://{0}:{1}/mgmt/tm/vcmp/guest/{2}".format(
name=self.want.name self.client.provider['server'],
self.client.provider['server_port'],
self.want.name
) )
if resource: response = self.client.api.delete(uri)
resource.delete() if response.status == 200:
return True
raise F5ModuleError(response.content)
def read_current_from_device(self): def read_current_from_device(self):
resource = self.client.api.tm.vcmp.guests.guest.load( uri = "https://{0}:{1}/mgmt/tm/vcmp/guest/{2}".format(
name=self.want.name self.client.provider['server'],
self.client.provider['server_port'],
self.want.name
) )
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)
def remove_virtual_disk(self): def remove_virtual_disk(self):
if self.virtual_disk_exists(): if self.virtual_disk_exists():
return self.remove_virtual_disk_from_device() return self.remove_virtual_disk_from_device()
return False return False
def virtual_disk_exists(self): def get_virtual_disk_on_device(self):
"""Checks if a virtual disk exists for a guest """Checks if a virtual disk exists for a guest
The virtual disk names can differ based on the device vCMP is installed on. The virtual disk names can differ based on the device vCMP is installed on.
@ -624,23 +677,49 @@ class ModuleManager(object):
for the virtual-disk without the trailing slot. for the virtual-disk without the trailing slot.
Returns: Returns:
bool: True on success. False otherwise. dict
""" """
collection = self.client.api.tm.vcmp.virtual_disks.get_collection()
for resource in collection: uri = "https://{0}:{1}/mgmt/tm/vcmp/virtual-disk/".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)
for resource in response['items']:
check = '{0}'.format(self.have.virtual_disk) check = '{0}'.format(self.have.virtual_disk)
if resource.name.startswith(check): if resource['name'].startswith(check):
return resource
else:
return False
def virtual_disk_exists(self):
response = self.get_virtual_disk_on_device()
if response:
return True return True
return False return False
def remove_virtual_disk_from_device(self): def remove_virtual_disk_from_device(self):
collection = self.client.api.tm.vcmp.virtual_disks.get_collection() response = self.get_virtual_disk_on_device()
for resource in collection: uri = "https://{0}:{1}/mgmt/tm/vcmp/virtual-disk/{2}".format(
check = '{0}'.format(self.have.virtual_disk) self.client.provider['server'],
if resource.name.startswith(check): self.client.provider['server_port'],
resource.delete() response['name']
return True )
return False response = self.client.api.delete(uri)
if response.status == 200:
return True
raise F5ModuleError(response.content)
def is_configured(self): def is_configured(self):
"""Checks to see if guest is disabled """Checks to see if guest is disabled
@ -650,35 +729,85 @@ class ModuleManager(object):
:return: :return:
""" """
uri = "https://{0}:{1}/mgmt/tm/vcmp/guest/{2}/stats".format(
self.client.provider['server'],
self.client.provider['server_port'],
self.want.name
)
resp = self.client.api.get(uri)
try: try:
res = self.client.api.tm.vcmp.guests.guest.load(name=self.want.name) response = resp.json()
Stats(res.stats.load()) except ValueError as ex:
return False raise F5ModuleError(str(ex))
except iControlUnexpectedHTTPError as ex:
if 'Object not found - ' in str(ex): if resp.status == 404 or 'code' in response and response['code'] == 404:
return True return True
raise
if 'code' in response and response['code'] == 400:
if 'message' in response:
raise F5ModuleError(response['message'])
else:
raise F5ModuleError(resp.content)
return False
def is_provisioned(self): def is_provisioned(self):
uri = "https://{0}:{1}/mgmt/tm/vcmp/guest/{2}/stats".format(
self.client.provider['server'],
self.client.provider['server_port'],
self.want.name
)
resp = self.client.api.get(uri)
try: try:
res = self.client.api.tm.vcmp.guests.guest.load(name=self.want.name) response = resp.json()
stats = Stats(res.stats.load()) except ValueError:
if stats.stat['requestedState']['description'] == 'provisioned': return False
if stats.stat['vmStatus']['description'] == 'stopped': if 'code' in response and response['code'] == 400:
if 'message' in response:
raise F5ModuleError(response['message'])
else:
raise F5ModuleError(resp.content)
if resp.status == 404 or 'code' in response and response['code'] == 404:
return False
result = parseStats(response)
if 'stats' in result:
if result['requestedState']['description'] == 'provisioned':
if result['vmStatus']['description'] == 'stopped':
return True return True
except iControlUnexpectedHTTPError:
pass
return False return False
def is_deployed(self): def is_deployed(self):
uri = "https://{0}:{1}/mgmt/tm/vcmp/guest/{2}/stats".format(
self.client.provider['server'],
self.client.provider['server_port'],
self.want.name
)
resp = self.client.api.get(uri)
try: try:
res = self.client.api.tm.vcmp.guests.guest.load(name=self.want.name) response = resp.json()
stats = Stats(res.stats.load()) except ValueError:
if stats.stat['requestedState']['description'] == 'deployed': return False
if stats.stat['vmStatus']['description'] == 'running': if 'code' in response and response['code'] == 400:
if 'message' in response:
raise F5ModuleError(response['message'])
else:
raise F5ModuleError(resp.content)
if resp.status == 404 or 'code' in response and response['code'] == 404:
return False
result = parseStats(response)
if 'stats' in result:
if result['requestedState']['description'] == 'deployed':
if result['vmStatus']['description'] == 'running':
return True return True
except iControlUnexpectedHTTPError:
pass
return False return False
def configure(self): def configure(self):
@ -689,8 +818,23 @@ class ModuleManager(object):
return True return True
def configure_on_device(self): def configure_on_device(self):
resource = self.client.api.tm.vcmp.guests.guest.load(name=self.want.name) params = dict(state='configured')
resource.modify(state='configured') uri = "https://{0}:{1}/mgmt/tm/vcmp/guest/{2}".format(
self.client.provider['server'],
self.client.provider['server_port'],
self.want.name
)
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 wait_for_configured(self): def wait_for_configured(self):
nops = 0 nops = 0
@ -706,8 +850,23 @@ class ModuleManager(object):
self.wait_for_provisioned() self.wait_for_provisioned()
def provision_on_device(self): def provision_on_device(self):
resource = self.client.api.tm.vcmp.guests.guest.load(name=self.want.name) params = dict(state='provisioned')
resource.modify(state='provisioned') uri = "https://{0}:{1}/mgmt/tm/vcmp/guest/{2}".format(
self.client.provider['server'],
self.client.provider['server_port'],
self.want.name
)
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 wait_for_provisioned(self): def wait_for_provisioned(self):
nops = 0 nops = 0
@ -723,8 +882,23 @@ class ModuleManager(object):
self.wait_for_deployed() self.wait_for_deployed()
def deploy_on_device(self): def deploy_on_device(self):
resource = self.client.api.tm.vcmp.guests.guest.load(name=self.want.name) params = dict(state='deployed')
resource.modify(state='deployed') uri = "https://{0}:{1}/mgmt/tm/vcmp/guest/{2}".format(
self.client.provider['server'],
self.client.provider['server_port'],
self.want.name
)
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 wait_for_deployed(self): def wait_for_deployed(self):
nops = 0 nops = 0
@ -776,18 +950,17 @@ def main():
argument_spec=spec.argument_spec, 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") 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__':

View file

@ -17,7 +17,8 @@ if sys.version_info < (2, 7):
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
try: try:
from library.modules.bigip_vcmp_guest import Parameters from library.modules.bigip_vcmp_guest import ModuleParameters
from library.modules.bigip_vcmp_guest import ApiParameters
from library.modules.bigip_vcmp_guest import ModuleManager from library.modules.bigip_vcmp_guest import ModuleManager
from library.modules.bigip_vcmp_guest import ArgumentSpec from library.modules.bigip_vcmp_guest import ArgumentSpec
@ -29,7 +30,8 @@ try:
from test.units.modules.utils import set_module_args from test.units.modules.utils import set_module_args
except ImportError: except ImportError:
try: try:
from ansible.modules.network.f5.bigip_vcmp_guest import Parameters from ansible.modules.network.f5.bigip_vcmp_guest import ModuleParameters
from ansible.modules.network.f5.bigip_vcmp_guest import ApiParameters
from ansible.modules.network.f5.bigip_vcmp_guest import ModuleManager from ansible.modules.network.f5.bigip_vcmp_guest import ModuleManager
from ansible.modules.network.f5.bigip_vcmp_guest import ArgumentSpec from ansible.modules.network.f5.bigip_vcmp_guest import ArgumentSpec
@ -76,7 +78,7 @@ class TestParameters(unittest.TestCase):
] ]
) )
p = Parameters(params=args) p = ModuleParameters(params=args)
assert p.initial_image == 'BIGIP-12.1.0.1.0.1447-HF1.iso' assert p.initial_image == 'BIGIP-12.1.0.1.0.1447-HF1.iso'
assert p.mgmt_network == 'bridged' assert p.mgmt_network == 'bridged'
@ -86,7 +88,7 @@ class TestParameters(unittest.TestCase):
mgmt_address='1.2.3.4' mgmt_address='1.2.3.4'
) )
p = Parameters(params=args) p = ModuleParameters(params=args)
assert p.mgmt_network == 'bridged' assert p.mgmt_network == 'bridged'
assert p.mgmt_address == '1.2.3.4/32' assert p.mgmt_address == '1.2.3.4/32'
@ -96,7 +98,7 @@ class TestParameters(unittest.TestCase):
mgmt_address='1.2.3.4/24' mgmt_address='1.2.3.4/24'
) )
p = Parameters(params=args) p = ModuleParameters(params=args)
assert p.mgmt_network == 'bridged' assert p.mgmt_network == 'bridged'
assert p.mgmt_address == '1.2.3.4/24' assert p.mgmt_address == '1.2.3.4/24'
@ -106,7 +108,7 @@ class TestParameters(unittest.TestCase):
mgmt_address='1.2.3.4/255.255.255.0' mgmt_address='1.2.3.4/255.255.255.0'
) )
p = Parameters(params=args) p = ModuleParameters(params=args)
assert p.mgmt_network == 'bridged' assert p.mgmt_network == 'bridged'
assert p.mgmt_address == '1.2.3.4/24' assert p.mgmt_address == '1.2.3.4/24'
@ -115,7 +117,7 @@ class TestParameters(unittest.TestCase):
mgmt_route='1.2.3.4' mgmt_route='1.2.3.4'
) )
p = Parameters(params=args) p = ModuleParameters(params=args)
assert p.mgmt_route == '1.2.3.4' assert p.mgmt_route == '1.2.3.4'
def test_module_parameters_vcmp_software_image_facts(self): def test_module_parameters_vcmp_software_image_facts(self):
@ -126,7 +128,7 @@ class TestParameters(unittest.TestCase):
initial_image='BIGIP-12.1.0.1.0.1447-HF1.iso/1', initial_image='BIGIP-12.1.0.1.0.1447-HF1.iso/1',
) )
p = Parameters(params=args) p = ModuleParameters(params=args)
assert p.initial_image == 'BIGIP-12.1.0.1.0.1447-HF1.iso/1' assert p.initial_image == 'BIGIP-12.1.0.1.0.1447-HF1.iso/1'
def test_api_parameters(self): def test_api_parameters(self):
@ -142,7 +144,7 @@ class TestParameters(unittest.TestCase):
] ]
) )
p = Parameters(params=args) p = ApiParameters(params=args)
assert p.initial_image == 'BIGIP-tmos-tier2-13.1.0.0.0.931.iso' assert p.initial_image == 'BIGIP-tmos-tier2-13.1.0.0.0.931.iso'
assert p.mgmt_route == '2.2.2.2' assert p.mgmt_route == '2.2.2.2'
assert p.mgmt_address == '1.1.1.1/24' assert p.mgmt_address == '1.1.1.1/24'