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 - 创建区域、集群

扫描二维码关注公众号,回复: 11218033 查看本文章

ZStack - 创建物理机

ZStack - 创建主存储

ZStack - 创建2层3层网络

ZStack - 创建云主机计算规格

ZStack - 创建镜像

ZStack - 创建云主机计算规格

ZStack - 创建云主机

ZStack - 全流程代码

原创文章 42 获赞 51 访问量 1万+

猜你喜欢

转载自blog.csdn.net/F1004145107/article/details/106063211
今日推荐