Skip to content

Commit 5f26b63

Browse files
authored
feat: inventory item component (#1078)
* component to inventory item
1 parent 2cbea9b commit 5f26b63

File tree

6 files changed

+259
-3
lines changed

6 files changed

+259
-3
lines changed

plugins/module_utils/netbox_utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
"cluster_groups": "cluster_groups",
205205
"cluster_type": "cluster_types",
206206
"cluster_types": "cluster_types",
207+
"component": "interfaces",
207208
"config_context": "config_contexts",
208209
"contact_groups": "contact_groups",
209210
"dcim.consoleport": "console_ports",
@@ -386,6 +387,7 @@
386387
"cluster": set(["name", "type"]),
387388
"cluster_group": set(["slug"]),
388389
"cluster_type": set(["slug"]),
390+
"component": set(["name", "device"]),
389391
"config_context": set(
390392
[
391393
"name",
@@ -437,7 +439,7 @@
437439
"interface_a": set(["name", "device"]),
438440
"interface_b": set(["name", "device"]),
439441
"interface_template": set(["name", "device_type"]),
440-
"inventory_item": set(["name", "device"]),
442+
"inventory_item": set(["name", "device", "component", "component_type"]),
441443
"inventory_item_role": set(["name"]),
442444
"ip_address": set(["address", "vrf", "device", "interface", "assigned_object"]),
443445
"ip_addresses": set(["address", "vrf", "device", "interface", "assigned_object"]),
@@ -561,6 +563,7 @@
561563
"circuit_type": "type",
562564
"cluster_type": "type",
563565
"cluster_group": "group",
566+
"component": "component_id",
564567
"contact_group": "group",
565568
"device_role": "role",
566569
"fhrp_group": "group",
@@ -831,7 +834,7 @@ def _convert_identical_keys(self, data):
831834
temp_dict[key] = data[key]
832835
elif key in CONVERT_KEYS:
833836
# This will keep the original key for keys in list, but also convert it.
834-
if key in ("assigned_object", "scope"):
837+
if key in ("assigned_object", "scope", "component"):
835838
temp_dict[key] = data[key]
836839
new_key = CONVERT_KEYS[key]
837840
temp_dict[new_key] = data[key]
@@ -1134,6 +1137,8 @@ def _find_ids(self, data, user_query_params):
11341137
endpoint = CONVERT_TO_ID[data.get("termination_b_type")]
11351138
elif k == "assigned_object":
11361139
endpoint = "interfaces"
1140+
elif k == "component":
1141+
endpoint = CONVERT_TO_ID[data.get("component_type")]
11371142
elif k == "scope":
11381143
# Determine endpoint name for scope ID resolution
11391144
endpoint = SCOPE_TO_ENDPOINT[data["scope_type"]]

plugins/modules/netbox_inventory_item.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,35 @@
8282
required: false
8383
default: false
8484
type: bool
85+
component_type:
86+
description:
87+
- The type of the component. Required if component is defined.
88+
choices:
89+
- dcim.consoleport
90+
- dcim.consoleserverport
91+
- dcim.frontport
92+
- dcim.interface
93+
- dcim.poweroutlet
94+
- dcim.powerport
95+
- dcim.rearport
96+
required: false
97+
type: str
98+
component:
99+
description:
100+
- The associated component
101+
required: false
102+
type: dict
103+
suboptions:
104+
name:
105+
description:
106+
- The name of the component
107+
type: str
108+
required: False
109+
device:
110+
description:
111+
- The device the component is attached to.
112+
type: str
113+
required: False
85114
tags:
86115
description:
87116
- Any tags that the device may need to be associated with
@@ -145,6 +174,19 @@
145174
device: test100
146175
state: present
147176
177+
- name: Create inventory item with component
178+
netbox.netbox.netbox_inventory_item:
179+
netbox_url: http://netbox.local
180+
netbox_token: thisIsMyToken
181+
data:
182+
name: "10G-SFP+"
183+
device: test100
184+
component_type: "dcim.interface"
185+
component:
186+
name: GigabitEthernet2
187+
device: "test100"
188+
state: present
189+
148190
- name: Delete inventory item within netbox
149191
netbox.netbox.netbox_inventory_item:
150192
netbox_url: http://netbox.local
@@ -198,6 +240,27 @@ def main():
198240
asset_tag=dict(required=False, type="str"),
199241
description=dict(required=False, type="str"),
200242
discovered=dict(required=False, type="bool", default=False),
243+
component_type=dict(
244+
required=False,
245+
choices=[
246+
"dcim.consoleport",
247+
"dcim.consoleserverport",
248+
"dcim.frontport",
249+
"dcim.interface",
250+
"dcim.poweroutlet",
251+
"dcim.powerport",
252+
"dcim.rearport",
253+
],
254+
type="str",
255+
),
256+
component=dict(
257+
required=False,
258+
type="dict",
259+
options=dict(
260+
name=dict(required=False, type="str"),
261+
device=dict(required=False, type="str"),
262+
),
263+
),
201264
tags=dict(required=False, type="list", elements="raw"),
202265
custom_fields=dict(required=False, type="dict"),
203266
inventory_item_role=dict(required=False, type="raw"),
@@ -210,9 +273,13 @@ def main():
210273
("state", "present", ["device", "name"]),
211274
("state", "absent", ["device", "name"]),
212275
]
276+
required_together = [("component_type", "component")]
213277

214278
module = NetboxAnsibleModule(
215-
argument_spec=argument_spec, supports_check_mode=True, required_if=required_if
279+
argument_spec=argument_spec,
280+
supports_check_mode=True,
281+
required_if=required_if,
282+
required_together=required_together,
216283
)
217284

218285
netbox_inventory_item = NetboxDcimModule(module, NB_INVENTORY_ITEMS)

tests/integration/targets/v3.3/tasks/netbox_inventory_item.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,49 @@
155155
that:
156156
- test_six.failed
157157
- test_six.msg == "Could not resolve id of inventory_item_role: Foo"
158+
159+
- name: "INVENTORY_ITEM 7: Create inventory item with component"
160+
netbox.netbox.netbox_inventory_item:
161+
netbox_url: http://localhost:32768
162+
netbox_token: 0123456789abcdef0123456789abcdef01234567
163+
data:
164+
device: test100
165+
name: test_component
166+
component_type: "dcim.interface"
167+
component:
168+
name: GigabitEthernet2
169+
device: "test100"
170+
state: present
171+
register: test_seven
172+
173+
- name: "INVENTORY_ITEM 7: ASSERT - Inventory item creation with component"
174+
ansible.builtin.assert:
175+
that:
176+
- test_seven is changed
177+
- test_seven.diff.before.state == "absent"
178+
- test_seven.diff.after.state == "present"
179+
- test_seven.inventory_item.name == "test_component"
180+
- test_seven.inventory_item.component_type == "dcim.interface"
181+
- test_seven.inventory_item.component_id == 4
182+
- test_seven.inventory_item.device == 1
183+
- test_seven.msg == "inventory_item test_component created"
184+
185+
- name: "INVENTORY_ITEM 8: Create inventory item with missing component_type"
186+
netbox.netbox.netbox_inventory_item:
187+
netbox_url: http://localhost:32768
188+
netbox_token: 0123456789abcdef0123456789abcdef01234567
189+
data:
190+
device: test100
191+
name: test_component
192+
component:
193+
name: GigabitEthernet2
194+
device: "test100"
195+
state: present
196+
ignore_errors: true
197+
register: test_eight
198+
199+
- name: "INVENTORY_ITEM 8: ASSERT - Inventory item creation with missing component_type"
200+
ansible.builtin.assert:
201+
that:
202+
- test_eight.failed
203+
- test_eight.msg == "parameters are required together: component_type, component"

tests/integration/targets/v3.4/tasks/netbox_inventory_item.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,49 @@
155155
that:
156156
- test_six.failed
157157
- test_six.msg == "Could not resolve id of inventory_item_role: Foo"
158+
159+
- name: "INVENTORY_ITEM 7: Create inventory item with component"
160+
netbox.netbox.netbox_inventory_item:
161+
netbox_url: http://localhost:32768
162+
netbox_token: 0123456789abcdef0123456789abcdef01234567
163+
data:
164+
device: test100
165+
name: test_component
166+
component_type: "dcim.interface"
167+
component:
168+
name: GigabitEthernet2
169+
device: "test100"
170+
state: present
171+
register: test_seven
172+
173+
- name: "INVENTORY_ITEM 7: ASSERT - Inventory item creation with component"
174+
ansible.builtin.assert:
175+
that:
176+
- test_seven is changed
177+
- test_seven.diff.before.state == "absent"
178+
- test_seven.diff.after.state == "present"
179+
- test_seven.inventory_item.name == "test_component"
180+
- test_seven.inventory_item.component_type == "dcim.interface"
181+
- test_seven.inventory_item.component_id == 4
182+
- test_seven.inventory_item.device == 1
183+
- test_seven.msg == "inventory_item test_component created"
184+
185+
- name: "INVENTORY_ITEM 8: Create inventory item with missing component_type"
186+
netbox.netbox.netbox_inventory_item:
187+
netbox_url: http://localhost:32768
188+
netbox_token: 0123456789abcdef0123456789abcdef01234567
189+
data:
190+
device: test100
191+
name: test_component
192+
component:
193+
name: GigabitEthernet2
194+
device: "test100"
195+
state: present
196+
ignore_errors: true
197+
register: test_eight
198+
199+
- name: "INVENTORY_ITEM 8: ASSERT - Inventory item creation with missing component_type"
200+
ansible.builtin.assert:
201+
that:
202+
- test_eight.failed
203+
- test_eight.msg == "parameters are required together: component_type, component"

tests/integration/targets/v3.5/tasks/netbox_inventory_item.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,49 @@
155155
that:
156156
- test_six.failed
157157
- test_six.msg == "Could not resolve id of inventory_item_role: Foo"
158+
159+
- name: "INVENTORY_ITEM 7: Create inventory item with component"
160+
netbox.netbox.netbox_inventory_item:
161+
netbox_url: http://localhost:32768
162+
netbox_token: 0123456789abcdef0123456789abcdef01234567
163+
data:
164+
device: test100
165+
name: test_component
166+
component_type: "dcim.interface"
167+
component:
168+
name: GigabitEthernet2
169+
device: "test100"
170+
state: present
171+
register: test_seven
172+
173+
- name: "INVENTORY_ITEM 7: ASSERT - Inventory item creation with component"
174+
ansible.builtin.assert:
175+
that:
176+
- test_seven is changed
177+
- test_seven.diff.before.state == "absent"
178+
- test_seven.diff.after.state == "present"
179+
- test_seven.inventory_item.name == "test_component"
180+
- test_seven.inventory_item.component_type == "dcim.interface"
181+
- test_seven.inventory_item.component_id == 4
182+
- test_seven.inventory_item.device == 1
183+
- test_seven.msg == "inventory_item test_component created"
184+
185+
- name: "INVENTORY_ITEM 8: Create inventory item with missing component_type"
186+
netbox.netbox.netbox_inventory_item:
187+
netbox_url: http://localhost:32768
188+
netbox_token: 0123456789abcdef0123456789abcdef01234567
189+
data:
190+
device: test100
191+
name: test_component
192+
component:
193+
name: GigabitEthernet2
194+
device: "test100"
195+
state: present
196+
ignore_errors: true
197+
register: test_eight
198+
199+
- name: "INVENTORY_ITEM 8: ASSERT - Inventory item creation with missing component_type"
200+
ansible.builtin.assert:
201+
that:
202+
- test_eight.failed
203+
- test_eight.msg == "parameters are required together: component_type, component"

tests/integration/targets/v3.6/tasks/netbox_inventory_item.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,49 @@
155155
that:
156156
- test_six.failed
157157
- test_six.msg == "Could not resolve id of inventory_item_role: Foo"
158+
159+
- name: "INVENTORY_ITEM 7: Create inventory item with component"
160+
netbox.netbox.netbox_inventory_item:
161+
netbox_url: http://localhost:32768
162+
netbox_token: 0123456789abcdef0123456789abcdef01234567
163+
data:
164+
device: test100
165+
name: test_component
166+
component_type: "dcim.interface"
167+
component:
168+
name: GigabitEthernet2
169+
device: "test100"
170+
state: present
171+
register: test_seven
172+
173+
- name: "INVENTORY_ITEM 7: ASSERT - Inventory item creation with component"
174+
ansible.builtin.assert:
175+
that:
176+
- test_seven is changed
177+
- test_seven.diff.before.state == "absent"
178+
- test_seven.diff.after.state == "present"
179+
- test_seven.inventory_item.name == "test_component"
180+
- test_seven.inventory_item.component_type == "dcim.interface"
181+
- test_seven.inventory_item.component_id == 4
182+
- test_seven.inventory_item.device == 1
183+
- test_seven.msg == "inventory_item test_component created"
184+
185+
- name: "INVENTORY_ITEM 8: Create inventory item with missing component_type"
186+
netbox.netbox.netbox_inventory_item:
187+
netbox_url: http://localhost:32768
188+
netbox_token: 0123456789abcdef0123456789abcdef01234567
189+
data:
190+
device: test100
191+
name: test_component
192+
component:
193+
name: GigabitEthernet2
194+
device: "test100"
195+
state: present
196+
ignore_errors: true
197+
register: test_eight
198+
199+
- name: "INVENTORY_ITEM 8: ASSERT - Inventory item creation with missing component_type"
200+
ansible.builtin.assert:
201+
that:
202+
- test_eight.failed
203+
- test_eight.msg == "parameters are required together: component_type, component"

0 commit comments

Comments
 (0)