diff --git a/site/api/http_client.py b/site/api/http_client.py index 7438474..e431fa4 100644 --- a/site/api/http_client.py +++ b/site/api/http_client.py @@ -19,6 +19,22 @@ def detect_endpoint(token): return None +def stand_name(endpoint): + """dev/test по URL стенда.""" + for name in ("dev", "test"): + if name in (endpoint or ""): + return name + return "?" + + +def create_client(token, fallback_endpoint=None): + """HttpClient с автоопределением стенда по токену.""" + ep = detect_endpoint(token) or fallback_endpoint + if not ep: + return None + return HttpClient(ep, token), ep + + class HttpClient: def __init__(self, endpoint, token): self._endpoint = endpoint.rstrip("/") diff --git a/site/app.py b/site/app.py index 29c2d1e..a8c6f24 100644 --- a/site/app.py +++ b/site/app.py @@ -6,7 +6,7 @@ from routes.main import bp as main_bp from routes.api import bp as api_bp from routes.api_test import bp as api_test_bp -VERSION = "1.0.84" +VERSION = "1.0.85" app = Flask(__name__, template_folder="templates", static_folder="static") app.config["NUBES_API_ENDPOINT"] = os.getenv("NUBES_API_ENDPOINT", "https://lk-api-gateway-dev.ngcloud.ru/api/v1/svc") diff --git a/site/routes/api.py b/site/routes/api.py index 01f6d45..c467d15 100644 --- a/site/routes/api.py +++ b/site/routes/api.py @@ -2,6 +2,7 @@ import threading from flask import Blueprint, current_app, jsonify, request +from api.http_client import create_client from runner import run_tests, get_status, load_config, save_config bp = Blueprint("api", __name__) @@ -9,9 +10,14 @@ bp = Blueprint("api", __name__) @bp.route("/api/run", methods=["POST"]) def api_run(): + token = current_app.config["NUBES_API_TOKEN"] + result = create_client(token, current_app.config["NUBES_API_ENDPOINT"]) + if not result: + return jsonify({"error": "Не удалось определить стенд"}), 500 + client, endpoint = result t = threading.Thread( target=run_tests, - args=(current_app.config["NUBES_API_ENDPOINT"], current_app.config["NUBES_API_TOKEN"]), + args=(endpoint, token), daemon=True, ) t.start() diff --git a/site/routes/main.py b/site/routes/main.py index 2112aba..e3680c3 100644 --- a/site/routes/main.py +++ b/site/routes/main.py @@ -1,6 +1,6 @@ from flask import Blueprint, current_app, render_template, request, make_response, jsonify, redirect -from api.http_client import HttpClient, detect_endpoint +from api.http_client import HttpClient, detect_endpoint, create_client, stand_name from operations.get_instances import get_organization, get_instances from operations.get_services import get_services, get_service_detail from operations.tracker import list_all as tracker_list @@ -84,6 +84,7 @@ def index(): "client_id": _client_id(active_token), "token_info": _token_info(active_token), "config": config, + "stand": stand, "services": services, "instances": instances, "instance_groups": instance_groups, @@ -110,11 +111,13 @@ def index(): instances = [] instance_groups = {} config = {} + stand = "?" if active_token: - endpoint = detect_endpoint(active_token) - if endpoint: + result = create_client(active_token, current_app.config["NUBES_API_ENDPOINT"]) + if result: + client, endpoint = result + stand = stand_name(endpoint) try: - client = HttpClient(endpoint, active_token) org = get_organization(client) raw_svc = get_services(client) services = sorted(raw_svc, key=lambda s: (s.get("svcId", 0), s.get("svc", ""))) @@ -149,7 +152,10 @@ def api_operations(svc_id): user_token = request.cookies.get("token") or "" active_token = user_token or env_token try: - client = HttpClient(current_app.config["NUBES_API_ENDPOINT"], active_token) + result = create_client(active_token, current_app.config["NUBES_API_ENDPOINT"]) + if not result: + return jsonify({"error": "Не удалось определить стенд"}), 500 + client, _ = result detail = get_service_detail(client, svc_id) ops = detail.get("operations", []) tracked = tracker_list() diff --git a/site/templates/index.html b/site/templates/index.html index a77bfea..595970b 100644 --- a/site/templates/index.html +++ b/site/templates/index.html @@ -44,7 +44,7 @@