Commit 29f3c688 authored by Matthias's avatar Matthias
Browse files

add import reporting endpoint

parent dd67cf52
Pipeline #23770 passed with stages
in 2 minutes and 44 seconds
......@@ -12,7 +12,7 @@ from kubernetes.config import ConfigException
# from import_api_app.resources.JobReport import JobReport
# from import_api_app.resources.Job import Job
# from import_api_app.resources.KafkaTopics import KafkaTopics
# from import_api_app.resources.WriteJobResultsToDrupal import WriteJobResultToDrupal
from import_api_app.resources.WriteJobResultsToDrupal import WriteJobResultToDrupal
# from import_api_app.resources.ReadJobOptionsFromDrupal import ReadJobOptionsFromDrupal
from import_api_app.resources.MonitorDrupalJsonApiInstitution import MonitorDrupalJsonApiInstitution
from import_api_app.resources.MonitorDrupalJsonApiRecordSet import MonitorDrupalJsonApiRecordSet
......@@ -41,6 +41,8 @@ def create_app(test_config=None):
try:
app.config['import-api-url'] = os.environ['IMPORT_API_URL']
app.config['kafka-broker-url'] = os.environ['KAFKA_BOOTSTRAP_SERVERS']
app.config['drupal-cms-admin-key'] = os.environ['DRUPAL_CMS_ADMIN_KEY']
app.config['drupal-api-url'] = os.environ['DRUPAL_API_URL']
app.config['drupal-api-key'] = os.environ['DRUPAL_API_KEY']
app.config['sftp_host'] = os.environ['SFTP_HOST']
app.config['sftp_port'] = os.environ['SFTP_PORT']
......@@ -100,7 +102,7 @@ def create_app(test_config=None):
# api.add_resource(HelmStop, '/v1/helm/<process_id>/<job_name>/stop')
#
# api.add_resource(Job, '/v1/job/<process_id>/<job_name>')
# api.add_resource(WriteJobResultToDrupal, '/v1/drupal/<job_log_drupal_uuid>/<report>')
api.add_resource(WriteJobResultToDrupal, '/v1/drupal/<job_drupal_uuid>/<job_log_drupal_uuid>')
# api.add_resource(ReadJobOptionsFromDrupal, '/v1/drupal/<job_name>/<job_drupal_uuid>')
#
api.add_resource(MonitorDrupalJsonApiInstitution, '/v1/drupal/institution/<institutionId>')
......
......@@ -5,55 +5,79 @@ import requests
import json
def write_results(job_log_drupal_uuid, report):
current_app.logger.debug("uuid to write: " + job_log_drupal_uuid)
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: " + report)
headers = {
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
'X-API-Key': current_app.config['drupal-api-key']
'X-API-Key': current_app.config['drupal-cms-admin-key'],
'Authorization': 'Basic bWVtb2Jhc2U6MjAyMA=='
}
calls = {}
url = current_app.config['drupal-api-url'] + \
'/jsonapi/node/import_process/' + job_drupal_uuid
data = {
"data": {
"id": job_log_drupal_uuid,
"type": "paragraph--job_log_result",
"id": job_drupal_uuid,
"type": "node--import_process",
"attributes": {
"field_summary": report,
"field_end_date": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S+00:00")
"field_state": 0
}
}
}
calls[url] = data
url = current_app.config['DRUPAL_API_URL'] + \
'/jsonapi/paragraph/job_log_result/' + job_log_drupal_uuid
url = current_app.config['drupal-api-url'] + \
'/jsonapi/node/log_result/' + job_log_drupal_uuid
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.error('Updated: '+url)
return {'message': 'Updated: ' + url}
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)
else:
message = "Unknown response status code for drupal api for url " + url
current_app.logger.error(message)
raise ImportApiError(message)
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
}
}
}
calls[url] = data
returnVal = {'message': ''}
for call in calls:
url = call
data = calls[call]
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)
else:
message = "Unknown response status code for drupal api for url " + url
current_app.logger.error(message)
raise ImportApiError(message)
return returnVal
# make the job as not active any more and write the end data
......
......@@ -5,7 +5,7 @@ from import_api_app.helpers.Error import ImportApiError
class WriteJobResultToDrupal(Resource):
def post(self, job_log_drupal_uuid):
def post(self, job_drupal_uuid, job_log_drupal_uuid):
"""
Write the job summary to Drupal job log (in field_summary)
---
......@@ -13,21 +13,30 @@ class WriteJobResultToDrupal(Resource):
- Drupal
parameters:
- in: path
name: job_log_drupal_uuid
name: job_drupal_uuid
required: true
description: job log uuid
default: 2b07341f-3b99-408c-a729-6e18dd77c6d5
type: string
- in: path
name: job_log_drupal_uuid
required: true
description: job report log uuid
type: string
- in: body
name: body
required: true
schema:
id: report
items:
type: object
properties:
report:
type: string
example: reporting json...
description: The aggregated report of an import
status:
type: string
example: reporting json...
description: The aggregated report of an import
example: SUCCESS
description: The status of an import
responses:
200:
description: It was successful
......@@ -37,7 +46,8 @@ class WriteJobResultToDrupal(Resource):
description: There was a problem
"""
try:
report = request.json['report']
return write_results(job_log_drupal_uuid, report)
status = request.json[0]['status']
report = request.json[0]['report']
return write_results(job_drupal_uuid, job_log_drupal_uuid, status, report)
except ImportApiError as e:
return {'error': e.message}, 500
Supports Markdown
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