python-api使用,注释
import json,requests
# 导入依赖包
auth_token_url="http://controller:5000/v3/auth/tokens"
headers={}
#创建了一个空的字典 headers
headers["Content-Type"]="application/json"
#将键名为 "Content-Type" 的值设置为 "application/json",表示请求的内容类型是 JSON 格式。
body={
"auth": {
"identity": {
"methods": ["password"],
"password": {
"user": {
"name": "admin",
"password": "000000",
"domain": {"name": "demo"}
}
}
},
"scope": {
"project": {
"name": "admin",
"domain": {"name": "demo"}
}
}
}
}
# 根据API规范,构造请求体
def get_token():
## 构造头部信息代码中的 requests.post() 方法发送了一个 POST 请求给指定的 auth_token_url 地址,并传递了请求体 (data=json.dumps(body))、请求头部信息 (headers=headers) 进行认证。一旦服务器返回响应,我们使用 .headers["X-Subject-Token"] 从响应的头部信息中获取了认证令牌,并将其保存到变量 results 中。json.dumps() 函数用于将 Python 对象转换为 JSON 字符串。body 是一个 Python 对象,可能是一个字典或其他可序列化的数据类型。通过调用 json.dumps(body),将 body 对象转换为对应的 JSON 字符串。
results=requests.post(url=auth_token_url,data=json.dumps(body),headers=headers).headers["X-Subject-Token"]
return results
print(get_token())
#create_network
#将键名为 "X-Auth-Token" 的值设置为 "get_token的返回"
headers["X-Auth-Token"]=get_token()
create_image_url="http://controller:9292/v2.0/images"
#
#海峰师兄的写法带f的是
#ip = "10.0.0.10"
#imageUrl = f"http://{ip}:9292/v2/images"
#f是字符串插值
#最终生成的 URL 地址是根据具体的 ip 值动态生成的
#前面会有变量定义ip,f使用ip值生成最终值
#
def create_image():
## 构造头部信息,通过使用 requests.get() 函数发送一个 GET 请求,访问 URL "http://controller:9292/v2.0/images",并在请求头部中携带上述定义的 headers 参数。然后解析返回的 JSON 数据,提取其中的 "images" 字段,将结果存储在变量 get_image 中。
get_image=requests.get(url="http://controller:9292/v2.0/images",headers=headers).json()["images"]
#遍历 get_image 列表中的每个镜像对象,如果镜像的名称为 "cqbvc_image"
for i in get_image:
if i["name"]=="cqbvc_image":
#则使用 requests.delete() 函数发送 DELETE 请求到特定的镜像地址,并在请求头部中携带 headers 参数。打印删除操作的结果。i被循环赋值,使用i来访问["name"]并赋值
print(requests.delete(url="http://controller:9292/v2.0/images/"+i["id"],headers=headers))
#定义一个字典类型的变量 images,其中包含了创建镜像所需的相关参数,如镜像名称 "pvm_image",磁盘格式 "qcow2",容器格式 "bare"。
images={
"name":"pvm_image",
"disk_format":"qcow2",
"container_format":"bare"
}
#使用 requests.post() 函数发送一个 POST 请求到之前定义的 create_image_url 地址,请求数据为经过 JSON 序列化的 images 参数,并在请求头部中携带 headers 参数。将返回的 JSON 数据解析为字典对象,存储在变量 create_image 中。
create_image=requests.post(url=create_image_url,data=json.dumps(images),headers=headers).json()
print(create_image)
#从 create_image 字典中提取镜像的 ID 和文件 URL,并分别存储在变量 image_id 和 upload_file 中。create_image 字典代表了在 OpenStack 中创建镜像时返回的响应数据。
image_id=create_image["id"]
upload_file=create_image["file"]
#根据之前获取的文件 URL,构造镜像上传的完整 URL 地址,并将其中的 "controller:9292" 替换为 "http://controller:9292"。然后将请求头部信息中的 "Content-Type" 字段设置为 "application/octet-stream",表示上传的数据为二进制流。
uploud_image="http://controller:9292"+upload_file
headers["Content-Type"]="application/octet-stream"
#使用 requests.put() 函数发送一个 PUT 请求,访问镜像上传的 URL 地址,携带之前构造的请求头部信息和打开指定文件 "/root/openstack_api/cirros-0.3.3-x86_64-disk.img" 的二进制数据进行上传。
upload_image=requests.put(url=uploud_image,headers=headers,data=open("/root/openstack_api/cirros-0.3.3-x86_64-disk.img","rb"))
print(upload_image)
print("创建镜像成功!id:",image_id)
create_image()