openstack-api-manage/keystone/project.py

70 lines
2.7 KiB
Python
Raw Normal View History

2024-06-07 17:03:47 +08:00
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}