| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2020, Adam Vaughan (@adamvaughan) [email protected] |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: pagerduty_change |
| 10 |
short_description: Track a code or infrastructure change as a PagerDuty change event |
| 11 |
version_added: 1.3.0 |
| 12 |
description: |
| 13 |
- This module lets you create a PagerDuty change event each time the module is run. |
| 14 |
- This is not an idempotent action and a new change event is created each time it is run. |
| 15 |
author: |
| 16 |
- Adam Vaughan (@adamvaughan) |
| 17 |
requirements: |
| 18 |
- PagerDuty integration key |
| 19 |
extends_documentation_fragment: |
| 20 |
- community.general._attributes |
| 21 |
attributes: |
| 22 |
check_mode: |
| 23 |
support: full |
| 24 |
details: |
| 25 |
- Check mode simply does nothing except returning RV(ignore:changed=true) in case the O(url) seems to be correct. |
| 26 |
diff_mode: |
| 27 |
support: none |
| 28 |
options: |
| 29 |
integration_key: |
| 30 |
description: |
| 31 |
- The integration key that identifies the service the change was made to. This can be found by adding an integration |
| 32 |
to a service in PagerDuty. |
| 33 |
required: true |
| 34 |
type: str |
| 35 |
summary: |
| 36 |
description: |
| 37 |
- A short description of the change that occurred. |
| 38 |
required: true |
| 39 |
type: str |
| 40 |
source: |
| 41 |
description: |
| 42 |
- The source of the change event. |
| 43 |
default: Ansible |
| 44 |
type: str |
| 45 |
user: |
| 46 |
description: |
| 47 |
- The name of the user or process that triggered this deployment. |
| 48 |
type: str |
| 49 |
repo: |
| 50 |
description: |
| 51 |
- The URL of the project repository. |
| 52 |
type: str |
| 53 |
revision: |
| 54 |
description: |
| 55 |
- An identifier of the revision being deployed, typically a number or SHA from a version control system. |
| 56 |
type: str |
| 57 |
environment: |
| 58 |
description: |
| 59 |
- The environment name, typically V(production), V(staging), and so on. |
| 60 |
type: str |
| 61 |
link_url: |
| 62 |
description: |
| 63 |
- A URL where more information about the deployment can be obtained. |
| 64 |
type: str |
| 65 |
link_text: |
| 66 |
description: |
| 67 |
- Descriptive text for a URL where more information about the deployment can be obtained. |
| 68 |
type: str |
| 69 |
url: |
| 70 |
description: |
| 71 |
- URL to submit the change event to. |
| 72 |
default: https://events.pagerduty.com/v2/change/enqueue |
| 73 |
type: str |
| 74 |
validate_certs: |
| 75 |
description: |
| 76 |
- If V(false), SSL certificates for the target URL are not validated. This should only be used on personally controlled |
| 77 |
sites using self-signed certificates. |
| 78 |
default: true |
| 79 |
type: bool |
| 80 |
""" |
| 81 |
|
| 82 |
EXAMPLES = r""" |
| 83 |
- name: Track the deployment as a PagerDuty change event |
| 84 |
community.general.pagerduty_change: |
| 85 |
integration_key: abc123abc123abc123abc123abc123ab |
| 86 |
summary: The application was deployed |
| 87 |
|
| 88 |
- name: Track the deployment as a PagerDuty change event with more details |
| 89 |
community.general.pagerduty_change: |
| 90 |
integration_key: abc123abc123abc123abc123abc123ab |
| 91 |
summary: The application was deployed |
| 92 |
source: Ansible Deploy |
| 93 |
user: ansible |
| 94 |
repo: github.com/ansible/ansible |
| 95 |
revision: '4.2' |
| 96 |
environment: production |
| 97 |
link_url: https://github.com/ansible-collections/community.general/pull/1269 |
| 98 |
link_text: View changes on GitHub |
| 99 |
""" |
| 100 |
|
| 101 |
from ansible.module_utils.basic import AnsibleModule |
| 102 |
from ansible.module_utils.urls import fetch_url |
| 103 |
|
| 104 |
from ansible_collections.community.general.plugins.module_utils._datetime import ( |
| 105 |
now, |
| 106 |
) |
| 107 |
|
| 108 |
|
| 109 |
def main(): |
| 110 |
module = AnsibleModule( |
| 111 |
argument_spec=dict( |
| 112 |
integration_key=dict(required=True, type="str", no_log=True), |
| 113 |
summary=dict(required=True, type="str"), |
| 114 |
source=dict(default="Ansible", type="str"), |
| 115 |
user=dict(type="str"), |
| 116 |
repo=dict(type="str"), |
| 117 |
revision=dict(type="str"), |
| 118 |
environment=dict(type="str"), |
| 119 |
link_url=dict(type="str"), |
| 120 |
link_text=dict(type="str"), |
| 121 |
url=dict(default="https://events.pagerduty.com/v2/change/enqueue", type="str"), |
| 122 |
validate_certs=dict(default=True, type="bool"), |
| 123 |
), |
| 124 |
supports_check_mode=True, |
| 125 |
) |
| 126 |
|
| 127 |
# API documented at https://developer.pagerduty.com/docs/events-api-v2/send-change-events/ |
| 128 |
|
| 129 |
url = module.params["url"] |
| 130 |
headers = {"Content-Type": "application/json"} |
| 131 |
|
| 132 |
if module.check_mode: |
| 133 |
_response, info = fetch_url(module, url, headers=headers, method="POST") |
| 134 |
|
| 135 |
if info["status"] == 400: |
| 136 |
module.exit_json(changed=True) |
| 137 |
else: |
| 138 |
module.fail_json( |
| 139 |
msg=f"Checking the PagerDuty change event API returned an unexpected response: {info['status']}" |
| 140 |
) |
| 141 |
|
| 142 |
custom_details = {} |
| 143 |
|
| 144 |
if module.params["user"]: |
| 145 |
custom_details["user"] = module.params["user"] |
| 146 |
|
| 147 |
if module.params["repo"]: |
| 148 |
custom_details["repo"] = module.params["repo"] |
| 149 |
|
| 150 |
if module.params["revision"]: |
| 151 |
custom_details["revision"] = module.params["revision"] |
| 152 |
|
| 153 |
if module.params["environment"]: |
| 154 |
custom_details["environment"] = module.params["environment"] |
| 155 |
|
| 156 |
timestamp = now().strftime("%Y-%m-%dT%H:%M:%S.%fZ") |
| 157 |
|
| 158 |
payload = { |
| 159 |
"summary": module.params["summary"], |
| 160 |
"source": module.params["source"], |
| 161 |
"timestamp": timestamp, |
| 162 |
"custom_details": custom_details, |
| 163 |
} |
| 164 |
|
| 165 |
event = {"routing_key": module.params["integration_key"], "payload": payload} |
| 166 |
|
| 167 |
if module.params["link_url"]: |
| 168 |
link = {"href": module.params["link_url"]} |
| 169 |
|
| 170 |
if module.params["link_text"]: |
| 171 |
link["text"] = module.params["link_text"] |
| 172 |
|
| 173 |
event["links"] = [link] |
| 174 |
|
| 175 |
_response, info = fetch_url(module, url, data=module.jsonify(event), headers=headers, method="POST") |
| 176 |
|
| 177 |
if info["status"] == 202: |
| 178 |
module.exit_json(changed=True) |
| 179 |
else: |
| 180 |
module.fail_json(msg=f"Creating PagerDuty change event failed with {info['status']}") |
| 181 |
|
| 182 |
|
| 183 |
if __name__ == "__main__": |
| 184 |
main() |