Python 3は、Crowdユーザーの一括作成とグループの割り当てを実現
6112 ワード
背景
移行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には少し違いがあります.
注意:ここで
Python実装スクリプト
Crowdユーザーの追加を実現し、ユーザーを指定したグループに追加し、csvファイルを読み込んでユーザーと設定した複数のグループを一括追加します.
crowdUsers.csvユーザデータcsvファイル
addCrowdUsers.py Crowdユーザーとユーザーグループスクリプトの一括追加
微信公衆番号:daodaotest
移行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に適用されているユーザー名とパスワードであり、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,displayName,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,status_code,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,
headers=headers,
auth=HTTPBasicAuth(auth_username,auth_password),
data=data
)
#
status_code=response.status_code
#
text=response.text
#
if str(status_code).startswith("2"):
print("%s %s , :%s , :%s" % (username,groupname,status_code,text))
else:
print("%s %s , :%s , :%s" % (username,groupname,status_code,text))
# ,
return status_code,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()
微信公衆番号:daodaotest