獲得token並執行OpenStack的API

2021-12-30 10:19:50 字數 3544 閱讀 8303

搭建起一套openstack的環境之後,若要執行指定的api怎麼做?openstack各個元件/專案也都是基於web service的,因此一樣用curl發http請求即可。原理都是一樣。首先,通過提供租戶、使用者名稱和密碼,獲得認證用的token和指定api所在專案的url;然後利用此token和該url就可以執行想要執行的指定api了。下面是詳細**講解。

1. 利用get_token.sh獲得token和compute url(因為我們後面將執行乙個nova的api). get_token.sh如下:

#!/bin/sh

export os_auth_url=''

export os_tenant_name='admin'

export os_username='admin'

export os_password='admin'

# get token

curl -x post $/tokens -h "content-type: application/json" -d '"'",

"passwordcredentials": "'",

"password": "'"$"'"}}

}' | python -m json.tool > token.txt 2>&1

token=`python parse_token_file.py --token_file ./token.txt --get_token`

os_compute_url=`python parse_token_file.py --token_file ./token.txt --get_compute_url`

echo "export token=$token" > mytoken.txt

echo "export os_compute_url=$os_compute_url" >> mytoken.txt

在這其中,乙個非常重要的分析指令碼使用python寫的。它主要用來分析上述shell指令碼中的curl命令所獲取到的http response. 關於該獲取token的curl命令而該python指令碼為parse_token_file.py如下:import argparse

import ******json

import sys

# return a dict representing json

def get_auth_result(token_file):

try:

with open(token_file, "r") as token_file:

body = token_file.read()

json_body = ******json.loads(body)

except exception as ex:

print("error: %s" % ex)

json_body = none

finally:

return json_body

def get_token(json_body):

try:

token = json_body['access']['token']['id']

except exception as ex:

print("error: %s" % ex)

token = none

finally:

return token

def get_compute_url(json_body):

compute_url = none

try:

service_catalog = json_body['access']['servicecatalog']

for service in service_catalog:

if service.get('name', none) == 'nova':

compute_url = service['endpoints'][0]['publicurl']

break

except exception as ex:

print("error: %s" % ex)

finally:

return compute_url

def main():

parser = argparse.argumentparser(prog="python %s" % sys.ar**[0])

parser.add_argument("--token_file", dest='token_file', required=true,

help="specify the token file which is generated by curl command")

parser.add_argument("--get_token", dest='need_token', action='store_true', default=false,

help="parse the token file and return the token")

parser.add_argument("--get_compute_url", dest='need_compute_url', action='store_true', default=false,

help="parse the token file and return the compute url")

args = parser.parse_args()

token_file = args.token_file

need_token = args.need_token

need_compute_url = args.need_compute_url

json_body = get_auth_result(token_file)

if json_body is none:

return

token = get_token(json_body)

if token is none:

print("error: token is none")

return -1

if need_token:

print(token)

if need_compute_url:

compute_url = get_compute_url(json_body)

if compute_url is not none:

print(compute_url)

else:

print("error: computeurl is none")

if __name__ == "__main__":

main()

2. 最後,拿到token後,在其過期前,可以一直執行自己想要執行的api了,例如下面這個指令碼exec_api.sh:#!/bin/sh

source ./mytoken.txt

# try nova api

curl -x get $/servers -h "content-type: application/json" -h "x-auth-token: $" -s | python -m json.tool

(完)

使用CCheckListBox並獲得檢查狀態通知

介紹 我喜歡mfc提供 的cchecklistbox類,但是,它的使用並不明顯,嚮導的幫助僅限於clistbox類。我將描述如何輕鬆地將其插入到專案中。也許還有更簡單的方法,但我就是這麼做的,而且確實有效。我還將展示如何新增事件通知,以便在核取方塊狀態發生變化時查詢事件通知。建立cchecklist...

VB後台獲得按鍵,並執行自己的函式 非鉤子及熱鍵

option explicit 按鈕事件 private declare function getasynckeystate lib user32 byval vkey as long as integer public sub shift 你自己的事件 msgbox 你按下了shift end s...

ABP EF 執行儲存 獲得OutPut

在網上查了大多數的資料 都不全。今天實現了一下完美的實現 需要以下步驟 第一 public class docnoclass fullauditedentity 第二步 根據單據型別獲取對應的單號 單據型別 public string getdocnobydoctypes string doctyp...