This commit is contained in:
Administrator 2024-06-07 17:03:47 +08:00
parent 2a55aca169
commit 6fe5d2bd2a
5 changed files with 140 additions and 4 deletions

Binary file not shown.

70
keystone/project.py Normal file
View File

@ -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}

View File

@ -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)

58
main.py
View File

@ -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