Files
Daniel Lindmark ac6e373824 Initial commit
2026-06-04 17:04:11 -05:00

89 lines
2.3 KiB
Python

import argparse
import os
import sys
DEFAULT_HUB_URL = "https://hub.allspice.io"
LOG_PREFIX = "[py-allspice-bist]"
def log(message: str) -> None:
print(f"{LOG_PREFIX} {message}")
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
prog="Allspice_API_BIST",
description="Test connection and execution of API actions",
)
parser.add_argument(
"--allspice_hub_url",
default=DEFAULT_HUB_URL,
help=f"The URL of your AllSpice Hub instance. Defaults to {DEFAULT_HUB_URL}.",
)
parser.add_argument(
"--allspice_token",
help=(
"Your AllSpice application token. If omitted, ALLSPICE_AUTH_TOKEN "
"will be used."
),
)
return parser.parse_args()
def main() -> int:
args = parse_args()
log("Starting BIST")
auth_token = args.allspice_token or os.environ.get("ALLSPICE_AUTH_TOKEN")
if not auth_token:
log(
"ERROR: supply a token with --allspice_token or set "
"ALLSPICE_AUTH_TOKEN"
)
return 1
log(f"Hub URL: {args.allspice_hub_url}")
log(f"Token: present length={len(auth_token)}")
try:
from allspice import AllSpice
except ModuleNotFoundError:
log(
"ERROR: py-allspice is not installed. Run "
"`pip install -r .allspice/utils/requirements.txt` first."
)
return 1
try:
allspice = AllSpice(
token_text=auth_token,
allspice_hub_url=args.allspice_hub_url,
)
except Exception as exc:
log(f"ERROR: failed to initialize AllSpice client: {type(exc).__name__}: {exc}")
return 1
log("Finished making connection")
try:
log("Testing public/version API call")
log("AllSpice Version: " + allspice.get_version())
except Exception as exc:
log(f"ERROR: version API call failed: {type(exc).__name__}: {exc}")
return 1
try:
log("Testing private/user API call")
log("API token belongs to user: " + allspice.get_user().username)
except Exception as exc:
log(f"ERROR: authenticated user API call failed: {type(exc).__name__}: {exc}")
return 1
log("BIST completed successfully")
return 0
if __name__ == "__main__":
sys.exit(main())