Skip to content
This repository was archived by the owner on Mar 20, 2025. It is now read-only.

Commit a7e9288

Browse files
authored
feat: Fallback to first PASSTHROUGH config when no matches found (#102)
* feat: Fallback to first PASSTHROUGH config when no matches found * fix: workaround for python3.11 support for task runner
1 parent f305cac commit a7e9288

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

c8ylp/cli/core.py

+22-8
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,9 @@ def get_config_id(ctx: click.Context, mor: Dict[str, Any], config: str) -> str:
438438
439439
Args:
440440
mor (Dict[str, Any]): Device managed object
441-
config (str): Expected configuration type
441+
config (str): Expected configuration name. If no matching name
442+
is found, but there are other PASSTHROUGH configuration available it will
443+
use the first config (and log a warning)
442444
443445
Returns:
444446
str: Remote access configuration id
@@ -483,16 +485,28 @@ def extract_config_id(matching_config):
483485
if item.get("name", "").casefold() == config.casefold()
484486
]
485487

486-
if not matches:
487-
logging.error(
488-
'Provided config name "%s" for "%s" was not found or none with protocal set to "%s"',
489-
config,
490-
device_name,
488+
if matches:
489+
return extract_config_id(matches[0])
490+
491+
if valid_configs:
492+
# Fallback to using the first valid config (if not matches names were found)
493+
logging.warning(
494+
"No matching %s configs found (name=%s), so falling back to first config (name=%s)",
491495
PASSTHROUGH,
496+
config,
497+
valid_configs[0].get("name", ""),
492498
)
493-
ctx.exit(ExitCodes.DEVICE_NO_MATCHING_PASSTHROUGH_CONFIG)
499+
return extract_config_id(valid_configs[0])
494500

495-
return extract_config_id(matches[0])
501+
# Technically it should never reach here, however leave in place in
502+
# case the above logic is changed
503+
logging.error(
504+
'Provided config name "%s" for "%s" was not found or none with protocol set to "%s"',
505+
config,
506+
device_name,
507+
PASSTHROUGH,
508+
)
509+
ctx.exit(ExitCodes.DEVICE_NO_MATCHING_PASSTHROUGH_CONFIG)
496510

497511

498512
def run_proxy_in_background(

tasks.py

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
import shutil
2222
import subprocess
2323
import sys
24+
25+
# WORKAROUND
26+
# Workaround until python 3.11 is supported by py-invoke
27+
# https://github.com/pyinvoke/invoke/issues/833#issuecomment-1293148106
28+
import inspect
29+
30+
if not hasattr(inspect, "getargspec"):
31+
inspect.getargspec = inspect.getfullargspec
32+
2433
from invoke import task
2534
from pathlib import Path
2635

tests/c8ylp/cli/connect/test_ssh.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,23 @@ def run():
329329
"exit_code": ExitCodes.DEVICE_NO_PASSTHROUGH_CONFIG,
330330
},
331331
{
332-
"description": "Missing matching PASSTHROUGH name",
332+
"description": "Not an exact PASSTHROUGH name match",
333333
"fragments": {
334334
REMOTE_ACCESS_FRAGMENT: [
335335
{"id": 1, "name": "example-ssh", "protocol": "ssh"},
336336
{"id": 2, "name": "custom-passthrough", "protocol": PASSTHROUGH},
337337
]
338338
},
339-
"exit_code": ExitCodes.DEVICE_NO_MATCHING_PASSTHROUGH_CONFIG,
339+
"exit_code": ExitCodes.MISSING_ROLE_REMOTE_ACCESS_ADMIN,
340+
},
341+
{
342+
"description": "Missing matching PASSTHROUGH name",
343+
"fragments": {
344+
REMOTE_ACCESS_FRAGMENT: [
345+
{"id": 1, "name": "example-ssh", "protocol": "ssh"},
346+
]
347+
},
348+
"exit_code": ExitCodes.DEVICE_NO_PASSTHROUGH_CONFIG,
340349
},
341350
{
342351
"description": "Custom PASSTHROUGH name matching but still missing role",

0 commit comments

Comments
 (0)