Repository: ansible-collections/community.general

Issue:
community.general.newrelic_deployment is broken
### Summary

The module is using a NewRelic endpoint that is no longer available
` # Send the data to NewRelic
    url = "https://rpm.newrelic.com/deployments.xml"
    data = urlencode(params)
    headers = {
        'x-api-key': module.params["token"],
    }`

It should be https://api.newrelic.com/v2/applications/{application_id}/deployments.json
[Here](https://rpm.newrelic.com/api/explore/application_deployments/create) is the documentation 

### Issue Type

Bug Report

### Component Name

community.general.newrelic_deployment

### Ansible Version

```
  ansible 2.10.8
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.9.2 (default, Feb 28 2021, 17:03:44) [GCC 10.2.1 20210110]
```


### Community.general Version

```
Collection        Version
----------------- -------
community.general 5.0.1
```

### Steps to Reproduce

```
---
- name: Deploy
  hosts: 127.0.0.1
  tasks:
  - name:  Notify newrelic about an app deployment
    community.general.newrelic_deployment:
      token: <insert-token>
      app_name: 'some_app'
      revision: 'v7.11'
```


### Expected Results

I expected the module to work, got a http code 403 instead

### Actual Results

```
FAILED! => {"changed": false, "msg": "unable to update newrelic: HTTP Error 403: Forbidden"}
```


### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct


Discussion:
Files identified in the description:
* [`plugins/modules/monitoring/newrelic_deployment.py`](https://github.com/ansible-collections/community.general/blob/main/plugins/modules/monitoring/newrelic_deployment.py)

If these files are incorrect, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @mcodd
[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Hi!  I apologize, but I'm not able to maintain this module as I don't use or have access to newrelic anymore - @matiasba would you mind putting in a PR for the fix you are looking for here?  I suspect given the brokenness you found, that this module is not heavily used and whatever you propose to fix things should be accepted as at the very least a step in the right direction!
@mcodd thanks for authoring and maintaining the module in the past! If you no longer want to be pinged for PRs and issues for this module, you need to create a PR to change `.github/BOTMETA.yml` where you change the line `maintainers: mcodd` for `newrelic_deployment` into `ignore: mcodd`.
> Hi! I apologize, but I'm not able to maintain this module as I don't use or have access to newrelic anymore - @matiasba would you mind putting in a PR for the fix you are looking for here? I suspect given the brokenness you found, that this module is not heavily used and whatever you propose to fix things should be accepted as at the very least a step in the right direction!

I can give it a try sure! I will look into it this week 

Resolving patch:
diff --git a/plugins/modules/monitoring/newrelic_deployment.py b/plugins/modules/monitoring/newrelic_deployment.py
--- a/plugins/modules/monitoring/newrelic_deployment.py
+++ b/plugins/modules/monitoring/newrelic_deployment.py
@@ -13,24 +13,26 @@
 ---
 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 @@
     type: str
     description:
       - A revision number (e.g., git commit SHA)
-    required: false
+    required: true
   user:
     type: str
     description:
@@ -55,12 +57,14 @@
   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 @@
     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 @@
 
 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)
+    # 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 = {
-        'x-api-key': module.params["token"],
+        'Api-Key': module.params["token"],
+        'Content-Type': 'application/json',
     }
-    response, info = fetch_url(module, url, data=data, headers=headers)
+    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__':


Test patch: