Python调用OpenStack API
2021/10/6 9:11:16
本文主要是介绍Python调用OpenStack API,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
返回目录
Python调用OpenStack API
获取token值(除了身份认证,还可以用
openstack token issue
+ awk 命令得到,或直接写死【1小时生效】)
import json import requests headers = {"Content-Type": "application/json"} body = { "auth": { "identity": { "methods": ["password"], "password": { "user": { "domain": { "name": "demo" }, "name": "admin", "password": "000000" } } }, "scope": { "project": { "domain": { "name": "demo" }, "name": "admin" } } } } headers["X-Auth-Token"] = requests.post('http://192.168.100.10:5000/v3/auth/tokens', headers=headers, json=body).headers["X-Subject-Token"]
import os headers["X-Auth-Token"] = os.popen("source /etc/keystone/admin-openrc.sh && openstack token issue | awk '/ id/{print $4}'").read().strip('\n') headers["X-Auth-Token"] = os.popen("source /etc/keystone/admin-openrc.sh && openstack token issue | sed -n '/ id/s/|.*| //p'").read().strip('\n')
调用PythonAPI创建flavor
data = { 'flavor': { 'name': 'test_flavor', 'id': '666', 'ram': 2048, 'disk': 20, 'vcpus': 2 } } print(json.dumps(requests.post('http://192.168.100.10:8774/v2.1/flavors', headers=headers, json=data).json(), indent=4)) print('云主机类型创建成功')
调用PythonAPI创建镜像
data = { "container_format": "bare", "disk_format": "qcow2", "name": "cirros", "visibility": "public" } request = requests.post('http://192.168.100.10:9292/v2/images', headers=headers, json=data).json() headers["Content-Type"] = "application/octet-stream" print(requests.put(f"http://192.168.100.10:9292{request['file']}", headers=headers, data=open("cirros-0.3.4-x86_64-disk.img", 'rb')).status_code) print('云主机镜像上传成功')
这篇关于Python调用OpenStack API的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-27Python编程基础教程
- 2024-12-27Python编程基础指南
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型
- 2024-12-23使用python部署一个usdt合约,部署自己的usdt稳定币
- 2024-12-20Python编程入门指南
- 2024-12-20Python编程基础与进阶
- 2024-12-19Python基础编程教程
- 2024-12-19python 文件的后缀名是什么 怎么运行一个python文件?-icode9专业技术文章分享