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

newrelic_deployment: add app_name_exact_match parameter #7355

Merged
merged 11 commits into from
Oct 8, 2023
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"
matiasba marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 19 additions & 2 deletions plugins/modules/newrelic_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
required: false
default: true
type: bool
app_name_exact_match:
type: bool
description:
- If this flag is set to True then the app id lookup by name would only work for an exact match.
matiasba marked this conversation as resolved.
Show resolved Hide resolved
If set to false it returns the first result
matiasba marked this conversation as resolved.
Show resolved Hide resolved
required: false
matiasba marked this conversation as resolved.
Show resolved Hide resolved
requirements: []
'''

Expand Down Expand Up @@ -102,6 +108,7 @@ 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')
matiasba marked this conversation as resolved.
Show resolved Hide resolved
),
required_one_of=[['app_name', 'application_id']],
matiasba marked this conversation as resolved.
Show resolved Hide resolved
supports_check_mode=True
Expand All @@ -111,7 +118,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)
Expand Down Expand Up @@ -150,6 +156,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 +168,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