| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright 2013 Matt Coddington <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: newrelic_deployment |
| 11 |
author: "Matt Coddington (@mcodd)" |
| 12 |
short_description: Notify New Relic about app deployments |
| 13 |
description: |
| 14 |
- Notify New Relic about app deployments (see U(https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/record-monitor-deployments/)). |
| 15 |
extends_documentation_fragment: |
| 16 |
- community.general._attributes |
| 17 |
attributes: |
| 18 |
check_mode: |
| 19 |
support: full |
| 20 |
diff_mode: |
| 21 |
support: none |
| 22 |
options: |
| 23 |
token: |
| 24 |
type: str |
| 25 |
description: |
| 26 |
- API token to place in the Api-Key header. |
| 27 |
required: true |
| 28 |
app_name: |
| 29 |
type: str |
| 30 |
description: |
| 31 |
- The value of C(app_name) in the C(newrelic.yml) file used by the application. |
| 32 |
- One of O(app_name) or O(application_id) is required. |
| 33 |
application_id: |
| 34 |
type: str |
| 35 |
description: |
| 36 |
- The application ID found in the metadata of the application in APM. |
| 37 |
- One of O(app_name) or O(application_id) is required. |
| 38 |
changelog: |
| 39 |
type: str |
| 40 |
description: |
| 41 |
- A list of changes for this deployment. |
| 42 |
description: |
| 43 |
type: str |
| 44 |
description: |
| 45 |
- Text annotation for the deployment - notes for you. |
| 46 |
revision: |
| 47 |
type: str |
| 48 |
description: |
| 49 |
- A revision number (for example, git commit SHA). |
| 50 |
required: true |
| 51 |
user: |
| 52 |
type: str |
| 53 |
description: |
| 54 |
- The name of the user/process that triggered this deployment. |
| 55 |
validate_certs: |
| 56 |
description: |
| 57 |
- If V(false), SSL certificates are not validated. This should only be used on personally controlled sites using self-signed |
| 58 |
certificates. |
| 59 |
default: true |
| 60 |
type: bool |
| 61 |
app_name_exact_match: |
| 62 |
type: bool |
| 63 |
description: |
| 64 |
- If this flag is set to V(true) then the application ID lookup by name would only work for an exact match. If set to |
| 65 |
V(false) it returns the first result. |
| 66 |
default: false |
| 67 |
version_added: 7.5.0 |
| 68 |
requirements: [] |
| 69 |
""" |
| 70 |
|
| 71 |
EXAMPLES = r""" |
| 72 |
- name: Notify New Relic about an app deployment |
| 73 |
community.general.newrelic_deployment: |
| 74 |
token: AAAAAA |
| 75 |
app_name: myapp |
| 76 |
user: ansible deployment |
| 77 |
revision: '1.0' |
| 78 |
""" |
| 79 |
|
| 80 |
import json |
| 81 |
from urllib.parse import quote |
| 82 |
|
| 83 |
from ansible.module_utils.basic import AnsibleModule |
| 84 |
from ansible.module_utils.urls import fetch_url |
| 85 |
|
| 86 |
# =========================================== |
| 87 |
# Module execution. |
| 88 |
# |
| 89 |
|
| 90 |
|
| 91 |
def main(): |
| 92 |
module = AnsibleModule( |
| 93 |
argument_spec=dict( |
| 94 |
token=dict(required=True, no_log=True), |
| 95 |
app_name=dict(), |
| 96 |
application_id=dict(), |
| 97 |
changelog=dict(), |
| 98 |
description=dict(), |
| 99 |
revision=dict(required=True), |
| 100 |
user=dict(), |
| 101 |
validate_certs=dict(default=True, type="bool"), |
| 102 |
app_name_exact_match=dict(type="bool", default=False), |
| 103 |
), |
| 104 |
required_one_of=[["app_name", "application_id"]], |
| 105 |
required_if=[("app_name_exact_match", True, ["app_name"])], |
| 106 |
supports_check_mode=True, |
| 107 |
) |
| 108 |
|
| 109 |
# build list of params |
| 110 |
params = {} |
| 111 |
if module.params["app_name"] and module.params["application_id"]: |
| 112 |
module.fail_json(msg="only one of 'app_name' or 'application_id' can be set") |
| 113 |
app_id = None |
| 114 |
if module.params["app_name"]: |
| 115 |
app_id = get_application_id(module) |
| 116 |
elif module.params["application_id"]: |
| 117 |
app_id = module.params["application_id"] |
| 118 |
else: |
| 119 |
module.fail_json(msg="you must set one of 'app_name' or 'application_id'") |
| 120 |
|
| 121 |
if app_id is None: |
| 122 |
module.fail_json(msg=f"No application with name {module.params['app_name']} is found in NewRelic") |
| 123 |
|
| 124 |
for item in ["changelog", "description", "revision", "user"]: |
| 125 |
if module.params[item]: |
| 126 |
params[item] = module.params[item] |
| 127 |
|
| 128 |
# If we're in check mode, just exit pretending like we succeeded |
| 129 |
if module.check_mode: |
| 130 |
module.exit_json(changed=True) |
| 131 |
|
| 132 |
# Send the data to New Relic |
| 133 |
url = f"https://api.newrelic.com/v2/applications/{quote(str(app_id), safe='')}/deployments.json" |
| 134 |
data = {"deployment": params} |
| 135 |
headers = { |
| 136 |
"Api-Key": module.params["token"], |
| 137 |
"Content-Type": "application/json", |
| 138 |
} |
| 139 |
response, info = fetch_url(module, url, data=module.jsonify(data), headers=headers, method="POST") |
| 140 |
if info["status"] in (200, 201): |
| 141 |
module.exit_json(changed=True) |
| 142 |
else: |
| 143 |
module.fail_json(msg=f"Unable to insert deployment marker: {info['msg']}") |
| 144 |
|
| 145 |
|
| 146 |
def get_application_id(module): |
| 147 |
url = "https://api.newrelic.com/v2/applications.json" |
| 148 |
data = f"filter[name]={module.params['app_name']}" |
| 149 |
application_id = None |
| 150 |
headers = { |
| 151 |
"Api-Key": module.params["token"], |
| 152 |
} |
| 153 |
response, info = fetch_url(module, url, data=data, headers=headers) |
| 154 |
if info["status"] not in (200, 201): |
| 155 |
module.fail_json(msg=f"Unable to get application: {info['msg']}") |
| 156 |
|
| 157 |
result = json.loads(response.read()) |
| 158 |
if result is None or len(result.get("applications", "")) == 0: |
| 159 |
module.fail_json(msg=f'No application found with name "{module.params["app_name"]}"') |
| 160 |
|
| 161 |
if module.params["app_name_exact_match"]: |
| 162 |
for item in result["applications"]: |
| 163 |
if item["name"] == module.params["app_name"]: |
| 164 |
application_id = item["id"] |
| 165 |
break |
| 166 |
if application_id is None: |
| 167 |
module.fail_json(msg=f'No application found with exact name "{module.params["app_name"]}"') |
| 168 |
else: |
| 169 |
application_id = result["applications"][0]["id"] |
| 170 |
|
| 171 |
return application_id |
| 172 |
|
| 173 |
|
| 174 |
if __name__ == "__main__": |
| 175 |
main() |