From 85e866ccdd6cdbd30377f57acc2235b5aca8c711 Mon Sep 17 00:00:00 2001 From: matiasba Date: Sun, 8 Oct 2023 13:24:43 -0300 Subject: [PATCH] newrelic_deployment: add app_name_exact_match parameter (#7355) * newrelic_deployment: add app_name_exact_match parameter * add changelog * fix ident * fix line ending format * Update plugins/modules/newrelic_deployment.py Co-authored-by: Felix Fontein * Update changelogs/fragments/7355-newrelic-deployment-add-exact-name.yml Co-authored-by: Felix Fontein * Update plugins/modules/newrelic_deployment.py Co-authored-by: Felix Fontein * Update plugins/modules/newrelic_deployment.py Co-authored-by: Felix Fontein * Update plugins/modules/newrelic_deployment.py Co-authored-by: Felix Fontein * Update plugins/modules/newrelic_deployment.py Co-authored-by: Felix Fontein * Write out abbreviation. --------- Co-authored-by: Felix Fontein --- ...355-newrelic-deployment-add-exact-name.yml | 2 ++ plugins/modules/newrelic_deployment.py | 24 +++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/7355-newrelic-deployment-add-exact-name.yml diff --git a/changelogs/fragments/7355-newrelic-deployment-add-exact-name.yml b/changelogs/fragments/7355-newrelic-deployment-add-exact-name.yml new file mode 100644 index 0000000000..f077a99056 --- /dev/null +++ b/changelogs/fragments/7355-newrelic-deployment-add-exact-name.yml @@ -0,0 +1,2 @@ +minor_changes: + - "newrelic_deployment - add option ``app_name_exact_match``, which filters results for the exact app_name provided (https://github.com/ansible-collections/community.general/pull/7355)." \ No newline at end of file diff --git a/plugins/modules/newrelic_deployment.py b/plugins/modules/newrelic_deployment.py index 9b52f34557..e5a1160822 100644 --- a/plugins/modules/newrelic_deployment.py +++ b/plugins/modules/newrelic_deployment.py @@ -68,6 +68,14 @@ options: required: false default: true type: bool + app_name_exact_match: + type: bool + description: + - If this flag is set to V(true) then the application ID lookup by name would only work for an exact match. + If set to V(false) it returns the first result. + required: false + default: false + version_added: 7.5.0 requirements: [] ''' @@ -102,8 +110,10 @@ def main(): revision=dict(required=True), user=dict(required=False), validate_certs=dict(default=True, type='bool'), + app_name_exact_match=dict(required=False, type='bool', default=False), ), required_one_of=[['app_name', 'application_id']], + required_if=[('app_name_exact_match', True, ['app_name'])], supports_check_mode=True ) @@ -111,7 +121,6 @@ def main(): 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") - app_id = None if module.params["app_name"]: app_id = get_application_id(module) @@ -150,6 +159,7 @@ def main(): def get_application_id(module): url = "https://api.newrelic.com/v2/applications.json" data = "filter[name]=%s" % module.params["app_name"] + application_id = None headers = { 'Api-Key': module.params["token"], } @@ -161,7 +171,17 @@ def get_application_id(module): 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 module.params["app_name_exact_match"]: + for item in result["applications"]: + if item["name"] == module.params["app_name"]: + application_id = item["id"] + break + if application_id is None: + module.fail_json(msg='No application found with exact name "%s"' % module.params["app_name"]) + else: + application_id = result["applications"][0]["id"] + + return application_id if __name__ == '__main__':