Issue #45948 - Netbox Modules

Here is the link: https://github.com/ansible/ansible/issues/45948

I was hoping to get some peer review on the first module I have made for interacting with Netbox to see if the code makes sense and where any improvements can be made. The link to my Ansible branch is within the issues’ comments.

What I’m trying to accomplish:

  • Re-useable functions in module_utils/net_tools/netbox/netbox_utils.py for future Netbox modules
  • Little code in the actual netbox modules themselves (i.e. modules/net_tools/netbox/netbox_device.py)

netbox_add():
This function will normalize the data, find any keys that require the IDs of the objects and then attempt to create the device within Netbox

normalize_data():
This function will convert the data to JSON and then attempt to normalize any of the values in the data that will be used to find the ID of the object (uses the QUERY_TYPES dictionary to determine how this data should look)

find_ids():
This function iterates through the keys in the data provided and checks them against the CONVERT_TO_ID dictionary. If the key is within the dictionary, it will then go through the process of building the Pynetbox object to get the ID. If it comes back with data, it will use the ID it found, if not, it will then check to see if the key is in the NO_DEFAULT_ID dictionary to make sure it keeps the data as is else it will give a default value of 1.

find_app():
This function is kicked off currently within the find_ids() method. This helps return the application that we need for any endpoint that may be in the data that we need to query for.

netbox_delete():
This uses the normalize_data function and then attempts to retrieve the object using pynetbox and a name(this returns either an object or None). If it gets an object back, it will then attempt to delete the device.

Thanks in advance!