__init__.py 5.64 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

7
8
9
# from import_api_app.resources.HelmStart import HelmStart
# from import_api_app.resources.HelmStop import HelmStop
# from import_api_app.resources.KafkaTopics import KafkaTopics
Matthias's avatar
Matthias committed
10
from import_api_app.resources.WriteJobResultsToDrupal import WriteJobResultToDrupal
11
from import_api_app.resources.WriteTypeReportToDrupal import WriteTypeReportToDrupal
12
# from import_api_app.resources.ReadJobOptionsFromDrupal import ReadJobOptionsFromDrupal
13
from import_api_app.resources.UpdateInstitution import UpdateInstitution
14
from import_api_app.resources.ClearCache import ClearCache
Jonas Waeber's avatar
Jonas Waeber committed
15
from import_api_app.resources.UpdateRecordSet import UpdateRecordSet
16
from import_api_app.resources.ImportProcessStart import ImportProcessStart
Matthias's avatar
Matthias committed
17
from import_api_app.resources.FetchMappingFile import FetchMappingFile
Matthias's avatar
Matthias committed
18
19
20
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
21
22
from import_api_app.helpers.Error import ImportApiError
import import_api_app.configuration
23
import os
24

25
26
27
28
29
30
31
32
33
34

def create_app(test_config=None):
    app = Flask(__name__)
    app.config.from_object(import_api_app.configuration)
    # app.config.from_envvar('YOURAPPLICATION_SETTINGS')

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

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

36
    try:
Matthias's avatar
Matthias committed
37
        app.config['import-api-url'] = os.environ['IMPORT_API_URL']
38
        app.config['kafka-broker-url'] = os.environ['KAFKA_BOOTSTRAP_SERVERS']
39
40
        app.config['drupal-user'] = os.environ['DRUPAL_USERNAME']
        app.config['drupal-password'] = os.environ['DRUPAL_PASSWORD']
Matthias's avatar
Matthias committed
41
        app.config['drupal-api-url'] = os.environ['DRUPAL_API_URL']
42
        app.config['drupal-api-key'] = os.environ['DRUPAL_API_KEY']
43
        app.config['clear-cache-url'] = os.environ['CLEAR_CACHE_URL']
Matthias's avatar
Matthias committed
44
45
        app.config['topic-configs'] = os.environ['TOPIC_CONFIGS']
        app.config['topic-drupal-export'] = os.environ['TOPIC_DRUPAL_EXPORT']
46
47
48
49
        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']
50
51
52
53
        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']
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

        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))
70
71
72
73
74
75
76
77
78
79
80
81
82
83

    @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')

84
85
86
87
    api.add_resource(
        ImportProcessStart,
        '/v1/importprocess/<institution_id>/<record_set_id>/start'
    )
Matthias's avatar
Matthias committed
88
89
90

    api.add_resource(
        FetchMappingFile,
91
        '/v1/FetchMappingFile/<recordset_id>/<session_id>'
Matthias's avatar
Matthias committed
92
93
    )

94
95
96
97
98
99
    # api.add_resource(
    #      HelmStart,
    #      '/v1/helm/<institution_id>/<record_set_id>/<process_id>/<job_name>/start'
    # )
    # api.add_resource(HelmStop, '/v1/helm/<process_id>/<job_name>/stop')
    # api.add_resource(ReadJobOptionsFromDrupal, '/v1/drupal/<job_name>/<job_drupal_uuid>')
100
101
102
103
    # api.add_resource(KafkaTopics, '/v1/kafka/topics')
    # api.add_resource(JobReport, '/v1/job/<topic_name>/report')
    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
104
105
    api.add_resource(UpdateInstitution, '/v1/drupal/institution/<institution_drupal_uuid>')
    api.add_resource(UpdateRecordSet, '/v1/drupal/recordset/<record_set_drupal_uuid>')
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    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>'
    )
121
    api.add_resource(ClearCache, '/v1/drupal/clearcache')
122
123
124
125
126
127

    # 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
128
    except ConfigException:
129
130
131
132
        try:
            # use .kube directory
            # for local development
            config.load_kube_config()
Lionel Walter's avatar
Lionel Walter committed
133
        except ConfigException:
134
135
136
            app.logger.error("No kubernetes cluster defined")

    return app