Python3批量創(chuàng)建Crowd用戶并分配組
背景
遷移 Crowd 完成后(之前采用 LDAP 方式,新遷移 Crowd 不采用),需要批量創(chuàng)建公司所有員工的用戶以及分配組,手工創(chuàng)建以及之前 Postman 的方式還是比較低效。
Python 在 N 多年前入門,寫了幾個(gè)爬蟲腳本后,再也沒用過,借這個(gè)機(jī)會(huì)順便再熟悉下 Python 腳本。
歸根結(jié)底的原因就是:本人很懶~
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\" : \"test.user@ourdomain.com\", \"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的參數(shù)為 Crowd 中應(yīng)用(Application)的用戶名和密碼,Crowd 的管理員是不能添加用戶。
Python 實(shí)現(xiàn)腳本
實(shí)現(xiàn)添加 Crowd 用戶,用戶添加到指定組,讀取 csv 文件批量添加用戶和設(shè)定的多個(gè)組。
crowdUsers.csv 用戶數(shù)據(jù) csv 文件
name,displayName,email
daodaotest1,daodaotest1,daodaotest1@daodaotest.com
daodaotest2,daodaotest2,daodaotest2@daodaotest.com
daodaotest3,daodaotest3,daodaotest3@daodaotest.com
......
addCrowdUsers.py 批量添加 Crowd 用戶和用戶組腳本
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
# Filename addCrowdUsers.py
# Revision 0.0.1
# Date 2020/5/14
# Author jiangliheng
# Email jiang_liheng@163.com
# 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 訪問基礎(chǔ)路徑
base_url='http://localhost:8095'
# 添加用戶的默認(rèn)用戶組和密碼
auth_username='application-name'
auth_password='password'
# 用戶默認(rèn)密碼
password='daodaotest'
def addUser(name,displayName,email):
"""
添加單用戶
:param name: 登錄用戶,建議拼音全稱,如:jiangliheng
:param displayName: 顯示名稱,建議中文全稱,如:蔣李恒
:param email: 郵箱地址
:return: status_code 狀態(tài)碼,text 響應(yīng)報(bào)文信息
"""
# 請求 json 數(shù)據(jù)
data = '{ \
"name" :"' + name + '", \
"email" : "' + email + '", \
"active" : true, \
"first-name" : "' + displayName + '", \
"last-name" : "' + displayName + '", \
"display-name" : "'+ displayName + '", \
"password" : { \
"value" : "' + password + '" \
} \
}'
# 發(fā)起請求
# 解決中文亂碼問題 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")
)
# 狀態(tài)碼
status_code=response.status_code
# 響應(yīng)報(bào)文信息
text=response.text
# 狀態(tài)判斷
if str(status_code).startswith("2"):
print("%s 用戶添加成功,狀態(tài)碼:%s ,響應(yīng)報(bào)文信息:%s" % (name,status_code,text))
else:
print("%s 用戶添加失敗,狀態(tài)碼:%s ,響應(yīng)報(bào)文信息:%s" % (name,status_code,text))
# 返回 狀態(tài)碼,響應(yīng)報(bào)文信息
return status_code,text
def addGroup(username,groupname):
"""
用戶添加到組
:param username: 登錄用戶,建議拼音全稱,如:jiangliheng
:param groups: 用戶組,用逗號(hào)隔開,如:bitbucket-users,bamboo-users
:return: status_code 狀態(tài)碼,text 響應(yīng)報(bào)文信息
"""
# 請求 json 數(shù)據(jù)
data = '{ \
"name" :"' + groupname + '" \
}'
# 發(fā)起請求
response = requests.post(
base_url + '/crowd/rest/usermanagement/1/user/group/direct?username='+username,
headers=headers,
auth=HTTPBasicAuth(auth_username,auth_password),
data=data
)
# 狀態(tài)碼
status_code=response.status_code
# 響應(yīng)報(bào)文信息
text=response.text
# 狀態(tài)判斷
if str(status_code).startswith("2"):
print("%s 用戶添加組 %s 成功,狀態(tài)碼:%s ,響應(yīng)報(bào)文信息:%s" % (username,groupname,status_code,text))
else:
print("%s 用戶添加組 %s 失敗,狀態(tài)碼:%s ,響應(yīng)報(bào)文信息:%s" % (username,groupname,status_code,text))
# 返回 狀態(tài)碼,響應(yīng)報(bào)文信息
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"])
# 添加多個(gè)組
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","叨叨軟件測試","daodaotest@daodaotest.com")
# 添加用戶到組
# addGroup("daodaotest","all-users")
if __name__ == "__main__":
main()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用python創(chuàng)建生成動(dòng)態(tài)鏈接庫dll的方法
- Python基于當(dāng)前時(shí)間批量創(chuàng)建文件
- python+selenium+chrome批量文件下載并自動(dòng)創(chuàng)建文件夾實(shí)例
- 使用Pycharm(Python工具)新建項(xiàng)目及創(chuàng)建Python文件的教程
- 在python里創(chuàng)建一個(gè)任務(wù)(Task)實(shí)例
- python GUI庫圖形界面開發(fā)之PyQt5結(jié)合Qt Designer創(chuàng)建信號(hào)與槽的詳細(xì)方法與實(shí)例
- python GUI編程(Tkinter) 創(chuàng)建子窗口及在窗口上用圖片繪圖實(shí)例
- Python創(chuàng)建空列表的字典2種方法詳解
相關(guān)文章
解決python訪問報(bào)錯(cuò):jinja2.exceptions.TemplateNotFound:index.html
這篇文章主要介紹了解決python訪問報(bào)錯(cuò):jinja2.exceptions.TemplateNotFound:index.html,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Python AutoCAD 系統(tǒng)設(shè)置的實(shí)現(xiàn)方法
這篇文章主要介紹了Python AutoCAD 系統(tǒng)設(shè)置的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python semaphore evevt生產(chǎn)者消費(fèi)者模型原理解析
這篇文章主要介紹了Python semaphore evevt生產(chǎn)者消費(fèi)者模型原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
使用Python的web.py框架實(shí)現(xiàn)類似Django的ORM查詢的教程
這篇文章主要介紹了使用Python的web.py框架實(shí)現(xiàn)類似Django的ORM查詢的教程,集成的ORM操作數(shù)據(jù)庫向來是Python最強(qiáng)大的功能之一,本文則探討如何在web.py框架上實(shí)現(xiàn),需要的朋友可以參考下2015-05-05
Python 循環(huán)終止語句的三種方法小結(jié)
今天小編就為大家分享一篇Python 循環(huán)終止語句的三種方法小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Python中利用pyqt5制作指針鐘表顯示實(shí)時(shí)時(shí)間(指針時(shí)鐘)
這篇文章主要介紹了Python中利用pyqt5制作指針鐘表顯示實(shí)時(shí)時(shí)間(動(dòng)態(tài)指針時(shí)鐘),本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02

