Skip to content

Commit e0a0327

Browse files
authored
Add module netbox_service_template (#908)
* Add module service template * Add status to wireless LAN * Add tests for netbox_service_template
1 parent be86ac1 commit e0a0327

File tree

7 files changed

+320
-1
lines changed

7 files changed

+320
-1
lines changed

meta/runtime.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ action_groups:
6565
- netbox_rir
6666
- netbox_route_target
6767
- netbox_service
68+
- netbox_service_template
6869
- netbox_site
6970
- netbox_site_group
7071
- netbox_tag

plugins/module_utils/netbox_ipam.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
NB_VLAN_GROUPS = "vlan_groups"
2929
NB_VRFS = "vrfs"
3030
NB_SERVICES = "services"
31+
NB_SERVICE_TEMPLATES = "service_templates"
3132
NB_L2VPNS = "l2vpns"
3233
NB_L2VPN_TERMINATIONS = "l2vpn_terminations"
3334

plugins/module_utils/netbox_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"rirs",
9494
"roles",
9595
"route_targets",
96+
"service_templates",
9697
"vlans",
9798
"vlan_groups",
9899
"vrfs",
@@ -161,6 +162,7 @@
161162
region="slug",
162163
rir="slug",
163164
route_targets="name",
165+
service_templates="name",
164166
slug="slug",
165167
site="slug",
166168
site_group="slug",
@@ -256,6 +258,7 @@
256258
# Just a placeholder as scope can be several different types including sites.
257259
"scope": "sites",
258260
"services": "services",
261+
"service_templates": "service_templates",
259262
"site": "sites",
260263
"site_groups": "site_groups",
261264
"site_group": "site_groups",
@@ -337,6 +340,7 @@
337340
"rirs": "rir",
338341
"roles": "role",
339342
"route_targets": "route_target",
343+
"service_templates": "service_template",
340344
"services": "services",
341345
"sites": "site",
342346
"site_groups": "site_group",
@@ -456,6 +460,7 @@
456460
"role": set(["slug"]),
457461
"route_target": set(["name"]),
458462
"services": set(["device", "virtual_machine", "name", "port", "protocol"]),
463+
"service_template": set(["name"]),
459464
"site": set(["slug", "name"]),
460465
"site_group": set(["slug"]),
461466
"tags": set(["slug"]),
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# Copyright: (c) 2022, Martin Rødvand (@rodvand)
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_service_template
13+
short_description: Create, update or delete service templates within NetBox
14+
description:
15+
- Creates, updates or removes service templates 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+
- Martin Rødvand (@rodvand)
21+
requirements:
22+
- pynetbox
23+
version_added: '3.10.0'
24+
extends_documentation_fragment:
25+
- netbox.netbox.common
26+
options:
27+
data:
28+
type: dict
29+
required: true
30+
description:
31+
- Defines the service template configuration
32+
suboptions:
33+
name:
34+
description:
35+
- The name of the service template
36+
required: true
37+
type: str
38+
ports:
39+
description:
40+
- The ports attached to the service template
41+
required: false
42+
type: list
43+
elements: int
44+
protocol:
45+
description:
46+
- The protocol
47+
choices:
48+
- tcp
49+
- udp
50+
- sctp
51+
required: false
52+
type: str
53+
description:
54+
description:
55+
- Description of the service template
56+
required: false
57+
type: str
58+
comments:
59+
description:
60+
- Comments
61+
required: false
62+
type: str
63+
tags:
64+
description:
65+
- Any tags that the service template may need to be associated with
66+
required: false
67+
type: list
68+
elements: raw
69+
custom_fields:
70+
description:
71+
- Must exist in NetBox and in key/value format
72+
required: false
73+
type: dict
74+
"""
75+
76+
EXAMPLES = r"""
77+
- name: "Test NetBox modules"
78+
connection: local
79+
hosts: localhost
80+
gather_facts: False
81+
82+
tasks:
83+
- name: Create service template within NetBox with only required information
84+
netbox.netbox.netbox_service_template:
85+
netbox_url: http://netbox.local
86+
netbox_token: thisIsMyToken
87+
data:
88+
name: SSH
89+
ports:
90+
- 22
91+
protocol: tcp
92+
state: present
93+
94+
- name: Update service template with other fields
95+
netbox.netbox.netbox_service_template:
96+
netbox_url: http://netbox.local
97+
netbox_token: thisIsMyToken
98+
data:
99+
name: SSH
100+
ports:
101+
- 22
102+
protocol: tcp
103+
description: SSH service template
104+
state: present
105+
106+
- name: Delete service template within netbox
107+
netbox.netbox.netbox_service_template:
108+
netbox_url: http://netbox.local
109+
netbox_token: thisIsMyToken
110+
data:
111+
name: SSH
112+
state: absent
113+
"""
114+
115+
RETURN = r"""
116+
service_template:
117+
description: Serialized object as created or already existent within NetBox
118+
returned: success (when I(state=present))
119+
type: dict
120+
msg:
121+
description: Message indicating failure or info about what has been achieved
122+
returned: always
123+
type: str
124+
"""
125+
126+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
127+
NetboxAnsibleModule,
128+
NETBOX_ARG_SPEC,
129+
)
130+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_ipam import (
131+
NetboxIpamModule,
132+
NB_SERVICE_TEMPLATES,
133+
)
134+
from copy import deepcopy
135+
136+
137+
def main():
138+
"""
139+
Main entry point for module execution
140+
"""
141+
argument_spec = deepcopy(NETBOX_ARG_SPEC)
142+
argument_spec.update(
143+
dict(
144+
data=dict(
145+
type="dict",
146+
required=True,
147+
options=dict(
148+
name=dict(required=True, type="str"),
149+
ports=dict(required=False, type="list", elements="int"),
150+
protocol=dict(
151+
required=False,
152+
choices=[
153+
"tcp",
154+
"udp",
155+
"sctp",
156+
],
157+
type="str",
158+
),
159+
description=dict(required=False, type="str"),
160+
comments=dict(required=False, type="str"),
161+
tags=dict(required=False, type="list", elements="raw"),
162+
custom_fields=dict(required=False, type="dict"),
163+
),
164+
),
165+
)
166+
)
167+
168+
required_if = [
169+
("state", "present", ["name", "ports", "protocol"]),
170+
("state", "absent", ["name"]),
171+
]
172+
173+
module = NetboxAnsibleModule(
174+
argument_spec=argument_spec, supports_check_mode=True, required_if=required_if
175+
)
176+
177+
netbox_service_template = NetboxIpamModule(module, NB_SERVICE_TEMPLATES)
178+
netbox_service_template.run()
179+
180+
181+
if __name__ == "__main__": # pragma: no cover
182+
main()

plugins/modules/netbox_wireless_lan.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
- The wireless LAN group
4444
required: false
4545
type: raw
46+
status:
47+
description:
48+
- Status of the wireless LAN
49+
required: false
50+
type: raw
4651
vlan:
4752
description:
4853
- The VLAN of the Wireless LAN
@@ -167,6 +172,7 @@ def main():
167172
ssid=dict(required=True, type="str"),
168173
description=dict(required=False, type="str"),
169174
wireless_lan_group=dict(required=False, type="raw"),
175+
status=dict(required=False, type="raw"),
170176
vlan=dict(required=False, type="raw"),
171177
auth_type=dict(
172178
required=False,

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,13 @@
263263
tags:
264264
- netbox_module_type
265265
tags:
266-
- netbox_module_type
266+
- netbox_module_type
267+
268+
- name: "NETBOX_SERVICE_TEMPLATE TESTS"
269+
include_tasks:
270+
file: "netbox_service_template.yml"
271+
apply:
272+
tags:
273+
- netbox_service_template
274+
tags:
275+
- netbox_service_template
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
##
3+
##
4+
### NETBOX_SERVICE_TEMPLATE
5+
##
6+
##
7+
- name: "SERVICE_TEMPLATE 1: Necessary info creation"
8+
netbox.netbox.netbox_service_template:
9+
netbox_url: http://localhost:32768
10+
netbox_token: 0123456789abcdef0123456789abcdef01234567
11+
data:
12+
name: Service Template for SSH
13+
ports:
14+
- 22
15+
protocol: tcp
16+
state: present
17+
register: test_one
18+
19+
- name: "SERVICE_TEMPLATE 1: ASSERT - Necessary info creation"
20+
assert:
21+
that:
22+
- test_one is changed
23+
- test_one['diff']['before']['state'] == "absent"
24+
- test_one['diff']['after']['state'] == "present"
25+
- test_one['service_template']['name'] == "Service Template for SSH"
26+
- test_one['service_template']['ports'] == [22]
27+
- test_one['service_template']['protocol'] == "tcp"
28+
- test_one['msg'] == "service_template Service Template for SSH created"
29+
30+
- name: "SERVICE_TEMPLATE 2: Create duplicate"
31+
netbox.netbox.netbox_service_template:
32+
netbox_url: http://localhost:32768
33+
netbox_token: 0123456789abcdef0123456789abcdef01234567
34+
data:
35+
name: Service Template for SSH
36+
ports:
37+
- 22
38+
protocol: tcp
39+
state: present
40+
register: test_two
41+
42+
- name: "SERVICE_TEMPLATE 2: ASSERT - Create duplicate"
43+
assert:
44+
that:
45+
- not test_two['changed']
46+
- test_two['service_template']['name'] == "Service Template for SSH"
47+
- test_two['service_template']['ports'] == [22]
48+
- test_two['service_template']['protocol'] == "tcp"
49+
- test_two['msg'] == "service_template Service Template for SSH already exists"
50+
51+
- name: "SERVICE_TEMPLATE 3: Update Service Template with other fields"
52+
netbox.netbox.netbox_service_template:
53+
netbox_url: http://localhost:32768
54+
netbox_token: 0123456789abcdef0123456789abcdef01234567
55+
data:
56+
name: Service Template for SSH
57+
ports:
58+
- 22
59+
protocol: tcp
60+
comments: For SSH service
61+
state: present
62+
register: test_three
63+
64+
- name: "SERVICE_TEMPLATE 3: ASSERT - Update Service Template with other fields"
65+
assert:
66+
that:
67+
- test_three is changed
68+
- test_three['diff']['after']['comments'] == "For SSH service"
69+
- test_three['service_template']['name'] == "Service Template for SSH"
70+
- test_three['service_template']['ports'] == [22]
71+
- test_three['service_template']['protocol'] == "tcp"
72+
- test_three['service_template']['comments'] == "For SSH service"
73+
- test_three['msg'] == "service_template Service Template for SSH updated"
74+
75+
- name: "SERVICE_TEMPLATE 4: Create Service Template for Delete Test"
76+
netbox.netbox.netbox_service_template:
77+
netbox_url: http://localhost:32768
78+
netbox_token: 0123456789abcdef0123456789abcdef01234567
79+
data:
80+
name: Service Template for DNS
81+
ports:
82+
- 53
83+
protocol: udp
84+
comments: Domain Name System
85+
state: present
86+
register: test_four
87+
88+
- name: "SERVICE_TEMPLATE 4: ASSERT - Create Service Template for Delete Test"
89+
assert:
90+
that:
91+
- test_four is changed
92+
- test_four['diff']['before']['state'] == "absent"
93+
- test_four['diff']['after']['state'] == "present"
94+
- test_four['service_template']['name'] == "Service Template for DNS"
95+
- test_four['service_template']['ports'] == [53]
96+
- test_four['service_template']['protocol'] == "udp"
97+
- test_four['service_template']['comments'] == "Domain Name System"
98+
- test_four['msg'] == "service_template Service Template for DNS created"
99+
100+
- name: "SERVICE_TEMPLATE 5: Delete Service Template"
101+
netbox.netbox.netbox_service_template:
102+
netbox_url: http://localhost:32768
103+
netbox_token: 0123456789abcdef0123456789abcdef01234567
104+
data:
105+
name: Service Template for DNS
106+
state: absent
107+
register: test_five
108+
109+
- name: "SERVICE_TEMPLATE 5: ASSERT - Delete Service Template"
110+
assert:
111+
that:
112+
- test_five is changed
113+
- test_five['diff']['before']['state'] == "present"
114+
- test_five['diff']['after']['state'] == "absent"
115+
- test_five['msg'] == "service_template Service Template for DNS deleted"

0 commit comments

Comments
 (0)