import requests import json def create_image(ip, token, image_name, image_description,disk_format, container_format): url = "http://" + ip + ":9292/v2/images" headers = { "X-Auth-Token": token, "Content-Type": "application/json" } data = { "name": image_name, "description": image_description, "disk_format": disk_format, "container_format": container_format, "visibility": "public" } response = requests.post(url, headers=headers, data=json.dumps(data)) return response.json() def get_image(ip, token): url = "http://" + ip + ":9292/v2/images" headers = { "X-Auth-Token": token, "Content-Type": "application/json" } response = requests.get(url, headers=headers) return response.json() def get_image_id(ip, token, image_name): url = "http://" + ip + ":9292/v2/images" headers = { "X-Auth-Token": token, "Content-Type": "application/json" } response = requests.get(url, headers=headers) image_id = "" for image in response.json()["images"]: if image["name"] == image_name: image_id = image["id"] return image_id def show_image(ip, token, image_name): image_id=get_image_id(ip, token, image_name) if image_id is None: return {"error":"镜像不存在"} url = "http://" + ip + ":9292/v2/images/" + image_id headers = { "X-Auth-Token": token, "Content-Type": "application/json" } response = requests.get(url, headers=headers) return response.json() def delete_image(ip, token, image_name): image_id=get_image_id(ip, token, image_name) if image_id is None: return {"error":"镜像不存在"} url = "http://" + ip + ":9292/v2/images/" + image_id headers = { "X-Auth-Token": token, "Content-Type": "application/json" } response = requests.delete(url, headers=headers) if response.status_code == 204: return {"message":"删除成功"} else: return {"error":"删除失败"+str(response.status_code)} def upload_iamge_file(ip,token,image_name,image_path): image_id=get_image_id(ip, token, image_name) if image_id is None: return {"error":"镜像不存在"} url = "http://" + ip + ":9292/v2/images/" + image_id + "/file" with open(image_path, "rb") as e: image_data = e.read() headers = { "X-Auth-Token": token, "Content-Type": "application/octet-stream" } response = requests.patch(url, headers=headers, data=image_data) if response.status_code == 200: return {"message":"上传成功"} else: return {"error":"上传失败"+str(response.status_code)} def update_image(ip, token, image_name, new_image_name, new_image_description): image_id=get_image_id(ip, token, image_name,) if image_id is None: return {"error":"镜像不存在"} url = "http://" + ip + ":9292/v2/images/" + image_id headers = { "X-Auth-Token": token, "Content-Type": "application/octet-stream" } data = { "name": new_image_name, "description": new_image_description, } response = requests.patch(url, headers=headers, data=json.dumps(data)) if response.status_code == 200: return {"message":"更新成功"} else: return {"error":"更新失败"+str(response.status_code)}