This commit is contained in:
Administrator 2024-06-07 15:08:22 +08:00
parent d241efd180
commit 2a55aca169
8 changed files with 64 additions and 30 deletions

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

Binary file not shown.

View File

@ -13,6 +13,8 @@ def get_domain_id(ip, token, domain_name):
if i['name'] == domain_name: if i['name'] == domain_name:
domain_id = i['id'] domain_id = i['id']
return domain_id return domain_id
else:
return None
def create_domain(ip, token, domain_name, description): def create_domain(ip, token, domain_name, description):
url = "http://" + ip + ":5000/v3/domains" url = "http://" + ip + ":5000/v3/domains"
headers = {'X-Auth-Token': token} headers = {'X-Auth-Token': token}
@ -27,6 +29,8 @@ def create_domain(ip, token, domain_name, description):
return response.json() return response.json()
def show_domain(ip, token,domain_name): def show_domain(ip, token,domain_name):
domain_id = get_domain_id(ip, token, domain_name) domain_id = get_domain_id(ip, token, domain_name)
if domain_id == None:
return None
url = "http://" + ip + ":5000/v3/domains/" + domain_id url = "http://" + ip + ":5000/v3/domains/" + domain_id
headers = {'X-Auth-Token': token} headers = {'X-Auth-Token': token}
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)

View File

@ -1,50 +1,73 @@
import requests import requests
import json import json
def create_group(ip, token, group_name, description): from keystone import domain
def create_group(ip, token, group_name,domain_name, description):
domain_id=domain.get_domain_id(ip, token, domain_name)
if domain_id is None:
return {"error": "域不存在"}
url = "http://" + ip + ":5000/v3/groups" url = "http://" + ip + ":5000/v3/groups"
headers = {'X-Auth-Token': token} headers = {'X-Auth-Token': token}
data = { data = {
"group": { "group": {
"name": group_name, "name": group_name,
"domain_id": "default", "domain_id": domain_id,
"description": description "description": description
} }
} }
response = requests.post(url, headers=headers, data=json.dumps(data)) response = requests.post(url, headers=headers, data=json.dumps(data))
print(domain_id)
return response.json() return response.json()
def get_group_id(ip, token, group_name): def get_group_id(ip, token, group_name):
url = "http://" + ip + ":5000/v3/groups" url = "http://" + ip + ":5000/v3/groups"
headers = {'X-Auth-Token': token} headers = {'X-Auth-Token': token}
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers).json()
for i in response.json()['groups']: for i in response['groups']:
if i['name'] == group_name: if i['name'] == group_name:
return i['id'] return i['id']
return None
def show_group(ip, token, group_name): def show_group(ip, token, group_name):
group_id = get_group_id(ip, token, group_name) group_id = get_group_id(ip, token, group_name)
if group_id is None:
return {"error": "组不存在"}
url = "http://" + ip + ":5000/v3/groups/" + group_id url = "http://" + ip + ":5000/v3/groups/" + group_id
headers = {'X-Auth-Token': token} headers = {'X-Auth-Token': token}
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
return response.json() return response.json()
def delete_group(ip, token, group_name): def delete_group(ip, token, group_name):
group_id = get_group_id(ip, token, group_name) group_id = get_group_id(ip, token, group_name)
if group_id is None:
return {"error": "组不存在"}
url = "http://" + ip + ":5000/v3/groups/" + group_id url = "http://" + ip + ":5000/v3/groups/" + group_id
headers = {'X-Auth-Token': token} headers = {'X-Auth-Token': token}
response = requests.delete(url, headers=headers) response = requests.delete(url, headers=headers)
return response.json() if response.status_code == 204:
return {"message": "删除成功"}
else:
return {"error": "删除失败","status_code": response.status_code}
def get_group(ip, token): def get_group(ip, token):
url = "http://" + ip + ":5000/v3/groups" url = "http://" + ip + ":5000/v3/groups"
headers = {'X-Auth-Token': token} headers = {'X-Auth-Token': token}
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
return response.json() return response.json()
def update_group(ip, token, group_name, new_group_name, new_description): def update_group(ip, token, group_name, new_group_name,new_domain_name, new_description):
group_id = get_group_id(ip, token, group_name) group_id = get_group_id(ip, token, group_name)
if group_id is None:
return {"error": "组不存在"}
new_domain_id = domain.get_domain_id(ip, token, new_domain_name)
url = "http://" + ip + ":5000/v3/groups/" + group_id url = "http://" + ip + ":5000/v3/groups/" + group_id
headers = {'X-Auth-Token': token} headers = {'X-Auth-Token': token}
data = { data = {
"group": { "group": {
"name": new_group_name, "name": new_group_name,
"domain_id": new_domain_id,
"description": new_description "description": new_description
} }
} }
response = requests.patch(url, headers=headers, data=json.dumps(data)) response = requests.patch(url, headers=headers, data=json.dumps(data))
return response.json() if response.status_code == 200:
return {"message": "更新成功"}
else:
return {"error": "更新失败","status_code": response.status_code}

View File

@ -10,13 +10,13 @@ def create_user(ip, token, user_name, password, description):
} }
} }
resp = requests.post(f"http://{ip}:5000/v3/users", data=json.dumps(body), headers=headers) resp = requests.post(f"http://{ip}:5000/v3/users", data=json.dumps(body), headers=headers)
return resp.json() # Assuming the API returns JSON return resp.json()
def get_users(ip, token): def get_users(ip, token):
headers = {'X-Auth-Token': token} headers = {'X-Auth-Token': token}
resp = requests.get(f"http://{ip}:5000/v3/users", headers=headers) resp = requests.get(f"http://{ip}:5000/v3/users", headers=headers)
return resp.json() # Assuming the API returns JSON return resp.json()
def get_user_id(ip, token, user_name): def get_user_id(ip, token, user_name):
@ -25,24 +25,24 @@ def get_user_id(ip, token, user_name):
for item in result['users']: for item in result['users']:
if item['name'] == user_name: if item['name'] == user_name:
return item['id'] return item['id']
return "NONE" else:
return None
def get_user(ip, token, user_name): def get_user(ip, token, user_name):
user_id = get_user_id(ip, token, user_name) user_id = get_user_id(ip, token, user_name)
if user_id == "NONE": if user_id == "NONE":
return {"error": "User not found"} return {"error": "用户不存在"}
headers = {'X-Auth-Token': token} headers = {'X-Auth-Token': token}
api_url = f"http://{ip}:5000/v3/users/{user_id}" api_url = f"http://{ip}:5000/v3/users/{user_id}"
resp = requests.get(api_url, headers=headers) resp = requests.get(api_url, headers=headers)
return resp.json() # Assuming the API returns JSON return resp.json()
def delete_user(ip, token, user_name): def delete_user(ip, token, user_name):
user_id = get_user_id(ip, token, user_name) user_id = get_user_id(ip, token, user_name)
if user_id == "NONE": if user_id == "NONE":
return {"error": "User not found"} return {"error": "用户不存在"}
headers = {'X-Auth-Token': token} headers = {'X-Auth-Token': token}
api_url = f"http://{ip}:5000/v3/users/{user_id}" api_url = f"http://{ip}:5000/v3/users/{user_id}"

31
main.py
View File

@ -1,4 +1,4 @@
from keystone import get_token from keystone import get_token, group
from keystone import user from keystone import user
from keystone import domain from keystone import domain
import time import time
@ -77,39 +77,39 @@ def main():
choice = input("输入选项 (1-5): ") choice = input("输入选项 (1-5): ")
print("====================") print("====================")
if choice == '1': if choice == '1':
user_name = input("请输入组名: ") group_name = input("请输入组名: ")
password = input("请输入域: ") domain_name = input("请输入域: ")
description = input("请输入描述: ") description = input("请输入描述: ")
result = user.create_user(ip, token, user_name, password, description) result = group.create_group(ip, token,group_name,domain_name, description)
print(result) print(result)
print("====================") print("====================")
main() main()
elif choice == '2': elif choice == '2':
result = user.get_users(ip, token) result = group.get_group(ip, token)
print(result) print(result)
main() main()
elif choice == '3': elif choice == '3':
user_name = input("请输入用户名: ") group_name = input("请输入组名: ")
result = user.get_user(ip, token, user_name) result = group.show_group(ip, token,group_name)
print(result) print(result)
main() main()
elif choice == '4': elif choice == '4':
user_name = input("请输入用户名: ") group_name:str = input("请输入组名: ")
result = user.delete_user(ip, token, user_name) result = group.delete_group(ip, token, group_name)
print(result) print(result)
main() main()
elif choice == '5': elif choice == '5':
user_name = input("请输入用户名: ") group_name = input("请输入原组名: ")
new_password = input("请输入新密码: ") new_group_name= input("请输入新组名: ")
original_password = input("请输入原始密码: ") new_domain_name = input("请输入新域名: ")
result = user.update_user_password(ip, token, user_name, new_password, original_password) new_description = input("请输入新描述: ")
result = group.update_group(ip, token,group_name,new_group_name,new_domain_name, new_description)
print(result) print(result)
main() main()
else: else:
print("无效选项") print("无效选项")
main() main()
@ -117,7 +117,8 @@ def main():
# elif choice == '3': # elif choice == '3':
# #
# elif choice == '4': # elif choice == '4':
# #2
# elif choice == '5': # elif choice == '5':
else: else:
print("无效选项") print("无效选项")