ADD: Some change for multiple tenant

This commit is contained in:
Сергей Филимонов 2025-02-05 22:55:03 +07:00
parent 426059daa4
commit 3f8b1fadd3
5 changed files with 31 additions and 23 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
config.py config.py
config.yaml
__pycache__** __pycache__**
res.xml res.xml
.venv** .venv**

View File

19
app.py
View File

@ -1,19 +1,16 @@
import config import yaml
import cloud #import cloud
import cloudad
c = cloud.cloud(config.API_TOKEN) y = yaml.safe_load(open("config.yaml", "r"))
a = cloudad.AD()
#r = a.getUserInfo("M-kab4", "sAMAccountName", ['sAMAccountName', "cn", "mail", "Enabled"])
#print(a.isDisabled("Esina.Svetlana1", "sAMAccountName"))
for r in c.getUserList(): print(dict(y[0]).keys())
print(r["name"], a.isDisabled(r["name"], "sAMAccountName"), a.isEnabled(r["name"], "sAMAccountName"))
for config in y:
print(config["SITE"])
# c = cloud.cloud(config.API_TOKEN)
# print(c.getVMList()) # print(c.getVMList())
# print(c.getUserList())

View File

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

5
test.py Normal file
View File

@ -0,0 +1,5 @@
import yaml
y = yaml.safe_load(open("config.yaml", 'r'))
print(y)