-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from 5genesis/rest_task
v3.0.1
- Loading branch information
Showing
46 changed files
with
680 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .jenkins_api import JenkinsJob, JenkinsStatus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from Task import Task | ||
from Settings import EvolvedConfig | ||
from Interfaces import Evolved5gJenkinsApi | ||
from Helper import Level | ||
|
||
|
||
class JenkinsBase(Task): | ||
def __init__(self, name, parent, params, logMethod): | ||
super().__init__(name, parent, params, logMethod, None) | ||
self.config = EvolvedConfig().JenkinsApi | ||
self.client = None | ||
|
||
def Run(self): | ||
try: | ||
self.client = self.getApiClient() | ||
except Exception as e: | ||
self.Log(Level.Error, f"Unable to create Jenkins API client: {e}") | ||
self.client = None | ||
|
||
def getApiClient(self) -> Evolved5gJenkinsApi: | ||
if not self.config.Enabled: | ||
raise RuntimeError(f"Trying to run {self.name} Task while Jenkins API is not enabled") | ||
|
||
return Evolved5gJenkinsApi(self.config.Host, self.config.Port, | ||
self.config.User, self.config.Password) | ||
|
||
|
||
class JenkinsJob(JenkinsBase): | ||
def __init__(self, logMethod, parent, params): | ||
super().__init__("Jenkins Job", parent, params, logMethod) | ||
self.paramRules = { | ||
'Instance': (None, True), | ||
'Job': (None, True), | ||
'GitUrl': (None, True), | ||
'GitBranch': (None, True), | ||
'Version': ('1.0', False), | ||
'PublishKey': ('JenkinsJobId', False), | ||
} | ||
|
||
def Run(self): | ||
super().Run() | ||
if self.client is None: return | ||
|
||
instance = self.params["Instance"] | ||
job = self.params["Job"] | ||
url = self.params["GitUrl"] | ||
branch = self.params["GitBranch"] | ||
version = self.params["Version"] | ||
|
||
self.Log(Level.DEBUG, | ||
f"Trying to trigger job '{job}' on instance '{instance}' ({url}|{branch}|{version})") | ||
|
||
try: | ||
jobId = self.client.TriggerJob(instance, job, url, branch, version) | ||
self.Log(Level.INFO, f"Triggered '{job}'. Received Job Id: {jobId}") | ||
self.Publish(self.params["PublishKey"], jobId) | ||
except Exception as e: | ||
self.Log(Level.ERROR, f"Unable to trigger job: {e}") | ||
|
||
|
||
class JenkinsStatus(JenkinsBase): | ||
def __init__(self, logMethod, parent, params): | ||
super().__init__("Jenkins Status", parent, params, logMethod) | ||
self.paramRules = { | ||
'JobId': (None, True), | ||
'PublishKey': ('JenkinsJobStatus', False), | ||
} | ||
|
||
def Run(self): | ||
super().Run() | ||
if self.client is None: return | ||
|
||
jobId = self.params['JobId'] | ||
|
||
try: | ||
status, message = self.client.CheckJob(jobId) | ||
message = message if message is not None else "<No details>" | ||
self.Log(Level.INFO, f"Status of job '{jobId}': {status} ('{message}')") | ||
self.Publish(self.params["PublishKey"], status) | ||
except Exception as e: | ||
self.Log(Level.ERROR, f"Unable to check job '{jobId}' status: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from Task import Task | ||
from Helper import Config | ||
from Settings import Config | ||
|
||
|
||
TIMEOUT = Config().EastWest.Timeout | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from Task import Task | ||
from Helper import Level | ||
from REST import RestClient, Payload | ||
|
||
|
||
class RestApi(Task): | ||
def __init__(self, logMethod, parent, params): | ||
super().__init__("REST API", parent, params, logMethod, None) | ||
self.paramRules = { | ||
'Host': (None, True), | ||
'Port': (None, True), | ||
'Endpoint': (None, True), | ||
'Https': (False, False), | ||
'Insecure': (False, False), | ||
'Method': ('GET', False), | ||
'Payload': ('{}', False), | ||
'PayloadMode': (None, False), | ||
'Responses': (None, False), | ||
'Timeout': (10, False), | ||
'Headers': (None, False) | ||
} | ||
|
||
def Run(self): | ||
client = RestClient(self.params['Host'], self.params['Port'], "", | ||
self.params['Https'], self.params['Insecure']) | ||
|
||
endpoint = self.params['Endpoint'] | ||
method = str(self.params['Method']).upper() | ||
payload = self.params['Payload'] | ||
payloadMode = self.params['PayloadMode'] | ||
timeout = self.params['Timeout'] | ||
headers = self.params['Headers'] | ||
statusCodes = self.params['Responses'] | ||
|
||
if statusCodes is not None: | ||
if not isinstance(statusCodes, (tuple, list)): | ||
statusCodes = [statusCodes] | ||
|
||
if "Success" in statusCodes: | ||
statusCodes.remove("Success") | ||
statusCodes.extend([*range(200, 300)]) | ||
|
||
self.Log(Level.INFO, f"Sending '{method}' request to '{client.api_url}', endpoint '{endpoint}'.") | ||
self.Log(Level.DEBUG, f"Timeout: {timeout}; Extra Headers: {headers}") | ||
self.Log(Level.DEBUG, f"Payload: {payload}") | ||
if statusCodes is not None: | ||
self.Log(Level.DEBUG, f"Accepted status codes: {statusCodes}") | ||
|
||
match method: | ||
case "GET": | ||
response = client.HttpGet(endpoint, extra_headers=headers, timeout=timeout) | ||
case "POST": | ||
payloadMode = None if payloadMode is None else Payload[payloadMode] | ||
response = client.HttpPost(endpoint, extra_headers=headers, | ||
body=payload, payload=payloadMode, timeout=timeout) | ||
case "PATCH": | ||
response = client.HttpPatch(endpoint, extra_headers=headers, body=payload, timeout=timeout) | ||
case "DELETE": | ||
response = client.HttpDelete(endpoint, extra_headers=headers, timeout=timeout) | ||
case _: | ||
self.Log(Level.ERROR, f"Unsupported method '{method}'") | ||
return | ||
|
||
status = client.ResponseStatusCode(response) | ||
try: | ||
data = client.ResponseToJson(response) | ||
except RuntimeError: | ||
try: | ||
data = client.ResponseToRaw(response) | ||
except RuntimeError: | ||
data = None | ||
|
||
self.Log(Level.INFO, f"Status '{status}'; Response: '{data}'") | ||
if statusCodes is not None: | ||
if status not in statusCodes: | ||
message = f"Unexpected status code received: {status}" | ||
self.Log(Level.ERROR, message) | ||
raise RuntimeError(message) |
Oops, something went wrong.