Python3批量建立Crowd使用者並分配組
背景
遷移 Crowd 完成後(之前採用 LDAP 方式,新遷移 Crowd 不採用),需要批量建立公司所有員工的使用者以及分配組,手工建立以及之前 Postman 的方式還是比較低效。
Python 在 N 多年前入門,寫了幾個爬蟲指令碼後,再也沒用過,借這個機會順便再熟悉下 Python 指令碼。
歸根結底的原因就是:本人很懶~
Crowd Api
https://docs.atlassian.com/atlassian-crowd/3.2.0/REST/
如下示例是基於 Crowd 3.2.0 版本的 Api,不同版本間的 Api 稍有差異。
# 新增使用者
$ curl -u "application-name:password" -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d "{\"name\" : \"test.user\",\"display-name\" : \"Test User\",\"active\" : true,\"first-name\" : \"Test\",\"email\" : \"[email protected]\",\"last-name\" : \"User\",\"password\" : {\"value\" : \"mypassword\"} }" http://localhost:8095/crowd/rest/usermanagement/1/user# 使用者新增到組
$ curl -u "application-name:password" -X POST -H "Content-Type: application/json" -d "{\"name\" : \"all-users\"}" http://localhost:8095/crowd/rest/usermanagement/1/user/group/direct\?username\=daodaotest
注意:此處-u的引數為 Crowd 中應用(Application)的使用者名稱和密碼,Crowd 的管理員是不能新增使用者。
Python 實現指令碼
實現新增 Crowd 使用者,使用者新增到指定組,讀取 csv 檔案批量新增使用者和設定的多個組。
crowdUsers.csv 使用者資料 csv 檔案
name,displayName,email
daodaotest1,daodaotest1,[email protected]
daodaotest2,daodaotest2,[email protected]
daodaotest3,daodaotest3,[email protected]
......
addCrowdUsers.py 批量新增 Crowd 使用者和使用者組指令碼
#!/usr/bin/python # -*- coding: UTF-8 -*- # # Filename addCrowdUsers.py # Revision 0.0.1 # Date 2020/5/14 # Author jiangliheng # Email [email protected] # Website https://jiangliheng.github.io/ # Description 批量新增 Crowd 使用者和使用者組 import requests from requests.auth import HTTPBasicAuth import csv from itertools import islice # 請求 headers headers = { 'Accept': 'application/json','Content-type': 'application/json',} # crowd 訪問基礎路徑 base_url='http://localhost:8095' # 新增使用者的預設使用者組和密碼 auth_username='application-name' auth_password='password' # 使用者預設密碼 password='daodaotest' def addUser(name,email): """ 新增單使用者 :param name: 登入使用者,建議拼音全稱,如:jiangliheng :param displayName: 顯示名稱,建議中文全稱,如:蔣李恆 :param email: 郵箱地址 :return: status_code 狀態碼,text 響應報文資訊 """ # 請求 json 資料 data = '{ \ "name" :"' + name + '",\ "email" : "' + email + '",\ "active" : true,\ "first-name" : "' + displayName + '",\ "last-name" : "' + displayName + '",\ "display-name" : "'+ displayName + '",\ "password" : { \ "value" : "' + password + '" \ } \ }' # 發起請求 # 解決中文亂碼問題 data.encode("utf-8").decode("latin1") response = requests.post( base_url + '/crowd/rest/usermanagement/1/user',headers=headers,auth=HTTPBasicAuth(auth_username,auth_password),data=data.encode("utf-8").decode("latin1") ) # 狀態碼 status_code=response.status_code # 響應報文資訊 text=response.text # 狀態判斷 if str(status_code).startswith("2"): print("%s 使用者新增成功,狀態碼:%s ,響應報文資訊:%s" % (name,status_code,text)) else: print("%s 使用者新增失敗,狀態碼:%s ,響應報文資訊:%s" % (name,text)) # 返回 狀態碼,響應報文資訊 return status_code,text def addGroup(username,groupname): """ 使用者新增到組 :param username: 登入使用者,建議拼音全稱,如:jiangliheng :param groups: 使用者組,用逗號隔開,如:bitbucket-users,bamboo-users :return: status_code 狀態碼,text 響應報文資訊 """ # 請求 json 資料 data = '{ \ "name" :"' + groupname + '" \ }' # 發起請求 response = requests.post( base_url + '/crowd/rest/usermanagement/1/user/group/direct?username='+username,data=data ) # 狀態碼 status_code=response.status_code # 響應報文資訊 text=response.text # 狀態判斷 if str(status_code).startswith("2"): print("%s 使用者新增組 %s 成功,狀態碼:%s ,響應報文資訊:%s" % (username,groupname,text)) else: print("%s 使用者新增組 %s 失敗,狀態碼:%s ,響應報文資訊:%s" % (username,text def addUserByCsv(csvfile): """ 通過 CSV 檔案批量新增使用者,並加到組 :param filename: Crowd 使用者 csv 檔案 """ # 批量讀取 csv 的使用者 with open(csvfile,'r',encoding='utf-8') as f: fieldnames = ("name","displayName","email") reader = csv.DictReader(f,fieldnames) for row in islice(reader,1,None): print("批量新增使用者 %s" % (row["name"])) # 新增使用者 addUser(row["name"],row["displayName"],row["email"]) # 新增多個組 addGroup(row["name"],"all-users") addGroup(row["name"],"bitbucket-users") addGroup(row["name"],"confluence-users") addGroup(row["name"],"jira-software-users") addGroup(row["name"],"sonar-users") f.close() def main(): # 通過 CSV 檔案批量新增使用者,並加到組 addUserByCsv("crowdUsers.csv") # 新增單使用者 # addUser("daodaotest","叨叨軟體測試","[email protected]") # 新增使用者到組 # addGroup("daodaotest","all-users") if __name__ == "__main__": main()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。