get_iplist.py 1.68 KB
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import json
import commands
import sys,os
reload(sys)
sys.setdefaultencoding('utf8')

base_dir=os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
base_dir = base_dir + "/playbooks/module_utils"
sys.path.append(base_dir)
from qcloud_api import QcloudApi

# 获取SecretId和SecretKey值
def get_secrets():
    (status, output) = commands.getstatusoutput(
        'curl -s -H "X-Vault-Token: s.0pbNAI6Oy94m2svlEFBadl74" -X GET http://10.66.107.2:8200/v1/yoho/ops/qcloud')
    result = {}
    result['status'] = status
    if status == 0:
        output = json.loads(output)
        result['data'] = output['data']
    return result

# 调用腾讯云查询集群节点列表接口 ( DescribeClusterInstances ) 获取容器集群节点内网IP
def ip_inqure(clusterId):
    ret = {}
    URLS = 'ccs.api.qcloud.com/v2/index.php'
    try:
        datas = get_secrets()
        if datas['status'] != 0:
            ret["code"] = datas['status']
            ret["err"] = "未获取到正确的SecretId和SecretKey值"
        else:
            SecretId = datas['data']['SecretId'].encode('utf-8')
            SecretKey = datas['data']['SecretKey'].encode('utf-8')
            ips_query = QcloudApi(secretId=SecretId, secretKey=SecretKey)
            ip_list = ips_query.do_query(params={'Action': 'DescribeClusterInstances', 'clusterId': clusterId, 'Region': 'ap-beijing'}, req_url=URLS)
            if ip_list['code'] == 0:
                ret['code'] = 0
                ret["data"] = ip_list
            else:
                ret['code'] = ip_list['code']
    except Exception,e:
        ret["code"] = 500
        ret["err"] = e.message
    return ret