目錄
- 寫在前面
- 獲取token
- 小栗子:獲取所有實例詳情
寫在前面
近期有openstack和vmware相關的開發(fā)需求,N年前從事虛擬化運維的時候玩過了,所以對它倆的API使用套路早忘得一干二凈。這次借這個機會把使用套路做個文檔并分享出來,希望可以幫到有需要的盆友。本篇先分享openstack的API使用套路,后續(xù)再分享vmware api的使用套路。在本篇中,只給出了1個小例子,更多例子可自行去寫代碼,API的使用套路當然是萬變不離其宗。
OpenStack官方API文檔:https://docs.openstack.org/zed/api/
還有最重要的一點,請確保你已經(jīng)搭建好openstack環(huán)境
獲取token
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')