From 4481e3d04ae302bb616ea38d8b53dd310806d790 Mon Sep 17 00:00:00 2001 From: Davinder Pal Date: Thu, 17 Dec 2020 00:54:38 +0200 Subject: [PATCH 1/7] * updated code to use v2 api --- .../modules/monitoring/newrelic_deployment.py | 144 +++++++++--------- 1 file changed, 73 insertions(+), 71 deletions(-) diff --git a/plugins/modules/monitoring/newrelic_deployment.py b/plugins/modules/monitoring/newrelic_deployment.py index af953e0a759..67a2fab04e9 100644 --- a/plugins/modules/monitoring/newrelic_deployment.py +++ b/plugins/modules/monitoring/newrelic_deployment.py @@ -1,95 +1,82 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# Copyright 2013 Matt Coddington -# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +# Copyright 2020 Davinder Pal +# GNU General Public License v3.0+ +# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function __metaclass__ = type - DOCUMENTATION = ''' --- module: newrelic_deployment -author: "Matt Coddington (@mcodd)" -short_description: Notify newrelic about app deployments +version_added: "1.2.0" +author: + - "Davinder Pal (@116davinder)" + - "Matt Coddington (@mcodd)" +short_description: Notify newrelic about app deployments using newrelic v2 api description: - - Notify newrelic about app deployments (see https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/deployment-notifications#api) + - Notify newrelic about app deployments + (https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/record-deployments) options: token: - type: str description: - API token, to place in the x-api-key header. required: true - app_name: type: str + app_name: description: - - (one of app_name or application_id are required) The value of app_name in the newrelic.yml file used by the application + - (one of app_name or application_id is required) + The value of app_name in the newrelic.yml file used by the application required: false - application_id: type: str + application_id: description: - - (one of app_name or application_id are required) The application id, found in the URL when viewing the application in RPM + - (one of app_name or application_id is required) + (see https://rpm.newrelic.com/api/explore/applications/list) required: false - changelog: type: str + changelog: description: - A list of changes for this deployment required: false - description: type: str + description: description: - Text annotation for the deployment - notes for you required: false - revision: type: str + revision: description: - A revision number (e.g., git commit SHA) - required: false - user: + required: true type: str + user: description: - The name of the user/process that triggered this deployment required: false - appname: type: str - description: - - Name of the application - required: false - environment: - type: str - description: - - The environment for this deployment - required: false - validate_certs: - description: - - If C(no), SSL certificates will not be validated. This should only be used - on personally controlled sites using self-signed certificates. - required: false - default: 'yes' - type: bool - -requirements: [] ''' EXAMPLES = ''' - name: Notify newrelic about an app deployment community.general.newrelic_deployment: - token: AAAAAA - app_name: myapp - user: ansible deployment - revision: '1.0' + token: XXXXXXXXX + app_name: ansible_app + user: ansible_deployment_user + revision: '1.X' ''' from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.urls import fetch_url -from ansible.module_utils.six.moves.urllib.parse import urlencode +import json + # =========================================== # Module execution. # - def main(): module = AnsibleModule( @@ -99,47 +86,62 @@ def main(): application_id=dict(required=False), changelog=dict(required=False), description=dict(required=False), - revision=dict(required=False), + revision=dict(required=True), user=dict(required=False), - appname=dict(required=False), - environment=dict(required=False), - validate_certs=dict(default=True, type='bool'), ), - required_one_of=[['app_name', 'application_id']], - supports_check_mode=True + required_one_of=[['app_name', 'application_id']] ) - # 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") - - if module.params["app_name"]: - params["app_name"] = module.params["app_name"] - elif module.params["application_id"]: - params["application_id"] = module.params["application_id"] + if module.params['app_name'] and module.params['application_id']: + module.fail_json(msg="both app_name' and 'application_id'\ + are defined") + + if module.params['app_name']: + data = 'filter[name]=' + str(module.params['app_name']) + newrelic_api = 'https://api.newrelic.com/v2/applications.json' + headers = {'x-api-key': module.params['token'], + 'Content-Type': 'application/x-www-form-urlencoded'} + (resp, info) = fetch_url(module, + newrelic_api, + headers=headers, + data=data, + method='GET') + if info['status'] != 200: + module.fail_json(msg="unable to get application list from\ + newrelic: %s" % info['msg']) + else: + body = json.loads(resp.read()) + if body is None: + module.fail_json(msg='No Data for applications') + else: + app_id = body['applications'][0]['id'] + if app_id is None: + module.fail_json(msg="App not found in\ + NewRelic Registerd Applications List") else: - module.fail_json(msg="you must set one of 'app_name' or 'application_id'") - - for item in ["changelog", "description", "revision", "user", "appname", "environment"]: - 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) + app_id = module.params['application_id'] # Send the data to NewRelic - url = "https://rpm.newrelic.com/deployments.xml" - data = urlencode(params) - headers = { - 'x-api-key': module.params["token"], + url = 'https://api.newrelic.com/v2/applications/' + str(app_id) \ + + '/deployments.json' + data = { + 'deployment': { + 'revision': str(module.params['revision']), + 'changelog': str(module.params['changelog']), + 'description': str(module.params['description']), + 'user': str(module.params['user']), } } - response, info = fetch_url(module, url, data=data, headers=headers) - if info['status'] in (200, 201): + + headers = {'x-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'] == 201: module.exit_json(changed=True) else: - module.fail_json(msg="unable to update newrelic: %s" % info['msg']) + module.fail_json(msg='unable to update newrelic: %s' + % info['msg']) if __name__ == '__main__': From a4b00536138a3a0a92ce61432d4aeadcfdbd3299 Mon Sep 17 00:00:00 2001 From: Davinder Pal Date: Thu, 17 Dec 2020 01:05:02 +0200 Subject: [PATCH 2/7] * added changelog fragment --- changelogs/fragments/1501_newrelic_v2_api_change.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelogs/fragments/1501_newrelic_v2_api_change.yml diff --git a/changelogs/fragments/1501_newrelic_v2_api_change.yml b/changelogs/fragments/1501_newrelic_v2_api_change.yml new file mode 100644 index 00000000000..e061449e1d9 --- /dev/null +++ b/changelogs/fragments/1501_newrelic_v2_api_change.yml @@ -0,0 +1,3 @@ +major_changes: + - removed v1 newrelic api + - added support for v2 newrelic api (https://github.com/ansible-collections/community.general/pull/1501). From 2aba43efb71dca4b9a5b7c14a187af616ea412d2 Mon Sep 17 00:00:00 2001 From: Davinder Pal Date: Thu, 17 Dec 2020 20:07:41 +0200 Subject: [PATCH 3/7] * added suggested changes. --- .../fragments/1501_newrelic_v2_api_change.yml | 6 ++- .../modules/monitoring/newrelic_deployment.py | 48 ++++++++++++------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/changelogs/fragments/1501_newrelic_v2_api_change.yml b/changelogs/fragments/1501_newrelic_v2_api_change.yml index e061449e1d9..8a8578f5fc1 100644 --- a/changelogs/fragments/1501_newrelic_v2_api_change.yml +++ b/changelogs/fragments/1501_newrelic_v2_api_change.yml @@ -1,3 +1,5 @@ major_changes: - - removed v1 newrelic api - - added support for v2 newrelic api (https://github.com/ansible-collections/community.general/pull/1501). + - newrelic_deployment - removed newrelic v1 API, added support for v2 API (https://github.com/ansible-collections/community.general/pull/1501). +breaking_changes: + - newrelic_deployment - I(revision) is required in v2 API. + - newrelic_deployment - check_mode is not supported. \ No newline at end of file diff --git a/plugins/modules/monitoring/newrelic_deployment.py b/plugins/modules/monitoring/newrelic_deployment.py index 67a2fab04e9..1b9ca642dff 100644 --- a/plugins/modules/monitoring/newrelic_deployment.py +++ b/plugins/modules/monitoring/newrelic_deployment.py @@ -1,7 +1,7 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# Copyright 2020 Davinder Pal +# Copyright 2013 Matt Coddington # GNU General Public License v3.0+ # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) @@ -11,14 +11,13 @@ DOCUMENTATION = ''' --- module: newrelic_deployment -version_added: "1.2.0" author: - "Davinder Pal (@116davinder)" - "Matt Coddington (@mcodd)" -short_description: Notify newrelic about app deployments using newrelic v2 api +short_description: Notify newrelic about app deployments description: - Notify newrelic about app deployments - (https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/record-deployments) + (U(https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/record-deployments)). options: token: description: @@ -27,31 +26,48 @@ type: str app_name: description: - - (one of app_name or application_id is required) - The value of app_name in the newrelic.yml file used by the application + - The value of C(app_name) in the C(newrelic.yml) file used by the application. + - Exactly one of I(app_name) or I(application_id) is required. required: false type: str application_id: description: - - (one of app_name or application_id is required) - (see https://rpm.newrelic.com/api/explore/applications/list) + - See U(https://rpm.newrelic.com/api/explore/applications/list). + - Exactly one of I(app_name) or I(application_id) is required. required: false type: str changelog: description: - - A list of changes for this deployment + - A list of changes for this deployment. required: false type: str description: description: - - Text annotation for the deployment - notes for you + - Text annotation for the deployment - notes for you. required: false type: str revision: description: - - A revision number (e.g., git commit SHA) + - A revision number (for example, git commit SHA). required: true type: str + appname: + type: str + description: + - (deprecated) Name of the application + required: false + environment: + type: str + description: + - (deprecated) The environment for this deployment. + required: false + validate_certs: + description: + - (deprecated) If C(no), SSL certificates will not be validated. This should only be used + on personally controlled sites using self-signed certificates. + required: false + default: 'yes' + type: bool user: description: - The name of the user/process that triggered this deployment @@ -89,13 +105,10 @@ def main(): revision=dict(required=True), user=dict(required=False), ), - required_one_of=[['app_name', 'application_id']] + required_one_of=[['app_name', 'application_id']], + mutually_exclusive=[['app_name', 'application_id']], ) - if module.params['app_name'] and module.params['application_id']: - module.fail_json(msg="both app_name' and 'application_id'\ - are defined") - if module.params['app_name']: data = 'filter[name]=' + str(module.params['app_name']) newrelic_api = 'https://api.newrelic.com/v2/applications.json' @@ -107,8 +120,7 @@ def main(): data=data, method='GET') if info['status'] != 200: - module.fail_json(msg="unable to get application list from\ - newrelic: %s" % info['msg']) + module.fail_json(msg="unable to get application list from newrelic: %s" % info['msg']) else: body = json.loads(resp.read()) if body is None: From c3da10a79a4a76bc02d94998e0e181335594b115 Mon Sep 17 00:00:00 2001 From: Davinder Pal Date: Thu, 17 Dec 2020 20:15:22 +0200 Subject: [PATCH 4/7] * trailing spaces --- .../modules/monitoring/newrelic_deployment.py | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/plugins/modules/monitoring/newrelic_deployment.py b/plugins/modules/monitoring/newrelic_deployment.py index 1b9ca642dff..294a673746b 100644 --- a/plugins/modules/monitoring/newrelic_deployment.py +++ b/plugins/modules/monitoring/newrelic_deployment.py @@ -51,22 +51,22 @@ - A revision number (for example, git commit SHA). required: true type: str - appname: + appname: type: str - description: - - (deprecated) Name of the application - required: false - environment: - type: str - description: + description: + - (deprecated) Name of the application + required: false + environment: + type: str + description: - (deprecated) The environment for this deployment. - required: false - validate_certs: - description: - - (deprecated) If C(no), SSL certificates will not be validated. This should only be used - on personally controlled sites using self-signed certificates. - required: false - default: 'yes' + required: false + validate_certs: + description: + - (deprecated) If C(no), SSL certificates will not be validated. This should only be used + on personally controlled sites using self-signed certificates. + required: false + default: 'yes' type: bool user: description: From 05fa8e07ee8ab42209785cdc94b3f88f76cc895e Mon Sep 17 00:00:00 2001 From: Davinder Pal Date: Thu, 17 Dec 2020 20:18:11 +0200 Subject: [PATCH 5/7] * added deprecated args to argument_spec --- plugins/modules/monitoring/newrelic_deployment.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/modules/monitoring/newrelic_deployment.py b/plugins/modules/monitoring/newrelic_deployment.py index 294a673746b..c8c0251e95d 100644 --- a/plugins/modules/monitoring/newrelic_deployment.py +++ b/plugins/modules/monitoring/newrelic_deployment.py @@ -104,6 +104,9 @@ def main(): description=dict(required=False), revision=dict(required=True), user=dict(required=False), + appname=dict(required=False), + environment=dict(required=False), + validate_certs=dict(default=True, type='bool'), ), required_one_of=[['app_name', 'application_id']], mutually_exclusive=[['app_name', 'application_id']], From 0452808080521a43e8d827d6aca145c0a6c79bb6 Mon Sep 17 00:00:00 2001 From: Davinder Pal Date: Sat, 19 Dec 2020 22:57:52 +0200 Subject: [PATCH 6/7] * added more suggested changes --- .../fragments/1501_newrelic_v2_api_change.yml | 6 +++-- .../modules/monitoring/newrelic_deployment.py | 27 ++++++++++--------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/changelogs/fragments/1501_newrelic_v2_api_change.yml b/changelogs/fragments/1501_newrelic_v2_api_change.yml index 8a8578f5fc1..c66229c0986 100644 --- a/changelogs/fragments/1501_newrelic_v2_api_change.yml +++ b/changelogs/fragments/1501_newrelic_v2_api_change.yml @@ -1,5 +1,7 @@ major_changes: - newrelic_deployment - removed newrelic v1 API, added support for v2 API (https://github.com/ansible-collections/community.general/pull/1501). breaking_changes: - - newrelic_deployment - I(revision) is required in v2 API. - - newrelic_deployment - check_mode is not supported. \ No newline at end of file + - newrelic_deployment - the ``revision`` option is required in v2 API (https://github.com/ansible-collections/community.general/pull/1501). + - newrelic_deployment - check_mode is not supported. +deprecated_features: + - newrelic_deployment - ``check_mode`` , ``appname`` , ``environment`` and ``validate_certs`` are deprecated as not required in v2 API. \ No newline at end of file diff --git a/plugins/modules/monitoring/newrelic_deployment.py b/plugins/modules/monitoring/newrelic_deployment.py index c8c0251e95d..462ad1cb0f9 100644 --- a/plugins/modules/monitoring/newrelic_deployment.py +++ b/plugins/modules/monitoring/newrelic_deployment.py @@ -32,6 +32,7 @@ type: str application_id: description: + - The I(application_id), found in the URL when viewing the application in RPM. - See U(https://rpm.newrelic.com/api/explore/applications/list). - Exactly one of I(app_name) or I(application_id) is required. required: false @@ -54,17 +55,20 @@ appname: type: str description: - - (deprecated) Name of the application + - Name of the application. The value is not used for the v2 API. + - This option has been deprecated and will be removed in community.general 4.0.0. required: false environment: type: str description: - - (deprecated) The environment for this deployment. + - Name of the environment for this deployment. The value is not used for the v2 API. + - This option has been deprecated and will be removed in community.general 4.0.0. required: false validate_certs: description: - - (deprecated) If C(no), SSL certificates will not be validated. This should only be used - on personally controlled sites using self-signed certificates. + - If C(no), SSL certificates will not be validated. The value is not used for the v2 API. + - This should only be used on personally controlled sites using self-signed certificates. + - This option has been deprecated and will be removed in community.general 4.0.0. required: false default: 'yes' type: bool @@ -104,9 +108,9 @@ def main(): description=dict(required=False), revision=dict(required=True), user=dict(required=False), - appname=dict(required=False), - environment=dict(required=False), - validate_certs=dict(default=True, type='bool'), + appname=dict(required=False, removed_in_version='4.0.0', removed_from_collection='community.general'), + environment=dict(required=False, removed_in_version='4.0.0', removed_from_collection='community.general'), + validate_certs=dict(default=True, type='bool', removed_in_version='4.0.0', removed_from_collection='community.general'), ), required_one_of=[['app_name', 'application_id']], mutually_exclusive=[['app_name', 'application_id']], @@ -131,14 +135,12 @@ def main(): else: app_id = body['applications'][0]['id'] if app_id is None: - module.fail_json(msg="App not found in\ - NewRelic Registerd Applications List") + module.fail_json(msg="App not found in NewRelic Registerd Applications List") else: app_id = module.params['application_id'] # Send the data to NewRelic - url = 'https://api.newrelic.com/v2/applications/' + str(app_id) \ - + '/deployments.json' + url = 'https://api.newrelic.com/v2/applications/' + str(app_id) + '/deployments.json' data = { 'deployment': { 'revision': str(module.params['revision']), @@ -155,8 +157,7 @@ def main(): if info['status'] == 201: module.exit_json(changed=True) else: - module.fail_json(msg='unable to update newrelic: %s' - % info['msg']) + module.fail_json(msg='unable to update newrelic: %s' % info['msg']) if __name__ == '__main__': From 070ceb591fe744c03b547171f52e7fda788b2374 Mon Sep 17 00:00:00 2001 From: DAVINDER PAL Date: Sat, 26 Jun 2021 22:19:58 +0530 Subject: [PATCH 7/7] Update plugins/modules/monitoring/newrelic_deployment.py Co-authored-by: Felix Fontein --- plugins/modules/monitoring/newrelic_deployment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/monitoring/newrelic_deployment.py b/plugins/modules/monitoring/newrelic_deployment.py index 462ad1cb0f9..9a72eadb315 100644 --- a/plugins/modules/monitoring/newrelic_deployment.py +++ b/plugins/modules/monitoring/newrelic_deployment.py @@ -66,7 +66,7 @@ required: false validate_certs: description: - - If C(no), SSL certificates will not be validated. The value is not used for the v2 API. + - If C(no), SSL certificates will not be validated. - This should only be used on personally controlled sites using self-signed certificates. - This option has been deprecated and will be removed in community.general 4.0.0. required: false