一、认证
def get_headers(base_url,username,password): data = { "username": username, "password": password } url = "%s/api/v1/users/auth/" % base_url res = requests.post(url=url, data=data) token = json.loads(res.content)["token"] headers = { "Authorization": "Bearer %s" %token, "Content-Type": "application/json" } return headers
二、查询用户组
def get_user_group_info(headers): ''' :param groupname: :return: ''' ret = { "result":True, "message":"", "data":{} } url = "%s/api/users/v1/groups/" % base_url res = requests.get(url=url,headers=headers) res_dic = json.loads(res.content) for item in res_dic: ret["data"][item["name"]] = item["id"] return ret
三、添加用户
def add_user(headers,name,username,email,role="User",otp_level=0,is_active="true",source="LDAP/AD"): ret = { "result":False, "message":"", "data":"", } try: get_user_group_info_res = get_user_group_info() data = { "name":name, "username":username, "email":email, "role":role, "otp_level":otp_level, "is_active":is_active, "source":source, "groups":[get_user_group_info_res["data"]["Default"]], } url = "%s/api/v1/users/users/" % base_url requests.post(url=url, data=json.dumps(data),headers=headers) ret["result"] = True except Exception as e: print e return ret