❝
近期有openstack和vmware相关的开发需求,N年前从事虚拟化运维的时候玩过了,所以对它俩的API使用套路早忘得一干二净。这次借这个机会把使用套路做个文档并分享出来,希望可以帮到有需要的盆友。本篇先分享openstack的API使用套路,后续再分享vmware api的使用套路。在本篇中,只给出了1个小例子,更多例子可自行去写代码,API的使用套路当然是万变不离其宗。
❞
OpenStack官方API文档:https://docs.openstack.org/zed/api/
还有最重要的一点,请确保你已经搭建好openstack环境
❝
https://docs.openstack.org/api-quick-start/api-quick-start.html
❞
❝
通过上面的API页面可得知,认证服务的API是:http://controller:5000/v3/
❞
使用python代码请求token
import requests
import json
USER = "admin"
PASSWORD = "xxxxxx"
DOMAIN = "Default"
PROJECT = "admin"
TOKEN_URL = "http://10.0.0.10:5000/v3/auth/tokens?nocatalog"
def getToken():
reqJson = {
"auth":{
"identity":{
"methods":["password"],
"password":{
"user":{
"domain":{"name":DOMAIN},
"name":USER,
"password":PASSWORD
}
}
},
"scope":{
"project":{
"domain":{
"name":DOMAIN
},
"name":PROJECT
}
}
}
}
payload = json.dumps(reqJson)
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", TOKEN_URL, headers=headers, data=payload)
return response.headers.get("X-Subject-Token")
if __name__ == "__main__":
token = getToken()
print(token)
获取到的token如下:
gAAAAABj2cwy_AOflA6gDQiR8fGK0VzLGawXPScsKB-1hIp46JdKCY68Nuz8ZQ8-USTcyY_3cCoJRpIsq8OrcZl8ztMSkUU26VF_dnkBLm4lXAyM3dZF8lHWoqmEHOevfuC0IwqQdQ66FTUF7Nl9sFElFlB39QEuSR1mZSTBrJy6bwycd8a_C5c
❝
假设有个需求,获取所有实例的详情信息。提到虚拟机实例,那肯定就是nova服务,所以要知道nova服务的api。在之前访问API的页面中,就是Compute服务。服务端点是http://controller:8774/v2.1
❞
❝
Compute API 官方文档:https://docs.openstack.org/api-ref/compute/
❞
❝
openstack的api是完全遵循REST风格的api,通过上面的api文档可得知,请求方法是GET,点开detail,可以看到更详细的API说明。
❞
下面使用python写代码
import requests
import json
USER = "admin"
PASSWORD = "xxxxxxxxx"
DOMAIN = "Default"
PROJECT = "admin"
TOKEN_URL = "http://10.0.0.10:5000/v3/auth/tokens?nocatalog"
SERVER_DETAIL_URL = "http://10.0.0.10:8774/v2.1/servers/detail"
def getToken():
reqJson = {
"auth":{
"identity":{
"methods":["password"],
"password":{
"user":{
"domain":{"name":DOMAIN},
"name":USER,
"password":PASSWORD
}
}
},
"scope":{
"project":{
"domain":{
"name":DOMAIN
},
"name":PROJECT
}
}
}
}
payload = json.dumps(reqJson)
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", TOKEN_URL, headers=headers, data=payload)
return response.headers.get("X-Subject-Token")
def serversDetail(token):
headers = {
'X-Auth-Token': token
}
response = requests.request("GET", SERVER_DETAIL_URL, headers=headers)
serversList = response.json().get("servers")
for s in serversList:
status = s.get("status")
name = s.get("name")
ip = s.get("addresses").get("my-net")[0].get("addr")
print(status, name, ip)
if __name__ == "__main__":
token = getToken()
serversDetail(token)
结果
(u'SHUTOFF', u'app-2', u'172.18.45.60')
(u'SHUTOFF', u'app-4', u'172.18.45.34')
(u'SHUTOFF', u'app-5', u'172.18.45.71')
(u'SHUTOFF', u'app-3', u'172.18.45.67')
(u'SHUTOFF', u'app-1', u'172.18.45.26')
(u'SHUTOFF', u'web', u'172.18.45.38')
本文转载于(喜欢的盆友关注我们):https://mp.weixin.qq.com/s/cGKn18y-9XLOU-W4opnwZw
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。