v1.0.63: autotest namespace and duplicate prevention
This commit is contained in:
+30
-1
@@ -1,4 +1,5 @@
|
||||
from flask import Blueprint, current_app, jsonify, request
|
||||
import uuid
|
||||
|
||||
from api.http_client import HttpClient, detect_endpoint
|
||||
from operations.get_services import get_services, get_service_detail
|
||||
@@ -8,6 +9,33 @@ from operations.tracker import remove as tracker_remove
|
||||
|
||||
bp = Blueprint("api_test", __name__)
|
||||
|
||||
AUTOTEST_PREFIX = "autotest-"
|
||||
|
||||
|
||||
def _with_prefix(name):
|
||||
name = (name or "").strip() or "instance"
|
||||
return name if name.startswith(AUTOTEST_PREFIX) else f"{AUTOTEST_PREFIX}{name}"
|
||||
|
||||
|
||||
def _unique_display_name(client, requested_name):
|
||||
base_name = _with_prefix(requested_name)
|
||||
try:
|
||||
existing = {
|
||||
i.get("displayName", "")
|
||||
for i in get_instances(client)
|
||||
if str(i.get("displayName", "")).startswith(AUTOTEST_PREFIX)
|
||||
}
|
||||
except Exception:
|
||||
existing = set()
|
||||
|
||||
if base_name not in existing:
|
||||
return base_name
|
||||
|
||||
while True:
|
||||
candidate = f"{base_name}-{uuid.uuid4().hex[:6]}"
|
||||
if candidate not in existing:
|
||||
return candidate
|
||||
|
||||
|
||||
def _client():
|
||||
token = request.cookies.get("token") or current_app.config["NUBES_API_TOKEN"]
|
||||
@@ -107,6 +135,7 @@ def api_test():
|
||||
|
||||
try:
|
||||
if op_name == "create":
|
||||
display_name = _unique_display_name(client, display_name)
|
||||
payload = {"serviceId": svc_id, "displayName": display_name, "descr": ""}
|
||||
resp = client.post("/instances", payload)
|
||||
instance_uid = resp.get("instanceUid") or _find_uid(resp) or _uid_from_location(resp.get("_location", ""))
|
||||
@@ -135,7 +164,7 @@ def api_test():
|
||||
|
||||
# фоном ждать завершения
|
||||
threading.Thread(target=_finish_op, args=(client, op_uid, instance_uid, svc_id, display_name, op_name, svc_op_id, True), daemon=True).start()
|
||||
return jsonify({"status": "RUNNING", "opUid": op_uid, "instanceUid": instance_uid})
|
||||
return jsonify({"status": "RUNNING", "opUid": op_uid, "instanceUid": instance_uid, "displayName": display_name})
|
||||
|
||||
else:
|
||||
if not instance_uid:
|
||||
|
||||
Reference in New Issue
Block a user