/ 前言 /
在ZStack的API中, 大多数的API返回的是一个任务结果查询地址, 此时我们就需要根据这个地址轮训去查询任务状态及结果
{
"location": "http://localhost:8080/v1/api-jobs/967a26b7431c49c0b1d50d709ef1aef3"
}
想要创建镜像我们先需要创建镜像服务器用来存储镜像, 镜像服务器会在管理节点内部开辟一个目录存储镜像, 而镜像是创建云主机所必需的条件
/ API /
添加镜像仓库服务器
-
API名称
添加镜像仓库服务器(AddImageStoreBackupStorage)
-
请求方式
POST
zstack/v1/backup-storage/image-store
-
curl示例
curl -H "Content-Type: application/json" \ -H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \ -X POST -d '{"params":{"hostname":"192.168.1.8","username":"admin","password":"admin% pass","sshPort":22.0,"url":"/data/imagestore","name":"ImageStore","importImages":false}}' \ http://localhost:8080/zstack/v1/backup-storage/image-store
-
返回示例
// 成功 {} // 失败 {"error": { "code": "SYS.1001", "description": "A message or a operation timeout", "details": "Create VM on KVM timeout after 300s" }}
挂载镜像服务器至区域
-
API名称
挂载镜像服务器至区域(AttachBackupStorageToZone)
-
请求方式
扫描二维码关注公众号,回复: 11218027 查看本文章POST
zstack/v1/zones/{zoneUuid}/backup-storage/{backupStorageUuid}
-
curl示例
curl -H "Content-Type: application/json" \ -H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \ -X POST http://localhost:8080/zstack/v1/zones/5776b543bc713f6d9abca9cd605c8199/backup- storage/53ef01cbf71d3bd68206908b87e51403
-
返回示例
{"inventory": { "name": "My Backup Storage", "description": "Public Backup Storage", "totalCapacity": 1.073741824E9, "availableCapacity": 9.68884224E8, "type": "Ceph", "state": "Enabled", "status": "Connected", "createDate": "Jun 7, 2017 9:20:19 PM", "lastOpDate": "Jun 7, 2017 9:20:19 PM", "attachedZoneUuids": [ "73799602dbf64c0d85694de34fc596c3"] }}
添加镜像
-
API名称
添加镜像(AddImage)
-
请求方式
POST
zstack/v1/images
-
curl示例
curl -H "Content-Type: application/json" \ -H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \ -X POST -d '{"params":{"name":"TinyLinux","url":"http://192.168.1.20/share/images/tinylinux. qcow2","mediaType":"RootVolumeTemplate","system":false,"format":"qcow2","platform":"Linux ","backupStorageUuids":["b8fc9c1c027438c28d36af24eca06595"]}}' \ http://localhost:8080/zstack/v1/images
-
返回示例
{ "inventory": { "uuid": "8c63603a810a4838a4cd62228e6e13b2", "name": "TinyLinux", "url": "http://192.168.1.20/share/images/tinylinux.qcow2", "mediaType": "RootVolumeTemplate", "platform": "Linux", "format": "qcow2", "backupStorageRefs": [ { "id": 0.0, "imageUuid": "8c63603a810a4838a4cd62228e6e13b2", "backupStorageUuid": "06ccc8e0322a4cd3ae5555d9d88451de", "installPath": "ceph://zs-images/f0b149e053b34c7eb7fe694b182ebffd", "status": "Ready" } ] }}
/ 代码 /
ZStack中, 大多数的API在调用后返回的是
user_name = 'admin'
user_password='password'
host = 'http://localhost:8080/'
# 添加镜像
def create_images(session_uuid):
# 创建镜像服务器
data = json.dumps({
"params":
{"hostname": "192.168.0.129",
"username": "root",
"password": "password",
"sshPort": 22,
"url": "/zs_images",
"name": "ImageStore",
"type": "image-store",
"description": "image_des"
}})
url = host + 'zstack/v1/backup-storage/image-store'
headers = {"Content-Type": "application/json", "Authorization": "OAuth " + session_uuid}
response = requests.post(url, data, headers=headers)
store_uuid = deal_response(response, True)
if store_uuid:
# 挂载镜像服务器到区域
url = host + 'zstack/v1/zones/' + zone_uuid + '/backup-storage/' + store_uuid
response = requests.post(url, headers=headers)
if deal_response(response, False):
# 向镜像服务器中添加镜像
data = json.dumps({
"params": {
"name": "Linux",
"url": "http://47.103.76.2/iso/centos7.iso",
"mediaType": "RootVolumeTemplate",
"system": "false",
"format": "qcow2",
"platform": "Linux",
"backupStorageUuids": [store_uuid]}})
url = host + 'zstack/v1/images'
response = requests.post(url, data, headers=headers)
image_uuid = deal_response(response, True)
if image_uuid :
return image_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:
# 创建计算规格
image_uuid = create_images(session_uuid)