暂无描述

seed-iris-demo-data.sh 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. read_env_var() {
  4. local key="$1"
  5. local file="$2"
  6. [[ -f "${file}" ]] || return 1
  7. sed -n "s/^${key}=//p" "${file}" | head -n1
  8. }
  9. SOC_ENV_FILE="${SOC_ENV_FILE:-soc-integrator/.env}"
  10. ROOT_ENV_FILE="${ROOT_ENV_FILE:-.env}"
  11. iris_api_key_from_env="${IRIS_API_KEY:-}"
  12. if [[ -z "${iris_api_key_from_env}" ]]; then
  13. iris_api_key_from_env="$(read_env_var "IRIS_API_KEY" "${SOC_ENV_FILE}" || true)"
  14. fi
  15. if [[ -z "${iris_api_key_from_env}" ]]; then
  16. iris_api_key_from_env="$(read_env_var "IRIS_API_KEY" "${ROOT_ENV_FILE}" || true)"
  17. fi
  18. integrator_url_from_env="${INTEGRATOR_URL:-}"
  19. if [[ -z "${integrator_url_from_env}" ]]; then
  20. integrator_url_from_env="$(read_env_var "INTEGRATOR_URL" "${SOC_ENV_FILE}" || true)"
  21. fi
  22. if [[ -z "${integrator_url_from_env}" ]]; then
  23. integrator_url_from_env="http://localhost:8088"
  24. fi
  25. iris_base_url_from_env="${IRIS_BASE_URL:-}"
  26. if [[ -z "${iris_base_url_from_env}" ]]; then
  27. iris_base_url_from_env="$(read_env_var "IRIS_BASE_URL" "${SOC_ENV_FILE}" || true)"
  28. fi
  29. if [[ -z "${iris_base_url_from_env}" ]]; then
  30. iris_base_url_from_env="https://localhost:8443"
  31. fi
  32. MODE="${MODE:-integrator}" # integrator | direct
  33. COUNT="${COUNT:-5}"
  34. PREFIX="${PREFIX:-SOC Demo Incident}"
  35. INTEGRATOR_URL="${integrator_url_from_env}"
  36. IRIS_BASE_URL="${iris_base_url_from_env}"
  37. IRIS_API_KEY="${iris_api_key_from_env}"
  38. IRIS_VERIFY_SSL="${IRIS_VERIFY_SSL:-false}" # false -> use -k
  39. CASE_CUSTOMER="${CASE_CUSTOMER:-1}"
  40. CASE_SOC_ID="${CASE_SOC_ID:-}"
  41. if [[ ! "${COUNT}" =~ ^[0-9]+$ ]] || [[ "${COUNT}" -lt 1 ]]; then
  42. echo "error: COUNT must be a positive integer"
  43. exit 1
  44. fi
  45. timestamp="$(date +%Y%m%d-%H%M%S)"
  46. for i in $(seq 1 "${COUNT}"); do
  47. severity="medium"
  48. if (( i % 4 == 0 )); then
  49. severity="critical"
  50. elif (( i % 3 == 0 )); then
  51. severity="high"
  52. elif (( i % 2 == 0 )); then
  53. severity="low"
  54. fi
  55. title="${PREFIX} #${i} (${timestamp})"
  56. description="Generated demo IRIS case ${i}/${COUNT} at ${timestamp}"
  57. if [[ "${MODE}" == "integrator" ]]; then
  58. response="$(
  59. curl -sS -X POST "${INTEGRATOR_URL}/iris/tickets" \
  60. -H "Content-Type: application/json" \
  61. -d "{
  62. \"title\": \"${title}\",
  63. \"description\": \"${description}\",
  64. \"case_customer\": ${CASE_CUSTOMER},
  65. \"case_soc_id\": \"${CASE_SOC_ID}\",
  66. \"payload\": {}
  67. }"
  68. )"
  69. elif [[ "${MODE}" == "direct" ]]; then
  70. curl_args=(-sS -X POST "${IRIS_BASE_URL}/api/v2/cases" -H "Content-Type: application/json")
  71. if [[ "${IRIS_VERIFY_SSL}" == "false" ]]; then
  72. curl_args+=(-k)
  73. fi
  74. if [[ -n "${IRIS_API_KEY}" ]]; then
  75. curl_args+=(-H "Authorization: Bearer ${IRIS_API_KEY}")
  76. fi
  77. response="$(
  78. curl "${curl_args[@]}" \
  79. -d "{
  80. \"case_name\": \"${title}\",
  81. \"case_description\": \"${description}\",
  82. \"case_customer\": ${CASE_CUSTOMER},
  83. \"case_soc_id\": \"${CASE_SOC_ID}\"
  84. }"
  85. )"
  86. else
  87. echo "error: MODE must be 'integrator' or 'direct'"
  88. exit 1
  89. fi
  90. RESPONSE="${response}" python3 - <<'PY'
  91. import json
  92. import os
  93. raw = os.environ.get("RESPONSE", "")
  94. try:
  95. data = json.loads(raw)
  96. except Exception:
  97. print(f"raw_response: {raw[:400]}")
  98. raise SystemExit(0)
  99. # integrator shape: {"ok": true, "data": {"iris": {...}}}
  100. if isinstance(data, dict) and "data" in data and isinstance(data["data"], dict):
  101. iris = data["data"].get("iris", {})
  102. case = iris.get("data", {}) if isinstance(iris, dict) else {}
  103. if case:
  104. print(
  105. f"created case_id={case.get('case_id')} case_uuid={case.get('case_uuid')} "
  106. f"title={case.get('case_name')}"
  107. )
  108. else:
  109. print(json.dumps(data))
  110. raise SystemExit(0)
  111. # direct IRIS shape: {"status":"success","data":{...}}
  112. case = data.get("data", {}) if isinstance(data, dict) else {}
  113. if isinstance(case, dict) and case:
  114. print(
  115. f"created case_id={case.get('case_id')} case_uuid={case.get('case_uuid')} "
  116. f"title={case.get('case_name')}"
  117. )
  118. else:
  119. print(json.dumps(data))
  120. PY
  121. done
  122. echo "done: created ${COUNT} demo cases (mode=${MODE})"