openstack-api-manage/keystone/group.py

73 lines
2.6 KiB
Python

import requests
import json
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": 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).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)
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_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))
if response.status_code == 200:
return {"message": "更新成功"}
else:
return {"error": "更新失败","status_code": response.status_code}