Skip to content

Commit 0c17391

Browse files
committed
Update netbox_l2vpn and netbox_l2vpn_termination modules
1 parent 404795c commit 0c17391

File tree

4 files changed

+212
-88
lines changed

4 files changed

+212
-88
lines changed

plugins/module_utils/netbox_utils.py

Lines changed: 110 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -32,89 +32,103 @@
3232

3333
# Used to map endpoints to applications dynamically
3434
API_APPS_ENDPOINTS = dict(
35-
circuits=[
36-
"circuits",
37-
"circuit_types",
38-
"circuit_terminations",
39-
"providers",
40-
"provider_networks",
41-
],
42-
dcim=[
43-
"cables",
44-
"console_ports",
45-
"console_port_templates",
46-
"console_server_ports",
47-
"console_server_port_templates",
48-
"device_bays",
49-
"device_bay_templates",
50-
"devices",
51-
"device_roles",
52-
"device_types",
53-
"front_ports",
54-
"front_port_templates",
55-
"interfaces",
56-
"interface_templates",
57-
"inventory_items",
58-
"inventory_item_roles",
59-
"locations",
60-
"manufacturers",
61-
"module_types",
62-
"platforms",
63-
"power_feeds",
64-
"power_outlets",
65-
"power_outlet_templates",
66-
"power_panels",
67-
"power_ports",
68-
"power_port_templates",
69-
"racks",
70-
"rack_groups",
71-
"rack_roles",
72-
"rear_ports",
73-
"rear-ports",
74-
"rear_port_templates",
75-
"regions",
76-
"sites",
77-
"site_groups",
78-
"virtual_chassis",
79-
],
80-
extras=[
81-
"config_contexts",
82-
"config_templates",
83-
"tags",
84-
"custom_fields",
85-
"custom_links",
86-
"export_templates",
87-
"journal_entries",
88-
"webhooks",
89-
],
90-
ipam=[
91-
"aggregates",
92-
"asns",
93-
"fhrp_groups",
94-
"fhrp_group_assignments",
95-
"ip_addresses",
96-
"l2vpns",
97-
"l2vpn_terminations",
98-
"prefixes",
99-
"rirs",
100-
"roles",
101-
"route_targets",
102-
"service_templates",
103-
"vlans",
104-
"vlan_groups",
105-
"vrfs",
106-
"services",
107-
],
108-
secrets=[],
109-
tenancy=["tenants", "tenant_groups", "contacts", "contact_groups", "contact_roles"],
110-
virtualization=[
111-
"cluster_groups",
112-
"cluster_types",
113-
"clusters",
114-
"virtual_machines",
115-
"virtual_disks",
116-
],
117-
wireless=["wireless_lans", "wireless_lan_groups", "wireless_links"],
35+
circuits={
36+
"circuits": {},
37+
"circuit_types": {},
38+
"circuit_terminations": {},
39+
"providers": {},
40+
"provider_networks": {},
41+
},
42+
dcim={
43+
"cables": {},
44+
"console_ports": {},
45+
"console_port_templates": {},
46+
"console_server_ports": {},
47+
"console_server_port_templates": {},
48+
"device_bays": {},
49+
"device_bay_templates": {},
50+
"devices": {},
51+
"device_roles": {},
52+
"device_types": {},
53+
"front_ports": {},
54+
"front_port_templates": {},
55+
"interfaces": {},
56+
"interface_templates": {},
57+
"inventory_items": {},
58+
"inventory_item_roles": {},
59+
"locations": {},
60+
"manufacturers": {},
61+
"module_types": {},
62+
"platforms": {},
63+
"power_feeds": {},
64+
"power_outlets": {},
65+
"power_outlet_templates": {},
66+
"power_panels": {},
67+
"power_ports": {},
68+
"power_port_templates": {},
69+
"racks": {},
70+
"rack_groups": {},
71+
"rack_roles": {},
72+
"rear_ports": {},
73+
"rear-ports": {},
74+
"rear_port_templates": {},
75+
"regions": {},
76+
"sites": {},
77+
"site_groups": {},
78+
"virtual_chassis": {},
79+
},
80+
extras={
81+
"config_contexts": {},
82+
"config_templates": {},
83+
"tags": {},
84+
"custom_fields": {},
85+
"custom_links": {},
86+
"export_templates": {},
87+
"journal_entries": {},
88+
"webhooks": {},
89+
},
90+
ipam={
91+
"aggregates": {},
92+
"asns": {},
93+
"fhrp_groups": {},
94+
"fhrp_group_assignments": {},
95+
"ip_addresses": {},
96+
"l2vpns": {"deprecated": "3.7"},
97+
"l2vpn_terminations": {"deprecated": "3.7"},
98+
"prefixes": {},
99+
"rirs": {},
100+
"roles": {},
101+
"route_targets": {},
102+
"service_templates": {},
103+
"vlans": {},
104+
"vlan_groups": {},
105+
"vrfs": {},
106+
"services": {},
107+
},
108+
secrets={},
109+
tenancy={
110+
"tenants": {},
111+
"tenant_groups": {},
112+
"contacts": {},
113+
"contact_groups": {},
114+
"contact_roles": {},
115+
},
116+
virtualization={
117+
"cluster_groups": {},
118+
"cluster_types": {},
119+
"clusters": {},
120+
"virtual_machines": {},
121+
"virtual_disks": {},
122+
},
123+
wireless={
124+
"wireless_lans": {},
125+
"wireless_lan_groups": {},
126+
"wireless_links": {},
127+
},
128+
vpn={
129+
"l2vpns": {"introduced": "3.7"},
130+
"l2vpn_terminations": {"introduced": "3.7"},
131+
},
118132
)
119133

120134
# Used to normalize data for the respective query types used to find endpoints
@@ -1122,7 +1136,19 @@ def _find_app(self, endpoint):
11221136
"""
11231137
nb_app = None
11241138
for k, v in API_APPS_ENDPOINTS.items():
1125-
if endpoint in v:
1139+
if endpoint in v.keys():
1140+
if "introduced" in v[endpoint]:
1141+
pre_introduction = self._version_check_greater(
1142+
v[endpoint]["introduced"], self.version
1143+
)
1144+
if pre_introduction:
1145+
continue
1146+
if "deprecated" in v[endpoint]:
1147+
after_deprecation = self._version_check_greater(
1148+
self.version, v[endpoint]["deprecated"], greater_or_equal=True
1149+
)
1150+
if after_deprecation:
1151+
continue
11261152
nb_app = k
11271153

11281154
if nb_app:

plugins/module_utils/netbox_vpn.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright: (c) 2024, Fred De Backer (@freddebacker) <[email protected]>
3+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4+
from __future__ import absolute_import, division, print_function
5+
6+
__metaclass__ = type
7+
8+
# Import necessary packages
9+
from ipaddress import ip_interface
10+
from ansible.module_utils._text import to_text
11+
12+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
13+
NetboxModule,
14+
ENDPOINT_NAME_MAPPING,
15+
SLUG_REQUIRED,
16+
)
17+
18+
19+
NB_L2VPNS = "l2vpns"
20+
NB_L2VPN_TERMINATIONS = "l2vpn_terminations"
21+
22+
23+
class NetboxVpnModule(NetboxModule):
24+
def __init__(self, module, endpoint):
25+
super().__init__(module, endpoint)
26+
27+
def run(self):
28+
"""
29+
This function should have all necessary code for endpoints within the application
30+
to create/update/delete the endpoint objects
31+
Supported endpoints:
32+
- l2vpns
33+
- l2vpn_terminations
34+
"""
35+
# Used to dynamically set key when returning results
36+
endpoint_name = ENDPOINT_NAME_MAPPING[self.endpoint]
37+
38+
self.result = {"changed": False}
39+
40+
application = self._find_app(self.endpoint)
41+
nb_app = getattr(self.nb, application)
42+
nb_endpoint = getattr(nb_app, self.endpoint)
43+
user_query_params = self.module.params.get("query_params")
44+
45+
data = self.data
46+
47+
if self.endpoint == "l2vpn_terminations":
48+
name = "l2vpn %s <> %s %s" % (
49+
data.get("l2vpn"),
50+
data.get("assigned_object_type"),
51+
data.get("assigned_object_id"),
52+
)
53+
else:
54+
name = data.get("name")
55+
56+
if self.endpoint in SLUG_REQUIRED:
57+
if not data.get("slug"):
58+
data["slug"] = self._to_slug(name)
59+
60+
object_query_params = self._build_query_params(
61+
endpoint_name, data, user_query_params
62+
)
63+
self.nb_object = self._nb_endpoint_get(nb_endpoint, object_query_params, name)
64+
65+
if self.state == "present":
66+
self._ensure_object_exists(nb_endpoint, endpoint_name, name, data)
67+
elif self.state == "absent":
68+
self._ensure_object_absent(endpoint_name, name)
69+
70+
try:
71+
serialized_object = self.nb_object.serialize()
72+
except AttributeError:
73+
serialized_object = self.nb_object
74+
75+
self.result.update({endpoint_name: serialized_object})
76+
77+
self.module.exit_json(**self.result)

plugins/modules/netbox_l2vpn.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,16 @@
143143

144144
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
145145
NetboxAnsibleModule,
146+
NetboxModule,
146147
NETBOX_ARG_SPEC,
147148
)
148149
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_ipam import (
149150
NetboxIpamModule,
150-
NB_L2VPNS,
151+
NB_L2VPNS as NB_IPAM_L2VPNS,
152+
)
153+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_vpn import (
154+
NetboxVpnModule,
155+
NB_L2VPNS as NB_VPN_L2VPNS,
151156
)
152157
from copy import deepcopy
153158

@@ -186,7 +191,12 @@ def main():
186191
argument_spec=argument_spec, supports_check_mode=True, required_if=required_if
187192
)
188193

189-
netbox_l2vpn = NetboxIpamModule(module, NB_L2VPNS)
194+
netbox_l2vpn = NetboxModule(module, "")
195+
if netbox_l2vpn._find_app(NB_IPAM_L2VPNS) == "ipam":
196+
netbox_l2vpn = NetboxIpamModule(module, NB_IPAM_L2VPNS)
197+
if netbox_l2vpn._find_app(NB_VPN_L2VPNS) == "vpn":
198+
netbox_l2vpn = NetboxVpnModule(module, NB_VPN_L2VPNS)
199+
190200
netbox_l2vpn.run()
191201

192202

plugins/modules/netbox_l2vpn_termination.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,17 @@
105105

106106
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
107107
NetboxAnsibleModule,
108+
NetboxModule,
108109
NETBOX_ARG_SPEC,
109110
)
110111

111112
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_ipam import (
112113
NetboxIpamModule,
113-
NB_L2VPN_TERMINATIONS,
114+
NB_L2VPN_TERMINATIONS as NB_IPAM_L2VPN_TERMINATIONS,
115+
)
116+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_vpn import (
117+
NetboxVpnModule,
118+
NB_L2VPN_TERMINATIONS as NB_VPN_L2VPN_TERMINATIONS,
114119
)
115120

116121

@@ -146,7 +151,13 @@ def main():
146151
)
147152

148153
module = NetboxAnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
149-
netbox_l2vpn_termination = NetboxIpamModule(module, NB_L2VPN_TERMINATIONS)
154+
155+
netbox_l2vpn_termination = NetboxModule(module, "")
156+
if netbox_l2vpn_termination._find_app(NB_IPAM_L2VPNS) == "ipam":
157+
netbox_l2vpn_termination = NetboxIpamModule(module, NB_IPAM_L2VPN_TERMINATIONS)
158+
if netbox_l2vpn_termination._find_app(NB_VPN_L2VPNS) == "vpn":
159+
netbox_l2vpn_termination = NetboxVpnModule(module, NB_VPN_L2VPN_TERMINATIONS)
160+
150161
netbox_l2vpn_termination.run()
151162

152163

0 commit comments

Comments
 (0)