Skip to content

Commit 6339c4b

Browse files
committed
feat: Add netbox_tunnel_group code
1 parent 076f4d5 commit 6339c4b

File tree

8 files changed

+319
-0
lines changed

8 files changed

+319
-0
lines changed

meta/runtime.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ action_groups:
7979
- netbox_tenant
8080
- netbox_tenant_group
8181
- netbox_tunnel
82+
- netbox_tunnel_group
8283
- netbox_virtual_chassis
8384
- netbox_virtual_machine
8485
- netbox_vlan

plugins/module_utils/netbox_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@
406406
"tenants": "tenant",
407407
"tenant_groups": "tenant_group",
408408
"tunnels": "tunnel",
409+
"tunnel_groups": "tunnel_group",
409410
"virtual_chassis": "virtual_chassis",
410411
"virtual_machines": "virtual_machine",
411412
"virtual_disks": "virtual_disk",
@@ -545,6 +546,7 @@
545546
"termination_a": set(["name", "device", "virtual_machine"]),
546547
"termination_b": set(["name", "device", "virtual_machine"]),
547548
"tunnel": set(["name"]),
549+
"tunnel_group": set(["slug"]),
548550
"untagged_vlan": set(["group", "name", "site", "vid", "vlan_group", "tenant"]),
549551
"virtual_chassis": set(["name", "master"]),
550552
"virtual_machine": set(["name", "cluster"]),
@@ -669,6 +671,7 @@
669671
"tags",
670672
"tenants",
671673
"tenant_groups",
674+
"tunnel_groups",
672675
"manufacturers",
673676
"platforms",
674677
"providers",

plugins/module_utils/netbox_vpn.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
NB_L2VPNS = "l2vpns"
1919
NB_L2VPN_TERMINATIONS = "l2vpn_terminations"
2020
NB_TUNNELS = "tunnels"
21+
NB_TUNNEL_GROUPS = "tunnel_groups"
2122

2223

2324
class NetboxVpnModule(NetboxModule):
@@ -32,6 +33,7 @@ def run(self):
3233
- l2vpns
3334
- l2vpn_terminations
3435
- tunnels
36+
- tunnel_groups
3537
"""
3638
# Used to dynamically set key when returning results
3739
endpoint_name = ENDPOINT_NAME_MAPPING[self.endpoint]
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# Copyright: (c) 2024, Rich Bibby (@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_group
13+
short_description: Create, update or delete tunnel groups within NetBox
14+
description:
15+
- Creates, updates or deletes tunnel groups 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 (@richbibby)
21+
requirements:
22+
- pynetbox
23+
version_added: '3.20.0'
24+
extends_documentation_fragment:
25+
- netbox.netbox.common
26+
options:
27+
data:
28+
required: true
29+
type: dict
30+
description:
31+
- Defines the tunnel group configuration
32+
suboptions:
33+
name:
34+
description:
35+
- The name of the tunnel group
36+
required: true
37+
type: str
38+
description:
39+
description:
40+
- The description of the tunnel group
41+
required: false
42+
type: str
43+
tags:
44+
description:
45+
- The tags to add/update
46+
required: false
47+
type: list
48+
elements: raw
49+
custom_fields:
50+
description:
51+
- Must exist in NetBox
52+
required: false
53+
type: dict
54+
"""
55+
56+
EXAMPLES = r"""
57+
- name: "Test NetBox Tunnel Group module"
58+
connection: local
59+
hosts: localhost
60+
gather_facts: false
61+
62+
tasks:
63+
- name: Create tunnel group within NetBox with only required information
64+
netbox.netbox.netbox_tunnel_group:
65+
netbox_url: http://netbox.local
66+
netbox_token: thisIsMyToken
67+
data:
68+
name: Test Cluster Group
69+
state: present
70+
71+
- name: Delete tunnel group within netbox
72+
netbox.netbox.netbox_tunnel_group:
73+
netbox_url: http://netbox.local
74+
netbox_token: thisIsMyToken
75+
data:
76+
name: Test Cluster Group
77+
state: absent
78+
"""
79+
80+
RETURN = r"""
81+
tunnel_group:
82+
description: Serialized object as created or already existent within NetBox
83+
returned: success (when I(state=present))
84+
type: dict
85+
msg:
86+
description: Message indicating failure or info about what has been achieved
87+
returned: always
88+
type: str
89+
"""
90+
91+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
92+
NetboxAnsibleModule,
93+
NETBOX_ARG_SPEC,
94+
)
95+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_vpn import (
96+
NetboxVpnModule,
97+
NB_TUNNEL_GROUPS,
98+
)
99+
from copy import deepcopy
100+
101+
102+
def main():
103+
"""
104+
Main entry point for module execution
105+
"""
106+
argument_spec = deepcopy(NETBOX_ARG_SPEC)
107+
argument_spec.update(
108+
dict(
109+
data=dict(
110+
type="dict",
111+
required=True,
112+
options=dict(
113+
name=dict(required=True, type="str"),
114+
description=dict(required=False, type="str"),
115+
tags=dict(required=False, type="list", elements="raw"),
116+
custom_fields=dict(required=False, type="dict"),
117+
),
118+
),
119+
)
120+
)
121+
122+
required_if = [("state", "present", ["name"]), ("state", "absent", ["name"])]
123+
124+
module = NetboxAnsibleModule(
125+
argument_spec=argument_spec, supports_check_mode=True, required_if=required_if
126+
)
127+
128+
netbox_tunnel_group = NetboxVpnModule(module, NB_TUNNEL_GROUPS)
129+
netbox_tunnel_group.run()
130+
131+
132+
if __name__ == "__main__": # pragma: no cover
133+
main()

tests/integration/targets/v3.7/tasks/main.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,12 @@
325325
- netbox_tunnel
326326
tags:
327327
- netbox_tunnel
328+
329+
- name: NETBOX_TUNNEL GROUP TESTS
330+
ansible.builtin.include_tasks:
331+
file: netbox_tunnel_group.yml
332+
apply:
333+
tags:
334+
- netbox_tunnel_group
335+
tags:
336+
- netbox_tunnel_group
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
##
3+
##
4+
### NETBOX_TUNNEL GROUP
5+
##
6+
##
7+
- name: "TUNNEL_GROUP 1: Necessary info creation"
8+
netbox.netbox.netbox_tunnel_group:
9+
netbox_url: http://localhost:32768
10+
netbox_token: "6bd1da4f7657a0748667f64b5786710383627d9a"
11+
data:
12+
name: Test Tunnel Group
13+
state: present
14+
register: test_one
15+
16+
- name: "TUNNEL_GROUP 1: ASSERT - Necessary info creation"
17+
ansible.builtin.assert:
18+
that:
19+
- test_one is changed
20+
- test_one['diff']['before']['state'] == "absent"
21+
- test_one['diff']['after']['state'] == "present"
22+
- test_one['tunnel_group']['name'] == "Test Tunnel Group"
23+
- test_one['tunnel_group']['slug'] == "test-tunnel-group"
24+
- test_one['msg'] == "tunnel_group Test Tunnel Group created"
25+
26+
- name: "TUNNEL_GROUP 2: Create duplicate"
27+
netbox.netbox.netbox_tunnel_group:
28+
netbox_url: http://localhost:32768
29+
netbox_token: "6bd1da4f7657a0748667f64b5786710383627d9a"
30+
data:
31+
name: Test Tunnel Group
32+
state: present
33+
register: test_two
34+
35+
- name: "TUNNEL_GROUP 2: ASSERT - Create duplicate"
36+
ansible.builtin.assert:
37+
that:
38+
- not test_two['changed']
39+
- test_two['tunnel_group']['name'] == "Test Tunnel Group"
40+
- test_two['tunnel_group']['slug'] == "test-tunnel-group"
41+
- test_two['msg'] == "tunnel_group Test Tunnel Group already exists"
42+
43+
- name: "TUNNEL_GROUP 3: Update Existing"
44+
netbox.netbox.netbox_tunnel_group:
45+
netbox_url: http://localhost:32768
46+
netbox_token: "6bd1da4f7657a0748667f64b5786710383627d9a"
47+
data:
48+
name: Test Tunnel Group
49+
description: Test Description
50+
tags:
51+
- Schnozzberry
52+
state: present
53+
register: test_three
54+
55+
- name: "TUNNEL_GROUP 3: ASSERT - Updated"
56+
ansible.builtin.assert:
57+
that:
58+
- test_three is changed
59+
- test_three['diff']['after']['description'] == "Test Description"
60+
- test_three['diff']['after']['tags'][0] == 4
61+
- test_three['tunnel_group']['name'] == "Test Tunnel Group"
62+
- test_three['tunnel_group']['description'] == "Test Description"
63+
- test_three['tunnel_group']['tags'][0] == 4
64+
- test_three['msg'] == "tunnel_group Test Tunnel Group updated"
65+
66+
- name: "TUNNEL_GROUP 4: Delete"
67+
netbox.netbox.netbox_tunnel_group:
68+
netbox_url: http://localhost:32768
69+
netbox_token: "6bd1da4f7657a0748667f64b5786710383627d9a"
70+
data:
71+
name: Test Tunnel Group
72+
state: absent
73+
register: test_four
74+
75+
- name: "TUNNEL_GROUP 4: ASSERT - Delete"
76+
ansible.builtin.assert:
77+
that:
78+
- test_four is changed
79+
- test_four['diff']['before']['state'] == "present"
80+
- test_four['diff']['after']['state'] == "absent"
81+
- test_four['msg'] == "tunnel_group Test Tunnel Group deleted"

tests/integration/targets/v4.0/tasks/main.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,12 @@
337337
- netbox_tunnel
338338
tags:
339339
- netbox_tunnel
340+
341+
- name: NETBOX_TUNNEL GROUP TESTS
342+
ansible.builtin.include_tasks:
343+
file: netbox_tunnel_group.yml
344+
apply:
345+
tags:
346+
- netbox_tunnel_group
347+
tags:
348+
- netbox_tunnel_group
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
##
3+
##
4+
### NETBOX_TUNNEL GROUP
5+
##
6+
##
7+
- name: "TUNNEL_GROUP 1: Necessary info creation"
8+
netbox.netbox.netbox_tunnel_group:
9+
netbox_url: http://localhost:32768
10+
netbox_token: "6bd1da4f7657a0748667f64b5786710383627d9a"
11+
data:
12+
name: Test Tunnel Group
13+
state: present
14+
register: test_one
15+
16+
- name: "TUNNEL_GROUP 1: ASSERT - Necessary info creation"
17+
ansible.builtin.assert:
18+
that:
19+
- test_one is changed
20+
- test_one['diff']['before']['state'] == "absent"
21+
- test_one['diff']['after']['state'] == "present"
22+
- test_one['tunnel_group']['name'] == "Test Tunnel Group"
23+
- test_one['tunnel_group']['slug'] == "test-tunnel-group"
24+
- test_one['msg'] == "tunnel_group Test Tunnel Group created"
25+
26+
- name: "TUNNEL_GROUP 2: Create duplicate"
27+
netbox.netbox.netbox_tunnel_group:
28+
netbox_url: http://localhost:32768
29+
netbox_token: "6bd1da4f7657a0748667f64b5786710383627d9a"
30+
data:
31+
name: Test Tunnel Group
32+
state: present
33+
register: test_two
34+
35+
- name: "TUNNEL_GROUP 2: ASSERT - Create duplicate"
36+
ansible.builtin.assert:
37+
that:
38+
- not test_two['changed']
39+
- test_two['tunnel_group']['name'] == "Test Tunnel Group"
40+
- test_two['tunnel_group']['slug'] == "test-tunnel-group"
41+
- test_two['msg'] == "tunnel_group Test Tunnel Group already exists"
42+
43+
- name: "TUNNEL_GROUP 3: Update Existing"
44+
netbox.netbox.netbox_tunnel_group:
45+
netbox_url: http://localhost:32768
46+
netbox_token: "6bd1da4f7657a0748667f64b5786710383627d9a"
47+
data:
48+
name: Test Tunnel Group
49+
description: Test Description
50+
tags:
51+
- Schnozzberry
52+
state: present
53+
register: test_three
54+
55+
- name: "TUNNEL_GROUP 3: ASSERT - Updated"
56+
ansible.builtin.assert:
57+
that:
58+
- test_three is changed
59+
- test_three['diff']['after']['description'] == "Test Description"
60+
- test_three['diff']['after']['tags'][0] == 4
61+
- test_three['tunnel_group']['name'] == "Test Tunnel Group"
62+
- test_three['tunnel_group']['description'] == "Test Description"
63+
- test_three['tunnel_group']['tags'][0] == 4
64+
- test_three['msg'] == "tunnel_group Test Tunnel Group updated"
65+
66+
- name: "TUNNEL_GROUP 4: Delete"
67+
netbox.netbox.netbox_tunnel_group:
68+
netbox_url: http://localhost:32768
69+
netbox_token: "6bd1da4f7657a0748667f64b5786710383627d9a"
70+
data:
71+
name: Test Tunnel Group
72+
state: absent
73+
register: test_four
74+
75+
- name: "TUNNEL_GROUP 4: ASSERT - Delete"
76+
ansible.builtin.assert:
77+
that:
78+
- test_four is changed
79+
- test_four['diff']['before']['state'] == "present"
80+
- test_four['diff']['after']['state'] == "absent"
81+
- test_four['msg'] == "tunnel_group Test Tunnel Group deleted"

0 commit comments

Comments
 (0)