Skip to content

Commit aed8868

Browse files
BugFix: netbox_device_interface - Allow modification of VC child interfaces when parent device specified (#126)
1 parent 52930f8 commit aed8868

File tree

3 files changed

+75
-11
lines changed

3 files changed

+75
-11
lines changed

plugins/module_utils/netbox_dcim.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,17 @@ def run(self):
9393
# This is logic to handle interfaces on a VC
9494
if self.endpoint == "interfaces":
9595
if self.nb_object:
96-
if self.nb_object.device:
97-
device = self.nb.dcim.devices.get(self.nb_object.device.id)
98-
if (
99-
device["virtual_chassis"]
100-
and self.nb_object.device.id != self.data["device"]
101-
):
102-
self.object = None
96+
device = self.nb.dcim.devices.get(self.nb_object.device.id)
97+
if (
98+
device["virtual_chassis"]
99+
and self.nb_object.device.id != self.data["device"]
100+
):
101+
if self.module.params.get("update_vc_child"):
102+
data["device"] = self.nb_object.device.id
103+
else:
104+
self._handle_errors(
105+
msg="Must set update_vc_child to True to allow child device interface modification"
106+
)
103107

104108
if self.state == "present":
105109
self._ensure_object_exists(nb_endpoint, endpoint_name, name, data)

plugins/modules/netbox_device_interface.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@
105105
- Any tags that the prefix may need to be associated with
106106
type: list
107107
required: true
108+
update_vc_child:
109+
type: boolean
110+
default: False
111+
description:
112+
- |
113+
Use when master device is specified for C(device) and the specified interface exists on a child device
114+
and needs updated
108115
state:
109116
description:
110117
- Use C(present) or C(absent) for adding or removing.
@@ -190,6 +197,15 @@
190197
mgmt_only: true
191198
mode: Tagged
192199
state: present
200+
- name: Update interface on child device on virtual chassis
201+
netbox_device_interface:
202+
netbox_url: http://netbox.local
203+
netbox_token: thisIsMyToken
204+
data:
205+
device: test100
206+
name: GigabitEthernet2/0/1
207+
enabled: false
208+
update_vc_child: True
193209
"""
194210

195211
RETURN = r"""
@@ -220,6 +236,7 @@ def main():
220236
argument_spec = NETBOX_ARG_SPEC
221237
argument_spec.update(
222238
dict(
239+
update_vc_child=dict(type="bool", required=False, default=False),
223240
data=dict(
224241
type="dict",
225242
required=True,
@@ -238,7 +255,7 @@ def main():
238255
),
239256
untagged_vlan=dict(required=False, type="raw"),
240257
tagged_vlans=dict(required=False, type="raw"),
241-
tags=dict(required=False, type=list),
258+
tags=dict(required=False, type="list"),
242259
),
243260
),
244261
)
@@ -247,6 +264,7 @@ def main():
247264
required_if = [
248265
("state", "present", ["device", "name"]),
249266
("state", "absent", ["device", "name"]),
267+
("update_vc_child", True, ["device"]),
250268
]
251269

252270
module = NetboxAnsibleModule(

tests/integration/integration-tests.yml

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@
438438
netbox_token: "0123456789abcdef0123456789abcdef01234567"
439439
data:
440440
device: Test Nexus Child One
441-
name: Ethernet1/1
441+
name: Ethernet2/1
442442
form_factor: 1000Base-T (1GE)
443443
state: present
444444
register: test_nine
@@ -447,14 +447,56 @@
447447
assert:
448448
that:
449449
- test_nine is changed
450-
- test_nine['msg'] == "interface Ethernet1/1 created"
450+
- test_nine['msg'] == "interface Ethernet2/1 created"
451451
- test_nine['diff']['before']['state'] == 'absent'
452452
- test_nine['diff']['after']['state'] == 'present'
453-
- test_nine['interface']['name'] == "Ethernet1/1"
453+
- test_nine['interface']['name'] == "Ethernet2/1"
454454
- test_nine['interface']['device'] == 5
455455
- test_nine['interface']['enabled'] == true
456456
- test_nine['interface']['form_factor'] == 1000
457457

458+
- name: "10 - Update interface on VC child"
459+
netbox_device_interface:
460+
netbox_url: "http://localhost:32768"
461+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
462+
data:
463+
device: Test Nexus One
464+
name: Ethernet2/1
465+
description: "Updated child interface from parent device"
466+
update_vc_child: True
467+
state: present
468+
register: test_ten
469+
470+
- name: "10 - ASSERT"
471+
assert:
472+
that:
473+
- test_ten is changed
474+
- test_ten['msg'] == "interface Ethernet2/1 updated"
475+
- test_ten['diff']['after']['description'] == 'Updated child interface from parent device'
476+
- test_ten['interface']['name'] == "Ethernet2/1"
477+
- test_ten['interface']['device'] == 5
478+
- test_ten['interface']['enabled'] == true
479+
- test_ten['interface']['form_factor'] == 1000
480+
- test_ten['interface']['description'] == 'Updated child interface from parent device'
481+
482+
- name: "11 - Update interface on VC child w/o update_vc_child"
483+
netbox_device_interface:
484+
netbox_url: "http://localhost:32768"
485+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
486+
data:
487+
device: Test Nexus One
488+
name: Ethernet2/1
489+
description: "Updated child interface from parent device - test"
490+
state: present
491+
ignore_errors: yes
492+
register: test_eleven
493+
494+
- name: "11 - ASSERT"
495+
assert:
496+
that:
497+
- test_eleven is failed
498+
- test_eleven['msg'] == "Must set update_vc_child to True to allow child device interface modification"
499+
458500
##
459501
##
460502
### NETBOX_IP_ADDRESS

0 commit comments

Comments
 (0)