OpenStack API
状态码
代码 | 原因 |
---|---|
400 - Bad Request |
请求中的某些内容无效。 |
401 - Unauthorized |
用户必须在提出请求之前进行身份验证。 |
403 - Forbidden |
策略不允许当前用户执行此操作。 |
404 - Not Found |
找不到请求的资源。 |
405 - Method Not Allowed |
方法对此端点无效。 |
413 - Request Entity Too Large |
请求大于服务器愿意或能够处理的。 |
503 - Service Unavailable |
服务不可用。这主要是由于服务配置错误导致服务无法成功启动。 |
Token
def get_token():
url = "http://192.168.200.37:35357/v3/auth/tokens"
body = {
"auth": {
"identity": {
"methods": ["password"],
"password": {
"user": {
"id": "2b70e2e3e794433a912c15edafa8c5f1",
"password": "000000"
}
}
},
"scope": {
"project": {
"id": "c3694df0e8b748baafcb68b0a92f8b0d"
}
}
}
}
result = requests.post(url, data=json.dumps(body)).headers['X-Subject-Token']
# print(result)
return result
User
import json
import requests
url = "http://192.168.100.10:5000/v3/users"
body = {
"user": {
"domain_id": "b64e5c08e1944b5fa6a8725240490aa7",
"name": "API-test-user",
"description": "API CREATER USER"
}
}
result = requests.post(url, headers={"X-Auth-Token": get_token()}, data=json.dumps(body)).json()
# print(result)
print("user")
Image
import json
import requests
from rc import RC
def jprint(data):
print(json.dumps(data, indent=2, sort_keys=True))
def get_images():
url = "http://178.120.2.100:9292/v2.1/images"
r1 = requests.get(url, headers={"X-Auth-Token": get_token()}).json()
jprint(r1)
def create_image():
url = "http://178.120.2.100:9292/v2.1/images"
body = {
"container_format": "bare",
"disk_format": "qcow2",
"name": "api-image",
}
r1 = requests.post(url, headers={"X-Auth-Token": get_token()}, data=json.dumps(body)).json()
jprint(r1)
return r1['id']
def upload_images():
id = create_image()
url = "http://178.120.2.100:9292/v2.1/images/" + id + "/file"
path_file = "./cirros-0.3.4-x86_64-disk.img"
data = open(path_file, 'rb')
headers = {"X-Auth-Token": get_token(), 'Content-Type': 'application/octet-stream'}
result = requests.put(url, headers=headers, data=data).status_code
print(result)
upload_images()
flavor
import json
import requests
def create_flavor():
url = "http://178.120.2.100:8774/v2.1/flavors"
body = {
"flavor": {
"id": "8848",
"name": "api_flavor",
"disk": "20",
"ram": "1024",
"vcpus": "1",
}
}
# r1 = requests.get(url, headers={"X-Auth-Token": get_token()}).json()
r1 = requests.post(url, headers={"X-Auth-Token": get_token()}, data=json.dumps(body)).json()
print(r1)
Network
import json
import requests
def jprint(data):
print(json.dumps(data, indent=2, sort_keys=True))
def create_net():
url = "http://192.168.200.37:9696/v2.0/networks"
# r1 = requests.get(url, headers={"X-Auth-Token": get_token()}).json()
body = {
"network": {
"name": "api-net",
}
}
r1 = requests.post(url, headers={"X-Auth-Token": get_token()}, data=json.dumps(body)).json()
# jprint(r1['network']['id'])
jprint(r1)
return r1['network']['id']
# net_id = r1['network']['id']
def create_sub():
net_id = create_net()
url = "http://192.168.200.37:9696/v2.0/subnets"
body = {
"subnet": {
"name": "api-sub2",
"cidr": "172.16.1.0/24",
"gateway_ip": "172.16.1.1",
"network_id": net_id,
"ip_version": 4,
}
}
r1 = requests.post(url, headers={"X-Auth-Token": get_token()}, data=json.dumps(body)).json()
jprint(r1)
get_token()
VM
import json
import requests
def jprint(data):
print(json.dumps(data, indent=2, sort_keys=True))
url = "http://192.168.200.37:8774/v2.1/servers"
body = {
"server": {
"imageRef": "97d6642f-4cf5-43b6-b505-645b1313fb6e",
"flavorRef": "8848",
"networks": [{"uuid": "e01e0ce3-4ef2-4b9e-b9e5-74726f351051"}],
"name": "api-vm"
}
}
r1 = requests.post(url, headers={"X-Auth-Token": get_token()}, data=json.dumps(body)).json()
jprint(r1)
评论