Remove f5-sdk from bigip_vcmp_guest (#48541)

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

@ -157,6 +157,7 @@ notes:
extends_documentation_fragment: f5
author:
- Tim Rupp (@caphrim007)
- Wojciech Wypior (@wojtek0806)
'''
EXAMPLES = r'''
@ -208,35 +209,29 @@ from ansible.module_utils.basic import env_fallback
from collections import namedtuple
try:
from library.module_utils.network.f5.bigip import HAS_F5SDK
from library.module_utils.network.f5.bigip import F5Client
from library.module_utils.network.f5.bigip 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 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 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.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:
from ansible.module_utils.network.f5.bigip import HAS_F5SDK
from ansible.module_utils.network.f5.bigip import F5Client
from ansible.module_utils.network.f5.bigip 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 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 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.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):
@ -291,16 +286,12 @@ class Parameters(AnsibleF5Parameters):
'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
def mgmt_route(self):
if self._values['mgmt_route'] is None:
@ -328,7 +319,6 @@ class Parameters(AnsibleF5Parameters):
@property
def mgmt_tuple(self):
result = None
Destination = namedtuple('Destination', ['ip', 'subnet'])
try:
parts = self._values['mgmt_address'].split('/')
@ -371,9 +361,19 @@ class Parameters(AnsibleF5Parameters):
)
def initial_image_exists(self, image):
collection = self.client.api.tm.sys.software.images.get_collection()
for resource in collection:
if resource.name.startswith(image):
uri = "https://{0}:{1}/mgmt/tm/sys/software/images/".format(
self.client.provider['server'],
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 False
@ -386,23 +386,27 @@ class Parameters(AnsibleF5Parameters):
return result
class ApiParameters(Parameters):
pass
class ModuleParameters(Parameters):
pass
class Changes(Parameters):
pass
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(Parameters):
class UsableChanges(Changes):
pass
class ReportableChanges(Parameters):
class ReportableChanges(Changes):
pass
@ -452,7 +456,8 @@ class ModuleManager(object):
self.module = kwargs.get('module', None)
self.client = kwargs.get('client', None)
self.want = ModuleParameters(client=self.client, params=self.module.params)
self.changes = Changes()
self.have = None
self.changes = ReportableChanges()
def _set_changed_options(self):
changed = {}
@ -462,7 +467,7 @@ class ModuleManager(object):
if 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)
updatables = Parameters.updatables
changed = dict()
@ -477,6 +482,14 @@ class ModuleManager(object):
return True
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):
result = self._update_changed_options()
if result:
@ -488,39 +501,28 @@ class ModuleManager(object):
result = dict()
state = self.want.state
try:
if state in ['configured', 'provisioned', 'deployed']:
changed = self.present()
elif state == "absent":
changed = self.absent()
except iControlUnexpectedHTTPError as e:
raise F5ModuleError(str(e))
changes = self.changes.to_return()
if state in ['configured', 'provisioned', 'deployed']:
changed = self.present()
elif state == "absent":
changed = self.absent()
reportable = ReportableChanges(params=self.changes.to_return())
changes = reportable.to_return()
result.update(**changes)
result.update(dict(changed=changed))
self._announce_deprecations(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):
if self.exists():
return self.update()
else:
return self.create()
def exists(self):
result = self.client.api.tm.vcmp.guests.guest.exists(
name=self.want.name
)
return result
def absent(self):
if self.exists():
return self.remove()
return False
def update(self):
self.have = self.read_current_from_device()
@ -569,45 +571,96 @@ class ModuleManager(object):
self.configure()
return True
def exists(self):
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.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.want.api_params()
self.client.api.tm.vcmp.guests.guest.create(
name=self.want.name,
**params
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):
params = self.changes.api_params()
resource = self.client.api.tm.vcmp.guests.guest.load(
name=self.want.name
uri = "https://{0}:{1}/mgmt/tm/vcmp/guest/{2}".format(
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 self.exists():
return self.remove()
return False
if 'code' in response and response['code'] == 400:
if 'message' in response:
raise F5ModuleError(response['message'])
else:
raise F5ModuleError(resp.content)
def remove_from_device(self):
resource = self.client.api.tm.vcmp.guests.guest.load(
name=self.want.name
uri = "https://{0}:{1}/mgmt/tm/vcmp/guest/{2}".format(
self.client.provider['server'],
self.client.provider['server_port'],
self.want.name
)
if resource:
resource.delete()
response = self.client.api.delete(uri)
if response.status == 200:
return True
raise F5ModuleError(response.content)
def read_current_from_device(self):
resource = self.client.api.tm.vcmp.guests.guest.load(
name=self.want.name
uri = "https://{0}:{1}/mgmt/tm/vcmp/guest/{2}".format(
self.client.provider['server'],
self.client.provider['server_port'],
self.want.name
)
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)
def remove_virtual_disk(self):
if self.virtual_disk_exists():
return self.remove_virtual_disk_from_device()
return False
def virtual_disk_exists(self):
def get_virtual_disk_on_device(self):
"""Checks if a virtual disk exists for a guest
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.
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)
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 False
def remove_virtual_disk_from_device(self):
collection = self.client.api.tm.vcmp.virtual_disks.get_collection()
for resource in collection:
check = '{0}'.format(self.have.virtual_disk)
if resource.name.startswith(check):
resource.delete()
return True
return False
response = self.get_virtual_disk_on_device()
uri = "https://{0}:{1}/mgmt/tm/vcmp/virtual-disk/{2}".format(
self.client.provider['server'],
self.client.provider['server_port'],
response['name']
)
response = self.client.api.delete(uri)
if response.status == 200:
return True
raise F5ModuleError(response.content)
def is_configured(self):
"""Checks to see if guest is disabled
@ -650,35 +729,85 @@ class ModuleManager(object):
: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:
res = self.client.api.tm.vcmp.guests.guest.load(name=self.want.name)
Stats(res.stats.load())
return False
except iControlUnexpectedHTTPError as ex:
if 'Object not found - ' in str(ex):
return True
raise
response = resp.json()
except ValueError as ex:
raise F5ModuleError(str(ex))
if resp.status == 404 or 'code' in response and response['code'] == 404:
return True
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):
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:
res = self.client.api.tm.vcmp.guests.guest.load(name=self.want.name)
stats = Stats(res.stats.load())
if stats.stat['requestedState']['description'] == 'provisioned':
if stats.stat['vmStatus']['description'] == 'stopped':
response = resp.json()
except ValueError:
return False
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
except iControlUnexpectedHTTPError:
pass
return False
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:
res = self.client.api.tm.vcmp.guests.guest.load(name=self.want.name)
stats = Stats(res.stats.load())
if stats.stat['requestedState']['description'] == 'deployed':
if stats.stat['vmStatus']['description'] == 'running':
response = resp.json()
except ValueError:
return False
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
except iControlUnexpectedHTTPError:
pass
return False
def configure(self):
@ -689,8 +818,23 @@ class ModuleManager(object):
return True
def configure_on_device(self):
resource = self.client.api.tm.vcmp.guests.guest.load(name=self.want.name)
resource.modify(state='configured')
params = dict(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):
nops = 0
@ -706,8 +850,23 @@ class ModuleManager(object):
self.wait_for_provisioned()
def provision_on_device(self):
resource = self.client.api.tm.vcmp.guests.guest.load(name=self.want.name)
resource.modify(state='provisioned')
params = dict(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):
nops = 0
@ -723,8 +882,23 @@ class ModuleManager(object):
self.wait_for_deployed()
def deploy_on_device(self):
resource = self.client.api.tm.vcmp.guests.guest.load(name=self.want.name)
resource.modify(state='deployed')
params = dict(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):
nops = 0
@ -776,18 +950,17 @@ def main():
argument_spec=spec.argument_spec,
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:
client = F5Client(**module.params)
mm = ModuleManager(module=module, client=client)
results = mm.exec_module()
cleanup_tokens(client)
module.exit_json(**results)
exit_json(module, results, client)
except F5ModuleError as ex:
cleanup_tokens(client)
module.fail_json(msg=str(ex))
fail_json(module, ex, client)
if __name__ == '__main__':

@ -17,7 +17,8 @@ if sys.version_info < (2, 7):
from ansible.module_utils.basic import AnsibleModule
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 ArgumentSpec
@ -29,7 +30,8 @@ try:
from test.units.modules.utils import set_module_args
except ImportError:
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 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.mgmt_network == 'bridged'
@ -86,7 +88,7 @@ class TestParameters(unittest.TestCase):
mgmt_address='1.2.3.4'
)
p = Parameters(params=args)
p = ModuleParameters(params=args)
assert p.mgmt_network == 'bridged'
assert p.mgmt_address == '1.2.3.4/32'
@ -96,7 +98,7 @@ class TestParameters(unittest.TestCase):
mgmt_address='1.2.3.4/24'
)
p = Parameters(params=args)
p = ModuleParameters(params=args)
assert p.mgmt_network == 'bridged'
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'
)
p = Parameters(params=args)
p = ModuleParameters(params=args)
assert p.mgmt_network == 'bridged'
assert p.mgmt_address == '1.2.3.4/24'
@ -115,7 +117,7 @@ class TestParameters(unittest.TestCase):
mgmt_route='1.2.3.4'
)
p = Parameters(params=args)
p = ModuleParameters(params=args)
assert p.mgmt_route == '1.2.3.4'
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',
)
p = Parameters(params=args)
p = ModuleParameters(params=args)
assert p.initial_image == 'BIGIP-12.1.0.1.0.1447-HF1.iso/1'
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.mgmt_route == '2.2.2.2'
assert p.mgmt_address == '1.1.1.1/24'

Loading…
Cancel
Save