用Python调用OpenStack API上传Glance镜像

内容目录

用Python调用OpenStack API上传Glance镜像

import requests
import json

def get_auth_token(controller_ip, domain_name, username, password):
    try:
        url = f"http://{controller_ip}:5000/v3/auth/tokens"
        body = {
            "auth": {
                "identity": {
                    "methods": ['password'],
                    "password": {
                        "user": {
                            "domain": {"name": domain_name},
                            "name": username,
                            "password": password
                        }
                    }
                },
                "scope": {
                    "project": {
                        "domain": {"name": domain_name},
                        "name": username,
                        "password": password
                    }
                }
            }
        }
        headers = {
            "Content-type": "application/json"
        }
        response = requests.post(url, headers=headers, json=body)
        token = response.headers['X-Subject-Token']
        headers = {
            "X-Auth-Token": token
        }
        print(f"获取到的Token值为{token}")
        return headers
    except Exception as e:
        print(f"获取Token值失败原因是{e}")

class ImageManager:
    def __init__(self, headers, url):
        self.headers = headers
        self.url = url

    def CreateImage(self, name, disk_format, container_format):
        body = {
            "name": name,
            "disk_format": disk_format,
            "container_format": container_format,
        }
        response = requests.post(self.url, headers=self.headers, json=body)
        req = response.text
        return req

    def GetID(self, name):
        response = requests.get(self.url, headers=self.headers)
        req = json.loads(response.text)
        for image in req['images']:
            if image['name'] == name: 
                return image['id']
        return "NONE"

    def UploadImage(self, id, image_file):
        url = self.url + "/" + id + "/" + "file"
        self.headers["Content-Type"] = "application/octet-stream"
        req = requests.put(url, headers=self.headers, data=open(image_file, 'rb').read())
        return req

    def DeleteImage(self, id)
        url = self.url + "/" + id
        self.headers["Content-type"] = "application/octet-stream"
        req = requests.delete(url, headers=self.headers)
        return req

if __name__ == "__main__":
    controller_ip = "192.168.200.8"
    domain_name= "demo"
    username = "admin"
    password = "000000"
    headers = get_auth_token(controller_ip, domain_name, username, password)
    image_m = ImageManager(headers, f"http://{controller_ip}:9292/v2/images")

    create = image_m.CreateImage("cirros01", "qcow2", "bare")
    print(f"镜像创建信息为{create}")

    imageid = image_m.GetID("cirros01")
    print(f"镜像ID为{imageid}")

    imageupload = image_m.UploadImage(imageid, "/root/cirros-0.3.4-x86_64-disk.img")
    print(f"镜像上传信息为{imageupload}")
    test = input("镜像创建完成,请输入y删除此镜像,否则退出\n")
    if test == "y":
          imageDelete = image_m.DeleteImage(imageid)
          print(f"镜像已经删除。信息为:{imageDelete}")
    else:
        print("镜像删除失败")
温馨提示:

1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:[email protected],工作室将第一时间处理!

2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。

3.所有资源仅限于参考和学习,版权归原作者所有。

Kotlin技术默认

我的世界Fabric Mod开发小Tips-1

2023-10-2 17:08:53

默认

PVE 解锁vGPU

2024-2-20 12:52:07

3 条回复 A文章作者 M管理员
  1. 小满1221
    小满1221给作者打赏了¥1
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索