如何在Excel中有效锁定单元格保护数据完整性
976
2022-05-29
使用Python调用ManageOne运营API
在运营环境中遇到一个需求,就是如何很方便的获取环境中各租户的资源使用情况,这里可以使用python requests 的方式来操作。
首先要通过API获取数据
import requests
import json
res_body = {"auth": {
"identity": {
"methods": ["password"],
"password": {
"user": {
"domain": {
"name": "mo_bss_admin"
},
"name": "xxx",
"password": "xxx",
}
}
},
"scope":
{
"domain":
{
"name": "mo_bss_admin"
},
},
},
}
res_headers = {
'Content-Type': 'application/json',
'Accept': 'application/json; charset=UTF-8'
}
res_url = "https://iam-apigateway-proxy.demo.com/v3/auth/tokens"
res = requests.post(url = res_url, headers = res_headers, json = res_body, verify = False)
# 将token从headers中提取出来
token = res.headers['X-Subject-Token']
MIIEEgYJKoZIhvcNAQcCoIIEAzCCxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxmgOHw=
import copy
# 使用token获取vdc的列表
url1 = "https://sc.demo.com/rest/vdc/v3.0/vdcs?start=0&limit=1000"
new_headers = copy.deepcopy(res_headers)
new_headers['X-Auth-Token'] = token
res1 = requests.get(url=url1, headers = new_headers, verify=False)
parsed = json.loads(res1.text)
print(res1.status_code, "\n\r", json.dumps(parsed, indent=4, sort_keys=True))
200
# 这里只展示其中一个vdc
{
"total": 1,
"vdcs": [
{
"az_id": null,
"create_at": 1589418019000,
"create_user_id": "385xxxxxxxxxd",
"create_user_name": "bss_admin",
"description": "",
"domain_id": "ea3xxxxxxxxx60",
"domain_name": "demo",
"ecs_used": 0.0,
"enabled": true,
"enterprise_id": null,
"enterprise_project_id": null,
"evs_used": 0.0,
"extra": "{\"manager\":\"\",\"phone\":\"\",\"email\":\"\"}",
"id": "bdc8cxxxxxxxxxxxxxxxxxx32a6a", # vdc的id
"idp_name": null,
"ldap_id": null,
"level": 2, # 2级vdc
"name": "demo",
"project_count": 1,
"region_id": null,
"tag": "vdc",
"third_id": null,
"third_type": "0",
"top_vdc_id": "d9xxxxxxxxxxxxxxxxxxxxxxxxxxxx72",
"upper_vdc_id": "d9xxxxxxxxxxxxxxxxxxxxxxxxxxxx72",
"upper_vdc_name": "demo",
"utc_create_at": "2020-05-14 01:00:19.0"
},
]
}
这里会将所有vdc的基本信息获取到,我们需要将vdc id记录下来,后面会用到。
import re
c = []
count = 0
id_dict = {}
for i in parsed['vdcs']:
id_dict[i['name']] = i['id']
# 将vdc的id打印出来
print("{0:<25}".format(i['name']),":",i['id'])
demo : d9xxxxxxxxxxxxxxxxxxxxxxxxxxxx72
利用vdc id去获取vdc的全部细节。这里可以使用一个for循环对所有的vdc进行标准化处理。
url2 = "https://sc.demo.com/rest/vdc/v3.0/vdcs/{}/details"
vid = "d9xxxxxxxxxxxxxxxxxxxxxxxxxxxx72"
res2 = requests.get(url=url2.format(vid),headers = new_headers,verify=False)
parsed1 = json.loads(res2.text)
print(res2.status_code,json.dumps(parsed1, indent=4, sort_keys=True))
返回的json示例如下(示例内容选自《ManageOne 8.0.0 运营面API参考》)
200 {
"vdc": {
"domain_id": "f8310f0f46f04656b10475d260b3026f",
"domain_name": "baiqiang1",
"upper_vdc_id": "0",
"regions": [{
"cloud_infras": [{
"quotas": [{
"service_id": "nfvivpc",
"resources": [{
"local_used": 45001,
"resource": "vpc",
"other_limit": 50000,
"other_used": 50001,
"local_limit": 50000
}]
}],
"name": "xian-alpha-2",
"cloud_infra_id": "NFVI_xian-alpha-2",
"status": "normal",
"available_zones": [{
"quotas": [{
"service_id": "nfviecs",
"resources": [{
"local_used": 0,
"resource": "cores",
"other_limit": 100000,
"other_used": 110002,
"local_limit": 100000
}]
}],
"az_id": "fsp.type2"
}]
}],
"name": "{\"zh_cn\":\"alpha-2\",\"en_us\":\"xian-alpha-2\"}",
"region_id": "xian-alpha-2",
"type": "private_cloud"
}],
"regionId": "xian-beta2-1",
"status": "normal"
}],
"level": 1,
"name": "baiqiang1",
"description": "",
"id": "a24b93f0-2c17-4c2f-b14c-0fb8128a1a5a",
"create_at": 1512169700000,
"enabled": true
}
}
VDCDetailsResponses对象包含如下属性:
VDCDetailBean对象包含如下属性:
Region对象包含如下属性:
Cloud_infra对象包含如下属性:
Available_zone对象包含如下属性:
Quota对象包含如下属性:
Dict对象包含如下属性:
Quota对象包含如下属性:
Dict对象包含如下属性:
接下来对我们需要的数据进行提取,由于我只关注有关ECS的服务quota,所以可以使用下面的方法进行提取。
regions = parsed1['vdc']['regions']
def print_vdc_detail(regions):
azs = []
for region in regions:
for ci in region['cloud_infras']:
for az in ci['available_zones']:
azs.append(az)
for az in azs:
print("\n\r",az['az_id'],':\n')
for src in az['quotas']: # az['quotas'] -> list
print('资源类型:{}'.format(src['service_id']))
for det in src['resources']:
print(" # {0:<25}: {1}".format(det['resource'],det['local_used']))
print_vdc_detail(regions)
#########结果如下################
az0.dc0 :
资源类型:ecs
# cores : 17.0 #使用的CPU核数
# ram : 40.0 #已经使用的内存数
# gpu_instances : 0.0
# npu_instances : 0.0
# instance_snapshot : 0.0
# instances : 44.0 #创建的ECS数
资源类型:evs
# gigabytes : 448.0 #使用的卷容量
# volumes : 25.0 #创建的云硬盘数量
az1.dc1 :
资源类型:bms
# cores : 192.0
# ram : 512.0
# instances : 2.0
资源类型:evs
# gigabytes : 0.0
# volumes : 0.0
az2.dc2 :
资源类型:ecs
# cores : 8.0
# ram : 16.0
# gpu_instances : 0.0
# npu_instances : 0.0
# instance_snapshot : 0.0
# instances : 1.0
资源类型:evs
# gigabytes : 400.0
# volumes : 2.0
可以循环调用上面的步骤,获取所有vdc的资源数据
def get_resource(name,vid):
url2 = "https://sc.armcloud.hcs.huawei.com/rest/vdc/v3.0/vdcs/{}/details"
res2 = requests.get(url=url2.format(vid),headers = new_headers,verify=False)
parsed1 = json.loads(res2.text)
regions = parsed1['vdc']['regions']
print("VDC: {}".format(name))
print_vdc_detail(regions)
print('\n')
for name, vid in id_dict.items():
get_resource(name, vid)
###########结果如下#################
VDC: demo
az0.dc0 :
资源类型:ecs
# cores : 16.0
# ram : 33.0
# gpu_instances : 0.0
# npu_instances : 0.0
# instance_snapshot : 0.0
# instances : 21.0
资源类型:evs
# gigabytes : 555.0
# volumes : 23.0
VDC: demo1
az0.dc0 :
资源类型:ecs
# cores : 0.0
# ram : 0.0
# gpu_instances : 0.0
# npu_instances : 0.0
# instance_snapshot : 0.0
# instances : 0.0
资源类型:evs
# gigabytes : 0.0
# volumes : 0.0
VDC: demo2
az0.dc0 :
资源类型:ecs
# cores : 24.0
# ram : 86.0
# gpu_instances : 0.0
# npu_instances : 0.0
# instance_snapshot : 0.0
# instances : 26.0
资源类型:evs
# gigabytes : 642.0
# volumes : 36.0
接下来可以基于这个调用链,将代码改成可复用的SDK的形式,前端展示可以通过Django、Flask或者GUI等,也可以输出成excel表格并自动发送邮件给干系人。
Python API
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。