/ 前言 /
在ZStack的API中, 大多數的API返回的是一個任務結果查詢地址, 此時我們就需要根據這個地址輪訓去查詢任務狀態及結果
{
"location": "http://localhost:8080/v1/api-jobs/967a26b7431c49c0b1d50d709ef1aef3"
}
/ API /
創建區域
-
API名稱
創建區域(CreateZone)
-
請求方式
POST
zstack/v1/zones
-
curl示例
curl -H "Content-Type: application/json" \ -H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \ -X POST -d '{"params":{"name":"TestZone","description":"test zone"}}' \ http://localhost:8080/zstack/v1/zones
-
返回示例
{"inventory": { "uuid": "0d26522305c34d07b5661ccda62053c0", "name": "TestZone", "description": "Test", "state": "Enabled", "type": "zstack", "createDate": "Jun 7, 2017 9:20:28 PM", "lastOpDate": "Jun 7, 2017 9:20:28 PM" }}
創建集羣
-
API名稱
創建一個集羣(CreateCluster)
-
請求方式
POST
zstack/v1/clusters
-
curl示例
curl -H "Content-Type: application/json" \ -H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \ -X POST -d '{"params":{"zoneUuid":"e1fb7d87073b397c857eaeeb16243103","name":"cluster1 ","description":"test","hypervisorType":"KVM"}}' \ http://localhost:8080/zstack/v1/clusters
-
返回示例
{"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 login():
# 登錄API需要對密碼進行SHA-512算法加密
sha512 = hashlib.sha512()
sha512.update(user_password)
password = sha512.hexdigest()
content = {
"logInByAccount": {
"password": password,
"accountName": user_name}
}
data = json.dumps(content)
url = host + 'zstack/v1/accounts/login'
headers = {"Content-Type": "application/json"}
response = requests.put(url, data, headers=headers)
# 這裏只是簡單的返回處理
if response:
rsp = json.loads(response.text)
if rsp:
return rsp['inventory']['uuid']
# 創建區域
def create_zone(session_uuid):
content = {
"params":
{"name": "Zone",
"description": "zone"}
}
data = json.dumps(content)
url = host + 'zstack/v1/zones'
headers = {"Content-Type": "application/json", "Authorization": "OAuth " + session_uuid}
response = requests.post(url, data, headers=headers)
zone_uuid = deal_response(response, True)
if zone_uuid:
return zone_uuid
# 創建集羣
def create_clusters(session_uuid, zone_uuid):
content = {
"params":
{"zoneUuid": zone_uuid,
"name": "cluster",
"description": "cluster",
"hypervisorType": "KVM"}
}
data = json.dumps(content)
url = host + 'zstack/v1/clusters'
headers = {"Content-Type": "application/json", "Authorization": "OAuth " + session_uuid}
response = requests.post(url, data, headers=headers)
cluster_uuid = deal_response(response, True)
if cluster_uuid:
return cluster_uuid
# 處理返回數據
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)