mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-26 14:41:23 -07:00
Fixes and refactor netbox device module (#50672)
* Adapt netbox_device to pynetbox changes Pynetbox API has changed, resulting in errors at the creation or deletion of any device with the netbox_device modules. Fixes these errors. rebase * Refactor netbox_device Better error handling Split the return values to `device` and `msg`, containing the created device (if any), and the message to get more info about what has been achieved (or the error if any)
This commit is contained in:
parent
2eaf0956b5
commit
8a5bb7e6c3
1 changed files with 62 additions and 37 deletions
|
@ -161,10 +161,14 @@ EXAMPLES = r'''
|
||||||
'''
|
'''
|
||||||
|
|
||||||
RETURN = r'''
|
RETURN = r'''
|
||||||
meta:
|
device:
|
||||||
description: Message indicating failure or returns results with the object created within Netbox
|
description: Serialized object as created or already existent within Netbox
|
||||||
|
returned: on creation
|
||||||
|
type: dict
|
||||||
|
msg:
|
||||||
|
description: Message indicating failure or info about what has been achieved
|
||||||
returned: always
|
returned: always
|
||||||
type: list
|
type: str
|
||||||
'''
|
'''
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
@ -177,31 +181,6 @@ except ImportError:
|
||||||
HAS_PYNETBOX = False
|
HAS_PYNETBOX = False
|
||||||
|
|
||||||
|
|
||||||
def netbox_create_device(nb, nb_endpoint, data):
|
|
||||||
norm_data = normalize_data(data)
|
|
||||||
if norm_data.get("status"):
|
|
||||||
norm_data["status"] = DEVICE_STATUS.get(norm_data["status"].lower(), 0)
|
|
||||||
if norm_data.get("face"):
|
|
||||||
norm_data["face"] = FACE_ID.get(norm_data["face"].lower(), 0)
|
|
||||||
data = find_ids(nb, norm_data)
|
|
||||||
try:
|
|
||||||
return nb_endpoint.create([norm_data])
|
|
||||||
except pynetbox.RequestError as e:
|
|
||||||
return json.loads(e.error)
|
|
||||||
|
|
||||||
|
|
||||||
def netbox_delete_device(nb_endpoint, data):
|
|
||||||
norm_data = normalize_data(data)
|
|
||||||
endpoint = nb_endpoint.get(name=norm_data["name"])
|
|
||||||
result = []
|
|
||||||
try:
|
|
||||||
if endpoint.delete():
|
|
||||||
result.append({'success': '%s deleted from Netbox' % (norm_data["name"])})
|
|
||||||
except AttributeError:
|
|
||||||
result.append({'failed': '%s not found' % (norm_data["name"])})
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
'''
|
'''
|
||||||
Main entry point for module execution
|
Main entry point for module execution
|
||||||
|
@ -222,7 +201,6 @@ def main():
|
||||||
module.fail_json(msg='pynetbox is required for this module')
|
module.fail_json(msg='pynetbox is required for this module')
|
||||||
|
|
||||||
# Assign variables to be used with module
|
# Assign variables to be used with module
|
||||||
changed = False
|
|
||||||
app = 'dcim'
|
app = 'dcim'
|
||||||
endpoint = 'devices'
|
endpoint = 'devices'
|
||||||
url = module.params["netbox_url"]
|
url = module.params["netbox_url"]
|
||||||
|
@ -242,15 +220,62 @@ def main():
|
||||||
module.fail_json(msg="Incorrect application specified: %s" % (app))
|
module.fail_json(msg="Incorrect application specified: %s" % (app))
|
||||||
|
|
||||||
nb_endpoint = getattr(nb_app, endpoint)
|
nb_endpoint = getattr(nb_app, endpoint)
|
||||||
|
norm_data = normalize_data(data)
|
||||||
|
try:
|
||||||
if 'present' in state:
|
if 'present' in state:
|
||||||
response = netbox_create_device(nb, nb_endpoint, data)
|
return module.exit_json(
|
||||||
if response[0].get('created'):
|
**ensure_device_present(nb, nb_endpoint, norm_data)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return module.exit_json(
|
||||||
|
**ensure_device_absent(nb_endpoint, norm_data)
|
||||||
|
)
|
||||||
|
except pynetbox.RequestError as e:
|
||||||
|
return module.fail_json(msg=json.loads(e.error))
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_device_present(nb, nb_endpoint, data):
|
||||||
|
'''
|
||||||
|
:returns dict(device, msg, changed): dictionary resulting of the request,
|
||||||
|
where `device` is the serialized device fetched or newly created in
|
||||||
|
Netbox
|
||||||
|
'''
|
||||||
|
nb_device = nb_endpoint.get(name=data["name"])
|
||||||
|
if not nb_device:
|
||||||
|
device = _netbox_create_device(nb, nb_endpoint, data).serialize()
|
||||||
|
changed = True
|
||||||
|
msg = "Device %s created" % (data["name"])
|
||||||
|
else:
|
||||||
|
msg = "Device %s already exists" % (data["name"])
|
||||||
|
device = nb_device.serialize()
|
||||||
|
changed = False
|
||||||
|
|
||||||
|
return {"device": device, "msg": msg, "changed": changed}
|
||||||
|
|
||||||
|
|
||||||
|
def _netbox_create_device(nb, nb_endpoint, data):
|
||||||
|
if data.get("status"):
|
||||||
|
data["status"] = DEVICE_STATUS.get(data["status"].lower(), 0)
|
||||||
|
if data.get("face"):
|
||||||
|
data["face"] = FACE_ID.get(data["face"].lower(), 0)
|
||||||
|
data = find_ids(nb, data)
|
||||||
|
return nb_endpoint.create(data)
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_device_absent(nb_endpoint, data):
|
||||||
|
'''
|
||||||
|
:returns dict(msg, changed)
|
||||||
|
'''
|
||||||
|
device = nb_endpoint.get(name=data["name"])
|
||||||
|
if device:
|
||||||
|
device.delete()
|
||||||
|
msg = 'Device %s deleted' % (data["name"])
|
||||||
changed = True
|
changed = True
|
||||||
else:
|
else:
|
||||||
response = netbox_delete_device(nb_endpoint, data)
|
msg = 'Device %s already absent' % (data["name"])
|
||||||
if 'success' in response[0]:
|
changed = False
|
||||||
changed = True
|
|
||||||
module.exit_json(changed=changed, meta=response)
|
return {"msg": msg, "changed": changed}
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue