| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2022, Joshua Coronado (@joshuajcoronado) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = { |
| 12 |
"metadata_version": "1.1", |
| 13 |
"status": ["deprecated"], |
| 14 |
"supported_by": "community", |
| 15 |
} |
| 16 |
|
| 17 |
DOCUMENTATION = r""" |
| 18 |
author: |
| 19 |
- Joshua Coronado (@joshuajcoronado) |
| 20 |
deprecated: |
| 21 |
alternative: cisco.meraki.networks_webhooks_payload_templates |
| 22 |
removed_in: 3.0.0 |
| 23 |
why: Updated modules released with increased functionality |
| 24 |
description: |
| 25 |
- Allows for querying, deleting, creating, and updating of webhook payload templates. |
| 26 |
extends_documentation_fragment: cisco.meraki.meraki |
| 27 |
module: meraki_webhook_payload_template |
| 28 |
options: |
| 29 |
body: |
| 30 |
description: |
| 31 |
- The liquid template used for the body of the webhook message. |
| 32 |
type: str |
| 33 |
headers: |
| 34 |
default: [] |
| 35 |
description: |
| 36 |
- List of the liquid templates used with the webhook headers. |
| 37 |
elements: dict |
| 38 |
suboptions: |
| 39 |
name: |
| 40 |
description: |
| 41 |
- The name of the header template. |
| 42 |
type: str |
| 43 |
template: |
| 44 |
description: |
| 45 |
- The liquid template for the headers |
| 46 |
type: str |
| 47 |
type: list |
| 48 |
name: |
| 49 |
description: |
| 50 |
- Name of the template. |
| 51 |
type: str |
| 52 |
net_id: |
| 53 |
description: |
| 54 |
- ID of network containing access points. |
| 55 |
type: str |
| 56 |
net_name: |
| 57 |
description: |
| 58 |
- Name of network containing access points. |
| 59 |
type: str |
| 60 |
state: |
| 61 |
choices: |
| 62 |
- absent |
| 63 |
- query |
| 64 |
- present |
| 65 |
default: query |
| 66 |
description: |
| 67 |
- Specifies whether payload template should be queried, created, modified, or |
| 68 |
deleted. |
| 69 |
type: str |
| 70 |
short_description: Manage webhook payload templates for a network in the Meraki cloud |
| 71 |
""" |
| 72 |
|
| 73 |
EXAMPLES = r""" |
| 74 |
- name: Query all configuration templates |
| 75 |
meraki_webhook_payload_template: |
| 76 |
auth_key: abc12345 |
| 77 |
org_name: YourOrg |
| 78 |
state: query |
| 79 |
delegate_to: localhost |
| 80 |
- name: Query specific configuration templates |
| 81 |
meraki_webhook_payload_template: |
| 82 |
auth_key: abc12345 |
| 83 |
org_name: YourOrg |
| 84 |
state: query |
| 85 |
name: Twitter |
| 86 |
delegate_to: localhost |
| 87 |
- name: Create payload template |
| 88 |
meraki_webhook_payload_template: |
| 89 |
auth_key: abc12345 |
| 90 |
org_name: YourOrg |
| 91 |
state: query |
| 92 |
name: TestTemplate |
| 93 |
body: Testbody |
| 94 |
headers: |
| 95 |
- name: testheader |
| 96 |
template: testheadertemplate |
| 97 |
delegate_to: localhost |
| 98 |
- name: Delete a configuration template |
| 99 |
meraki_config_template: |
| 100 |
auth_key: abc123 |
| 101 |
state: absent |
| 102 |
org_name: YourOrg |
| 103 |
name: TestTemplate |
| 104 |
delegate_to: localhost |
| 105 |
""" |
| 106 |
|
| 107 |
RETURN = r""" |
| 108 |
data: |
| 109 |
description: Information about queried object. |
| 110 |
returned: success |
| 111 |
type: complex |
| 112 |
contains: |
| 113 |
name: |
| 114 |
description: |
| 115 |
- The name of the template |
| 116 |
returned: success |
| 117 |
type: str |
| 118 |
sample: testTemplate |
| 119 |
body: |
| 120 |
description: |
| 121 |
- The liquid template used for the body of the webhook message. |
| 122 |
returned: success |
| 123 |
type: str |
| 124 |
sample: {"event_type":"{{ alertTypeId }}","client_payload":{"text":"{{ alertData }}"}} |
| 125 |
headers: |
| 126 |
description: List of the liquid templates used with the webhook headers. |
| 127 |
returned: success |
| 128 |
type: list |
| 129 |
contains: |
| 130 |
name: |
| 131 |
description: |
| 132 |
- The name of the template |
| 133 |
returned: success |
| 134 |
type: str |
| 135 |
sample: testTemplate |
| 136 |
template: |
| 137 |
description: |
| 138 |
- The liquid template for the header |
| 139 |
returned: success |
| 140 |
type: str |
| 141 |
sample: "Bearer {{ sharedSecret }}" |
| 142 |
""" |
| 143 |
|
| 144 |
from ansible.module_utils.basic import AnsibleModule, json |
| 145 |
from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import ( |
| 146 |
MerakiModule, |
| 147 |
meraki_argument_spec, |
| 148 |
) |
| 149 |
|
| 150 |
|
| 151 |
def get_webhook_payload_templates(meraki, net_id): |
| 152 |
path = meraki.construct_path("get_all", net_id=net_id) |
| 153 |
response = meraki.request(path, "GET") |
| 154 |
if meraki.status != 200: |
| 155 |
meraki.fail_json(msg="Unable to get webhook payload templates") |
| 156 |
return response |
| 157 |
|
| 158 |
|
| 159 |
def delete_template(meraki, net_id, template_id): |
| 160 |
changed = True |
| 161 |
if meraki.check_mode: |
| 162 |
return {}, changed |
| 163 |
else: |
| 164 |
path = meraki.construct_path( |
| 165 |
"update", net_id=net_id, custom={"template_id": template_id} |
| 166 |
) |
| 167 |
response = meraki.request(path, method="DELETE") |
| 168 |
if meraki.status != 204: |
| 169 |
meraki.fail_json(msg="Unable to remove webhook payload templates") |
| 170 |
return response, changed |
| 171 |
|
| 172 |
|
| 173 |
def create_template(meraki, net_id, template): |
| 174 |
changed = True |
| 175 |
|
| 176 |
if meraki.check_mode: |
| 177 |
return template, changed |
| 178 |
else: |
| 179 |
path = meraki.construct_path("get_all", net_id=net_id) |
| 180 |
response = meraki.request(path, "POST", payload=json.dumps(template)) |
| 181 |
if meraki.status != 201: |
| 182 |
meraki.fail_json(msg="Unable to create webhook payload template") |
| 183 |
return response, changed |
| 184 |
|
| 185 |
|
| 186 |
def update_template(meraki, net_id, template, payload): |
| 187 |
changed = False |
| 188 |
|
| 189 |
if template["body"] != payload["body"]: |
| 190 |
changed = True |
| 191 |
|
| 192 |
if meraki.is_update_required(template["headers"], payload["headers"]): |
| 193 |
changed = True |
| 194 |
|
| 195 |
if changed: |
| 196 |
meraki.generate_diff(template, payload) |
| 197 |
if meraki.check_mode: |
| 198 |
return payload, changed |
| 199 |
else: |
| 200 |
path = meraki.construct_path( |
| 201 |
"update", |
| 202 |
net_id=net_id, |
| 203 |
custom={"template_id": template["payloadTemplateId"]}, |
| 204 |
) |
| 205 |
response = meraki.request( |
| 206 |
path, method="PUT", payload=json.dumps(payload) |
| 207 |
) |
| 208 |
if meraki.status != 200: |
| 209 |
meraki.fail_json( |
| 210 |
msg="Unable to update webhook payload template" |
| 211 |
) |
| 212 |
return response, changed |
| 213 |
|
| 214 |
return template, changed |
| 215 |
|
| 216 |
|
| 217 |
def main(): |
| 218 |
|
| 219 |
# define the available arguments/parameters that a user can pass to |
| 220 |
# the module |
| 221 |
argument_spec = meraki_argument_spec() |
| 222 |
argument_spec.update( |
| 223 |
state=dict( |
| 224 |
type="str", choices=["absent", "query", "present"], default="query" |
| 225 |
), |
| 226 |
name=dict(type="str", default=None), |
| 227 |
net_name=dict(type="str"), |
| 228 |
net_id=dict(type="str"), |
| 229 |
body=dict(type="str", default=None), |
| 230 |
headers=dict( |
| 231 |
type="list", |
| 232 |
default=[], |
| 233 |
elements="dict", |
| 234 |
options=dict( |
| 235 |
name=dict(type="str"), |
| 236 |
template=dict(type="str"), |
| 237 |
), |
| 238 |
), |
| 239 |
) |
| 240 |
|
| 241 |
module = AnsibleModule( |
| 242 |
argument_spec=argument_spec, |
| 243 |
supports_check_mode=True, |
| 244 |
) |
| 245 |
meraki = MerakiModule(module, function="webhook_payload_template") |
| 246 |
meraki.params["follow_redirects"] = "all" |
| 247 |
|
| 248 |
query_all_urls = { |
| 249 |
"webhook_payload_template": "/networks/{net_id}/webhooks/payloadTemplates" |
| 250 |
} |
| 251 |
update_urls = { |
| 252 |
"webhook_payload_template": "/networks/{net_id}/webhooks/payloadTemplates/{template_id}" |
| 253 |
} |
| 254 |
|
| 255 |
meraki.url_catalog["get_all"].update(query_all_urls) |
| 256 |
meraki.url_catalog["update"] = update_urls |
| 257 |
|
| 258 |
org_id = meraki.params["org_id"] |
| 259 |
if meraki.params["org_name"]: |
| 260 |
org_id = meraki.get_org_id(meraki.params["org_name"]) |
| 261 |
net_id = meraki.params["net_id"] |
| 262 |
|
| 263 |
if net_id is None: |
| 264 |
if meraki.params["net_name"] is not None: |
| 265 |
nets = meraki.get_nets(org_id=org_id) |
| 266 |
net_id = meraki.get_net_id( |
| 267 |
net_name=meraki.params["net_name"], data=nets |
| 268 |
) |
| 269 |
|
| 270 |
templates = { |
| 271 |
template["name"]: template |
| 272 |
for template in get_webhook_payload_templates(meraki, net_id) |
| 273 |
} |
| 274 |
|
| 275 |
if meraki.params["state"] == "query": |
| 276 |
meraki.result["changed"] = False |
| 277 |
|
| 278 |
if meraki.params["name"]: |
| 279 |
if meraki.params["name"] in templates: |
| 280 |
meraki.result["data"] = templates[meraki.params["name"]] |
| 281 |
else: |
| 282 |
meraki.fail_json( |
| 283 |
msg="Unable to get webhook payload template named: {0}".format( |
| 284 |
meraki.params["name"] |
| 285 |
) |
| 286 |
) |
| 287 |
else: |
| 288 |
meraki.result["data"] = templates |
| 289 |
|
| 290 |
elif meraki.params["state"] == "present": |
| 291 |
if meraki.params["name"] is None: |
| 292 |
meraki.fail_json(msg="name is a required parameter") |
| 293 |
|
| 294 |
if meraki.params["body"] is None: |
| 295 |
meraki.fail_json( |
| 296 |
msg="body is a required parameter when state is present" |
| 297 |
) |
| 298 |
|
| 299 |
headers = [] |
| 300 |
|
| 301 |
for header in meraki.params["headers"]: |
| 302 |
for key in ["name", "template"]: |
| 303 |
if key not in header: |
| 304 |
meraki.fail_json( |
| 305 |
msg="{0} is a required parameter for a header".format( |
| 306 |
key |
| 307 |
) |
| 308 |
) |
| 309 |
if not header[key]: |
| 310 |
meraki.fail_json( |
| 311 |
msg="{0} in header must be a string".format(key) |
| 312 |
) |
| 313 |
headers.append( |
| 314 |
dict(name=header["name"], template=header["template"]) |
| 315 |
) |
| 316 |
|
| 317 |
payload = { |
| 318 |
"name": meraki.params["name"], |
| 319 |
"body": meraki.params["body"], |
| 320 |
"headers": meraki.params["headers"], |
| 321 |
} |
| 322 |
|
| 323 |
if meraki.params["name"] in templates: |
| 324 |
( |
| 325 |
meraki.result["data"], |
| 326 |
meraki.result["changed"], |
| 327 |
) = update_template( |
| 328 |
meraki, net_id, templates[meraki.params["name"]], payload |
| 329 |
) |
| 330 |
else: |
| 331 |
( |
| 332 |
meraki.result["data"], |
| 333 |
meraki.result["changed"], |
| 334 |
) = create_template(meraki, net_id, payload) |
| 335 |
|
| 336 |
elif meraki.params["state"] == "absent": |
| 337 |
if meraki.params["name"] in templates: |
| 338 |
( |
| 339 |
meraki.result["data"], |
| 340 |
meraki.result["changed"], |
| 341 |
) = delete_template( |
| 342 |
meraki, |
| 343 |
net_id, |
| 344 |
templates[meraki.params["name"]]["payloadTemplateId"], |
| 345 |
) |
| 346 |
else: |
| 347 |
meraki.result["changed"] = False |
| 348 |
meraki.result["data"] = {} |
| 349 |
|
| 350 |
# in the event of a successful module execution, you will want to |
| 351 |
# simple AnsibleModule.exit_json(), passing the key/value results |
| 352 |
meraki.exit_json(**meraki.result) |
| 353 |
|
| 354 |
|
| 355 |
if __name__ == "__main__": |
| 356 |
main() |