ZStack - 創建主存儲

/ 前言 /

       在ZStack的API中, 大多數的API返回的是一個任務結果查詢地址, 此時我們就需要根據這個地址輪訓去查詢任務狀態及結果

{ 
	"location": "http://localhost:8080/v1/api-jobs/967a26b7431c49c0b1d50d709ef1aef3" 
}

/ API /

  • API名稱

    添加本地存儲爲主存儲(AddLocalPrimaryStorage)

  • 請求方式

    POST zstack/v1/primary-storage/local-storage

  • curl示例

    curl -H "Content-Type: application/json" \
    -H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
    -X POST -d '{"params":{"url":"/zstack_ps","name":"PS1","zoneUuid":"3b9a66322185343e9068 b2bfd185b690"}}' \
    http://localhost:8080/zstack/v1/primary-storage/local-storage
    
  • 返回示例

    {"inventory": {
    	"name": "PS1",
    	"url": "/zstack_ps",
    	"type": "LocalStorage",
    	"state": "Enabled",
    	"status": "Connected", 
    	"attachedClusterUuids": 
    	[ 
    		"562116d368c8467c95c53321757538dc"
    	]
    }}
    
  • 返回示例

    {"inventory": {
    	"name": "cluster1",
    	"uuid": "dc7442bb39674d779d688369329ba845", 
    	"description": "test",
    	"state": "Enabled",
    	"hypervisorType": "KVM",
    	"createDate": "Jun 7, 2017 9:20:31 PM",
    	"lastOpDate": "Jun 7, 2017 9:20:31 PM",
    	"zoneUuid": "342b68c14869412984d6327a58b18f9b", 
    	"type": "zstack"
    }}
    

/ 代碼 /

ZStack中, 大多數的API在調用後返回的是

user_name = 'admin'
user_password='password'
host = 'http://localhost:8080/'


# 添加主存儲
def create_local_storage(zone_uuid, session_uuid):
    data = json.dumps({
        "params":
        	# url爲管理節點的本地目錄
            {"url": "/zstack_ps",
             "name": "PS1",
             "zoneUuid": zone_uuid}})
    url = host + 'zstack/v1/primary-storage/local-storage'
    headers = {"Content-Type": "application/json", "Authorization": "OAuth " + session_uuid}
    response = requests.post(url, data, headers=headers)
    deal_response(response, False)


# 處理返回數據
def deal_response(response, is_return):
    if response:
        rsp = json.loads(response.text)
        if rsp:
            print('rsp : {%s}' % rsp)
            json_str = query_until_done(rsp)
            if json_str:
                if not json_str.has_key('error'):
                    if is_return:
                        return json_str['inventory']['uuid']
                    else:
                        return True
    return False


# 輪詢查詢API結果
def query_until_done(rsp):
	# 截取任務id, 替換請求地址
        if rsp.has_key('location'):
        location = rsp['location']
        job_uuid = location.split('/')[-1]
        if job_uuid:
            while True:
                url = host + "zstack/v1/api-jobs/" + location.split('/')[-1]
                response = requests.get(url)
                text = response.text
                print(text)
                if text != '{}':
                    print('url : {%s}' % url)
                    return json.loads(text)


if __name__ == '__main__':
	session_uuid = login()
	if session_uuid:
		 # 創建區域
        zone_uuid = create_zone(session_uuid)
        if zone_uuid:
            # 創建集羣
            cluster_uuid = create_clusters(session_uuid, zone_uuid)
            # 創建本地存儲
            storage_uuid = create_local_storage(zone_uuid, session_uuid)

/ ZStack全流程相關博文鏈接 /

ZStack - 登錄

ZStack - 創建區域、集羣

ZStack - 創建物理機

ZStack - 創建主存儲

ZStack - 創建2層3層網絡

ZStack - 創建雲主機計算規格

ZStack - 創建鏡像

ZStack - 創建雲主機計算規格

ZStack - 創建雲主機

ZStack - 全流程代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章