Skip to content

Commit febe59d

Browse files
ThomasADavisThomas Davis
andauthored
Allows the addition of a parent to an interface. (#604)
* This allows the addition of the parent to an interface. Co-authored-by: Thomas Davis <tdavis@nersc.gov>
1 parent 8b43a73 commit febe59d

File tree

5 files changed

+93
-3
lines changed

5 files changed

+93
-3
lines changed

plugins/module_utils/netbox_utils.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@
177177
"nat_inside": "ip_addresses",
178178
"nat_outside": "ip_addresses",
179179
"platform": "platforms",
180+
"parent_interface": "interfaces",
181+
"parent_vm_interface": "interfaces",
180182
"parent_region": "regions",
181183
"parent_tenant_group": "tenant_groups",
182184
"power_panel": "power_panels",
@@ -314,6 +316,8 @@
314316
"manufacturer": set(["slug"]),
315317
"master": set(["name"]),
316318
"nat_inside": set(["vrf", "address"]),
319+
"parent_interface": set(["name"]),
320+
"parent_vm_interface": set(["name"]),
317321
"parent_region": set(["slug"]),
318322
"parent_tenant_group": set(["slug"]),
319323
"platform": set(["slug"]),
@@ -406,6 +410,8 @@
406410
"circuit_type": "type",
407411
"cluster_type": "type",
408412
"cluster_group": "group",
413+
"parent_interface": "parent",
414+
"parent_vm_interface": "parent",
409415
"parent_region": "parent",
410416
"parent_tenant_group": "parent",
411417
"power_port_template": "power_port",
@@ -733,6 +739,23 @@ def _build_query_params(
733739
# This is to skip any potential changes using module_data when the user
734740
# provides user_query_params
735741
pass
742+
743+
elif parent == "prefix" and module_data.get("parent"):
744+
query_dict.update({"prefix": module_data["parent"]})
745+
746+
elif parent == "parent_interface" and module_data.get("device"):
747+
if not child:
748+
query_dict["name"] = module_data.get("parent_interface")
749+
750+
if isinstance(module_data.get("device"), int):
751+
query_dict.update({"device_id": module_data.get("device")})
752+
else:
753+
query_dict.update({"device": module_data.get("device")})
754+
755+
elif parent == "parent_vm_interface" and module_data.get("virtual_machine"):
756+
if not child:
757+
query_dict["name"] = module_data["parent_vm_interface"]
758+
736759
elif parent == "lag":
737760
if not child:
738761
query_dict["name"] = module_data["lag"]
@@ -745,9 +768,6 @@ def _build_query_params(
745768
else:
746769
query_dict.update({"device": module_data["device"]})
747770

748-
elif parent == "prefix" and module_data.get("parent"):
749-
query_dict.update({"prefix": module_data["parent"]})
750-
751771
elif parent == "ip_addresses":
752772
if isinstance(module_data["device"], int):
753773
query_dict.update({"device_id": module_data["device"]})
@@ -937,6 +957,7 @@ def _find_ids(self, data, user_query_params):
937957
else:
938958
if k in [
939959
"lag",
960+
"parent_interface",
940961
"rear_port",
941962
"rear_port_template",
942963
"power_port",
@@ -951,6 +972,12 @@ def _find_ids(self, data, user_query_params):
951972
ENDPOINT_NAME_MAPPING[endpoint], "q"
952973
): search
953974
}
975+
elif k == "parent_vm_interface":
976+
nb_app = getattr(self.nb, "virtualization")
977+
nb_endpoint = getattr(nb_app, endpoint)
978+
query_params = self._build_query_params(
979+
k, data, user_query_params
980+
)
954981
else:
955982
query_params = {QUERY_TYPES.get(k, "q"): search}
956983
query_id = self._nb_endpoint_get(nb_endpoint, query_params, k)

plugins/modules/netbox_device_interface.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@
114114
- The mode of the interface
115115
required: false
116116
type: raw
117+
parent_interface:
118+
description:
119+
- the device's parent interface
120+
required: false
121+
type: raw
117122
untagged_vlan:
118123
description:
119124
- The untagged VLAN to be assigned to interface
@@ -313,6 +318,7 @@ def main():
313318
mgmt_only=dict(required=False, type="bool"),
314319
description=dict(required=False, type="str"),
315320
mode=dict(required=False, type="raw"),
321+
parent_interface=dict(required=False, type="raw"),
316322
untagged_vlan=dict(required=False, type="raw"),
317323
tagged_vlans=dict(required=False, type="raw"),
318324
tags=dict(required=False, type="list", elements="raw"),

plugins/modules/netbox_vm_interface.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
- The mode of the interface
8383
required: false
8484
type: raw
85+
parent_vm_interface:
86+
description:
87+
- the vm interface's parent interface.
88+
required: false
89+
type: raw
8590
untagged_vlan:
8691
description:
8792
- The untagged VLAN to be assigned to interface
@@ -208,6 +213,7 @@ def main():
208213
mac_address=dict(required=False, type="str"),
209214
description=dict(required=False, type="str"),
210215
mode=dict(required=False, type="raw"),
216+
parent_vm_interface=dict(required=False, type="raw"),
211217
untagged_vlan=dict(required=False, type="raw"),
212218
tagged_vlans=dict(required=False, type="raw"),
213219
tags=dict(required=False, type="list", elements="raw"),

tests/integration/targets/v2.11/tasks/netbox_device_interface.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,23 @@
311311
- test_twelve['interface']['name'] == "GigabitEthernet5"
312312
- test_twelve['interface']['device'] == 1
313313
- test_twelve['interface']['mark_connected'] == true
314+
315+
- name: "13 - Create a child virtual device GigabitEthernet5."
316+
netbox.netbox.netbox_device_interface:
317+
netbox_url: "http://localhost:32768"
318+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
319+
data:
320+
device: test100
321+
name: Child1
322+
type: virtual
323+
parent_interface: GigabitEthernet5
324+
register: test_thirteen
325+
326+
- name: "13- ASSERT"
327+
assert:
328+
that:
329+
- test_thirteen is changed
330+
- test_thirteen['msg'] == "interface Child1 created"
331+
- test_thirteen['diff']['before']['state'] == 'absent'
332+
- test_thirteen['diff']['after']['state'] == 'present'
333+
- test_thirteen['interface']['parent'] == test_twelve['interface']['id']

tests/integration/targets/v2.11/tasks/netbox_vm_interface.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,34 @@
157157
- test_five['interface']['tagged_vlans'] == [2, 3]
158158
- test_five['interface']['tags'][0] == 4
159159
- test_five['msg'] == "interface Eth0 updated"
160+
161+
- name: "NETBOX_VM_INTERFACE 6: Create a parent interface"
162+
netbox.netbox.netbox_vm_interface:
163+
netbox_url: http://localhost:32768
164+
netbox_token: 0123456789abcdef0123456789abcdef01234567
165+
data:
166+
virtual_machine: "test100-vm"
167+
name: "Eth10"
168+
state: present
169+
register: test_parent
170+
171+
- name: "NETBOX_VM_INTERFACE 6: Create a child virtual device Eth10."
172+
netbox.netbox.netbox_vm_interface:
173+
netbox_url: http://localhost:32768
174+
netbox_token: 0123456789abcdef0123456789abcdef01234567
175+
data:
176+
virtual_machine: "test100-vm"
177+
name: "Child1"
178+
parent_vm_interface: "Eth10"
179+
state: present
180+
register: test_child
181+
182+
- name: "NETBOX_VM_INTERFACE 6: ASSERT"
183+
assert:
184+
that:
185+
- test_child is changed
186+
- test_child['msg'] == "interface Child1 created"
187+
- test_child['diff']['before']['state'] == 'absent'
188+
- test_child['diff']['after']['state'] == 'present'
189+
- test_child['interface']['parent'] == test_parent['interface']['id']
190+

0 commit comments

Comments
 (0)