82 lines
2.9 KiB
Plaintext
82 lines
2.9 KiB
Plaintext
async def baidu_resource_query(ns={}):
|
|
"""
|
|
用户资源查询
|
|
:param ns:
|
|
:return:
|
|
"""
|
|
if ns.get('userid'):
|
|
userid = ns.get('userid')
|
|
else:
|
|
userid = await get_user()
|
|
db = DBPools()
|
|
async with db.sqlorContext('kboss') as sor:
|
|
baiduids = await sor.R('baidu_users', {'user_id': userid})
|
|
if baiduids:
|
|
baiduid = baiduids[0]['baidu_id']
|
|
else:
|
|
return {
|
|
'status': False,
|
|
'msg': 'User not synchronized'
|
|
}
|
|
ns['queryAccountId'] = baiduid
|
|
ns['pageSize'] = 200
|
|
method = 'POST'
|
|
ns_format = '&'.join(['%s=%s' % (k, v) for k, v in ns.items()])
|
|
url = 'https://billing.baidubce.com/v1/resource/query?%s' % ns_format
|
|
header = {
|
|
"Host": "billing.baidubce.com"
|
|
}
|
|
header = await get_auth_header(method=method, url=url, header=header)
|
|
async with aiohttp_client.request(
|
|
method=method,
|
|
url=url,
|
|
headers=header,
|
|
json=ns) as res:
|
|
result = await res.json()
|
|
return {'1': result}
|
|
|
|
result_new = []
|
|
for i in result['result']:
|
|
if i.get('status') != 'STOPPED':
|
|
result_new.append(i)
|
|
|
|
result['result'] = result_new
|
|
|
|
return {
|
|
'status': True,
|
|
'msg': 'get resource success',
|
|
'data': result
|
|
}
|
|
|
|
async def resource_overview(ns={}):
|
|
if ns.get('userid'):
|
|
userid = ns.get('userid')
|
|
else:
|
|
userid = await get_user()
|
|
init_data = [
|
|
{'id': 1, 'name': '云服务器', 'count': 0, 'unit': '实例'},
|
|
{'id': 2, 'name': '私有网络', 'count': 0, 'unit': 'VPC'},
|
|
{'id': 3, 'name': '负载均衡', 'count': 0, 'unit': '负载均衡'},
|
|
{'id': 4, 'name': '弹性块存储', 'count': 0, 'unit': '云盘'},
|
|
{'id': 5, 'name': 'NAT网关', 'count': 0, 'unit': '实例'},
|
|
{'id': 6, 'name': 'VPN连接', 'count': 0, 'unit': 'VPN网关'},
|
|
]
|
|
baidu_resource_data = await baidu_resource_query({'userid': userid})
|
|
print(baidu_resource_data)
|
|
if baidu_resource_data and baidu_resource_data.get('data') and baidu_resource_data['data'].get('result'):
|
|
for baidu_resource in baidu_resource_data['data']['result']:
|
|
if baidu_resource.get('serviceType') == 'CDS' and baidu_resource['status'] == 'RUNNING':
|
|
init_data[3]['count'] += 1
|
|
elif baidu_resource.get('serviceType') == 'EIP' and baidu_resource['status'] == 'RUNNING':
|
|
init_data[1]['count'] += 1
|
|
elif (baidu_resource.get('serviceType') == 'BCC' or baidu_resource.get('serviceType') == 'LS') and baidu_resource['status'] == 'RUNNING':
|
|
init_data[0]['count'] += 1
|
|
|
|
return {
|
|
'status': True,
|
|
'msg': 'get resource success',
|
|
'data': init_data
|
|
}
|
|
|
|
ret = await baidu_resource_query(params_kw)
|
|
return ret |