70 lines
2.8 KiB
Python
70 lines
2.8 KiB
Python
|
import config
|
|||
|
import requests
|
|||
|
import xml.dom.minidom
|
|||
|
|
|||
|
class cloud:
|
|||
|
def __init__(self, API_KEY: str):
|
|||
|
'''
|
|||
|
Запрос списка машин с перечнем атрибутов: Имя, ОС, IP-адрес, Состояние
|
|||
|
На входе нужен Access Token получить который можно при помощи функции getAccessToken(API Token)
|
|||
|
'''
|
|||
|
|
|||
|
url = "https://" + config.SITE + "/oauth/tenant/" + config.TENANT + "/token"
|
|||
|
headers = {
|
|||
|
"Accept": "application/json",
|
|||
|
"Content-Type": "application/x-www-form-urlencoded",
|
|||
|
"Content-Length": "71"
|
|||
|
}
|
|||
|
data = "grant_type=refresh_token&refresh_token=" + config.API_TOKEN
|
|||
|
|
|||
|
try:
|
|||
|
token = requests.post(url=url, headers=headers, data=data).json()['access_token']
|
|||
|
self.__access_token = token
|
|||
|
except:
|
|||
|
raise SystemError("Ошибка иницализации Access Token.")
|
|||
|
|
|||
|
def getVMList(self) -> list:
|
|||
|
'''
|
|||
|
Запрос списка машин с перечнем атрибутов: Имя, ОС, IP-адрес, Состояние
|
|||
|
'''
|
|||
|
url = "https://" + config.SITE + "/api/query/?type=vm&fields=name,detectedGuestOs,ipAddress,status&filter=isVAppTemplate==false"
|
|||
|
|
|||
|
headers = {
|
|||
|
"Accept": "application/*;version=" + config.API_VERSION,
|
|||
|
"Authorization": "Bearer " + self.__access_token
|
|||
|
}
|
|||
|
|
|||
|
res = requests.get(url=url, headers=headers)
|
|||
|
dom = xml.dom.minidom.parseString(res.text)
|
|||
|
|
|||
|
return [{
|
|||
|
"name": e.attributes['name'].value,
|
|||
|
"detectedGuestOs": e.attributes['detectedGuestOs'].value,
|
|||
|
"ipAddress": e.attributes['ipAddress'].value,
|
|||
|
"status": e.attributes['status'].value}
|
|||
|
for e in dom.getElementsByTagName("VMRecord")]
|
|||
|
|
|||
|
def getUserList(self) -> list:
|
|||
|
'''
|
|||
|
Запрос списка пользователей
|
|||
|
На выходе имеем список словарей:
|
|||
|
[{
|
|||
|
"name": "str('name')",
|
|||
|
"fullName": "str('fullName)",
|
|||
|
"isEnabled": bool('isEnabled')
|
|||
|
}, {
|
|||
|
.....
|
|||
|
}]
|
|||
|
'''
|
|||
|
url = "https://" + config.SITE + "/api/query?type=user&fields=name,fullName,isEnabled"
|
|||
|
|
|||
|
headers = {
|
|||
|
"Accept": "application/*;version=" + config.API_VERSION,
|
|||
|
"Authorization": "Bearer " + self.__access_token
|
|||
|
}
|
|||
|
|
|||
|
res = requests.get(url=url, headers=headers)
|
|||
|
dom = xml.dom.minidom.parseString(res.text)
|
|||
|
|
|||
|
return [{"name": e.attributes['name'].value, "fullName": e.attributes['fullName'].value, "isEnabled": bool(e.attributes['isEnabled'].value)} for e in dom.getElementsByTagName("UserRecord")]
|