import requests, json, datetime, sys, mysql.connector from requests.auth import HTTPDigestAuth HOST = "https://api.tidbcloud.com" def create_user( public_key: str, private_key: str, project_id: str, cluster_id: str, branch_name: str, ) -> str: # serverless_branch_info: strval($branch_cluster_id) . ":::" . $cluster_name . ":::" . $cluster_host . ":::" . strval($cluster_port) . ":::" . $admin_name . ":::" . $admin_password . ":::" . $main_cluster_id # p = serverless_branch_info.split(":::") # branch_name = p[1] url = f"{HOST}/api/internal/projects/{project_id}/clusters/{cluster_id}/branches/{branch_name}/users" # cur_date = datetime.datetime.now().strftime("%Y-%m-%d") data_for_create_user = {"type": "Admin"} data_for_create_user_json = json.dumps(data_for_create_user) resp = requests.post( url=url, data=data_for_create_user_json, auth=HTTPDigestAuth(public_key, private_key), ) if resp.status_code != 200: # print(f"request invalid, code : {resp.status_code}, message : {resp.text}") # raise Exception( # f"request invalid, code : {resp.status_code}, message : {resp.text}" # ) return "ERROR_CALLING_REST_USERS" return resp.json()["username"] + ":::" + resp.json()["password"] if __name__ == "__main__": public_key = sys.argv[1] private_key = sys.argv[2] project_id = "1372813089454311305" # TiDB Cloud Project branch_name = sys.argv[3] cluster_id = sys.argv[4] # tokyo-c1 ID new_user = create_user(public_key, private_key, project_id, cluster_id, branch_name) print(new_user)