-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
73 lines (66 loc) · 2.38 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# importing the requests library
import requests
import re
from requests.auth import HTTPBasicAuth
from urllib3.exceptions import InsecureRequestWarning
from os import path
import yaml
from nested_lookup import nested_lookup
# doc for API url : https://developer.vmware.com/apis/1083/nsx-t
url_list=[]
url_name = []
ListStateToCheck=[]
NameToCheck=[]
WordToCheck=[]
RegexWord=[]
if path.exists('config/config.yml'):
with open('config/config.yml', 'r') as config_file:
try:
config = yaml.safe_load(config_file)
user = config['user']
password = config['password']
target = config['target']
for URL in config['url_list']:
url_list.append(target+URL['link'])
url_name.append(URL['name'])
for URL in config['check_list']:
ListStateToCheck.append(target+URL['link'])
NameToCheck.append(URL['name'])
WordToCheck.append(URL['word'])
RegexWord.append(URL['regex_word'])
except yaml.YAMLError as error:
print(error)
def request_agent(URL):
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
# sending get request and saving the response as response object HTTPBasicAuth(user, password)
r = requests.get(url=URL,auth=(user,password),verify=False )
# extracting data in json format
data = r.json()
return data
def result_count_request():
data_list = []
for URL in url_list:
data = request_agent(URL)
data_list.append(data['result_count'])
return data_list, url_list, url_name
def connectivity_state():
ValueList = []
NameList = []
render=[]
renderAggreg=[]
for URL in ListStateToCheck:
index = ListStateToCheck.index(URL)
word=WordToCheck[index]
data = request_agent(URL)
ValueList=nested_lookup(RegexWord[index], data)
NameList = nested_lookup('display_name', data)
render = match_name_state(ValueList,NameList,word,NameToCheck[index])
renderAggreg.append(render)
return renderAggreg
def match_name_state(list1,list2,word,name):
render=[]
for element in list1:
index = list1.index(element)
if element == word:
render.append(list2[index])
return render,name