openstack resetfulapi参考文献:https://docs.openstack.org/api-ref/
# --*--coding: utf-8 --*--
import logging
import requests, json, pprint
url = "http://controller"
headers = {"Content-type": "application/json"}
def token():
data = {
"auth": {
"identity": {
"methods": ["password"],
"password": {
"user": {
"domain": {
"name": "demo"
},
"name": "admin",
"password": "000000"
}
}
},
"scope": {
"project": {
"domain": {
"name": "demo"
},
"name": "admin"
}
}
}
}
response = requests.post(url=f'{url}:5000/v3/auth/tokens', data=json.dumps(data))
headers['X-Auth-Token'] = response.headers['X-Subject-Token']
def pprints(func):
def print(*args):
if not args:
pprint.pprint(func())
else:
pprint.pprint(func(args[0]))
return print
@pprints
def create_flavor():
data = {
"flavor": {
"name": "test",
"id": 199999,
"vcpus": 1,
"ram": 2048,
"disk": 20,
}
}
return requests.post(url=f'{url}:8774/v2.1/flavors', data=json.dumps(data), headers=headers).json()
@pprints
def list_flavor():
return requests.get(url=f'{url}:8774/v2.1/flavors', headers=headers).json()
@pprints
def show_flavor(id):
return requests.get(url=f'{url}:8774/v2.1/flavors/{id}', headers=headers).json()
def delete_flavor(id):
requests.delete(url=f'{url}:8774/v2.1/flavors/{id}', headers=headers)
@pprints
def create_image():
data = {
"container_format": "bare",
"disk_format": "qcow2",
"name": "Ubuntu",
}
return requests.post(url=f'{url}:9292/v2/images', data=json.dumps(data), headers=headers).json()
@pprints
def list_image():
return requests.get(url=f'{url}:9292/v2/images', headers=headers).json()
@pprints
def show_image(id):
return requests.get(url=f'{url}:9292/v2/images/{id}', headers=headers).json()
def upload_image(id):
headers["Content-Type"] = "application/octet-stream"
openBi = {"file": ("cirros-0.5.2-x86_64-disk.img", open(r"d:/ISO/cirros-0.5.2-x86_64-disk.img", "rb"))}
res = requests.put(url=f'{url}:9292/v2/images/{id}/file', files=openBi, headers=headers)
print(res.text)
def delete_image(id):
requests.delete(url=f'{url}:9292/v2/images/{id}', headers=headers)
@pprints
def create_network():
data = {
"network": {
"admin_state_up": True,
"name": "net1",
"provider:network_type": "vlan",
"provider:physical_network": "provider",
"provider:segmentation_id": 150,
"router:external": True,
"shared": True,
}
}
return requests.post(url=f'{url}:9696/v2.0/networks', data=json.dumps(data), headers=headers).json()
@pprints
def list_network():
return requests.get(url=f'{url}:9696/v2.0/networks', headers=headers).json()
@pprints
def show_network(id):
return requests.get(url=f'{url}:9696/v2.0/networks/{id}', headers=headers).json()
def delete_network(id):
requests.delete(url=f'{url}:9696/v2.0/networks/{id}', headers=headers)
@pprints
def create_subnet():
data = {
"subnet": {
"name": "net1-sub",
"network_id": "eb7bb77b-e34f-47da-9afd-20a7972d1539",
"ip_version": 4,
"cidr": "192.168.199.0/24",
"gateway_ip": "192.168.199.1",
"allocation_pools": [
{
"start": "192.168.199.2",
"end": "192.168.199.254"
}
],
}
}
return requests.post(url=f'{url}:9696/v2.0/subnets/', data=json.dumps(data), headers=headers).json()
@pprints
def list_subnet():
return requests.get(url=f'{url}:9696/v2.0/subnets', headers=headers).json()
@pprints
def show_subnet(id):
return requests.get(url=f'{url}:9696/v2.0/subnets/{id}', headers=headers).json()
def delete_subnet(id):
requests.delete(url=f'{url}:9696/v2.0/subnets/{id}', headers=headers)
@pprints
def create_router():
data = {
"router": {
"name": "router1",
"external_gateway_info": {
"network_id": "0f98a0d0-76d9-4926-8371-09160624b1b2",
},
"admin_state_up": True
}
}
return requests.post(url=f'{url}:9696/v2.0/routers', data=json.dumps(data), headers=headers).json()
@pprints
def list_router():
return requests.get(url=f'{url}:9696/v2.0/routers', headers=headers).json()
@pprints
def show_router(id):
return requests.get(url=f'{url}:9696/v2.0/routers/{id}', headers=headers).json()
def delete_router(id):
print(requests.delete(url=f'{url}:9696/v2.0/routers/{id}', headers=headers).text)
@pprints
def add_router_interface(id):
data = {
"subnet_id": "64e6b323-14ca-4158-911f-c237fd660cc4"
}
return requests.put(url=f'{url}:9696/v2.0/routers/{id}/add_router_interface', headers=headers,
data=json.dumps(data)).json()
@pprints
def remove_router_interface(id):
data = {
"subnet_id": "64e6b323-14ca-4158-911f-c237fd660cc4"
}
return requests.put(url=f'{url}:9696/v2.0/routers/{id}/remove_router_interface', headers=headers,
data=json.dumps(data)).json()
@pprints
def create_server():
data = {
"server": {
"name": "device-tagging-server",
"flavorRef": "dc117c15-fd9f-4763-93ab-32909d32ad41",
"imageRef": "0cec09e8-ac18-42b5-a2ce-356c49c19326",
"networks": [{
"uuid": "0f98a0d0-76d9-4926-8371-09160624b1b2",
}],
}
}
return requests.post(url=f'{url}:8774/v2.1/servers', data=json.dumps(data), headers=headers).json()
@pprints
def list_server():
return requests.get(url=f'{url}:8774/v2.1/servers', headers=headers).json()
@pprints
def show_server(id):
return requests.get(url=f'{url}:8774/v2.1/servers/{id}', headers=headers).json()
def delete_server(id):
requests.delete(url=f'{url}:8774/v2.1/servers/{id}', headers=headers)
if __name__ == "__main__":
token()
# create_flavor()
list_flavor()
# show_flavor('dc117c15-fd9f-4763-93ab-32909d32ad41')
# delete_flavor('6c7b3dfe-b1a5-4751-9623-2bf782eb5ef2')
# create_image()
# upload_image('5b458f33-892a-42c2-b546-aa2f6cd1d8a3')
# list_image()
# show_image('8050654d-14a0-46ba-8921-08d902476ac5')
# delete_image('8050654d-14a0-46ba-8921-08d902476ac5')
# create_network()
# show_network('0f98a0d0-76d9-4926-8371-09160624b1b2')
# delete_network('d07f94f4-4cbe-4b4e-9135-b48fafd540f2')
# create_subnet()
# list_subnet()
# show_subnet('4a572b3e-3a6d-4de7-b4e9-4f787e104225')
# delete_subnet('4a572b3e-3a6d-4de7-b4e9-4f787e104225')
# create_router()
# list_router()
# show_router('b5e52ee8-9850-46aa-8934-f9f1222659a6')
# delete_router('b5e52ee8-9850-46aa-8934-f9f1222659a6')
# add_router_interface('b5e52ee8-9850-46aa-8934-f9f1222659a6')
# remove_router_interface('b5e52ee8-9850-46aa-8934-f9f1222659a6')
# create_server()
# list_server()
# show_server('14d99a63-cbf1-4d4d-9a91-e356e042fbec')
# delete_server('14d99a63-cbf1-4d4d-9a91-e356e042fbec')