Skip to content

Commit a2cf3f4

Browse files
authored
Add netbox_webhook as module (#738)
1 parent 011fd21 commit a2cf3f4

File tree

10 files changed

+366
-7
lines changed

10 files changed

+366
-7
lines changed

plugins/module_utils/netbox_extras.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
NB_CUSTOM_FIELDS = "custom_fields"
1717
NB_CUSTOM_LINKS = "custom_links"
1818
NB_EXPORT_TEMPLATES = "export_templates"
19+
NB_WEBHOOKS = "webhooks"
1920

2021

2122
class NetboxExtrasModule(NetboxModule):

plugins/module_utils/netbox_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"custom_fields",
8181
"custom_links",
8282
"export_templates",
83+
"webhooks",
8384
],
8485
ipam=[
8586
"aggregates",
@@ -166,6 +167,7 @@
166167
vlan_group="slug",
167168
vlan_role="name",
168169
vrf="name",
170+
webhook="name",
169171
wireless_lan="ssid",
170172
wireless_lan_group="slug",
171173
)
@@ -334,6 +336,7 @@
334336
"vlans": "vlan",
335337
"vlan_groups": "vlan_group",
336338
"vrfs": "vrf",
339+
"webhooks": "webhook",
337340
"wireless_lans": "wireless_lan",
338341
"wireless_lan_groups": "wireless_lan_group",
339342
"wireless_links": "wireless_link",
@@ -451,6 +454,7 @@
451454
"vlan": set(["group", "name", "site", "tenant", "vid", "vlan_group"]),
452455
"vlan_group": set(["slug", "site", "scope"]),
453456
"vrf": set(["name", "tenant"]),
457+
"webhook": set(["name"]),
454458
"wireless_lan": set(["ssid"]),
455459
"wireless_lan_group": set(["name"]),
456460
"wireless_link": set(["interface_a", "interface_b"]),

plugins/module_utils/netbox_wireless.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
# Copyright: (c) 2021, Martin Rødvand (@rodvand) <mikhail.yohman@gmail.com>
2+
# Copyright: (c) 2021, Martin Rødvand (@rodvand) <martin@rodvand.net>
33
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
44
from __future__ import absolute_import, division, print_function
55

plugins/modules/netbox_custom_field.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3-
# Copyright: (c) 2022, Martin Rødvand (@rodvand) <p@tristero.se>
3+
# Copyright: (c) 2022, Martin Rødvand (@rodvand) <martin@rodvand.net>
44
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
55

66
from __future__ import absolute_import, division, print_function

plugins/modules/netbox_custom_link.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3-
# Copyright: (c) 2022, Martin Rødvand (@rodvand) <p@tristero.se>
3+
# Copyright: (c) 2022, Martin Rødvand (@rodvand) <martin@rodvand.net>
44
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
55

66
from __future__ import absolute_import, division, print_function

plugins/modules/netbox_export_template.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3-
# Copyright: (c) 2022, Martin Rødvand (@rodvand) <p@tristero.se>
3+
# Copyright: (c) 2022, Martin Rødvand (@rodvand) <martin@rodvand.net>
44
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
55

66
from __future__ import absolute_import, division, print_function

plugins/modules/netbox_webhook.py

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# Copyright: (c) 2022, Martin Rødvand (@rodvand) <martin@rodvand.net>
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_webhook
13+
short_description: Creates, updates or deletes webhook configuration within NetBox
14+
description:
15+
- Creates, updates or removes webhook configuration within NetBox
16+
notes:
17+
- This should be ran with connection C(local) and hosts C(localhost)
18+
- Use C(!unsafe) when adding jinja2 code to C(additional_headers) or C(body_template)
19+
author:
20+
- Martin Rødvand (@rodvand)
21+
requirements:
22+
- pynetbox
23+
version_added: "3.6.0"
24+
extends_documentation_fragment:
25+
- netbox.netbox.common
26+
options:
27+
data:
28+
type: dict
29+
description:
30+
- Defines the custom field
31+
suboptions:
32+
content_types:
33+
description:
34+
- The content type(s) to apply this webhook to
35+
- Required when I(state=present)
36+
required: false
37+
type: list
38+
elements: raw
39+
name:
40+
description:
41+
- Name of the webhook
42+
required: true
43+
type: str
44+
type_create:
45+
description:
46+
- Call this webhook when a matching object is created
47+
required: false
48+
type: bool
49+
type_update:
50+
description:
51+
- Call this webhook when a matching object is updated
52+
required: false
53+
type: bool
54+
type_delete:
55+
description:
56+
- Call this webhook when a matching object is deleted
57+
required: false
58+
type: bool
59+
payload_url:
60+
description:
61+
- URL for the webhook to use.
62+
- Required when I(state=present)
63+
required: false
64+
type: str
65+
enabled:
66+
description:
67+
- Enable/disable the webhook.
68+
required: false
69+
type: bool
70+
http_method:
71+
description:
72+
- HTTP method of the webhook.
73+
required: false
74+
type: raw
75+
http_content_type:
76+
description:
77+
- The HTTP content type.
78+
required: false
79+
type: str
80+
additional_headers:
81+
description:
82+
- User-supplied HTTP headers. Supports jinja2 code.
83+
required: false
84+
type: str
85+
body_template:
86+
description:
87+
- Body template for webhook. Supports jinja2 code.
88+
required: false
89+
type: str
90+
secret:
91+
description:
92+
- Secret key to generate X-Hook-Signature to include in the payload.
93+
required: false
94+
type: str
95+
conditions:
96+
description:
97+
- A set of conditions which determine whether the webhook will be generated.
98+
required: false
99+
type: str
100+
ssl_verification:
101+
description:
102+
- Enable ssl verification.
103+
required: false
104+
type: bool
105+
ca_file_path:
106+
description:
107+
- CA certificate file to use for SSL verification
108+
required: false
109+
type: str
110+
required: true
111+
"""
112+
113+
EXAMPLES = r"""
114+
- name: "Test NetBox webhook module"
115+
connection: local
116+
hosts: localhost
117+
tasks:
118+
- name: Create a webhook
119+
netbox_webhook:
120+
netbox_url: http://netbox.local
121+
netbox_token: thisIsMyToken
122+
data:
123+
content_types:
124+
- dcim.device
125+
name: Example Webhook
126+
type_create: yes
127+
payload_url: https://payload.url/
128+
body_template: !unsafe >-
129+
{{ data }}
130+
131+
- name: Update the webhook to run on delete
132+
netbox_webhook:
133+
netbox_url: http://netbox.local
134+
netbox_token: thisIsMyToken
135+
data:
136+
name: Example Webhook
137+
type_create: yes
138+
type_delete: yes
139+
payload_url: https://payload.url/
140+
body_template: !unsafe >-
141+
{{ data }}
142+
143+
- name: Delete the webhook
144+
netbox_webhook:
145+
netbox_url: http://netbox.local
146+
netbox_token: thisIsMyToken
147+
data:
148+
name: Example Webhook
149+
type_create: yes
150+
type_delete: yes
151+
payload_url: https://payload.url/
152+
body_template: !unsafe >-
153+
{{ data }}
154+
state: absent
155+
"""
156+
157+
RETURN = r"""
158+
webhook:
159+
description: Serialized object as created/existent/updated/deleted within NetBox
160+
returned: always
161+
type: dict
162+
msg:
163+
description: Message indicating failure or info about what has been achieved
164+
returned: always
165+
type: str
166+
"""
167+
168+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
169+
NetboxAnsibleModule,
170+
NETBOX_ARG_SPEC,
171+
)
172+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_extras import (
173+
NetboxExtrasModule,
174+
NB_WEBHOOKS,
175+
)
176+
from copy import deepcopy
177+
178+
179+
def main():
180+
"""
181+
Main entry point for module execution
182+
"""
183+
argument_spec = deepcopy(NETBOX_ARG_SPEC)
184+
argument_spec.update(
185+
dict(
186+
data=dict(
187+
type="dict",
188+
required=True,
189+
options=dict(
190+
content_types=dict(required=False, type="list", elements="raw"),
191+
name=dict(required=True, type="str"),
192+
type_create=dict(required=False, type="bool"),
193+
type_update=dict(required=False, type="bool"),
194+
type_delete=dict(required=False, type="bool"),
195+
payload_url=dict(required=False, type="str"),
196+
enabled=dict(required=False, type="bool"),
197+
http_method=dict(required=False, type="raw"),
198+
http_content_type=dict(required=False, type="str"),
199+
additional_headers=dict(required=False, type="str"),
200+
body_template=dict(required=False, type="str"),
201+
secret=dict(required=False, type="str", no_log=False),
202+
conditions=dict(required=False, type="str"),
203+
ssl_verification=dict(required=False, type="bool"),
204+
ca_file_path=dict(required=False, type="str"),
205+
),
206+
)
207+
)
208+
)
209+
210+
required_if = [
211+
("state", "present", ["content_types", "name", "payload_url"]),
212+
("state", "absent", ["name"]),
213+
]
214+
215+
module = NetboxAnsibleModule(
216+
argument_spec=argument_spec, supports_check_mode=True, required_if=required_if
217+
)
218+
219+
netbox_webhook = NetboxExtrasModule(module, NB_WEBHOOKS)
220+
netbox_webhook.run()
221+
222+
223+
if __name__ == "__main__": # pragma: no cover
224+
main()

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,13 @@
225225
tags:
226226
- netbox_export_template
227227
tags:
228-
- netbox_export_template
228+
- netbox_export_template
229+
230+
- name: "NETBOX_WEBHOOK TESTS"
231+
include_tasks:
232+
file: "netbox_webhook.yml"
233+
apply:
234+
tags:
235+
- netbox_webhook
236+
tags:
237+
- netbox_webhook

tests/integration/targets/v3.1/tasks/netbox_export_template.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
- test_two['export_template']['name'] == "Example Export Template"
4949
- test_two['msg'] == "export_template Example Export Template already exists"
5050

51-
- name: "CUSTOM_FIELD 3: Update data and remove as_attachment"
51+
- name: "EXPORT_TEMPLATE 3: Update data and remove as_attachment"
5252
netbox.netbox.netbox_export_template:
5353
netbox_url: http://localhost:32768
5454
netbox_token: 0123456789abcdef0123456789abcdef01234567
@@ -62,7 +62,7 @@
6262
state: present
6363
register: test_three
6464

65-
- name: "CUSTOM_FIELD 3: ASSERT - Updated"
65+
- name: "EXPORT_TEMPLATE 3: ASSERT - Updated"
6666
assert:
6767
that:
6868
- test_three is changed

0 commit comments

Comments
 (0)