Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR #7355/85e866cc backport][stable-7] newrelic_deployment: add app_name_exact_match parameter #7367

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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