使用Python調用ManageOne運營API
使用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小時內刪除侵權內容。