|
| 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() |
0 commit comments