157 lines
3.5 KiB
Python
157 lines
3.5 KiB
Python
# # bind_agents.py
|
||
# import requests
|
||
# import urllib3
|
||
|
||
# urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||
|
||
# BASE_URL = "https://devb.zdht.hjkl01.cn:65432"
|
||
# ROLE_BIND_API = f"{BASE_URL}/api/v1/agent-roles/association"
|
||
|
||
# HEADERS = {
|
||
# "Accept": "application/json",
|
||
# "Content-Type": "application/json",
|
||
# }
|
||
|
||
# TIMEOUT = 10
|
||
# INPUT_FILE = "agent_ids.txt"
|
||
|
||
# # 角色 ID
|
||
# AGENT_ROLE_0 = "1" # 1-8
|
||
# AGENT_ROLE_1 = "2" # 1-4
|
||
# AGENT_ROLE_2 = "3" # 5-6
|
||
# AGENT_ROLE_3 = "4" # 7-8
|
||
|
||
|
||
# def calc_role_id_by_index(index: int) -> str:
|
||
# if 1 <= index <= 4:
|
||
# return AGENT_ROLE_1
|
||
# elif 5 <= index <= 6:
|
||
# return AGENT_ROLE_2
|
||
# elif 7 <= index <= 8:
|
||
# return AGENT_ROLE_3
|
||
# else:
|
||
# raise ValueError("非法 icon_index")
|
||
|
||
|
||
# def bind_agent(agent_id: str, role_id: str):
|
||
# payload = [
|
||
# {
|
||
# "agent_id": agent_id,
|
||
# "agent_role_id": role_id,
|
||
# "is_active": True,
|
||
# }
|
||
# ]
|
||
|
||
# resp = requests.post(
|
||
# ROLE_BIND_API,
|
||
# headers=HEADERS,
|
||
# json=payload,
|
||
# verify=False,
|
||
# timeout=TIMEOUT,
|
||
# )
|
||
|
||
# print(f"[BIND] {agent_id} -> role {role_id} => {resp.status_code}")
|
||
# if resp.status_code not in (200, 201):
|
||
# print(" ↳", resp.text)
|
||
|
||
|
||
# def main():
|
||
# with open(INPUT_FILE, "r", encoding="utf-8") as f:
|
||
# agent_ids = [line.strip() for line in f if line.strip()]
|
||
|
||
# print(f"=== 读取到 {len(agent_ids)} 个 Agent ===")
|
||
|
||
# for idx, agent_id in enumerate(agent_ids, start=1):
|
||
# role_id = calc_role_id_by_index(idx)
|
||
# bind_agent(agent_id, role_id)
|
||
|
||
# print("\n✅ 绑定完成")
|
||
|
||
|
||
# if __name__ == "__main__":
|
||
# main()
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
import requests
|
||
import urllib3
|
||
|
||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||
|
||
BASE_URL = "http://192.168.0.46:22000"
|
||
ROLE_BIND_API = f"{BASE_URL}/api/v1/agent-roles/association"
|
||
HEADERS = {
|
||
"Accept": "application/json",
|
||
"Content-Type": "application/json",
|
||
}
|
||
|
||
TIMEOUT = 10
|
||
INPUT_FILE = "agent_ids.txt"
|
||
|
||
# 角色 ID
|
||
AGENT_ROLE_0 = "1" # 所有 Agent 必绑
|
||
AGENT_ROLE_1 = "2" # 1-4
|
||
AGENT_ROLE_2 = "3" # 5-6
|
||
AGENT_ROLE_3 = "4" # 7-8
|
||
|
||
|
||
def calc_role_id_by_index(index: int) -> str:
|
||
if 1 <= index <= 4:
|
||
return AGENT_ROLE_1
|
||
elif 5 <= index <= 6:
|
||
return AGENT_ROLE_2
|
||
elif 7 <= index <= 8:
|
||
return AGENT_ROLE_3
|
||
else:
|
||
raise ValueError("非法 index")
|
||
|
||
|
||
def bind_agent(agent_id: str, role_id: str):
|
||
payload = [
|
||
{
|
||
"agent_id": agent_id,
|
||
"agent_role_id": role_id,
|
||
"is_active": True,
|
||
}
|
||
]
|
||
|
||
resp = requests.post(
|
||
ROLE_BIND_API,
|
||
headers=HEADERS,
|
||
json=payload,
|
||
verify=False,
|
||
timeout=TIMEOUT,
|
||
)
|
||
|
||
print(f"[BIND] {agent_id} -> role {role_id} => {resp.status_code}")
|
||
if resp.status_code not in (200, 201):
|
||
print(" ↳", resp.text)
|
||
|
||
|
||
def main():
|
||
with open(INPUT_FILE, "r", encoding="utf-8") as f:
|
||
agent_ids = [line.strip() for line in f if line.strip()]
|
||
|
||
print(f"=== 读取到 {len(agent_ids)} 个 Agent ===")
|
||
|
||
for idx, agent_id in enumerate(agent_ids, start=1):
|
||
# 1️⃣ 所有 Agent 统一绑定角色 1
|
||
bind_agent(agent_id, AGENT_ROLE_0)
|
||
|
||
# 2️⃣ 再绑定分组角色
|
||
group_role = calc_role_id_by_index(idx)
|
||
|
||
# 防止重复绑定
|
||
if group_role != AGENT_ROLE_0:
|
||
bind_agent(agent_id, group_role)
|
||
|
||
print("\n✅ 绑定完成(角色 1 + 分组角色)")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|