Files
tracer-cloud--opensre/integrations/tracer/tracer_integrations.py
T
wehub-resource-sync 4b6817381b
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:45 +08:00

116 lines
3.8 KiB
Python

"""Integration credential fetching from Tracer Web App."""
from __future__ import annotations
import json
import logging
from dataclasses import dataclass
from typing import Any
from integrations.tracer.tracer_client_base import TracerClientBase
logger = logging.getLogger(__name__)
@dataclass(frozen=True)
class GrafanaIntegrationCredentials:
"""Grafana credentials fetched from the integrations table."""
found: bool
endpoint: str = ""
api_key: str = ""
integration_id: str = ""
status: str = ""
@property
def is_configured(self) -> bool:
return bool(self.endpoint and self.api_key)
class TracerIntegrationsMixin(TracerClientBase):
"""Mixin for fetching integration credentials from the web app."""
def get_integration_credentials(self, service: str) -> list[dict[str, Any]]:
"""Fetch integration credentials for a service from /api/integrations.
Args:
service: Service name (e.g., "Grafana", "Slack")
Returns:
List of integration records with parsed credentials.
"""
params = {"orgId": self.org_id, "service": service}
data = self._get("/api/integrations", params)
if not data.get("success") or not data.get("data"):
return []
result: list[dict[str, Any]] = data["data"]
return result
def get_all_integrations(self) -> list[dict[str, Any]]:
"""Fetch all integration records for the org from /api/integrations.
Returns:
List of all integration records (all services) with parsed credentials.
"""
params = {"orgId": self.org_id}
data = self._get("/api/integrations", params)
if not data.get("success") or not data.get("data"):
return []
integrations: list[dict[str, Any]] = data["data"]
# Parse JSON-encoded credentials
for integration in integrations:
creds = integration.get("credentials", {})
if isinstance(creds, str):
try:
integration["credentials"] = json.loads(creds)
except (json.JSONDecodeError, TypeError):
logger.warning(
"Malformed integration JSON for integration id=%s",
integration.get("id", "unknown"),
)
integration["credentials"] = {}
return integrations
def get_grafana_credentials(self) -> GrafanaIntegrationCredentials:
"""Fetch the user's Grafana integration credentials from the web app.
Queries the integrations API filtered by service=Grafana and returns
the first active integration's endpoint and API key.
Returns:
GrafanaIntegrationCredentials with endpoint and api_key if found.
"""
integrations = self.get_integration_credentials("Grafana")
if not integrations:
return GrafanaIntegrationCredentials(found=False)
# Prefer active integrations, fall back to first available
active = [i for i in integrations if i.get("status") == "active"]
integration = active[0] if active else integrations[0]
credentials = integration.get("credentials", {})
if isinstance(credentials, str):
try:
credentials = json.loads(credentials)
except (json.JSONDecodeError, TypeError):
logger.warning(
"Malformed Grafana integration JSON for integration id=%s",
integration.get("id", "unknown"),
)
credentials = {}
return GrafanaIntegrationCredentials(
found=True,
endpoint=credentials.get("endpoint", ""),
api_key=credentials.get("api_key", ""),
integration_id=integration.get("id", ""),
status=integration.get("status", ""),
)