Skip to content

Commit 1e5e5b2

Browse files
authored
Add bridge as an option to netbox_device_interface and netbox_vm_interface (#713)
1 parent 6ae20ee commit 1e5e5b2

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

plugins/module_utils/netbox_utils.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from itertools import chain
1616

1717
from ansible.module_utils.common.text.converters import to_text
18-
1918
from ansible.module_utils._text import to_native
2019
from ansible.module_utils.common.collections import is_iterable
2120
from ansible.module_utils.basic import AnsibleModule, missing_required_lib, _load_params
@@ -90,7 +89,12 @@
9089
],
9190
secrets=[],
9291
tenancy=["tenants", "tenant_groups", "contacts", "contact_groups", "contact_roles"],
93-
virtualization=["cluster_groups", "cluster_types", "clusters", "virtual_machines"],
92+
virtualization=[
93+
"cluster_groups",
94+
"cluster_types",
95+
"clusters",
96+
"virtual_machines",
97+
],
9498
wireless=["wireless_lans", "wireless_lan_groups", "wireless_links"],
9599
)
96100

@@ -160,6 +164,7 @@
160164
# Specifies keys within data that need to be converted to ID and the endpoint to be used when queried
161165
CONVERT_TO_ID = {
162166
"assigned_object": "assigned_object",
167+
"bridge": "interfaces",
163168
"circuit": "circuits",
164169
"circuit_type": "circuit_types",
165170
"circuit_termination": "circuit_terminations",
@@ -246,6 +251,7 @@
246251
"virtual_chassis": "virtual_chassis",
247252
"virtual_machine": "virtual_machines",
248253
"virtual_machine_role": "device_roles",
254+
"vm_bridge": "interfaces",
249255
"vlan": "vlans",
250256
"vlan_group": "vlan_groups",
251257
"vlan_role": "roles",
@@ -323,6 +329,7 @@
323329
ALLOWED_QUERY_PARAMS = {
324330
"aggregate": set(["prefix", "rir"]),
325331
"assigned_object": set(["name", "device", "virtual_machine"]),
332+
"bridge": set(["name", "device"]),
326333
"circuit": set(["cid"]),
327334
"circuit_type": set(["slug"]),
328335
"circuit_termination": set(["circuit", "term_side"]),
@@ -422,6 +429,7 @@
422429
"untagged_vlan": set(["group", "name", "site", "vid", "vlan_group", "tenant"]),
423430
"virtual_chassis": set(["name", "master"]),
424431
"virtual_machine": set(["name", "cluster"]),
432+
"vm_bridge": set(["name"]),
425433
"vlan": set(["group", "name", "site", "tenant", "vid", "vlan_group"]),
426434
"vlan_group": set(["slug", "site", "scope"]),
427435
"vrf": set(["name", "tenant"]),
@@ -507,6 +515,7 @@
507515
"virtual_machine_role": "role",
508516
"vlan_role": "role",
509517
"vlan_group": "group",
518+
"vm_bridge": "bridge",
510519
"wireless_lan_group": "group",
511520
}
512521

@@ -851,6 +860,11 @@ def _build_query_params(
851860
if not child:
852861
query_dict["name"] = module_data["parent_vm_interface"]
853862

863+
elif parent == "vm_bridge" and module_data.get("virtual_machine"):
864+
if not child:
865+
query_dict["name"] = module_data["vm_bridge"]
866+
query_dict["virtual_machine_id"] = module_data["virtual_machine"]
867+
854868
elif parent == "lag":
855869
if not child:
856870
query_dict["name"] = module_data["lag"]
@@ -1090,6 +1104,12 @@ def _find_ids(self, data, user_query_params):
10901104
query_params = self._build_query_params(
10911105
k, data, user_query_params
10921106
)
1107+
elif k == "vm_bridge":
1108+
nb_app = getattr(self.nb, "virtualization")
1109+
nb_endpoint = getattr(nb_app, endpoint)
1110+
query_params = self._build_query_params(
1111+
k, data, user_query_params
1112+
)
10931113
else:
10941114
query_params = {QUERY_TYPES.get(k, "q"): search}
10951115
query_id = self._nb_endpoint_get(nb_endpoint, query_params, k)

plugins/modules/netbox_device_interface.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88

99
__metaclass__ = type
1010

11-
ANSIBLE_METADATA = {
12-
"metadata_version": "1.1",
13-
"status": ["preview"],
14-
"supported_by": "community",
15-
}
16-
1711
DOCUMENTATION = r"""
1812
---
1913
module: netbox_device_interface
@@ -76,6 +70,12 @@
7670
- Parent LAG interface will be a member of
7771
required: false
7872
type: raw
73+
bridge:
74+
description:
75+
- Bridge the interface will connected to
76+
required: false
77+
type: raw
78+
version_added: "3.6.0"
7979
mtu:
8080
description:
8181
- The MTU of the interface
@@ -280,6 +280,7 @@ def main():
280280
type=dict(required=False, type="str"),
281281
enabled=dict(required=False, type="bool"),
282282
lag=dict(required=False, type="raw"),
283+
bridge=dict(required=False, type="raw"),
283284
mtu=dict(required=False, type="int"),
284285
mac_address=dict(required=False, type="str"),
285286
mgmt_only=dict(required=False, type="bool"),

plugins/modules/netbox_vm_interface.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77

88
__metaclass__ = type
99

10-
ANSIBLE_METADATA = {
11-
"metadata_version": "1.1",
12-
"status": ["preview"],
13-
"supported_by": "community",
14-
}
15-
1610
DOCUMENTATION = r"""
1711
---
1812
module: netbox_vm_interface
@@ -69,6 +63,12 @@
6963
- The mode of the interface
7064
required: false
7165
type: raw
66+
vm_bridge:
67+
description:
68+
- The bridge the interface is connected to
69+
required: false
70+
type: raw
71+
version_added: "3.6.0"
7272
parent_vm_interface:
7373
description:
7474
- The virtual machine interface's parent interface.
@@ -144,6 +144,25 @@
144144
mtu: 1600
145145
mode: Tagged
146146
state: present
147+
148+
- name: Create bridge interface within NetBox
149+
netbox_vm_interface:
150+
netbox_url: http://netbox.local
151+
netbox_token: thisIsMyToken
152+
data:
153+
virtual_machine: test100
154+
name: br1000
155+
state: present
156+
157+
- name: Connect bridge interface within NetBox
158+
netbox_vm_interface:
159+
netbox_url: http://netbox.local
160+
netbox_token: thisIsMyToken
161+
data:
162+
virtual_machine: test100
163+
name: br1001
164+
vm_bridge: br1000
165+
state: present
147166
"""
148167

149168
RETURN = r"""
@@ -186,6 +205,7 @@ def main():
186205
mac_address=dict(required=False, type="str"),
187206
description=dict(required=False, type="str"),
188207
mode=dict(required=False, type="raw"),
208+
vm_bridge=dict(required=False, type="raw"),
189209
parent_vm_interface=dict(required=False, type="raw"),
190210
untagged_vlan=dict(required=False, type="raw"),
191211
tagged_vlans=dict(required=False, type="raw"),

0 commit comments

Comments
 (0)