Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
These include resolving a problem with interactive edits on mac not
working. This was likely due to a quirk with the NamedTemporaryFile
implementation. I insteda switched to manually creating a temporary file
and that worked.

It also includes an early escape for when a user's inventory is empty.
  • Loading branch information
JacobCallahan committed Oct 15, 2024
1 parent 55d312a commit ecd66d6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
3 changes: 3 additions & 0 deletions broker/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ def inventory(details, _list, sync, filter):
)
for host in inventory
]
if not curated_host_info:
CONSOLE.print("No hosts found in inventory.")
return
table = helpers.dictlist_to_table(curated_host_info, "Host Inventory", _id=True)
if _list:
table.title = None
Expand Down
11 changes: 5 additions & 6 deletions broker/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import Path
import pkgutil
import sys
from tempfile import NamedTemporaryFile

import click
from logzero import logger
Expand Down Expand Up @@ -56,11 +55,11 @@ def __init__(self, settings_path=None):

def _interactive_edit(self, chunk):
"""Write the chunk data to a temporary file and open it in an editor."""
with NamedTemporaryFile(mode="w+", suffix=".yaml") as tmp:
yaml.dump(chunk, tmp)
click.edit(filename=tmp.name)
tmp.seek(0)
new_data = tmp.read()
temp_file = Path("temp_settings.yaml")
yaml.dump(chunk, temp_file)
click.edit(filename=str(temp_file))
new_data = temp_file.read_text()
temp_file.unlink()
# first try to load it as yaml
try:
return yaml.load(new_data)
Expand Down
14 changes: 13 additions & 1 deletion broker/providers/ansible_tower.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ def extend(self, target_vm, new_expire_time=None, provider_labels=None):
provider_labels=provider_labels,
)

def provider_help(
def provider_help( # noqa: PLR0912, PLR0915 - Possible TODO refactor
self,
workflows=False,
workflow=None,
Expand All @@ -728,6 +728,9 @@ def provider_help(
for workflow in self._v2.workflow_job_templates.get(page_size=1000).results
if workflow.summary_fields.user_capabilities.get("start")
]
if not workflows:
logger.warning("No workflows found")
return
if res_filter := kwargs.get("results_filter"):
workflows = eval_filter(workflows, res_filter, "res")
workflows = workflows if isinstance(workflows, list) else [workflows]
Expand All @@ -739,6 +742,9 @@ def provider_help(
logger.info(f"Accepted additional nick fields:\n{helpers.yaml_format(inv)}")
elif inventories:
inv = [inv.name for inv in self._v2.inventory.get(kind="", page_size=1000).results]
if not inv:
logger.warning("No inventories found!")
return
if res_filter := kwargs.get("results_filter"):
inv = eval_filter(inv, res_filter, "res")
inv = inv if isinstance(inv, list) else [inv]
Expand All @@ -758,6 +764,9 @@ def provider_help(
for job_template in self._v2.job_templates.get(page_size=1000).results
if job_template.summary_fields.user_capabilities.get("start")
]
if not job_templates:
logger.warning("No job templates found!")
return
if res_filter := kwargs.get("results_filter"):
job_templates = eval_filter(job_templates, res_filter, "res")
job_templates = (
Expand All @@ -773,6 +782,9 @@ def provider_help(
]
)
)
if not templates:
logger.warning("No templates found!")
return
templates.sort(reverse=True)
if res_filter := kwargs.get("results_filter"):
templates = eval_filter(templates, res_filter, "res")
Expand Down

0 comments on commit ecd66d6

Please sign in to comment.