diff --git a/.gitignore b/.gitignore index 7e58dfb..ad4cda1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ config.py +config.yaml __pycache__** res.xml .venv** \ No newline at end of file diff --git a/cloudad.py b/ad.py similarity index 100% rename from cloudad.py rename to ad.py diff --git a/app.py b/app.py index 928f5d1..f5f83b7 100644 --- a/app.py +++ b/app.py @@ -1,19 +1,16 @@ -import config -import cloud -import cloudad +import yaml +#import cloud -c = cloud.cloud(config.API_TOKEN) -a = cloudad.AD() -#r = a.getUserInfo("M-kab4", "sAMAccountName", ['sAMAccountName', "cn", "mail", "Enabled"]) -#print(a.isDisabled("Esina.Svetlana1", "sAMAccountName")) +y = yaml.safe_load(open("config.yaml", "r")) -for r in c.getUserList(): - print(r["name"], a.isDisabled(r["name"], "sAMAccountName"), a.isEnabled(r["name"], "sAMAccountName")) +print(dict(y[0]).keys()) + +for config in y: + print(config["SITE"]) + # c = cloud.cloud(config.API_TOKEN) # print(c.getVMList()) -# print(c.getUserList()) - \ No newline at end of file diff --git a/cloud.py b/cloud.py index 04a8d23..2506002 100644 --- a/cloud.py +++ b/cloud.py @@ -1,36 +1,40 @@ -import config import requests import xml.dom.minidom class cloud: - def __init__(self, API_KEY: str): + def __init__(self, API_KEY: str, API_VERSION: str, SITE: str, TENANT: str): ''' Запрос списка машин с перечнем атрибутов: Имя, ОС, IP-адрес, Состояние На входе нужен 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 = { "Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded", "Content-Length": "71" } - data = "grant_type=refresh_token&refresh_token=" + config.API_TOKEN + data = "grant_type=refresh_token&refresh_token=" + self.API_TOKEN try: token = requests.post(url=url, headers=headers, data=data).json()['access_token'] self.__access_token = token except: - raise SystemError("Ошибка иницализации Access Token.") + 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" + url = "https://" + self.SITE + "/api/query/?type=vm&fields=name,detectedGuestOs,ipAddress,status&filter=isVAppTemplate==false" headers = { - "Accept": "application/*;version=" + config.API_VERSION, + "Accept": "application/*;version=" + self.API_VERSION, "Authorization": "Bearer " + self.__access_token } @@ -38,12 +42,13 @@ class cloud: dom = xml.dom.minidom.parseString(res.text) return [{ - "name": e.attributes['name'].value, - "detectedGuestOs": e.attributes['detectedGuestOs'].value, + "name": e.attributes['name'].value, + "detectedGuestOs": e.attributes['detectedGuestOs'].value, "ipAddress": e.attributes['ipAddress'].value, - "status": e.attributes['status'].value} + "status": e.attributes['status'].value} for e in dom.getElementsByTagName("VMRecord")] + 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 = { - "Accept": "application/*;version=" + config.API_VERSION, + "Accept": "application/*;version=" + self.API_VERSION, "Authorization": "Bearer " + self.__access_token } diff --git a/test.py b/test.py new file mode 100644 index 0000000..abb675e --- /dev/null +++ b/test.py @@ -0,0 +1,5 @@ +import yaml + +y = yaml.safe_load(open("config.yaml", 'r')) + +print(y) \ No newline at end of file