Commit c161e85d authored by Matthias's avatar Matthias
Browse files

add endpoint for FetchMappingFile

parent 7a6fe8c4
Pipeline #14210 passed with stages
in 5 minutes and 23 seconds
......@@ -12,16 +12,17 @@ from import_api_app.resources.JobStop import JobStop
from import_api_app.resources.JobReport import JobReport
from import_api_app.resources.Job import Job
from import_api_app.resources.KafkaTopics import KafkaTopics
import import_api_app.configuration
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
from import_api_app.resources.ImportProcessStart import ImportProcessStart
from import_api_app.resources.FetchMappingFile import FetchMappingFile
from import_api_app.helpers.Error import ImportApiError
# from import_api_app.resources.HelmStart import HelmStart
# from import_api_app.resources.HelmStop import HelmStop
import import_api_app.configuration
import os
from import_api_app.helpers.Error import ImportApiError
def create_app(test_config=None):
......@@ -76,6 +77,12 @@ def create_app(test_config=None):
ImportProcessStart,
'/v1/importprocess/<institution_id>/<record_set_id>/start'
)
api.add_resource(
FetchMappingFile,
'/v1/FetchMappingFile/<recordset_id>/<import_step>/<file_name>'
)
api.add_resource(JobList, '/v1/jobs')
api.add_resource(
JobStart,
......
from flask_restful import Resource, current_app
from kafka import KafkaProducer
from helpers.Error import ImportApiError
import paramiko
class FetchMappingFile(Resource):
def get(self, recordset_id, import_step, file_name):
"""
Fetches the mapping file form the sftp-server
---
tags:
- fetch mapping file
parameters:
- in: path
name: recordset_id
required: true
description: The name of the record set (matches folder name on sftp)
example: AfZ-Becker-Audiovisuals
type: string
- in: path
name: import_step
required: true
description: The name of import step
example: mapping-service
type: string
- in: path
name: file_name
required: true
description: The name of the mapping file
example: mapping.yml
type: string
responses:
200:
description: Success, the mapping file has been retrieved
schema:
properties:
status:
type: string
example: SUCCESS/FAILURE
enum: ['SUCCESS', 'FAILURE']
contents:
type: string/yml
example: the contents of the mapping file...
"""
# 1. read file contents from sftp:
host = current_app.config["sftp_host"]
port = current_app.config["sftp_port"]
user = current_app.config["sftp_user"]
pwd = current_app.config["sftp_password"]
# host = 'mb-wf2.memobase.unibas.ch'
# port = 80
# user = 'mb_sftp'
# pwd = '***'
remote_path = '/swissbib_index/mb_sftp/' + recordset_id + '/config/' + file_name
mapping_file_contents = ''
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=host,
port=port,
username=user,
password=pwd)
sftp_client = ssh_client.open_sftp()
try:
sftp_client.stat(remote_path)
remote_file = sftp_client.file(remote_path, 'r')
mapping_file_contents = remote_file.read()
except IOError:
message = 'remote path "' + remote_path + '" does not exist'
current_app.logger.debug(message)
raise ImportApiError(message)
sftp_client.close()
ssh_client.close()
except Exception as ex:
message = str(ex)
current_app.logger.debug(message)
raise ImportApiError(message)
# 2. write file content into kafka topic
try:
producer = KafkaProducer(bootstrap_servers=current_app.config['kafka-broker-url'],
key_serializer=str.encode)
key = recordset_id + '#' + import_step
producer.send('import-process-config', key=key, value=mapping_file_contents)
except Exception as ex:
message = str(ex)
current_app.logger.debug(message)
raise ImportApiError(message)
return {'status': 'SUCCESS'}, 200
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