delete_service.py 3.64 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Import API Service
# Copyright (C) 2020-2021 Memobase Project
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
Matthias's avatar
Matthias committed
16
import os
Matthias's avatar
Matthias committed
17
import json
18
19
from import_api_app.helm import Helm
from import_api_app.helpers.error import ImportApiError
Matthias's avatar
Matthias committed
20
21


Jonas Waeber's avatar
Jonas Waeber committed
22
23
24
25
26
27
28
class DeleteService:

    def __init__(self, logger, namespace, root_path):
        self.namespace = namespace
        self.root_path = root_path
        self.logger = logger
        self.helm = Helm()
Matthias's avatar
Matthias committed
29

30
    def delete_record(self, record_id, session_id, dryrun):
Matthias's avatar
Matthias committed
31
32
33
34
35
36
        set_values = {
            'sessionId': session_id,
            'deleteObject': 'record',
            'deleteId': record_id
        }
        try:
Jonas Waeber's avatar
Jonas Waeber committed
37
            return self.do_helm_install(set_values, dryrun)
Matthias's avatar
Matthias committed
38
39
        except Exception as ex:
            message = str(ex)
Jonas Waeber's avatar
Jonas Waeber committed
40
            self.logger.error(message)
Matthias's avatar
Matthias committed
41
            raise ImportApiError(message)
Matthias's avatar
Matthias committed
42

43
    def delete_recordset(self, recordset_id, session_id, dryrun):
Matthias's avatar
Matthias committed
44
45
46
47
48
        set_values = {
            'sessionId': session_id,
            'deleteObject': 'record-set',
            'deleteId': recordset_id
        }
Matthias's avatar
Matthias committed
49
        try:
Jonas Waeber's avatar
Jonas Waeber committed
50
            return self.do_helm_install(set_values, dryrun)
Matthias's avatar
Matthias committed
51
52
        except Exception as ex:
            message = str(ex)
Jonas Waeber's avatar
Jonas Waeber committed
53
            self.logger.error(message)
Matthias's avatar
Matthias committed
54
55
            raise ImportApiError(message)

56
    def delete_institution(self, institution_id, session_id, dryrun):
Matthias's avatar
Matthias committed
57
58
59
        set_values = {
            'sessionId': session_id,
            'deleteObject': 'institution',
60
            'deleteId': institution_id,
Matthias's avatar
Matthias committed
61
62
        }
        try:
Jonas Waeber's avatar
Jonas Waeber committed
63
            return self.do_helm_install(set_values, dryrun)
Matthias's avatar
Matthias committed
64
65
        except Exception as ex:
            message = str(ex)
Jonas Waeber's avatar
Jonas Waeber committed
66
            self.logger.error(message)
Matthias's avatar
Matthias committed
67
68
            raise ImportApiError(message)

Jonas Waeber's avatar
Jonas Waeber committed
69
    def do_helm_install(self, set_values, dryrun):
Matthias's avatar
Matthias committed
70
        self.logger.error(
71
72
73
74
            'calling delete service: type=' +
            set_values['deleteObject'] +
            ' / id=' + set_values['deleteId'] +
            ' / session=' + set_values['sessionId'])
75
76
77
        # We need to handle the fact that we can't use an array as argument
        # value when calling helm. For details see
        # https://github.com/helm/helm/issues/1987#issuecomment-682352463.
78
79
        job_args = ['--' + set_values['deleteObject'] + '-filter',
            set_values['deleteId']]
80
        if dryrun:
81
            job_args.extend(['--dry-run', set_values['sessionId']])
82
        else:
83
            job_args.append(set_values['sessionId'])
84
85
        job_args = {'jobArgs': "'" + "' '".join(job_args) + "'"}
        path_to_charts = os.path.join(self.root_path, "charts", "deletion-marker")
Matthias's avatar
Matthias committed
86
        cp = self.helm.install(  # noqa: F841
87
88
            chart=path_to_charts, 
            values_file=os.path.join(path_to_charts, "helm-values", "dd-marker-" + os.getenv("ENV", "") + ".yaml"),
89
            name=set_values['sessionId'] + '-deleter',
Jonas Waeber's avatar
Jonas Waeber committed
90
91
            namespace=self.namespace,
            set_values=job_args,
Matthias's avatar
Matthias committed
92
93
            fail_on_err=False
        )
Matthias's avatar
Matthias committed
94
95
        retJson = {'returncode': cp.returncode, 'stdout': cp.stdout, 'stderr': cp.stderr}
        return json.dumps(retJson, ensure_ascii=True, indent=2)