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