ImportProcessStart.py 3.82 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
import hashlib
Jonas Waeber's avatar
Jonas Waeber committed
2
3
import os

4
from flasgger import swag_from
Jonas Waeber's avatar
Jonas Waeber committed
5
from flask_restful import Resource, reqparse, current_app
6

Jonas Waeber's avatar
Jonas Waeber committed
7
from import_api_app.helm import Helm
8
from import_api_app.resources.FetchMappingFile import FetchMappingFile
Jonas Waeber's avatar
Jonas Waeber committed
9

10
11
12
13
14
15

class ImportProcessStart(Resource):
    # Todo validate requests
    # @swag.validate('job-parameters')
    @swag_from('ImportProcessStart.yml')
    def post(self, institution_id, record_set_id):
Matthias's avatar
Matthias committed
16
17
18
19
20
        # get parameters of request-body
        parser = reqparse.RequestParser()
        parser.add_argument('job-parameters', type=dict)
        args = parser.parse_args()
        job_parameters = args['job-parameters']
Matthias's avatar
Matthias committed
21
22
        job_parameters['institutionId'] = institution_id
        job_parameters['recordSetId'] = record_set_id
Matthias's avatar
Matthias committed
23

Matthias's avatar
Matthias committed
24
25
26
27
28
29
        if job_parameters is None:
            job_parameters = {}
        if 'drupalJobUuid' not in job_parameters:
            job_parameters['drupalJobUuid'] = 'adf48fb4-16ba-40b2-a207-8ff9d5aac657'
        if 'drupalJobLogResultUuid' not in job_parameters:
            job_parameters['drupalJobLogResultUuid'] = 'eef31a5c-7f73-4cac-b811-8ba948aef453'
Matthias's avatar
Matthias committed
30
31
32
        if 'sessionId' not in job_parameters:
            job_parameters['sessionId'] = 'drupal...'
        if 'xmlRecordTag' not in job_parameters:
33
            job_parameters['xmlRecordTag'] = 'record'
Matthias's avatar
Matthias committed
34
        if 'xmlIdentifierFieldName' not in job_parameters:
35
            job_parameters['xmlIdentifierFieldName'] = 'id'
Matthias's avatar
Matthias committed
36
37
        if 'isPublished' not in job_parameters:
            job_parameters['isPublished'] = 'defaultvalue...'
Matthias's avatar
Matthias committed
38

39
40
41
42
43
        # start fetch-mapping-file
        FetchMappingFile.fetchMappingFile(
            self, recordset_id=record_set_id, session_id=job_parameters['sessionId']
        )

Matthias's avatar
Matthias committed
44
        # start text-file-validation
45
46
47
48
49
50
51
52
53
54
55
56
        short_session_id = hashlib.sha1(job_parameters['sessionId'].encode("UTF-8")).hexdigest()[
                           :10]
        body = {'drupalJobUuid': job_parameters['drupalJobUuid'],
                'drupalJobLogResultUuid': job_parameters['drupalJobLogResultUuid'],
                'recordSetId': job_parameters['recordSetId'],
                'sessionId': job_parameters['sessionId'], 'shortSessionId': short_session_id,
                'institutionId': job_parameters['institutionId'],
                'isPublished': job_parameters['isPublished'],
                'kafkaConfigs': current_app.config['tfv-kafka-configs'],
                'sftpConfigs': current_app.config['tfv-sftp-configs'],
                'topicName': current_app.config['tfv-topic-name'],
                'reportingTopicName': current_app.config['tfv-reporting-topic-name']}
Jonas Waeber's avatar
Jonas Waeber committed
57

Matthias's avatar
Matthias committed
58
59
60
61
62
63
64
65
66
67
68
69
70
        if 'xmlRecordTag' in job_parameters:
            body['xmlRecordTag'] = job_parameters['xmlRecordTag']
        if 'xmlIdentifierFieldName' in job_parameters:
            body['xmlIdentifierFieldName'] = job_parameters['xmlIdentifierFieldName']
        if 'tableSheetIndex' in job_parameters:
            body['tableSheetIndex'] = job_parameters['tableSheetIndex']
        if 'tableHeaderCount' in job_parameters:
            body['tableHeaderCount'] = job_parameters['tableHeaderCount']
        if 'tableHeaderIndex' in job_parameters:
            body['tableHeaderIndex'] = job_parameters['tableHeaderIndex']
        if 'tableIdentifierIndex' in job_parameters:
            body['tableIdentifierIndex'] = job_parameters['tableIdentifierIndex']

Jonas Waeber's avatar
Jonas Waeber committed
71
72
73
        helm = Helm()
        response = helm.install(
            chart=os.path.join(current_app.root_path, "charts", 'text-file-validation'),
74
            name=short_session_id + '-' + record_set_id + '-validation',
Jonas Waeber's avatar
Jonas Waeber committed
75
76
77
78
79
80
81
            namespace=current_app.config['NAMESPACE'],
            set_values=body,
            fail_on_err=False
        )

        if response.stderr == '':
            return {'status': 'SUCCESS', 'message': response.stdout}, 200
Matthias's avatar
Matthias committed
82
        else:
Jonas Waeber's avatar
Jonas Waeber committed
83
            return {'status': 'FAILED', 'message': response.stderr}, 500