From 196d3205cab45473e75867e1243a1009af878607 Mon Sep 17 00:00:00 2001 From: Patrick Humpal Date: Sun, 17 May 2020 00:42:08 -0700 Subject: [PATCH] Use api v4 for airbrake deploy notices (#124) * Use api v4 for airbrake deploy notices * fix invalid deprecated version changes 2.14 to 2.12 * revert * ignore it --- ..._deployments-api_v4_for_deploy_notices.yml | 8 ++ .../modules/monitoring/airbrake_deployment.py | 118 +++++++++++++----- tests/sanity/ignore-2.10.txt | 3 +- tests/sanity/ignore-2.9.txt | 2 - 4 files changed, 96 insertions(+), 35 deletions(-) create mode 100644 changelogs/fragments/124-airbrake_deployments-api_v4_for_deploy_notices.yml diff --git a/changelogs/fragments/124-airbrake_deployments-api_v4_for_deploy_notices.yml b/changelogs/fragments/124-airbrake_deployments-api_v4_for_deploy_notices.yml new file mode 100644 index 0000000000..9253ebf220 --- /dev/null +++ b/changelogs/fragments/124-airbrake_deployments-api_v4_for_deploy_notices.yml @@ -0,0 +1,8 @@ +bugfixes: +- "airbrake_deployment - Allow deploy notifications for Airbrake compatible v2 api (e.g. Errbit)" + +minor_changes: +- "airbrake_deployment - Allow passing ``project_id`` and ``project_key`` for v4 api deploy compatibility" + +deprecated_features: +- "airbrake_deployment - Add deprecation notice for ``token`` parameter and v2 api deploys. This feature will be removed in Ansible 2.14" diff --git a/plugins/modules/monitoring/airbrake_deployment.py b/plugins/modules/monitoring/airbrake_deployment.py index dd1af0fd6f..3c76af046e 100644 --- a/plugins/modules/monitoring/airbrake_deployment.py +++ b/plugins/modules/monitoring/airbrake_deployment.py @@ -11,36 +11,50 @@ __metaclass__ = type DOCUMENTATION = ''' --- module: airbrake_deployment -author: "Bruce Pennypacker (@bpennypacker)" +author: +- "Bruce Pennypacker (@bpennypacker)" +- "Patrick Humpal (@phumpal)" short_description: Notify airbrake about app deployments description: - - Notify airbrake about app deployments (see http://help.airbrake.io/kb/api-2/deploy-tracking) + - Notify airbrake about app deployments (see U(https://airbrake.io/docs/api/#deploys-v4)). + - Parameter I(token) is deprecated in Ansible 2.10. Please remove entry. options: - token: + project_id: description: - - API token. - required: true + - Airbrake PROJECT_ID + required: false + type: str + project_key: + description: + - Airbrake PROJECT_KEY. + required: false + type: str environment: description: - The airbrake environment name, typically 'production', 'staging', etc. required: true + type: str user: description: - The username of the person doing the deployment required: false + type: str repo: description: - URL of the project repository required: false + type: str revision: description: - A hash, number, tag, or other identifier showing what revision was deployed required: false + type: str url: description: - Optional URL to submit the notification to. Use to send notifications to Airbrake-compliant tools like Errbit. required: false - default: "https://airbrake.io/deploys.txt" + default: "https://api.airbrake.io/api/v4/projects/" + type: str validate_certs: description: - If C(no), SSL certificates for the target url will not be validated. This should only be used @@ -48,6 +62,11 @@ options: required: false default: 'yes' type: bool + token: + description: + - This parameter (API token) has been deprecated in Ansible 2.10. Please remove it from your tasks. + required: false + type: str requirements: [] ''' @@ -55,7 +74,8 @@ requirements: [] EXAMPLES = ''' - name: Notify airbrake about an app deployment airbrake_deployment: - token: AAAAAA + project_id: '12345' + project_key: 'AAAAAA' environment: staging user: ansible revision: '4.2' @@ -74,44 +94,80 @@ def main(): module = AnsibleModule( argument_spec=dict( - token=dict(required=True, no_log=True), + token=dict(required=False, no_log=True), + project_id=dict(required=False, no_log=True), + project_key=dict(required=False, no_log=True), environment=dict(required=True), user=dict(required=False), repo=dict(required=False), revision=dict(required=False), - url=dict(required=False, default='https://api.airbrake.io/deploys.txt'), + url=dict(required=False, default='https://api.airbrake.io/api/v4/projects/'), validate_certs=dict(default='yes', type='bool'), ), - supports_check_mode=True + supports_check_mode=True, + required_together=[('project_id', 'project_key')], + mutually_exclusive=[('project_id', 'token')], ) - # build list of params + # Build list of params params = {} - if module.params["environment"]: - params["deploy[rails_env]"] = module.params["environment"] - - if module.params["user"]: - params["deploy[local_username]"] = module.params["user"] - - if module.params["repo"]: - params["deploy[scm_repository]"] = module.params["repo"] - - if module.params["revision"]: - params["deploy[scm_revision]"] = module.params["revision"] - - params["api_key"] = module.params["token"] - - url = module.params.get('url') - # 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 airbrake - data = urlencode(params) - response, info = fetch_url(module, url, data=data) - if info['status'] == 200: + if module.params["token"]: + if module.params["environment"]: + params["deploy[rails_env]"] = module.params["environment"] + + if module.params["user"]: + params["deploy[local_username]"] = module.params["user"] + + if module.params["repo"]: + params["deploy[scm_repository]"] = module.params["repo"] + + if module.params["revision"]: + params["deploy[scm_revision]"] = module.params["revision"] + + module.deprecate("Parameter 'token' is deprecated in 2.10. Please remove it and use 'project_id' and 'project_key' instead", version='2.14') + + params["api_key"] = module.params["token"] + + # Allow sending to Airbrake compliant v2 APIs + if module.params["url"] == 'https://api.airbrake.io/api/v4/projects/': + url = 'https://api.airbrake.io/deploys.txt' + else: + url = module.params["url"] + + # Send the data to airbrake + data = urlencode(params) + response, info = fetch_url(module, url, data=data) + + if module.params["project_id"] and module.params["project_key"]: + if module.params["environment"]: + params["environment"] = module.params["environment"] + + if module.params["user"]: + params["username"] = module.params["user"] + + if module.params["repo"]: + params["repository"] = module.params["repo"] + + if module.params["revision"]: + params["revision"] = module.params["revision"] + + # Build deploy url + url = module.params.get('url') + module.params["project_id"] + '/deploys?key=' + module.params["project_key"] + json_body = module.jsonify(params) + + # Build header + headers = {'Content-Type': 'application/json'} + + # Notify Airbrake of deploy + response, info = fetch_url(module, url, data=json_body, + headers=headers, method='POST') + + if info['status'] == 200 or info['status'] == 201: module.exit_json(changed=True) else: module.fail_json(msg="HTTP result code: %d connecting to %s" % (info['status'], url)) diff --git a/tests/sanity/ignore-2.10.txt b/tests/sanity/ignore-2.10.txt index 8fcb4fa757..15603c29ec 100644 --- a/tests/sanity/ignore-2.10.txt +++ b/tests/sanity/ignore-2.10.txt @@ -998,8 +998,7 @@ plugins/modules/identity/onepassword_info.py pylint:collection-invalid-deprecate plugins/modules/identity/onepassword_info.py validate-modules:parameter-list-no-elements plugins/modules/identity/opendj/opendj_backendprop.py validate-modules:doc-missing-type plugins/modules/identity/opendj/opendj_backendprop.py validate-modules:parameter-type-not-in-doc -plugins/modules/monitoring/airbrake_deployment.py validate-modules:doc-default-does-not-match-spec -plugins/modules/monitoring/airbrake_deployment.py validate-modules:doc-missing-type +plugins/modules/monitoring/airbrake_deployment.py pylint:collection-invalid-deprecated-version plugins/modules/monitoring/bigpanda.py validate-modules:collection-invalid-version plugins/modules/monitoring/bigpanda.py validate-modules:doc-default-does-not-match-spec plugins/modules/monitoring/bigpanda.py validate-modules:doc-missing-type diff --git a/tests/sanity/ignore-2.9.txt b/tests/sanity/ignore-2.9.txt index ce25b066ef..222dd73dc5 100644 --- a/tests/sanity/ignore-2.9.txt +++ b/tests/sanity/ignore-2.9.txt @@ -699,8 +699,6 @@ plugins/modules/identity/keycloak/keycloak_clienttemplate.py validate-modules:do plugins/modules/identity/keycloak/keycloak_clienttemplate.py validate-modules:parameter-type-not-in-doc plugins/modules/identity/opendj/opendj_backendprop.py validate-modules:doc-missing-type plugins/modules/identity/opendj/opendj_backendprop.py validate-modules:parameter-type-not-in-doc -plugins/modules/monitoring/airbrake_deployment.py validate-modules:doc-default-does-not-match-spec -plugins/modules/monitoring/airbrake_deployment.py validate-modules:doc-missing-type plugins/modules/monitoring/bigpanda.py validate-modules:doc-default-does-not-match-spec plugins/modules/monitoring/bigpanda.py validate-modules:doc-missing-type plugins/modules/monitoring/bigpanda.py validate-modules:undocumented-parameter