From 7e2a6cf1984781c523e62c31084f028226ef200d Mon Sep 17 00:00:00 2001
From: Wei Liao <wliao008@gmail.com>
Date: Wed, 12 Oct 2022 04:42:17 -0400
Subject: [PATCH] Updated to use the new newrelic v2 api (#5341)

* 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
---
 .../5341-newrelic-v2-api-changes.yml          |  6 ++
 .../modules/monitoring/newrelic_deployment.py | 76 +++++++++++++------
 2 files changed, 58 insertions(+), 24 deletions(-)
 create mode 100644 changelogs/fragments/5341-newrelic-v2-api-changes.yml

diff --git a/changelogs/fragments/5341-newrelic-v2-api-changes.yml b/changelogs/fragments/5341-newrelic-v2-api-changes.yml
new file mode 100644
index 0000000000..af47cc8cf3
--- /dev/null
+++ b/changelogs/fragments/5341-newrelic-v2-api-changes.yml
@@ -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).
diff --git a/plugins/modules/monitoring/newrelic_deployment.py b/plugins/modules/monitoring/newrelic_deployment.py
index 2fd4faf8e3..91f49fbe65 100644
--- a/plugins/modules/monitoring/newrelic_deployment.py
+++ b/plugins/modules/monitoring/newrelic_deployment.py
@@ -13,24 +13,26 @@ DOCUMENTATION = '''
 ---
 module: newrelic_deployment
 author: "Matt Coddington (@mcodd)"
-short_description: Notify newrelic about app deployments
+short_description: Notify New Relic about app deployments
 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:
   token:
     type: str
     description:
-      - API token, to place in the x-api-key header.
+      - API token to place in the Api-Key header.
     required: true
   app_name:
     type: str
     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
   application_id:
     type: str
     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
   changelog:
     type: str
@@ -46,7 +48,7 @@ options:
     type: str
     description:
       - A revision number (e.g., git commit SHA)
-    required: false
+    required: true
   user:
     type: str
     description:
@@ -55,12 +57,14 @@ options:
   appname:
     type: str
     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
   environment:
     type: str
     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
   validate_certs:
     description:
@@ -69,12 +73,11 @@ options:
     required: false
     default: true
     type: bool
-
 requirements: []
 '''
 
 EXAMPLES = '''
-- name:  Notify newrelic about an app deployment
+- name:  Notify New Relic about an app deployment
   community.general.newrelic_deployment:
     token: AAAAAA
     app_name: myapp
@@ -84,7 +87,8 @@ EXAMPLES = '''
 
 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
+from ansible.module_utils.six.moves.urllib.parse import quote
+import json
 
 # ===========================================
 # Module execution.
@@ -100,10 +104,10 @@ 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),
+            appname=dict(required=False, removed_in_version='7.0.0', removed_from_collection='community.general'),
+            environment=dict(required=False, removed_in_version='7.0.0', removed_from_collection='community.general'),
             validate_certs=dict(default=True, type='bool'),
         ),
         required_one_of=[['app_name', 'application_id']],
@@ -115,14 +119,18 @@ def main():
     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"]:
-        params["app_name"] = module.params["app_name"]
+        app_id = get_application_id(module)
     elif module.params["application_id"]:
-        params["application_id"] = 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'")
 
-    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]:
             params[item] = module.params[item]
 
@@ -130,17 +138,37 @@ def main():
     if module.check_mode:
         module.exit_json(changed=True)
 
-    # Send the data to NewRelic
-    url = "https://rpm.newrelic.com/deployments.xml"
-    data = urlencode(params)
-    headers = {
-        'x-api-key': module.params["token"],
+    # Send the data to New Relic
+    url = "https://api.newrelic.com/v2/applications/%s/deployments.json" % quote(str(app_id), safe='')
+    data = {
+        'deployment': params
     }
-    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):
         module.exit_json(changed=True)
     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__':