Due to a scheduled upgrade to version 14.10, GitLab will be unavailabe on Monday 30.05., from 19:00 until 20:00.

Commit fbfbad22 authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Refactor WriteJobResultsToDrupal for python and style.

Utilize class properties
Improve error messages and logging.
parent 7dbe04a9
Pipeline #32159 passed with stages
in 2 minutes and 39 seconds
import json
from datetime import datetime
import requests
from flask_restful import Resource, request
from flask_restful import current_app
from requests.auth import HTTPBasicAuth
from helpers.Error import ImportApiError
from datetime import datetime
import requests
import json
from requests.auth import HTTPBasicAuth
class WriteJobResultToDrupal(Resource):
def __init__(self):
self.logger = None
self.headers = {
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
'X-API-Key': current_app.config['drupal-api-key']
}
user = current_app.config['drupal-user']
password = current_app.config['drupal-password']
self.auth = HTTPBasicAuth(user, password)
self.job_url = f'{current_app.config["drupal-api-url"]}/jsonapi/node/log_result/'
self.import_process_endpoint = f'{current_app.config["drupal-api-url"]}' \
f'/jsonapi/node/import_process/'
def post(self, job_drupal_uuid, job_log_drupal_uuid):
"""
Write the job summary to Drupal job log (in field_summary)
......@@ -84,129 +99,107 @@ class WriteJobResultToDrupal(Resource):
500:
description: There was a problem
"""
self.logger = current_app.logger
body = request.json
current_app.logger.debug("Report Input Data: \n" + json.dumps(body, indent=2))
try:
body = request.json
current_app.logger.debug("report data: " + json.dumps(body, indent=2))
fatal = body['fatal']
status = 'FAILED'
if fatal == 0:
status = 'SUCCESS'
return write_results(job_drupal_uuid, job_log_drupal_uuid, status, body)
else:
status = 'FAILED'
return self.write_results(job_drupal_uuid, job_log_drupal_uuid, status, body)
except ImportApiError as e:
return {'error': e.message}, 500
def write_results(self, job_drupal_uuid, job_log_drupal_uuid, status, report):
self.logger.debug("Job UUID: " + job_drupal_uuid)
self.logger.debug("Job Log Report UUID: " + job_log_drupal_uuid)
get_url = self.job_url + job_log_drupal_uuid
response = requests.get(get_url, headers=self.headers, auth=self.auth)
if response.ok:
try:
job_log = response.json()
except json.decoder.JSONDecodeError as ex:
self.logger.error(f'Could not parse response as JSON: {response.text}.')
raise ImportApiError(ex)
log_data = job_log['data']
previous_status = log_data['attributes']['field_status']
previous_report = log_data['attributes']['field_message']
else:
raise ImportApiError(response.text)
def write_results(job_drupal_uuid, job_log_drupal_uuid, status, report):
current_app.logger.debug("job-uuid to write: " + job_drupal_uuid)
current_app.logger.debug("job-report-uuid to write: " + job_log_drupal_uuid)
current_app.logger.debug("report to write: " + json.dumps(report, indent=2))
headers = {
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
'X-API-Key': current_app.config['drupal-api-key']
}
user = current_app.config['drupal-user']
password = current_app.config['drupal-password']
auth = HTTPBasicAuth(user, password)
# read "status" and "message" from log_result
previous_status = ''
previous_report = ''
previous_report_json = ''
step = ''
try:
getUrl = current_app.config['drupal-api-url'] + \
'/jsonapi/node/log_result/' + job_log_drupal_uuid
response = requests.get(getUrl, headers=headers, auth=auth)
try:
logResult = response.json()['data']
previous_status = logResult['attributes']['field_status']
previous_report = logResult['attributes']['field_message']
except Exception:
msg = 'could not parse response from ' + getUrl
current_app.logger.error(msg)
except requests.exceptions.RequestException:
message = 'It was not possible to read form the Drupal API:' + getUrl +\
'\nresponse: ' + response
current_app.logger.error(message)
raise ImportApiError(message)
if previous_status == 'FAILED':
status = previous_status
if previous_report is None or previous_report == 'null':
previous_report_json = {}
else:
previous_report_json = json.loads(previous_report)
step = report['step']
previous_report_json[step] = report
reportString = json.dumps(previous_report_json)
# update values in drupal:
patchCalls = {}
patchUrl = current_app.config['drupal-api-url'] + \
'/jsonapi/node/import_process/' + job_drupal_uuid
if previous_status == 'FAILED':
status = previous_status
if previous_report is None or previous_report == 'null':
previous_report_json = {}
else:
previous_report_json = json.loads(previous_report)
step = report['step']
previous_report_json[step] = report
report_as_string = json.dumps(previous_report_json)
patchData = {
"data": {
"id": job_drupal_uuid,
"type": "node--import_process",
"attributes": {
"field_state": 0
# updating the status of the import process.
patch_calls = {}
patch_url = self.import_process_endpoint + job_drupal_uuid
patch_data = {
"data": {
"id": job_drupal_uuid,
"type": "node--import_process",
"attributes": {
"field_state": 0
}
}
}
}
patchCalls[patchUrl] = patchData
patchUrl = current_app.config['drupal-api-url'] + \
'/jsonapi/node/log_result/' + job_log_drupal_uuid
patchData = {
"data": {
"id": job_log_drupal_uuid,
"type": "node--job_result",
"attributes": {
"field_end_date": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S+00:00"),
"field_status": status,
"field_message": reportString
# updating the content of the job log entity
patch_calls[patch_url] = patch_data
patch_url = self.job_url + job_log_drupal_uuid
patch_data = {
"data": {
"id": job_log_drupal_uuid,
"type": "node--job_result",
"attributes": {
"field_end_date": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S+00:00"),
"field_status": status,
"field_message": report_as_string
}
}
}
}
patchCalls[patchUrl] = patchData
returnVal = {'message': ''}
patch_calls[patch_url] = patch_data
result = {'message': ''}
for patchCall in patchCalls:
url = patchCall
data = patchCalls[patchCall]
try:
response = requests.patch(
url,
headers=headers,
data=json.dumps(data)
)
except requests.exceptions.RequestException:
message = "It was not possible to write to Drupal API \
via the following url " + url
current_app.logger.error(message)
raise ImportApiError(message)
if response.status_code == 200:
current_app.logger.debug('Updated: ' + url)
returnVal['message'] += 'Updated: ' + url + '\n'
elif response.status_code == 403:
message = "Not authorized to write to: " + url
current_app.logger.error(message)
raise ImportApiError(message)
elif response.status_code == 404:
message = 'Not Found: ' + url
current_app.logger.error(message)
raise ImportApiError(message)
elif response.status_code == 500:
message = 'There was an internal server error for ' + url + ': ' + str(response) +\
'. Check the logs for details.'
current_app.logger.error(message)
raise ImportApiError(message)
else:
message = "Unknown response status code for drupal api for url " + url
current_app.logger.error(message)
raise ImportApiError(message)
return returnVal
for patchCall in patch_calls:
url = patchCall
data = patch_calls[patchCall]
try:
response = requests.patch(
url,
headers=self.headers,
data=json.dumps(data)
)
except requests.exceptions.RequestException:
message = "It was not possible to write to Drupal API \
via the following url " + url
current_app.logger.error(message)
raise ImportApiError(message)
if response.status_code == 200:
current_app.logger.debug('Updated: ' + url)
result['message'] += 'Updated: ' + url + '\n'
elif response.status_code == 403:
message = "Not authorized to write to: " + url
current_app.logger.error(message)
raise ImportApiError(message)
elif response.status_code == 404:
message = 'Not Found: ' + url
current_app.logger.error(message)
raise ImportApiError(message)
elif response.status_code == 500:
message = 'There was an internal server error for ' + url + ': ' + str(response) + \
'. Check the logs for details.'
current_app.logger.error(message)
raise ImportApiError(message)
else:
message = "Unknown response status code for drupal api for url " + url
current_app.logger.error(message)
raise ImportApiError(message)
return result
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment