1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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 <felix@fontein.de>

* Update changelogs/fragments/7355-newrelic-deployment-add-exact-name.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/newrelic_deployment.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/newrelic_deployment.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/newrelic_deployment.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/newrelic_deployment.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Write out abbreviation.

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
matiasba 2023-10-08 13:24:43 -03:00 committed by GitHub
parent 323a1dcff8
commit 85e866ccdd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View file

@ -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)."

View file

@ -68,6 +68,14 @@ options:
required: false required: false
default: true default: true
type: bool 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: [] requirements: []
''' '''
@ -102,8 +110,10 @@ def main():
revision=dict(required=True), revision=dict(required=True),
user=dict(required=False), user=dict(required=False),
validate_certs=dict(default=True, type='bool'), 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_one_of=[['app_name', 'application_id']],
required_if=[('app_name_exact_match', True, ['app_name'])],
supports_check_mode=True supports_check_mode=True
) )
@ -111,7 +121,6 @@ def main():
params = {} params = {}
if module.params["app_name"] and module.params["application_id"]: 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") module.fail_json(msg="only one of 'app_name' or 'application_id' can be set")
app_id = None app_id = None
if module.params["app_name"]: if module.params["app_name"]:
app_id = get_application_id(module) app_id = get_application_id(module)
@ -150,6 +159,7 @@ def main():
def get_application_id(module): def get_application_id(module):
url = "https://api.newrelic.com/v2/applications.json" url = "https://api.newrelic.com/v2/applications.json"
data = "filter[name]=%s" % module.params["app_name"] data = "filter[name]=%s" % module.params["app_name"]
application_id = None
headers = { headers = {
'Api-Key': module.params["token"], 'Api-Key': module.params["token"],
} }
@ -161,7 +171,17 @@ def get_application_id(module):
if result is None or len(result.get("applications", "")) == 0: if result is None or len(result.get("applications", "")) == 0:
module.fail_json(msg='No application found with name "%s"' % module.params["app_name"]) 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__': if __name__ == '__main__':