2024-06-07 10:16:54 +08:00
|
|
|
import requests
|
|
|
|
import json
|
2024-06-07 15:08:22 +08:00
|
|
|
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": "域不存在"}
|
2024-06-07 10:16:54 +08:00
|
|
|
url = "http://" + ip + ":5000/v3/groups"
|
|
|
|
headers = {'X-Auth-Token': token}
|
|
|
|
data = {
|
|
|
|
"group": {
|
2024-06-07 15:08:22 +08:00
|
|
|
"name": group_name,
|
|
|
|
"domain_id": domain_id,
|
2024-06-07 10:16:54 +08:00
|
|
|
"description": description
|
|
|
|
}
|
|
|
|
}
|
|
|
|
response = requests.post(url, headers=headers, data=json.dumps(data))
|
2024-06-07 15:08:22 +08:00
|
|
|
print(domain_id)
|
2024-06-07 10:16:54 +08:00
|
|
|
return response.json()
|
2024-06-07 15:08:22 +08:00
|
|
|
|
2024-06-07 10:16:54 +08:00
|
|
|
def get_group_id(ip, token, group_name):
|
|
|
|
url = "http://" + ip + ":5000/v3/groups"
|
|
|
|
headers = {'X-Auth-Token': token}
|
2024-06-07 15:08:22 +08:00
|
|
|
response = requests.get(url, headers=headers).json()
|
|
|
|
for i in response['groups']:
|
2024-06-07 10:16:54 +08:00
|
|
|
if i['name'] == group_name:
|
|
|
|
return i['id']
|
2024-06-07 15:08:22 +08:00
|
|
|
return None
|
|
|
|
|
2024-06-07 10:16:54 +08:00
|
|
|
def show_group(ip, token, group_name):
|
|
|
|
group_id = get_group_id(ip, token, group_name)
|
2024-06-07 15:08:22 +08:00
|
|
|
if group_id is None:
|
|
|
|
return {"error": "组不存在"}
|
2024-06-07 10:16:54 +08:00
|
|
|
url = "http://" + ip + ":5000/v3/groups/" + group_id
|
|
|
|
headers = {'X-Auth-Token': token}
|
|
|
|
response = requests.get(url, headers=headers)
|
|
|
|
return response.json()
|
2024-06-07 15:08:22 +08:00
|
|
|
|
2024-06-07 10:16:54 +08:00
|
|
|
def delete_group(ip, token, group_name):
|
|
|
|
group_id = get_group_id(ip, token, group_name)
|
2024-06-07 15:08:22 +08:00
|
|
|
if group_id is None:
|
|
|
|
return {"error": "组不存在"}
|
2024-06-07 10:16:54 +08:00
|
|
|
url = "http://" + ip + ":5000/v3/groups/" + group_id
|
|
|
|
headers = {'X-Auth-Token': token}
|
|
|
|
response = requests.delete(url, headers=headers)
|
2024-06-07 15:08:22 +08:00
|
|
|
if response.status_code == 204:
|
|
|
|
return {"message": "删除成功"}
|
|
|
|
else:
|
|
|
|
return {"error": "删除失败","status_code": response.status_code}
|
2024-06-07 10:16:54 +08:00
|
|
|
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()
|
2024-06-07 15:08:22 +08:00
|
|
|
def update_group(ip, token, group_name, new_group_name,new_domain_name, new_description):
|
2024-06-07 10:16:54 +08:00
|
|
|
group_id = get_group_id(ip, token, group_name)
|
2024-06-07 15:08:22 +08:00
|
|
|
if group_id is None:
|
|
|
|
return {"error": "组不存在"}
|
|
|
|
new_domain_id = domain.get_domain_id(ip, token, new_domain_name)
|
2024-06-07 10:16:54 +08:00
|
|
|
url = "http://" + ip + ":5000/v3/groups/" + group_id
|
|
|
|
headers = {'X-Auth-Token': token}
|
|
|
|
data = {
|
|
|
|
"group": {
|
|
|
|
"name": new_group_name,
|
2024-06-07 15:08:22 +08:00
|
|
|
"domain_id": new_domain_id,
|
2024-06-07 10:16:54 +08:00
|
|
|
"description": new_description
|
|
|
|
}
|
|
|
|
}
|
|
|
|
response = requests.patch(url, headers=headers, data=json.dumps(data))
|
2024-06-07 15:08:22 +08:00
|
|
|
if response.status_code == 200:
|
|
|
|
return {"message": "更新成功"}
|
|
|
|
else:
|
|
|
|
return {"error": "更新失败","status_code": response.status_code}
|