Skip to content

Commit

Permalink
newrelic_deployment: add app_name_exact_match parameter (#7355)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
matiasba and felixfontein authored Oct 8, 2023
1 parent 323a1dc commit 85e866c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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)."
24 changes: 22 additions & 2 deletions plugins/modules/newrelic_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@
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: []
'''

Expand Down Expand Up @@ -102,16 +110,17 @@ 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
)

# build list of params
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)
Expand Down Expand Up @@ -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"],
}
Expand All @@ -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__':
Expand Down

0 comments on commit 85e866c

Please sign in to comment.