__init__.py 4.95 KB
Newer Older
1
2
3
4
from flask import Flask, send_from_directory, redirect
from kubernetes import config
from flask_restful import Api
from flasgger import Swagger
Lionel Walter's avatar
Lionel Walter committed
5
6
from kubernetes.config import ConfigException

Matthias's avatar
Matthias committed
7
from import_api_app.resources.WriteJobResultsToDrupal import WriteJobResultToDrupal
8
from import_api_app.resources.WriteTypeReportToDrupal import WriteTypeReportToDrupal
9
from import_api_app.resources.UpdateInstitution import UpdateInstitution
10
from import_api_app.resources.ClearCache import ClearCache
Jonas Waeber's avatar
Jonas Waeber committed
11
from import_api_app.resources.UpdateRecordSet import UpdateRecordSet
12
from import_api_app.resources.ImportProcessStart import ImportProcessStart
Matthias's avatar
Matthias committed
13
from import_api_app.resources.FetchMappingFile import FetchMappingFile
Matthias's avatar
Matthias committed
14
15
16
from import_api_app.resources.DeleteRecord import DeleteRecord
from import_api_app.resources.DeleteRecordset import DeleteRecordset
from import_api_app.resources.DeleteInstitution import DeleteInstitution
Matthias's avatar
Matthias committed
17
18
from import_api_app.helpers.Error import ImportApiError
import import_api_app.configuration
19
import os
20

21

Jonas Waeber's avatar
Jonas Waeber committed
22
def create_app():
23
24
25
26
27
28
29
    app = Flask(__name__)
    app.config.from_object(import_api_app.configuration)

    # Based on this example :
    # https://github.com/flasgger/flasgger/blob/master/examples/restful.py

    api = Api(app)
Matthias's avatar
Matthias committed
30

31
    try:
Matthias's avatar
Matthias committed
32
        app.config['import-api-url'] = os.environ['IMPORT_API_URL']
33
        app.config['kafka-broker-url'] = os.environ['KAFKA_BOOTSTRAP_SERVERS']
34
35
        app.config['drupal-user'] = os.environ['DRUPAL_USERNAME']
        app.config['drupal-password'] = os.environ['DRUPAL_PASSWORD']
Matthias's avatar
Matthias committed
36
        app.config['drupal-api-url'] = os.environ['DRUPAL_API_URL']
37
        app.config['drupal-api-key'] = os.environ['DRUPAL_API_KEY']
38
        app.config['clear-cache-url'] = os.environ['CLEAR_CACHE_URL']
Matthias's avatar
Matthias committed
39
40
        app.config['topic-configs'] = os.environ['TOPIC_CONFIGS']
        app.config['topic-drupal-export'] = os.environ['TOPIC_DRUPAL_EXPORT']
41
42
43
44
        app.config['sftp_host'] = os.environ['SFTP_HOST']
        app.config['sftp_port'] = os.environ['SFTP_PORT']
        app.config['sftp_user'] = os.environ['SFTP_USER']
        app.config['sftp_password'] = os.environ['SFTP_PASSWORD']
45
46
47
48
        app.config['tfv-kafka-configs'] = os.environ['TFV_KAFKA_SERVER_CONFIGS']
        app.config['tfv-sftp-configs'] = os.environ['TFV_SFTP_CONFIGS']
        app.config['tfv-topic-name'] = os.environ['TFV_TOPIC_NAME']
        app.config['tfv-reporting-topic-name'] = os.environ['TFV_REPORTING_TOPIC_NAME']
49
        app.config['tfv-env'] = os.environ['TFV_ENV']
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

        app.config['SWAGGER'] = {
            'title': 'Memobase Import API',
            'version': 'dev',
            'uiversion': 3,
            'termsOfService': 'http://memobase.ch/de/disclaimer',
            'description': 'API to start, stop, manage import processes for '
                           'memobase. Will be used in the Admin Interface (Drupal).',
            'contact': {
                'name': 'UB Basel',
                'url': 'https://ub.unibas.ch',
                'email': 'swissbib-ub@unibas.ch'},
            'favicon': '/favicon.ico'}
        Swagger(app)
    except Exception as ex:
        raise ImportApiError(str(ex))
66
67
68
69
70
71
72
73
74
75
76
77
78
79

    @app.route("/")
    def home():
        return redirect("/apidocs")

    @app.route('/favicon.ico')
    def favicon():
        return send_from_directory(
            os.path.join(
                app.root_path,
                'assets'),
            'favicon.ico',
            mimetype='image/vnd.microsoft.icon')

80
81
82
83
    api.add_resource(
        ImportProcessStart,
        '/v1/importprocess/<institution_id>/<record_set_id>/start'
    )
Matthias's avatar
Matthias committed
84
85
86

    api.add_resource(
        FetchMappingFile,
87
        '/v1/FetchMappingFile/<recordset_id>/<session_id>'
Matthias's avatar
Matthias committed
88
    )
89
90
    api.add_resource(WriteJobResultToDrupal, '/v1/drupal/<job_drupal_uuid>/<job_log_drupal_uuid>')
    api.add_resource(WriteTypeReportToDrupal, '/v1/drupal/WriteElementReport')
Jonas Waeber's avatar
Jonas Waeber committed
91
92
    api.add_resource(UpdateInstitution, '/v1/drupal/institution/<institution_drupal_uuid>')
    api.add_resource(UpdateRecordSet, '/v1/drupal/recordset/<record_set_drupal_uuid>')
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    api.add_resource(
        DeleteRecord,
        '/v1/drupal/delete/record/<session_id>',
        '/v1/drupal/delete/record/<session_id>/<dryrun>'
    )
    api.add_resource(
        DeleteRecordset,
        '/v1/drupal/delete/recordset/<session_id>',
        '/v1/drupal/delete/recordset/<session_id>/<dryrun>'
    )
    api.add_resource(
        DeleteInstitution,
        '/v1/drupal/delete/institution/<session_id>',
        '/v1/drupal/delete/institution/<session_id>/<dryrun>'
    )
108
    api.add_resource(ClearCache, '/v1/drupal/clearcache')
109
110
111
112
113
114

    # TODO : maybe take that to a configuration (development vs pod running in
    # k8s cluster)
    try:
        # to be used when inside a kubernetes cluster
        config.load_incluster_config()
Lionel Walter's avatar
Lionel Walter committed
115
    except ConfigException:
116
117
118
119
        try:
            # use .kube directory
            # for local development
            config.load_kube_config()
Lionel Walter's avatar
Lionel Walter committed
120
        except ConfigException:
121
122
123
            app.logger.error("No kubernetes cluster defined")

    return app