Skip to content

Commit 4398a21

Browse files
committed
feat: begin work on support for tunnel terminations
1 parent d2d9c71 commit 4398a21

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed

plugins/module_utils/netbox_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
"tunnels": {"introduced": "3.7"},
135135
"tunnel_groups": {"introduced": "3.7"},
136136
"ipsec_profiles": {"introduced": "3.7"},
137+
"tunnel_terminations": {"introduced": "3.7"},
137138
},
138139
)
139140

@@ -318,6 +319,7 @@
318319
"tenant_groups": "tenant_groups",
319320
"termination_a": "interfaces",
320321
"termination_b": "interfaces",
322+
"tunnel": "tunnels",
321323
"tunnel_group": "tunnel_groups",
322324
"untagged_vlan": "vlans",
323325
"virtual_chassis": "virtual_chassis",
@@ -407,6 +409,7 @@
407409
"tenant_groups": "tenant_group",
408410
"tunnels": "tunnel",
409411
"tunnel_groups": "tunnel_group",
412+
"tunnel_terminations": "tunnel_termination",
410413
"virtual_chassis": "virtual_chassis",
411414
"virtual_machines": "virtual_machine",
412415
"virtual_disks": "virtual_disk",
@@ -547,6 +550,7 @@
547550
"termination_b": set(["name", "device", "virtual_machine"]),
548551
"tunnel": set(["name"]),
549552
"tunnel_group": set(["slug"]),
553+
"tunnel_termination": set([name]),
550554
"untagged_vlan": set(["group", "name", "site", "vid", "vlan_group", "tenant"]),
551555
"virtual_chassis": set(["name", "master"]),
552556
"virtual_machine": set(["name", "cluster"]),

plugins/module_utils/netbox_vpn.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
NB_L2VPN_TERMINATIONS = "l2vpn_terminations"
2020
NB_TUNNELS = "tunnels"
2121
NB_TUNNEL_GROUPS = "tunnel_groups"
22+
NB_TUNNEL_TERMINATIONS = "tunnel_terminations"
2223

2324

2425
class NetboxVpnModule(NetboxModule):
@@ -34,6 +35,7 @@ def run(self):
3435
- l2vpn_terminations
3536
- tunnels
3637
- tunnel_groups
38+
- tunnel_terminations
3739
"""
3840
# Used to dynamically set key when returning results
3941
endpoint_name = ENDPOINT_NAME_MAPPING[self.endpoint]
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# Copyright: (c) 2024, Rich Bibby, NetBox Labs (@richbibby)
4+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
6+
from __future__ import absolute_import, division, print_function
7+
8+
__metaclass__ = type
9+
10+
DOCUMENTATION = r"""
11+
---
12+
module: netbox_tunnel_termination
13+
short_description: Create, update or delete Tunnel terminations within NetBox
14+
description:
15+
- Creates, updates or removes tunnel terminations from NetBox
16+
notes:
17+
- Tags should be defined as a YAML list
18+
- This should be ran with connection C(local) and hosts C(localhost)
19+
author:
20+
- Rich Bibby, NetBox Labs (@richbibby)
21+
requirements:
22+
- pynetbox
23+
seealso:
24+
- name: Tunnel Termination reference
25+
description: NetBox Documentation for Tunnel Termination model.
26+
link: https://netboxlabs.com/docs/netbox/en/stable/models/vpn/tunneltermination/
27+
version_added: '3.20.0'
28+
extends_documentation_fragment:
29+
- netbox.netbox.common
30+
options:
31+
data:
32+
type: dict
33+
description:
34+
- Defines the L2VPN termination configuration
35+
suboptions:
36+
l2vpn:
37+
description:
38+
- L2vpn object id
39+
required: true
40+
type: int
41+
assigned_object_type:
42+
description:
43+
- Assigned object type
44+
required: true
45+
choices:
46+
- dcim.interface
47+
- ipam.vlan
48+
- virtualization.vminterface
49+
type: str
50+
assigned_object_id:
51+
description:
52+
- Assigned object id
53+
required: true
54+
type: int
55+
tags:
56+
description:
57+
- Any tags that the L2VPN termination may need to be associated with
58+
required: false
59+
type: list
60+
elements: raw
61+
custom_fields:
62+
description:
63+
- Must exist in NetBox
64+
required: false
65+
type: dict
66+
required: true
67+
"""
68+
69+
EXAMPLES = r"""
70+
- name: "Test NetBox Tunnel Termination module"
71+
connection: local
72+
hosts: localhost
73+
gather_facts: false
74+
75+
tasks:
76+
- name: Create tunnel termination within NetBox with only required information
77+
netbox.netbox.netbox_tunnel_termination:
78+
netbox_url: http://netbox.local
79+
netbox_token: thisIsMyToken
80+
data:
81+
state: present
82+
83+
- name: Delete tunnel termination within NetBox
84+
netbox.netbox.netbox_tunnel_termination:
85+
netbox_url: http://netbox.local
86+
netbox_token: thisIsMyToken
87+
data:
88+
state: absent
89+
90+
- name: Create tunnel termination with all information
91+
netbox.netbox.netbox_tunnel_termination:
92+
netbox_url: http://netbox.local
93+
netbox_token: thisIsMyToken
94+
data:
95+
state: present
96+
"""
97+
98+
RETURN = r"""
99+
tunnel termination:
100+
description: Serialized object as created or already existent within NetBox
101+
returned: success (when I(state=present))
102+
type: dict
103+
msg:
104+
description: Message indicating failure or info about what has been achieved
105+
returned: always
106+
type: str
107+
"""
108+
109+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
110+
NetboxAnsibleModule,
111+
NETBOX_ARG_SPEC,
112+
)
113+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_vpn import (
114+
NetboxVpnModule,
115+
NB_TUNNEL_TERMINATIONS,
116+
)
117+
from copy import deepcopy
118+
119+
120+
def main():
121+
"""
122+
Main entry point for module execution
123+
"""
124+
argument_spec = deepcopy(NETBOX_ARG_SPEC)
125+
argument_spec.update(
126+
dict(
127+
data=dict(
128+
type="dict",
129+
required=True,
130+
options=dict(
131+
tunnel=dict(required=True, type="str"),
132+
# status=dict(required=False, type="raw"),
133+
# tunnel_group=dict(required=False, type="raw"),
134+
# encapsulation=dict(
135+
# required=True,
136+
# type="str",
137+
# choices=[
138+
# "ipsec-transport",
139+
# "ipsec-tunnel",
140+
# "ip-ip",
141+
# "gre",
142+
# ],
143+
# ),
144+
# ipsec_profile=dict(required=False, type="raw"),
145+
# tenant=dict(required=False, type="raw"),
146+
# tunnel_id=dict(required=False, type="int"),
147+
# description=dict(required=False, type="str"),
148+
# comments=dict(required=False, type="str"),
149+
# tags=dict(required=False, type="list", elements="raw"),
150+
# custom_fields=dict(required=False, type="dict"),
151+
),
152+
),
153+
)
154+
)
155+
156+
required_if = [
157+
("state", "present", ["name"]),
158+
("state", "absent", ["name"]),
159+
]
160+
161+
module = NetboxAnsibleModule(
162+
argument_spec=argument_spec, supports_check_mode=True, required_if=required_if
163+
)
164+
165+
netbox_tunnel_termination = NetboxVpnModule(module, NB_TUNNEL_TERMINATIONS)
166+
netbox_tunnel_termination.run()
167+
168+
169+
if __name__ == "__main__": # pragma: no cover
170+
main()

0 commit comments

Comments
 (0)