93 lines
2.6 KiB
Python
93 lines
2.6 KiB
Python
import json
|
|
import re
|
|
import requests
|
|
import coloredlogs, logging
|
|
from Options import Options
|
|
|
|
class RestCall:
|
|
|
|
setting={}
|
|
|
|
logger = logging.getLogger("yamo")
|
|
|
|
def validate(self):
|
|
pass
|
|
|
|
def skip(self,execConfigValue):
|
|
skip=False
|
|
for x in execConfigValue:
|
|
if 'skipDeployment' in x:
|
|
if x['skipDeployment'] == 'true':
|
|
skip=True
|
|
return skip
|
|
|
|
def execByEnv(self, env):
|
|
self.__exec(env)
|
|
|
|
def exec(self):
|
|
if Options.getInstance().env:
|
|
self.execByEnv(Options.getInstance().env)
|
|
else:
|
|
self.execAll()
|
|
|
|
def execAll(self):
|
|
for env, setting in self.setting.items():
|
|
self.__exec(env)
|
|
|
|
def __exec(self,env):
|
|
#print(env)
|
|
#context= Context.getInstance()
|
|
self.logger.debug("try to execute "+self.name+ " for Evironment "+ env)
|
|
variabledBody = self.setting[env]
|
|
for vb in variabledBody:
|
|
compiledBody = self.__compileBody(vb)
|
|
|
|
envObj = self.context.getEnvironment(env)
|
|
url=envObj.env_url+self.endpoint
|
|
headers = {'Content-type': 'application/json', 'Authorization': 'Api-Token '+ envObj.token}
|
|
|
|
self.logger.debug("Rest body: "+compiledBody)
|
|
response = requests.post(url, headers=headers, data=compiledBody)
|
|
#self.logger.debug(response.text)
|
|
|
|
if response.status_code != 200:
|
|
self.logger.error(self.name+" Env: "+env+ " [Failed]" )
|
|
self.logger.error(response.text )
|
|
raise Exception(response.text)
|
|
else:
|
|
self.logger.info(self.name+" Env: "+env+" [SUCCESS]")
|
|
|
|
|
|
#for key, value in self.setting.items():
|
|
# print(key)
|
|
#if not self.skip(value):
|
|
# print()
|
|
#print(value)
|
|
|
|
#def __getVariable(self,variables,name):
|
|
|
|
def __compileBody(self, variabledBody):
|
|
variables=json.dumps(variabledBody['variables'])
|
|
plainBody=json.dumps(variabledBody['body'])
|
|
matches = re.findall("\{\{.([a-zA-Z]*)\}\}", plainBody)
|
|
|
|
|
|
for m in matches:
|
|
v = re.findall('"'+m+'": ([^}]*)', variables)
|
|
plainBody=plainBody.replace('"{{.'+m+'}}"',v[0])
|
|
|
|
return plainBody
|
|
|
|
|
|
def call(self):
|
|
print("Calling rest: "+ self.name)
|
|
|
|
def __init__(self, name, restConfig, executionConfig,context):
|
|
self.name=name
|
|
self.endpoint=restConfig["endpoint"]
|
|
self.schema=restConfig["schema"]
|
|
self.setting=executionConfig
|
|
self.context=context
|
|
#self.execConfig=execConfig
|
|
|