1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Updated to use the new newrelic v2 api (#5341) (#5354)

* updated to use the new newrelic v2 api

* check that application_id is set

* indenting issue

* added back app_name

* fix import ordering

* resolving various spellings & wordings

* fixed wordings

* validate_certs

* fixed unreachable code

(cherry picked from commit 7e2a6cf198)

Co-authored-by: Wei Liao <wliao008@gmail.com>
This commit is contained in:
patchback[bot] 2022-10-12 11:05:42 +02:00 committed by GitHub
parent 68e184eba8
commit 4ef80ecd46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 24 deletions

View file

@ -0,0 +1,6 @@
major_changes:
- newrelic_deployment - removed New Relic v1 API, added support for v2 API (https://github.com/ansible-collections/community.general/pull/5341).
breaking_changes:
- newrelic_deployment - ``revision`` is required for v2 API (https://github.com/ansible-collections/community.general/pull/5341).
deprecated_features:
- newrelic_deployment - ``appname`` and ``environment`` are no longer valid options in the v2 API. They will be removed in community.general 7.0.0 (https://github.com/ansible-collections/community.general/pull/5341).

View file

@ -13,24 +13,26 @@ DOCUMENTATION = '''
--- ---
module: newrelic_deployment module: newrelic_deployment
author: "Matt Coddington (@mcodd)" author: "Matt Coddington (@mcodd)"
short_description: Notify newrelic about app deployments short_description: Notify New Relic about app deployments
description: description:
- Notify newrelic about app deployments (see https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/deployment-notifications#api) - Notify New Relic about app deployments (see https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/record-monitor-deployments/)
options: options:
token: token:
type: str type: str
description: description:
- API token, to place in the x-api-key header. - API token to place in the Api-Key header.
required: true required: true
app_name: app_name:
type: str type: str
description: description:
- (one of app_name or application_id are required) The value of app_name in the newrelic.yml file used by the application - The value of app_name in the newrelic.yml file used by the application.
- One of I(app_name) or I(application_id) is required.
required: false required: false
application_id: application_id:
type: str type: str
description: description:
- (one of app_name or application_id are required) The application id, found in the URL when viewing the application in RPM - The application ID found in the metadata of the application in APM.
- One of I(app_name) or I(application_id) is required.
required: false required: false
changelog: changelog:
type: str type: str
@ -46,7 +48,7 @@ options:
type: str type: str
description: description:
- A revision number (e.g., git commit SHA) - A revision number (e.g., git commit SHA)
required: false required: true
user: user:
type: str type: str
description: description:
@ -55,12 +57,14 @@ options:
appname: appname:
type: str type: str
description: description:
- Name of the application - Name of the application.
- This option has been deprecated and will be removed in community.general 7.0.0. Please do not use.
required: false required: false
environment: environment:
type: str type: str
description: description:
- The environment for this deployment - The environment for this deployment.
- This option has been deprecated and will be removed community.general 7.0.0. Please do not use.
required: false required: false
validate_certs: validate_certs:
description: description:
@ -69,12 +73,11 @@ options:
required: false required: false
default: true default: true
type: bool type: bool
requirements: [] requirements: []
''' '''
EXAMPLES = ''' EXAMPLES = '''
- name: Notify newrelic about an app deployment - name: Notify New Relic about an app deployment
community.general.newrelic_deployment: community.general.newrelic_deployment:
token: AAAAAA token: AAAAAA
app_name: myapp app_name: myapp
@ -84,7 +87,8 @@ EXAMPLES = '''
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import fetch_url from ansible.module_utils.urls import fetch_url
from ansible.module_utils.six.moves.urllib.parse import urlencode from ansible.module_utils.six.moves.urllib.parse import quote
import json
# =========================================== # ===========================================
# Module execution. # Module execution.
@ -100,10 +104,10 @@ def main():
application_id=dict(required=False), application_id=dict(required=False),
changelog=dict(required=False), changelog=dict(required=False),
description=dict(required=False), description=dict(required=False),
revision=dict(required=False), revision=dict(required=True),
user=dict(required=False), user=dict(required=False),
appname=dict(required=False), appname=dict(required=False, removed_in_version='7.0.0', removed_from_collection='community.general'),
environment=dict(required=False), environment=dict(required=False, removed_in_version='7.0.0', removed_from_collection='community.general'),
validate_certs=dict(default=True, type='bool'), validate_certs=dict(default=True, type='bool'),
), ),
required_one_of=[['app_name', 'application_id']], required_one_of=[['app_name', 'application_id']],
@ -115,14 +119,18 @@ def main():
if module.params["app_name"] and module.params["application_id"]: 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") module.fail_json(msg="only one of 'app_name' or 'application_id' can be set")
app_id = None
if module.params["app_name"]: if module.params["app_name"]:
params["app_name"] = module.params["app_name"] app_id = get_application_id(module)
elif module.params["application_id"]: elif module.params["application_id"]:
params["application_id"] = module.params["application_id"] app_id = module.params["application_id"]
else: else:
module.fail_json(msg="you must set one of 'app_name' or 'application_id'") module.fail_json(msg="you must set one of 'app_name' or 'application_id'")
for item in ["changelog", "description", "revision", "user", "appname", "environment"]: 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]: if module.params[item]:
params[item] = module.params[item] params[item] = module.params[item]
@ -130,17 +138,37 @@ def main():
if module.check_mode: if module.check_mode:
module.exit_json(changed=True) module.exit_json(changed=True)
# Send the data to NewRelic # Send the data to New Relic
url = "https://rpm.newrelic.com/deployments.xml" url = "https://api.newrelic.com/v2/applications/%s/deployments.json" % quote(str(app_id), safe='')
data = urlencode(params) data = {
headers = { 'deployment': params
'x-api-key': module.params["token"],
} }
response, info = fetch_url(module, url, data=data, headers=headers) 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): if info['status'] in (200, 201):
module.exit_json(changed=True) module.exit_json(changed=True)
else: else:
module.fail_json(msg="unable to update newrelic: %s" % info['msg']) 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"]
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"])
return result["applications"][0]["id"]
if __name__ == '__main__': if __name__ == '__main__':