Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions cortexapps_cli/commands/solutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def _get_ui_url(ctx: typer.Context) -> str:
return api_url.replace("://api.", "://app.")


def _build_client(ctx: typer.Context) -> CortexClient:
def _build_client(ctx: typer.Context, solution: str | None = None) -> CortexClient:
"""Build a CortexClient from auth params stored by global_callback."""
params = ctx.obj.get("_auth_params", {})
api_key = params.get("api_key")
Expand Down Expand Up @@ -316,7 +316,8 @@ def _build_client(ctx: typer.Context) -> CortexClient:
url = url.strip("\"' /")

numeric_level = getattr(logging, log_level_str.upper(), logging.WARNING)
return CortexClient(api_key, tenant, numeric_level, url, rate_limit)
command = f"solutions/{solution}" if solution else "solutions"
return CortexClient(api_key, tenant, numeric_level, url, rate_limit, command=command)


def _read_catalog_tag(content: str) -> str | None:
Expand Down Expand Up @@ -763,7 +764,13 @@ def install(
typer.echo(f"Error: Solution '{solution}' not found. Available: {avail}")
raise typer.Exit(1)

ctx.obj["client"] = _build_client(ctx)
ctx.obj["client"] = _build_client(ctx, solution=solution)
# Emit one API call so this install is always recorded in Datadog, even for
# setup-script-only solutions that make no backup import requests.
try:
ctx.obj["client"].get("api/v1/catalog/definitions", params={"pageSize": 1})
except Exception:
pass

import cortexapps_cli.commands.backup as backup

Expand Down Expand Up @@ -859,6 +866,51 @@ def post_install(
ctx.obj["client"] = _build_client(ctx)
_run_post_install_script(solution, solutions_dir=solutions_dir, ctx=ctx, no_prompt=no_prompt)

if not no_prompt:
readme = _get_readme(solution, solutions_dir)
if readme:
entity_tags: set[str] = set()
ui_url = _get_ui_url(ctx)
resources: dict[str, list[str]] = {}
try:
root = files("cortexapps_cli.solutions")
if solutions_dir:
resources = _collect_solution_resources(Path(solutions_dir) / solution)
else:
with as_file(root / solution) as sp:
resources = _collect_solution_resources(sp)
entity_tags = set(resources.get("catalog", []))
if solutions_dir:
entity_tags |= _extract_tf_entity_tags(Path(solutions_dir) / solution)
else:
with as_file(root / solution) as sp:
entity_tags |= _extract_tf_entity_tags(sp)
except Exception:
pass
# Build a synthetic import report from the solution's known resources
kind_labels = {
"entity-types": "Entity type",
"entity-relationship-types": "Entity relationship type",
"catalog": "Entity",
"scorecards": "Scorecard",
"workflows": "Workflow",
"plugins": "Plugin",
"catalogs": "Catalog",
}
report_lines: list[str] = []
for kind, tags in resources.items():
label = kind_labels.get(kind, kind)
for tag in tags:
report_lines.append(f" {label}: {tag}")
import_report = "\n".join(report_lines) if report_lines else ""
_post_install_menu(
readme,
import_report=import_report,
entity_tags=entity_tags,
ui_url=ui_url,
solution_tag=solution,
)


@app.command()
def uninstall(
Expand Down
6 changes: 4 additions & 2 deletions cortexapps_cli/cortex_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def acquire(self, tokens=1):


class CortexClient:
def __init__(self, api_key, tenant, numeric_level, base_url='https://api.getcortexapp.com', rate_limit=None):
def __init__(self, api_key, tenant, numeric_level, base_url='https://api.getcortexapp.com', rate_limit=None, command=None):
self.api_key = api_key
self.tenant = tenant
self.base_url = base_url
Expand All @@ -93,6 +93,8 @@ def __init__(self, api_key, tenant, numeric_level, base_url='https://api.getcort
except importlib.metadata.PackageNotFoundError:
self.version = 'unknown'

self.command = command

logging.basicConfig(level=numeric_level)
self.logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -132,7 +134,7 @@ def request(self, method, endpoint, params={}, headers={}, data=None, raw_body=F
req_headers = {
'Authorization': f'Bearer {self.api_key}',
'Content-Type': content_type,
'User-Agent': f'cortexapps-cli/{self.version}',
'User-Agent': f'cortexapps-cli/{self.version} ({self.command})' if self.command else f'cortexapps-cli/{self.version}',
**headers
}
url = '/'.join([self.base_url.rstrip('/'), endpoint.lstrip('/')])
Expand Down
3 changes: 2 additions & 1 deletion tests/test_solutions_postinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@


def test_post_install_no_setup_for_ai_agents():
result = runner.invoke(app, ["solutions", "post-install", "-s", "ai-agents"])
with patch("cortexapps_cli.commands.solutions._post_install_menu"):
result = runner.invoke(app, ["solutions", "post-install", "-s", "ai-agents"])
assert result.exit_code == 0
assert "No post-install setup available" in result.output

Expand Down
Loading