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

View File

@ -1,50 +1,73 @@
import requests
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"
headers = {'X-Auth-Token': token}
data = {
"group": {
"name": group_name,
"domain_id": "default",
"name": group_name,
"domain_id": domain_id,
"description": description
}
}
response = requests.post(url, headers=headers, data=json.dumps(data))
print(domain_id)
return response.json()
def get_group_id(ip, token, group_name):
url = "http://" + ip + ":5000/v3/groups"
headers = {'X-Auth-Token': token}
response = requests.get(url, headers=headers)
for i in response.json()['groups']:
response = requests.get(url, headers=headers).json()
for i in response['groups']:
if i['name'] == group_name:
return i['id']
return None
def show_group(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
headers = {'X-Auth-Token': token}
response = requests.get(url, headers=headers)
return response.json()
def delete_group(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
headers = {'X-Auth-Token': token}
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):
url = "http://" + ip + ":5000/v3/groups"
headers = {'X-Auth-Token': token}
response = requests.get(url, headers=headers)
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)
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
headers = {'X-Auth-Token': token}
data = {
"group": {
"name": new_group_name,
"domain_id": new_domain_id,
"description": new_description
}
}
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)
return resp.json() # Assuming the API returns JSON
return resp.json()
def get_users(ip, token):
headers = {'X-Auth-Token': token}
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):
@ -25,24 +25,24 @@ def get_user_id(ip, token, user_name):
for item in result['users']:
if item['name'] == user_name:
return item['id']
return "NONE"
else:
return None
def get_user(ip, token, user_name):
user_id = get_user_id(ip, token, user_name)
if user_id == "NONE":
return {"error": "User not found"}
return {"error": "用户不存在"}
headers = {'X-Auth-Token': token}
api_url = f"http://{ip}:5000/v3/users/{user_id}"
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):
user_id = get_user_id(ip, token, user_name)
if user_id == "NONE":
return {"error": "User not found"}
return {"error": "用户不存在"}
headers = {'X-Auth-Token': token}
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 domain
import time
@ -77,39 +77,39 @@ def main():
choice = input("输入选项 (1-5): ")
print("====================")
if choice == '1':
user_name = input("请输入组名: ")
password = input("请输入域: ")
group_name = input("请输入组名: ")
domain_name = 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("====================")
main()
elif choice == '2':
result = user.get_users(ip, token)
result = group.get_group(ip, token)
print(result)
main()
elif choice == '3':
user_name = input("请输入用户名: ")
result = user.get_user(ip, token, user_name)
group_name = input("请输入组名: ")
result = group.show_group(ip, token,group_name)
print(result)
main()
elif choice == '4':
user_name = input("请输入用户名: ")
result = user.delete_user(ip, token, user_name)
group_name:str = input("请输入组名: ")
result = group.delete_group(ip, token, group_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)
group_name = input("请输入原组名: ")
new_group_name= input("请输入新组名: ")
new_domain_name = input("请输入新域名: ")
new_description = input("请输入新描述: ")
result = group.update_group(ip, token,group_name,new_group_name,new_domain_name, new_description)
print(result)
main()
else:
print("无效选项")
main()
@ -117,7 +117,8 @@ def main():
# elif choice == '3':
#
# elif choice == '4':
#
#2
# elif choice == '5':
else:
print("无效选项")