forked from ortelius/ms-compitem-crud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
467 lines (418 loc) · 18.9 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import os
from collections import OrderedDict
import uvicorn
import psycopg2
import psycopg2.extras
import requests
from sqlalchemy import create_engine
from fastapi import FastAPI, Request, Response, HTTPException, status
from pydantic import BaseModel
from typing import List, Optional
from sqlalchemy.exc import OperationalError, StatementError
from time import sleep
import logging
# Init Globals
service_name = 'ortelius-ms-compitem-crud'
db_conn_retry = 3
app = FastAPI(
title=service_name,
description=service_name
)
# Init db connection
db_host = os.getenv("DB_HOST", "localhost")
db_name = os.getenv("DB_NAME", "postgres")
db_user = os.getenv("DB_USER", "postgres")
db_pass = os.getenv("DB_PASS", "postgres")
db_port = os.getenv("DB_PORT", "5432")
validateuser_url = os.getenv("VALIDATEUSER_URL", "http://localhost:5000")
engine = create_engine("postgresql+psycopg2://" + db_user + ":" + db_pass + "@" + db_host +":"+ db_port + "/" + db_name, pool_pre_ping=True)
# health check endpoint
class StatusMsg(BaseModel):
status: str
service_name: Optional[str] = None
@app.get("/health",
responses={
503: {"model": StatusMsg,
"description": "DOWN Status for the Service",
"content": {
"application/json": {
"example": {"status": 'DOWN'}
},
},
},
200: {"model": StatusMsg,
"description": "UP Status for the Service",
"content": {
"application/json": {
"example": {"status": 'UP', "service_name": service_name}
}
},
},
}
)
async def health(response: Response):
try:
with engine.connect() as connection:
conn = connection.connection
cursor = conn.cursor()
cursor.execute('SELECT 1')
if cursor.rowcount > 0:
return {"status": 'UP', "service_name": service_name}
response.status_code = status.HTTP_503_SERVICE_UNAVAILABLE
return {"status": 'DOWN'}
except Exception as err:
print(str(err))
response.status_code = status.HTTP_503_SERVICE_UNAVAILABLE
return {"status": 'DOWN'}
# end health check
class CompItemModel(BaseModel):
compid: int
id: int
repositoryid: Optional[int] = None
target: Optional[str] = None
name: Optional[str] = None
summary: Optional[str] = None
predecessorid: Optional[int] = None
xpos: Optional[int] = None
ypos: Optional[int] = None
creatorid: Optional[int] = None
created: Optional[int] = None
modifierid: Optional[int] = None
modified: Optional[int] = None
status: Optional[str] = None
rollup: Optional[int] = None
rollback: Optional[int] = None
kind: Optional[str] = None
buildid: Optional[str] = None
buildurl: Optional[str] = None
chart: Optional[str] = None
operator: Optional[str] = None
builddate: Optional[str] = None
dockersha: Optional[str] = None
gitcommit: Optional[str] = None
gitrepo: Optional[str] = None
gittag: Optional[str] = None
giturl: Optional[str] = None
dockerrepo: Optional[str] = None
chartversion: Optional[str] = None
chartnamespace: Optional[str] = None
dockertag: Optional[str] = None
chartrepo: Optional[str] = None
chartrepourl: Optional[str] = None
serviceowner: Optional[str] = None
serviceowneremail: Optional[str] = None
serviceownerphone: Optional[str] = None
slackchannel: Optional[str] = None
discordchannel: Optional[str] = None
hipchatchannel: Optional[str] = None
pagerdutyurl: Optional[str] = None
pagerdutybusinessurl: Optional[str] = None
class CompItemModelList(BaseModel):
data: List[CompItemModel]
class Message(BaseModel):
detail: str
@app.get('/msapi/compitem',
response_model=List[CompItemModel],
responses={
401: {"model": Message,
"description": "Authorization Status",
"content": {
"application/json": {
"example": {"detail": "Authorization failed"}
},
},
},
500: {"model": Message,
"description": "SQL Error",
"content": {
"application/json": {
"example": {"detail": "SQL Error: 30x"}
},
},
} #,
# 200: {
# "description": "List of domain ids the user belongs to.",
# "content": {
# "application/json": {
# "example": [1, 200, 201, 5033]
# }
# },
# },
}
)
async def get_compitem(request: Request, compitemid:int):
try:
result = requests.get(validateuser_url + "/msapi/validateuser", cookies=request.cookies)
if (result is None):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Authorization Failed")
if (result.status_code != status.HTTP_200_OK):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Authorization Failed status_code=" + str(result.status_code))
except Exception as err:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Authorization Failed:" + str(err)) from None
try:
#Retry logic for failed query
no_of_retry = db_conn_retry
attempt = 1;
while True:
try:
with engine.connect() as connection:
conn = connection.connection
authorized = False # init to not authorized
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
sql = """select compid, id, name, rollup, rollback, repositoryid, target, xpos, ypos,
kind, buildid, buildurl, chart, operator, builddate, dockersha, gitcommit,
gitrepo, gittag, giturl, chartversion, chartnamespace, dockertag, chartrepo,
chartrepourl, serviceowner, serviceowneremail, serviceownerphone,
slackchannel, discordchannel, hipchatchannel, pagerdutyurl, pagerdutybusinessurl
from dm.dm_componentitem where id = %s"""
params = (str(compitemid),)
cursor.execute(sql, params)
result = cursor.fetchall()
if (not result):
result = [OrderedDict([('compid', -1), ('id', compitemid), ('name', None), ('rollup', None), ('rollback', None), ('repositoryid', None),
('target', None), ('xpos', None), ('ypos', None), ('kind', None), ('buildid', None), ('buildurl', None),
('chart', None), ('operator', None), ('builddate', None), ('dockersha', None), ('gitcommit', None),
('gitrepo', None), ('gittag', None), ('giturl', None), ('chartversion', None), ('chartnamespace', None), ('dockertag', None), ('chartrepo', None),
('chartrepourl', None), ('serviceowner', None), ('serviceowneremail', None), ('serviceownerphone', None),
('slackchannel', None), ('discordchannel', None), ('hipchatchannel', None), ('pagerdutyurl', None), ('pagerdutybusinessurl', None)])]
cursor.close()
conn.close()
return result
except (InterfaceError, OperationalError) as ex:
if attempt < no_of_retry:
logging.error(
"Database connection error: {} - sleeping for {}s"
" and will retry (attempt #{} of {})".format(
ex, sleep_for, attempt, no_of_retry
)
)
#200ms of sleep time in cons. retry calls
sleep(0.2)
attempt += 1
continue
else:
raise
except HTTPException:
raise
except Exception as err:
print(err)
# conn.rollback()
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(err)) from None
# Not implemented fully. SQL query is not complete
@app.post("/msapi/compitem",
responses={
401: {"model": Message,
"description": "Authorization Status",
"content": {
"application/json": {
"example": {"detail": "Authorization failed"}
},
},
},
500: {"model": Message,
"description": "SQL Error",
"content": {
"application/json": {
"example": {"detail": "SQL Error: 30x"}
},
},
}
}
)
async def create_compitem(response: Response, request: Request, compItemList: List[CompItemModel]):
try:
result = requests.get(validateuser_url + "/msapi/validateuser", cookies=request.cookies)
if (result is None):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Authorization Failed")
if (result.status_code != status.HTTP_200_OK):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Authorization Failed status_code=" + str(result.status_code))
except Exception as err:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Authorization Failed:" + str(err)) from None
try:
data_list = []
for col in compItemList:
row = (col.id, col.compid, col.status, col.buildid, col.buildurl, col.dockersha, col.dockertag, col.gitcommit, col.gitrepo, col.giturl) # this will be changed
data_list.append(row)
records_list_template = ','.join(['%s'] * len(data_list))
sql = 'INSERT INTO dm.dm_componentitem(id, compid, status, buildid, buildurl, dockersha, dockertag, gitcommit, gitrepo, giturl) \
VALUES {}'.format(records_list_template)
#Retry logic for failed query
no_of_retry = db_conn_retry
attempt = 1;
while True:
try:
with engine.connect() as connection:
conn = connection.connection
cursor = conn.cursor()
cursor.execute(sql, data_list)
# commit the changes to the database
rows_inserted = cursor.rowcount
# Commit the changes to the database
conn.commit()
conn.close()
if rows_inserted > 0:
response.status_code = status.HTTP_201_CREATED
return {"message": 'components created succesfully'}
response.status_code = status.HTTP_200_OK
return {"message": 'components not created'}
except (InterfaceError, OperationalError) as ex:
if attempt < no_of_retry:
logging.error(
"Database connection error: {} - sleeping for {}s"
" and will retry (attempt #{} of {})".format(
ex, sleep_for, attempt, no_of_retry
)
)
#200ms of sleep time in cons. retry calls
sleep(0.2)
attempt += 1
continue
else:
raise
except HTTPException:
raise
except Exception as err:
print(err)
# conn.rollback()
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(err)) from None
@app.delete("/msapi/compitem",
responses={
401: {"model": Message,
"description": "Authorization Status",
"content": {
"application/json": {
"example": {"detail": "Authorization failed"}
},
},
},
500: {"model": Message,
"description": "SQL Error",
"content": {
"application/json": {
"example": {"detail": "SQL Error: 30x"}
},
},
}
}
)
async def delete_compitem(request: Request, compid: int):
try:
result = requests.get(validateuser_url + "/msapi/validateuser", cookies=request.cookies)
if (result is None):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Authorization Failed")
if (result.status_code != status.HTTP_200_OK):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Authorization Failed status_code=" + str(result.status_code))
except Exception as err:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Authorization Failed:" + str(err)) from None
try:
#Retry logic for failed query
no_of_retry = db_conn_retry
attempt = 1;
while True:
try:
with engine.connect() as connection:
conn = connection.connection
cursor = conn.cursor()
sql1 = "DELETE from dm.dm_compitemprops where compitemid in (select id from dm.dm_componentitem where compid = " + str(compid) + ")"
sql2 = "DELETE from dm.dm_componentitem where compid=" + str(compid)
rows_deleted = 0
cursor.execute(sql1)
cursor.execute(sql2)
rows_deleted = cursor.rowcount
# Commit the changes to the database
conn.commit()
# response.status_code = status.HTTP_200_OK
return {"message": 'component deleted succesfully'}
except (InterfaceError, OperationalError) as ex:
if attempt < no_of_retry:
logging.error(
"Database connection error: {} - sleeping for {}s"
" and will retry (attempt #{} of {})".format(
ex, sleep_for, attempt, no_of_retry
)
)
#200ms of sleep time in cons. retry calls
sleep(0.2)
attempt += 1
continue
else:
raise
except HTTPException:
raise
except Exception as err:
print(err)
# conn.rollback()
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(err)) from None
@app.put("/msapi/compitem",
responses={
401: {"model": Message,
"description": "Authorization Status",
"content": {
"application/json": {
"example": {"detail": "Authorization failed"}
},
},
},
500: {"model": Message,
"description": "SQL Error",
"content": {
"application/json": {
"example": {"detail": "SQL Error: 30x"}
},
},
}
}
)
async def update_compitem(request: Request, compitemList: List[CompItemModel]):
try:
result = requests.get(validateuser_url + "/msapi/validateuser", cookies=request.cookies)
if (result is None):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Authorization Failed")
if (result.status_code != status.HTTP_200_OK):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Authorization Failed status_code=" + str(result.status_code))
except Exception as err:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Authorization Failed:" + str(err)) from None
try:
data_list = []
for col in compitemList:
row = (col.compid, col.status, col.buildid, col.buildurl, col.dockersha, col.dockertag, col.gitcommit, col.gitrepo, col.giturl, col.id) # this will be changed
data_list.append(row)
#Retry logic for failed query
no_of_retry = db_conn_retry
attempt = 1;
while True:
try:
with engine.connect() as connection:
conn = connection.connection
cursor = conn.cursor()
# # execute the INSERT statement
# records_list_template = ','.join(['%s'] * len(data_list))
sql = 'UPDATE dm.dm_componentitem set compid=%s, status=%s, buildid=%s, buildurl=%s, dockersha=%s, dockertag=%s, gitcommit=%s, gitrepo=%s, giturl=%s \
WHERE id = %s'
cursor.executemany(sql, data_list)
# commit the changes to the database
rows_inserted = cursor.rowcount
# Commit the changes to the database
conn.commit()
if rows_inserted > 0:
return {"message": 'components updated succesfully'}
return {"message": 'components not updated'}
except (InterfaceError, OperationalError) as ex:
if attempt < no_of_retry:
logging.error(
"Database connection error: {} - sleeping for {}s"
" and will retry (attempt #{} of {})".format(
ex, sleep_for, attempt, no_of_retry
)
)
#200ms of sleep time in cons. retry calls
sleep(0.2)
attempt += 1
continue
else:
raise
except Exception as err:
print(err)
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(err)) from None
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)