From 6fe5d2bd2a9ec193213a6774dfeac7081d84de38 Mon Sep 17 00:00:00 2001 From: Administrator Date: Fri, 7 Jun 2024 17:03:47 +0800 Subject: [PATCH] update --- keystone/__pycache__/project.cpython-36.pyc | Bin 0 -> 2480 bytes keystone/__pycache__/user.cpython-36.pyc | Bin 2280 -> 2602 bytes keystone/project.py | 70 ++++++++++++++++++++ keystone/user.py | 16 ++++- main.py | 58 +++++++++++++++- 5 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 keystone/__pycache__/project.cpython-36.pyc create mode 100644 keystone/project.py diff --git a/keystone/__pycache__/project.cpython-36.pyc b/keystone/__pycache__/project.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..2eec7cad50e672653f92d9b6be7e4e7d159fd1be GIT binary patch literal 2480 zcmb_dO=~1Y7_RE?>G?=D>SiO0p9uCsHi6YkkVR219)h?FO4ucIrpld}%yf6EYSe@p zFY&Ml0Z*cAAPOt!K~dO)`wu+$3+g0jqWKG+eBbJs$z;sB2-`zfb-z9J^*qn}yjSY= zcA7nzCCp{sCuGfIJ`14tS;!*j z16E-%^dYOV8uSsXvj+4PMz4`%<``xrs|hs$%X?`%Yq}VKD}>vF`*pa>wW%iau--fw zxXjhGO$VML9jaaJF>lWq_#eHf{f@Isn7=^YAU7`z0v)hmfoNyb5zFu#E|6>RJhAo~lE~JS@ ze=%N`_PBA{EOAXF_*S1wC5?NFE8}%!o+TlCER3&k3R4@7TeqX!zUT@(ERK{#o=I+K z`#U0LA@=vxH&<>nbH;BJ+p3jkH}ZmKQl-t!#k6Qoy>0Sssqzf|TWdsbjt$}@aQ_yavN;E@raE!X8U!RxGCHJq44#{KEWB{a z0B$R&4S1M1o^OvuG>B=}wXz`~nM)|?HprCuR&F5+w4(!%8%TQ}z6>JebX@K0Pg$VDGFet2;3$Ip9*-+ytq|Ltk%nQ+MC^~4i+7*i=z zWxdTzm2ue=?LxKl%!E8kH@ci9PAM#pr7BGVfdrd6A~hap5jS3uOJyN9KI``inZy&w zBy|mQM4MPzwN%f>|^z;cyzr_#_)2qUoA!Td5P}*mJ^(3P$l(J)?0+@zSc~emNBdAya5Wj;`w-8qlJ{IpgFb7Jz zy83AFJN^~+Yr-CvGQGviT)6$!@t1dxZhv<8>A^(c@a|84-TMZ_jpUgs*rDpnb#%!| z4xo_mE>~PU5C2N0X%J6C^da~+$_YjcBrrlE#igwTLk41yuXq8XO*9p}(5}V?^D(?k zuz;25L6QwXvf)s&5m*e=Gmz}aVk%a|p^lW>@pNTqF|m$2K8t~&4C|_8sBUA8F+($t zMCRfNILXL*gw1x<$G<=$o`c3xc68_7(dR!-a99RTaad9moaB%3i&QmR z){(?ToX?V2oyw)P*^G}9q^zcOjEvpQ%-GixYn-P2!KT$Oz%<9E^%Cqs;zmmDZT6iq zEvte{@Ex4f7BQ?}CK{DwYrW<9uohX1v#D#9BXHnEMZ?N3Y*~w5>E*27#))AI(Br{RbBTL1zE} literal 0 HcmV?d00001 diff --git a/keystone/__pycache__/user.cpython-36.pyc b/keystone/__pycache__/user.cpython-36.pyc index ffd48bb054b7a9ff090319a183a9f4d18703f304..44a28eeba044dcbb4e0bac7a44a02fa2fb634b8b 100644 GIT binary patch literal 2602 zcmb_dTZi24vkG$t=Ttb!54urf@hI@y_>nVwWv8+WM@ zBqBiw_#mQemPCjUAG|@p_#gZQ^{TURe}UkW->IIN%#ND}A>DM9cLna6z0 z9t&8A*=G?eVGdZCRWOIF%H}Xf%zW2~=eO|(V<|S3$I@=2)q~{sdD>2zq8ZRTTopVd zzlkUN7y~n<*)j)#FxsXxq$%Ai_8=5i+tw=&t4M~-SPl4#%wl#6zmkz&pJL7mR?L-_ zw520M8A+dc*FBt95>DG?zBD!r7HrzWYx}Zv#guU5nms7Xa@)IN!02dcy?pNI8@X6H zdNy56ddf{WPkCO3X~P>g{=9o__`}!3>$mecD7L@-?cT`aPl8H`pOmB>yUm{CpWl{lQ>ZX12v6Wt@h1&pf@Ah3{IT#kcNmpbIzx1O*uTt z`rjM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EY@n9)y3Br78YusUa8?{GPJeBnyRV&HQr=6;OIc`+bNey zYA1=M(oRRlk#myParPnO_%hjRyW#I1rh@IE4>|c5cROLpy@Y3ZHf zNB}9YVf^9(Y%Y)A8;c)V$_3$p6w)q;sjYZ#5{=Rk8jUu)2%ktq5b?M~s-z4oiy7#T zP(f5jsJsUqWo1@IGJA_6lZ-&LvuNHn7!oXnb^}Zd=Uzs4t$nBOD$=nt7qIeye=qYo z)YvPiYp4Ex{hD^U5V1(7VEIR1KRDR_?XTZ=20wi}*n4o&f5!ruI%%z1I8=yZc5h@d z29rMlllG07+APP*2U9ds(LGf?!4Y0_u)CoZ048%!17zYdjGqn{gEx-{E*1dH*I9uT z4a_Bkkd^oBTwr0PE-t~p#M4kb1{4qffOWLr<5D(Pd5R{=X&G$aJN)kc!S+{!FTelK z@ZkQhe?R;ow9PJb362|;$_Ac7U{lmoZeEY^E>~PU2P5GmF=7_y87Du3t<&rbXDu#Z z9Q`s(lDIz$r3>N>2y#+H89xuBqs)M(#V9jm&>7xEn4$&sxmdJU+flRII7&Qc8)CCiJDLQafF$;x3ehwh_CSjTLKHoc<)+j{a%&{}lg5#`KlI3yN@;{{?^96S4pR diff --git a/keystone/project.py b/keystone/project.py new file mode 100644 index 0000000..5a50af3 --- /dev/null +++ b/keystone/project.py @@ -0,0 +1,70 @@ +import requests +import json +from keystone import domain +def get_project_id(ip, token, project_name): + url = "http://" + ip + ":5000/v3/projects" + headers = {"X-Auth-Token": token} + response = requests.get(url, headers=headers) + for i in response.json()["projects"]: + if i["name"] == project_name: + return i['id'] + return None +def get_project(ip, token): + url = "http://" + ip + ":5000/v3/projects" + headers = {"X-Auth-Token": token} + response = requests.get(url, headers=headers) + return response.json()["projects"] +def create_project(ip, token, project_name,domain_name, description): + url = "http://" + ip + ":5000/v3/projects" + domain_id = domain.get_domain_id(ip, token, domain_name) + if domain_id == None: + return {"error":"域不存在"} + headers = {"X-Auth-Token": token} + data = { + "project": { + "name": project_name, + "domain_id": domain_id, + "description": description, + "enabled": True + } + } + response = requests.post(url, headers=headers, data=json.dumps(data)) + return response.json() +def show_project(ip, token, project_name): + project_id=get_project_id(ip, token, project_name) + if project_id == None: + return {"error":"项目不存在"} + url = "http://" + ip + ":5000/v3/projects/" + project_id + headers = {"X-Auth-Token": token} + response = requests.get(url, headers=headers) + return response.json() +def delete_project(ip, token, project_name): + project_id=get_project_id(ip, token, project_name) + if project_id == None: + return {"项目不存在"} + url = "http://" + ip + ":5000/v3/projects/" + project_id + headers = {"X-Auth-Token": token} + response = requests.delete(url, headers=headers) + if response.status_code == 204: + return {"message": "删除成功"} + else: + return {"error": "删除失败", "status_code": response.status_code} +def update_project(ip, token, project_name, new_project_name,new_domain_id, new_description): + project_id=get_project_id(ip, token, project_name) + if project_id == None: + return {"error":"项目不存在"} + url = "http://" + ip + ":5000/v3/projects/" + project_id + headers = {"X-Auth-Token": token} + data = { + "project": { + "name": new_project_name, + "domain_id": new_domain_id, + "description": new_description, + "enabled": True + } + } + response = requests.patch(url, headers=headers, data=json.dumps(data)) + if response.status_code == 200: + return {"message": "更新成功"} + else: + return {"error": "更新失败","status_code": response.status_code} \ No newline at end of file diff --git a/keystone/user.py b/keystone/user.py index 5d93c07..5214969 100644 --- a/keystone/user.py +++ b/keystone/user.py @@ -1,12 +1,24 @@ import requests import json -def create_user(ip, token, user_name, password, description): +from keystone import domain +from keystone import project + +def create_user(ip, token, user_name,password, email,domain_name,default_project_name, description): headers = {'X-Auth-Token': token} + domain_id = domain.get_domain_id(ip, token, domain_name) + default_project_id = project.get_project_id(ip, token, default_project_name) + if domain_id is None: + return {"error": "域不存在"} + if default_project_id is None: + return {"项目不存在"} body = { "user": { "name": user_name, "password": password, - "description": description, + "email": email, + "domain_id": domain_id, + "default_project_id": default_project_id, + "description": description } } resp = requests.post(f"http://{ip}:5000/v3/users", data=json.dumps(body), headers=headers) diff --git a/main.py b/main.py index bacad85..ef585d2 100644 --- a/main.py +++ b/main.py @@ -10,6 +10,7 @@ def main(): print("1. 用户管理") print("2. 组管理") print("3. 域管理") + print("4. 项目管理") print("====================") choice = input("输入选项 (1-3): ") @@ -30,8 +31,11 @@ def main(): if choice == '1': user_name = input("请输入用户名: ") password = input("请输入密码: ") + email=input("请输入邮箱: ") + domain_name=input("请输入域名: ") + default_project_name=input("请输入默认项目: ") description = input("请输入描述: ") - result = user.create_user(ip, token, user_name, password, description) + result = user.create_user(ip, token, user_name,password, email,domain_name,default_project_name, description) print(result) print("====================") main() @@ -114,7 +118,57 @@ def main(): print("无效选项") main() # - # elif choice == '3': + elif choice == '3': + print("====================") + print("选择一个操作:") + print("1. 创建域") + print("2. 获取所有域信息") + print("3. 获取特定域信息") + print("4. 删除域") + print("5. 更新用户密码") + print("====================") + choice = input("输入选项 (1-5): ") + print("====================") + if choice == '1': + user_name = input("请输入用户名: ") + password = input("请输入密码: ") + email = input("请输入邮箱: ") + domain_name = input("请输入域名: ") + default_project_name = input("请输入默认项目: ") + description = input("请输入描述: ") + result = user.create_user(ip, token, user_name, password, email, domain_name, default_project_name,description) + print(result) + print("====================") + main() + + elif choice == '2': + result = user.get_users(ip, token) + print(result) + main() + + elif choice == '3': + user_name = input("请输入用户名: ") + result = user.get_user(ip, token, user_name) + print(result) + main() + + elif choice == '4': + user_name = input("请输入用户名: ") + result = user.delete_user(ip, token, user_name) + print(result) + main() + + elif choice == '5': + user_name = input("请输入用户名: ") + new_password = input("请输入新密码: ") + original_password = input("请输入原始密码: ") + result = user.update_user_password(ip, token, user_name, new_password, original_password) + print(result) + main() + + else: + print("无效选项") + main() # # elif choice == '4': #2