Skip to content

netbox_inventory_item: Allow assigning a component #1020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions docs/plugins/netbox_inventory_item_module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,155 @@ Parameters
- :ansible-option-choices-entry:`true`


.. raw:: html

</div>

* - .. raw:: html

<div class="ansible-option-indent"></div><div class="ansible-option-cell">
<div class="ansibleOptionAnchor" id="parameter-data/component"></div>

.. _ansible_collections.netbox.netbox.netbox_inventory_item_module__parameter-data/component:

.. rst-class:: ansible-option-title

**component**

.. raw:: html

<a class="ansibleOptionLink" href="#parameter-data/component" title="Permalink to this option"></a>

.. rst-class:: ansible-option-type-line

:ansible-option-type:`dictionary`

.. raw:: html

</div>

- .. raw:: html

<div class="ansible-option-indent-desc"></div><div class="ansible-option-cell">

The component


.. raw:: html

</div>

* - .. raw:: html

<div class="ansible-option-indent"></div><div class="ansible-option-indent"></div><div class="ansible-option-cell">
<div class="ansibleOptionAnchor" id="parameter-data/component/device"></div>

.. _ansible_collections.netbox.netbox.netbox_inventory_item_module__parameter-data/component/device:

.. rst-class:: ansible-option-title

**device**

.. raw:: html

<a class="ansibleOptionLink" href="#parameter-data/component/device" title="Permalink to this option"></a>

.. rst-class:: ansible-option-type-line

:ansible-option-type:`string`

.. raw:: html

</div>

- .. raw:: html

<div class="ansible-option-indent-desc"></div><div class="ansible-option-indent-desc"></div><div class="ansible-option-cell">

The device the interface is attached to.


.. raw:: html

</div>

* - .. raw:: html

<div class="ansible-option-indent"></div><div class="ansible-option-indent"></div><div class="ansible-option-cell">
<div class="ansibleOptionAnchor" id="parameter-data/assigned_object/name"></div>

.. _ansible_collections.netbox.netbox.netbox_inventory_item_module__parameter-data/assigned_object/name:

.. rst-class:: ansible-option-title

**name**

.. raw:: html

<a class="ansibleOptionLink" href="#parameter-data/component/name" title="Permalink to this option"></a>

.. rst-class:: ansible-option-type-line

:ansible-option-type:`string`

.. raw:: html

</div>

- .. raw:: html

<div class="ansible-option-indent-desc"></div><div class="ansible-option-indent-desc"></div><div class="ansible-option-cell">

The name of the interface


.. raw:: html

</div>

* - .. raw:: html

<div class="ansible-option-indent"></div><div class="ansible-option-cell">
<div class="ansibleOptionAnchor" id="parameter-data/component_type"></div>

.. _ansible_collections.netbox.netbox.netbox_inventory_item_module__parameter-data/component_type:

.. rst-class:: ansible-option-title

**component_type**

.. raw:: html

<a class="ansibleOptionLink" href="#parameter-data/component_type" title="Permalink to this option"></a>

.. rst-class:: ansible-option-type-line

:ansible-option-type:`string`

.. raw:: html

</div>

- .. raw:: html

<div class="ansible-option-indent-desc"></div><div class="ansible-option-cell">

The type of the component


.. rst-class:: ansible-option-line

:ansible-option-choices:`Choices:`

- :ansible-option-choices-entry:`"dcim.consoleport"`
- :ansible-option-choices-entry:`"dcim.consoleserverport"`
- :ansible-option-choices-entry:`"dcim.frontport"`
- :ansible-option-choices-entry:`"dcim.interface"`
- :ansible-option-choices-entry:`"dcim.poweroutlet"`
- :ansible-option-choices-entry:`"dcim.powerport"`
- :ansible-option-choices-entry:`"dcim.rearport"`


.. raw:: html

</div>
Expand Down
11 changes: 9 additions & 2 deletions plugins/module_utils/netbox_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
"cluster_groups": "cluster_groups",
"cluster_type": "cluster_types",
"cluster_types": "cluster_types",
"component": "component",
"config_context": "config_contexts",
"contact_groups": "contact_groups",
"dcim.consoleport": "console_ports",
Expand Down Expand Up @@ -385,6 +386,7 @@
"cluster": set(["name", "type"]),
"cluster_group": set(["slug"]),
"cluster_type": set(["slug"]),
"component": set(["device", "name"]),
"config_context": set(
[
"name",
Expand Down Expand Up @@ -436,7 +438,7 @@
"interface_a": set(["name", "device"]),
"interface_b": set(["name", "device"]),
"interface_template": set(["name", "device_type"]),
"inventory_item": set(["name", "device"]),
"inventory_item": set(["name", "device", "component"]),
"inventory_item_role": set(["name"]),
"ip_address": set(["address", "vrf", "device", "interface", "assigned_object"]),
"ip_addresses": set(["address", "vrf", "device", "interface", "assigned_object"]),
Expand Down Expand Up @@ -560,6 +562,7 @@
"circuit_type": "type",
"cluster_type": "type",
"cluster_group": "group",
"component": "component_id",
"contact_group": "group",
"fhrp_group": "group",
"parent_contact_group": "parent",
Expand Down Expand Up @@ -822,7 +825,7 @@ def _convert_identical_keys(self, data):
temp_dict[key] = data[key]
elif key in CONVERT_KEYS:
# This will keep the original key for keys in list, but also convert it.
if key in ("assigned_object", "scope"):
if key in ("assigned_object", "scope", "component"):
temp_dict[key] = data[key]
new_key = CONVERT_KEYS[key]
temp_dict[new_key] = data[key]
Expand Down Expand Up @@ -884,6 +887,8 @@ def _build_query_params(
parent = module_data["termination_a_type"]
elif parent == "termination_b" and module_data.get("termination_b_type"):
parent = module_data["termination_b_type"]
elif parent == "component" and module_data.get("component_type"):
parent = module_data["component_type"]
elif parent == "scope":
parent = ENDPOINT_NAME_MAPPING[SCOPE_TO_ENDPOINT[module_data["scope_type"]]]

Expand Down Expand Up @@ -1116,6 +1121,8 @@ def _find_ids(self, data, user_query_params):
endpoint = CONVERT_TO_ID[data.get("termination_b_type")]
elif k == "assigned_object":
endpoint = "interfaces"
elif k == "component":
endpoint = CONVERT_TO_ID[data.get("component_type")]
elif k == "scope":
# Determine endpoint name for scope ID resolution
endpoint = SCOPE_TO_ENDPOINT[data["scope_type"]]
Expand Down
50 changes: 50 additions & 0 deletions plugins/modules/netbox_inventory_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,35 @@
required: false
default: false
type: bool
component_type:
description:
- The type of the component
choices:
- dcim.consoleport
- dcim.consoleserverport
- dcim.frontport
- dcim.interface
- dcim.poweroutlet
- dcim.powerport
- dcim.rearport
required: false
type: str
component:
description:
- The component
required: false
type: dict
suboptions:
name:
description:
- The name of the interface
type: str
required: False
device:
description:
- The device the interface is attached to.
type: str
required: False
tags:
description:
- Any tags that the device may need to be associated with
Expand Down Expand Up @@ -191,6 +220,27 @@ def main():
asset_tag=dict(required=False, type="str"),
description=dict(required=False, type="str"),
discovered=dict(required=False, type="bool", default=False),
component_type=dict(
required=False,
choices=[
"dcim.consoleport",
"dcim.consoleserverport",
"dcim.frontport",
"dcim.interface",
"dcim.poweroutlet",
"dcim.powerport",
"dcim.rearport",
],
type="str",
),
component=dict(
required=False,
type="dict",
options=dict(
name=dict(required=False, type="str"),
device=dict(required=False, type="str"),
),
),
tags=dict(required=False, type="list", elements="raw"),
custom_fields=dict(required=False, type="dict"),
),
Expand Down