-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
183 lines (172 loc) · 7.99 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from fastapi import FastAPI, Depends, params
from typing import Annotated
from dotenv import load_dotenv
from auth import get_current_username
import os
import requests
import urllib3
from ratelimit import limits
import xmltodict
import json
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
app = FastAPI()
load_dotenv()
def ovirt_get_token(cluster: str, username: Annotated[str, Depends(get_current_username)]):
urlToken = "https://" + cluster + ".turkcell.tgc/ovirt-engine/sso/oauth/token"
reqToken = requests.request("POST", urlToken, verify=False, data = os.getenv(cluster), headers={"Content-Type":"application/x-www-form-urlencoded", "Accept":"application/json", "Authorization":"Basic Og==", "Cookie":"locale=en_US"})
print("INFO: " + username + " has called " + urlToken)
return reqToken.json()["access_token"]
@limits(calls=15, period=300)
@app.get("/ovirt-engine-version")
async def ovirtApiVersion(cluster: str, username: Annotated[str, Depends(get_current_username)]):
url = "https://" + cluster + ".turkcell.tgc/ovirt-engine/api/v4"
access_token = ovirt_get_token(cluster, username)
req = requests.get(url, verify=False, headers={'Authorization': 'Bearer ' + access_token})
print("INFO: " + username + " has called " + url)
xpars = xmltodict.parse(req.text)
reqjson = json.dumps(xpars)
data = json.loads(reqjson)["api"]["product_info"]
temp = []
outjson = {'cluster' : cluster,
'name' : data['name'],
'version' : data['version'],
}
temp.append(outjson)
return temp
@limits(calls=15, period=300)
@app.get("/ovirt-engine-hosts")
async def ovirtApiHosts(cluster: str, username: Annotated[str, Depends(get_current_username)]):
url = "https://" + cluster + ".turkcell.tgc/ovirt-engine/api/hosts"
access_token = ovirt_get_token(cluster, username)
req = requests.get(url, verify=False, headers={'Authorization': 'Bearer ' + access_token})
print("INFO: " + username + " has called " + url)
xpars = xmltodict.parse(req.text)
reqjson = json.dumps(xpars)
data = json.loads(reqjson)["hosts"]["host"]
temp = []
for i in data:
outjson = {'address' : i['address'],
'status' : i['status'],
'memory' : i['memory'],
'cpu' : i['cpu'],
'hardware_information' : i['hardware_information'],
}
temp.append(outjson)
return temp
@limits(calls=15, period=300)
@app.get("/ovirt-engine-events")
async def ovirtApiEvents(cluster: str, username: Annotated[str, Depends(get_current_username)]):
url = "https://" + cluster + ".turkcell.tgc/ovirt-engine/api/events?max=250"
access_token = ovirt_get_token(cluster, username)
req = requests.get(url, verify=False, headers={'Authorization': 'Bearer ' + access_token})
print("INFO: " + username + " has called " + url)
xpars = xmltodict.parse(req.text)
reqjson = json.dumps(xpars)
data = json.loads(reqjson)["events"]["event"]
temp = []
for i in data:
outjson = {'time' : i['time'],
'description' : i['description'],
}
temp.append(outjson)
return temp
@limits(calls=15, period=300)
@app.get("/ovirt-engine-events-remove-vm")
async def ovirtApiEventsRemoveVM(cluster: str, username: Annotated[str, Depends(get_current_username)]):
url = "https://" + cluster + ".turkcell.tgc/ovirt-engine/api/events?search=code%3D10"
access_token = ovirt_get_token(cluster, username)
req = requests.get(url, verify=False, headers={'Authorization': 'Bearer ' + access_token})
print("INFO: " + username + " has called " + url)
xpars = xmltodict.parse(req.text)
reqjson = json.dumps(xpars)
data = json.loads(reqjson)["events"]["event"]
temp = []
for i in data:
outjson = {'time' : i['time'],
'description' : i['description'],
}
temp.append(outjson)
return temp
@limits(calls=15, period=300)
@app.get("/ovirt-engine")
async def ovirtApi(cluster: str, path_prefix: str, username: Annotated[str, Depends(get_current_username)]):
url = "https://" + cluster + ".turkcell.tgc/ovirt-engine/api/" + path_prefix
access_token = ovirt_get_token(cluster, username)
req = requests.get(url, verify=False, headers={'Authorization': 'Bearer ' + access_token})
print("INFO: " + username + " has called " + url)
xpars = xmltodict.parse(req.text)
reqjson = json.dumps(xpars)
return json.loads(reqjson)
@limits(calls=15, period=300)
@app.get("/ocp-engine")
async def ocpApi(cluster: str, path_prefix: str, username: Annotated[str, Depends(get_current_username)]):
url = "https://api." + cluster + ".tcs.turkcell.tgc:6443/" + path_prefix
req = requests.get(url, verify=False, headers={'Authorization': 'Bearer ' + os.getenv(cluster)})
# print(json.dumps(req.json(), indent=4, sort_keys=True))
print("INFO: " + username + " has called " + url)
return req.json()["items"]
@limits(calls=15, period=300)
@app.get("/ocp-clusterversion")
async def ocpClusterVersion(cluster: str, username: Annotated[str, Depends(get_current_username)]):
url = "https://api." + cluster + ".tcs.turkcell.tgc:6443/apis/config.openshift.io/v1/clusterversions"
req = requests.get(url, verify=False, headers={'Authorization': 'Bearer ' + os.getenv(cluster)})
data = req.json()['items']
temp = []
for i in data:
outjson = {'cluster' : cluster,
'version' : i['status']['desired']['version'],
'info' : i['status']['desired']['url'],
}
temp.append(outjson)
print("INFO: " + username + " has called " + url)
return temp
@limits(calls=15, period=300)
@app.get("/ocp-users")
async def ocpUsers(cluster: str, username: Annotated[str, Depends(get_current_username)]):
url = "https://api." + cluster + ".tcs.turkcell.tgc:6443/apis/user.openshift.io/v1/users"
req = requests.get(url, verify=False, headers={'Authorization': 'Bearer ' + os.getenv(cluster)})
data = req.json()['items']
temp = []
for i in data:
outjson = {'name' : i['metadata']['name'],
'identities' : i['identities'][0],
'creationtime' : i['metadata']['creationTimestamp']
}
temp.append(outjson)
print("INFO: " + username + " has called " + url)
return temp
@limits(calls=15, period=300)
@app.get("/ocp-user-rolebindings")
async def ocpRolebindings(cluster: str, username: Annotated[str, Depends(get_current_username)]):
temp = []
nsUrl = "https://api." + cluster + ".tcs.turkcell.tgc:6443/api/v1/namespaces"
nsReq = requests.get(nsUrl, verify=False, headers={'Authorization': 'Bearer ' + os.getenv(cluster)})
nsData = nsReq.json()['items']
for ns in nsData:
print(ns['metadata']['name'])
if "openshift" in ns['metadata']['name'] or "kube" in ns['metadata']['name'] or "default" in ns['metadata']['name'] or "istio-system" in ns['metadata']['name']:
continue
url = "https://api." + cluster + ".tcs.turkcell.tgc:6443/apis/rbac.authorization.k8s.io/v1/namespaces/" + ns['metadata']['name'] +"/rolebindings"
req = requests.get(url, verify=False, headers={'Authorization': 'Bearer ' + os.getenv(cluster)})
data = req.json()['items']
for i in data:
subjects = i['subjects']
usertemp = []
for j in subjects:
if j['kind'] != 'User':
continue
user = {'kind' : j['kind'],
'name' : j['name']
}
usertemp.append(user)
if not usertemp:
continue
outjson = {'namespace' : i['metadata']['namespace'],
'role_kind' : i['roleRef']['kind'],
'role_name' : i['roleRef']['name'],
'rolbinding_name' : i['metadata']['name'],
'user' : usertemp
}
temp.append(outjson)
print("INFO: " + username + " has called " + url)
return temp