161 lines
5.2 KiB
Python
161 lines
5.2 KiB
Python
import re
|
|
|
|
from key_request_parser import patterns, keyrequests, helper
|
|
|
|
from enum import Flag, auto
|
|
|
|
|
|
class KROption(Flag):
|
|
VALIDATE_EXISTS = auto()
|
|
VALIDATE_HASDATA = auto()
|
|
|
|
class KRParser:
|
|
patterns=[patterns.Pattern1(), patterns.Pattern2(), patterns.Pattern3(), patterns.Pattern4()]
|
|
|
|
def normalize(self,x):
|
|
#tmp=x.replace("~","")
|
|
tmp=x.replace("\n","")
|
|
#tmp=tmp.replace("\"/","\"")
|
|
#tmp=tmp.replace("\"/","") -_>was active
|
|
#tmp=tmp.replace("/\"","\"")
|
|
tmp=tmp.replace("/\"","")
|
|
tmp=tmp.replace("\"","")
|
|
tmp=tmp.replace("\t","")
|
|
|
|
tmp=re.sub("([\s]*)\)", ")", tmp)
|
|
|
|
|
|
|
|
tmp=re.sub("\([\s\n\r]*", "(", tmp)
|
|
tmp=re.sub("\,[\s\n\r]*", ",", tmp)
|
|
tmp=re.sub("\)[\s\n\r]*,", "),", tmp)
|
|
tmp=re.sub("in[\s\n\r]*\(", "in(", tmp)
|
|
|
|
return tmp
|
|
|
|
|
|
def applyPatterns(self,subject):
|
|
groups=None
|
|
for p in self.patterns:
|
|
groups=p.parseServicesAndMethods(subject)
|
|
|
|
if len(groups) > 0:
|
|
break
|
|
|
|
return groups
|
|
|
|
|
|
# def checkKeyRequetsExists(self,kr, DTAPIURL, DTAPIToken):
|
|
|
|
# DTAPIURL = DTAPIURL + "/api/v2/entities"
|
|
|
|
# headers = {
|
|
# 'Content-Type': 'application/json',
|
|
# 'Authorization': 'Api-Token ' + DTAPIToken
|
|
# }
|
|
|
|
# for group in kr.matchedGroups:
|
|
# params={"entitySelector": group["existsQuery"]}
|
|
# response = helper.get_request(DTAPIURL, headers, params)
|
|
# entities = (response.json())['entities']
|
|
|
|
# for method in group["methods"]:
|
|
# comparer=None
|
|
# if method.startswith('SERVICE_METHOD-'):
|
|
# comparer="entityId"
|
|
# else:
|
|
# comparer="displayName"
|
|
|
|
# found = [x for x in entities if x[comparer] == method]
|
|
|
|
# if len(found) > 0:
|
|
# #Keyrequest exists
|
|
# tmp=found[0]
|
|
# tmp["exists"]=True
|
|
# else:
|
|
# #Keyrequest not exists
|
|
# tmp={"displayName":method,"type": "SERVICE_METHOD", "entityId":method, "exists":False}
|
|
|
|
# kr.keyRequests.append(tmp)
|
|
|
|
# return kr
|
|
|
|
def checkKeyRequetsExists(self,kr, DTAPIURL, DTAPIToken):
|
|
|
|
DTAPIURL = DTAPIURL + "/api/v2/entities"
|
|
|
|
headers = {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Api-Token ' + DTAPIToken
|
|
}
|
|
|
|
for gid, group in enumerate(kr.matchedGroups):
|
|
params={"entitySelector": group["existsQuery"]}
|
|
response = helper.get_request(DTAPIURL, headers, params)
|
|
entities = (response.json())['entities']
|
|
|
|
if len(entities) > 0:
|
|
y=0
|
|
for method in kr.keyRequests:
|
|
if method["groupId"] == gid:
|
|
found = [x for x in entities if x[method["comparer"]] == method[method["comparer"]]]
|
|
|
|
if len(found) > 0:
|
|
method["exists"]=True
|
|
method["displayName"]=found[0]["displayName"]
|
|
method["entityId"]=found[0]["entityId"]
|
|
else:
|
|
method["exists"]=False
|
|
|
|
|
|
|
|
def process(self, kr):
|
|
|
|
for gid, group in enumerate(kr.matchedGroups):
|
|
for method in group["methods"]:
|
|
if method.startswith('SERVICE_METHOD-'):
|
|
tmp={"displayName": None,"comparer": "entityId", "entityId":method, "exists":None, "groupId":gid}
|
|
else:
|
|
tmp={"displayName":method,"comparer": "displayName", "entityId":None, "exists":None, "groupId":gid}
|
|
|
|
kr.keyRequests.append(tmp)
|
|
|
|
|
|
if KROption.VALIDATE_EXISTS in self.options:
|
|
self.checkKeyRequetsExists(kr,self.DTAPIURL, self.DTAPIToken)
|
|
|
|
if KROption.VALIDATE_HASDATA in self.options:
|
|
x=0
|
|
|
|
return kr
|
|
|
|
|
|
def parseBySLO(self, row):
|
|
#normalize
|
|
normFilter=self.normalize(row['filter'])
|
|
normExpresseion=self.normalize(row['metricExpression'])
|
|
|
|
tmp_KR = keyrequests.KR({"sloName":row["name"], "env":row["env"], "metricExpression": normExpresseion, "filter": normFilter, "matchedGroups": None})
|
|
|
|
|
|
#SLO with Filter
|
|
if normFilter.upper().startswith("TYPE(SERVICE_METHOD),"):
|
|
subject=normFilter
|
|
else:
|
|
subject=normExpresseion
|
|
|
|
groups=self.applyPatterns(subject)
|
|
|
|
for g in groups:
|
|
if g["methods"] != None and len(g["methods"]) > 0:
|
|
tmp_KR.matchedGroups.append(g)
|
|
|
|
#self.process(tmp_KR)
|
|
|
|
return self.process(tmp_KR)
|
|
|
|
|
|
def __init__(self, options: KROption , DTAPIURL, DTAPIToken ):
|
|
self.DTAPIURL= DTAPIURL
|
|
self.DTAPIToken=DTAPIToken
|
|
self.options=options |