Skip to content

Commit

Permalink
Add checks for bad input for AT provider help paths
Browse files Browse the repository at this point in the history
Until this change, if a user gave an incorrect name, Broker would give a
traceback that wasn't helpful. Now, when the items can't be found, it
will give back a more helpful message.
  • Loading branch information
JacobCallahan authored and ogajduse committed Oct 16, 2024
1 parent 27a9edb commit 03456dc
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions broker/providers/ansible_tower.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ def extend(self, target_vm, new_expire_time=None, provider_labels=None):
provider_labels=provider_labels,
)

def provider_help( # noqa: PLR0912, PLR0915 - Possible TODO refactor
def provider_help( # noqa: PLR0911, PLR0912, PLR0915 - Possible TODO refactor
self,
workflows=False,
workflow=None,
Expand All @@ -660,7 +660,11 @@ def provider_help( # noqa: PLR0912, PLR0915 - Possible TODO refactor
"""Get a list of extra vars and their defaults from a workflow."""
results_limit = kwargs.get("results_limit", settings.ANSIBLETOWER.results_limit)
if workflow:
wfjt = self._v2.workflow_job_templates.get(name=workflow).results.pop()
if wfjt := self._v2.workflow_job_templates.get(name=workflow).results:
wfjt = wfjt.pop()
else:
logger.warning(f"Workflow {workflow} not found!")
return
default_inv = self._v2.inventory.get(id=wfjt.inventory).results.pop()
logger.info(
f"\nDescription:\n{wfjt.description}\n\n"
Expand All @@ -682,7 +686,11 @@ def provider_help( # noqa: PLR0912, PLR0915 - Possible TODO refactor
workflows = "\n".join(workflows[:results_limit])
logger.info(f"Available workflows:\n{workflows}")
elif inventory:
inv = self._v2.inventory.get(name=inventory, kind="").results.pop()
if inv := self._v2.inventory.get(name=inventory, kind="").results:
inv = inv.pop()
else:
logger.warning(f"Inventory {inventory} not found!")
return
inv = {"Name": inv.name, "ID": inv.id, "Description": inv.description}
logger.info(f"Accepted additional nick fields:\n{helpers.yaml_format(inv)}")
elif inventories:
Expand All @@ -696,7 +704,11 @@ def provider_help( # noqa: PLR0912, PLR0915 - Possible TODO refactor
inv = "\n".join(inv[:results_limit])
logger.info(f"Available Inventories:\n{inv}")
elif job_template:
jt = self._v2.job_templates.get(name=job_template).results.pop()
if jt := self._v2.job_templates.get(name=job_template).results:
jt = jt.pop()
else:
logger.warning(f"Job Template {job_template} not found!")
return
default_inv = self._v2.inventory.get(id=jt.inventory).results.pop()
logger.info(
f"\nDescription:\n{jt.description}\n\n"
Expand Down

0 comments on commit 03456dc

Please sign in to comment.