/ 前言 /
在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)