Skip to content

Commit

Permalink
Better determine what the checkout actions per-provider are
Browse files Browse the repository at this point in the history
This change moves away from trying to determine the checkout actions by
the click options and instead get them from registered provider actions.

fixes #313
  • Loading branch information
JacobCallahan committed Sep 11, 2024
1 parent 87f0b56 commit 44c9308
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
4 changes: 2 additions & 2 deletions broker/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from broker import exceptions, helpers, settings
from broker.broker import Broker
from broker.logger import LOG_LEVEL
from broker.providers import PROVIDER_HELP, PROVIDERS
from broker.providers import PROVIDER_ACTIONS, PROVIDER_HELP, PROVIDERS

signal.signal(signal.SIGINT, helpers.handle_keyboardinterrupt)

Expand Down Expand Up @@ -297,7 +297,7 @@ def inventory(details, curated, sync, filter):
table.add_column("Action", justify="left", style="yellow")
table.add_column("OS", justify="left", style="blue")

for host in helpers.get_host_inventory_fields(inventory, PROVIDERS):
for host in helpers.get_host_inventory_fields(inventory, PROVIDER_ACTIONS):
table.add_row(
str(host["id"]), host["host"], host["provider"], host["action"], host["os"]
)
Expand Down
31 changes: 14 additions & 17 deletions broker/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,22 +268,20 @@ def yaml_format(in_struct):
return yaml.dump(in_struct, default_flow_style=False, sort_keys=False)


def get_checkout_options(provider_cls):
"""Return the checkout options for a provider."""
options = []
# iterate through the _checkout_options list
for option in provider_cls._checkout_options:
# now we need to dig into the baked-in attributes of the click decorator
for param in option.__closure__:
if isinstance(param.cell_contents, tuple):
for opt in param.cell_contents:
if opt.startswith("--"):
options.append(opt[2:].replace("-", "_")) # noqa: PERF401
return options


def get_host_inventory_fields(inv_dict, providers):
def flip_provider_actions(provider_actions):
"""Flip the mapping of actions->provider to provider->actions."""
flipped = {}
for action, (provider, _) in provider_actions.items():
provider_name = provider.__name__
if provider_name not in flipped:
flipped[provider_name] = []
flipped[provider_name].append(action)
return flipped


def get_host_inventory_fields(inv_dict, provider_actions):
"""Get a more focused set of fields from the host inventory."""
flipped_prov_actions = flip_provider_actions(provider_actions)
curated_hosts = []
for num, host in enumerate(inv_dict):
match host:
Expand All @@ -294,8 +292,7 @@ def get_host_inventory_fields(inv_dict, providers):
}:
os_name = host.get("os_distribution", "Unknown")
os_version = host.get("os_distribution_version", "")
checkout_opts = get_checkout_options(providers[provider])
for opt in checkout_opts:
for opt in flipped_prov_actions[provider]:
if action := host["_broker_args"].get(opt):
curated_hosts.append(
{
Expand Down

0 comments on commit 44c9308

Please sign in to comment.