Files
polygon/site/routes/services_routes.py
T
naeel 58d54f2a95 v0.5.0: 3 стенда — изоляция состояния, WSGI middleware, OpenAPI server selector, POLYGON_STANDS env
- loader.py: STANDS dict, get_services/get_ops_index per stand, POLYGON_STANDS env
- mock_state.py: get_state(stand_id) — ленивая инициализация
- app.py: StandMiddleware — перезапись PATH_INFO до Flask-роутинга
- Все роуты: LocalProxy(state), LocalProxy(SERVICES/OPS_INDEX) через g.stand_id
- openapi.py: _build_servers() — по одному серверу на стенд
- services/stand{1,2,3}/ — 37 YAML на стенд из STANDS/dev и STANDS/test
- tests/test_api.py: +4 multi-stand теста (изоляция, fail-next, OpenAPI servers)
- Обратная совместимость: /api/v1/svc/... → стенд по умолчанию
2026-08-01 20:10:19 +04:00

66 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
routes/services_routes.py — эндпоинты сервисов.
Blueprint "services" с префиксом /api/v1/svc/services.
GET /services — список всех загруженных сервисов
GET /services/<id> — детали конкретного сервиса (список операций)
"""
from flask import Blueprint, g, jsonify
from werkzeug.local import LocalProxy
import config.loader as _cfg
SERVICES = LocalProxy(lambda: _cfg.get_services(g.stand_id))
bp = Blueprint("services", __name__, url_prefix="/api/v1/svc/services")
@bp.route("", methods=["GET"])
@bp.route("/", methods=["GET"])
def list_services():
"""GET /api/v1/svc/services — список всех сервисов.
Возвращает {results: [{svcId, svc, svcShort}, ...]}.
Формат совпадает с реальным Nubes API:
svcId — числовой ID сервиса (1, 90, 91, ...)
svc — человекочитаемое имя (Болванка, PostgreSQL, ...)
svcShort — короткое имя (dummy, postgres, ...)
"""
results = []
for svc_id, svc_def in SERVICES.items():
results.append({
"svcId": svc_id,
"svc": svc_def.get("service_display_name", ""),
"svcShort": svc_def.get("service_short_name", svc_def.get("name", "")),
})
return jsonify({"results": results})
@bp.route("/<int:svc_id>", methods=["GET"])
def get_service(svc_id):
"""GET /api/v1/svc/services/{id} — детали сервиса.
Возвращает {svc: {svc, svcShort, operations: [{svcOperationId, operation}]}}.
Если сервис не найден — 404 с JSON-ошибкой.
"""
svc_def = SERVICES.get(svc_id)
if not svc_def:
return jsonify({"error": "service not found"}), 404
# Собрать список операций сервиса (create, modify, delete, suspend, ...)
ops = []
for op in svc_def.get("operations", []):
ops.append({
"svcOperationId": op["svcOperationId"],
"operation": op["operation"],
})
return jsonify({
"svc": {
"svc": svc_def.get("service_display_name", ""),
"svcShort": svc_def.get("service_short_name", svc_def.get("name", "")),
"operations": ops,
}
})