Commit 5ad89c77 authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Refactor delete service

parent a7f53438
Pipeline #34094 passed with stages
in 2 minutes and 7 seconds
......@@ -14,13 +14,17 @@
# 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/>.
import os
from import_api_app.app import app
from flask_restful import Resource
from import_api_app.helm import Helm
from import_api_app.helpers.error import ImportApiError
class DeleteService(Resource):
class DeleteService:
def __init__(self, logger, namespace, root_path):
self.namespace = namespace
self.root_path = root_path
self.logger = logger
self.helm = Helm()
def delete_record(self, record_id, session_id, dryrun):
set_values = {
......@@ -29,13 +33,11 @@ class DeleteService(Resource):
'deleteId': record_id
}
try:
self.do_helm_install(set_values, dryrun)
return self.do_helm_install(set_values, dryrun)
except Exception as ex:
message = str(ex)
app.logger.error(message)
self.logger.error(message)
raise ImportApiError(message)
return -1
return 0
def delete_recordset(self, recordset_id, session_id, dryrun):
set_values = {
......@@ -44,13 +46,11 @@ class DeleteService(Resource):
'deleteId': recordset_id
}
try:
self.do_helm_install(set_values, dryrun)
return self.do_helm_install(set_values, dryrun)
except Exception as ex:
message = str(ex)
app.logger.error(message)
self.logger.error(message)
raise ImportApiError(message)
return -1
return 0
def delete_institution(self, institution_id, session_id, dryrun):
set_values = {
......@@ -59,34 +59,30 @@ class DeleteService(Resource):
'deleteId': institution_id,
}
try:
self.do_helm_install(set_values, dryrun)
return self.do_helm_install(set_values, dryrun)
except Exception as ex:
message = str(ex)
app.logger.error(message)
self.logger.error(message)
raise ImportApiError(message)
return -1
return 0
@staticmethod
def do_helm_install(set_values, dryrun):
app.logger.debug(
def do_helm_install(self, set_values, dryrun):
self.logger.debug(
'calling delete service: type=' +
set_values['deleteObject'] +
' / id=' + set_values['deleteId'] +
' / session=' + set_values['sessionId'])
jobArgs = [
job_args = [
'--' + set_values['deleteObject'] + '-filter',
set_values['deleteId'],
set_values['sessionId']
]
if dryrun:
jobArgs.append('--dry-run')
jobArgs = {'jobArgs': jobArgs}
helm = Helm()
response = helm.install( # noqa: F841
chart=os.path.join(app.root_path, "charts", 'dd-marker-prod'),
job_args.append('--dry-run')
job_args = {'jobArgs': job_args}
return self.helm.install( # noqa: F841
chart=os.path.join(self.root_path, "charts", 'dd-marker-prod'),
name=set_values['sessionId'] + '-deleter',
namespace=app.config['NAMESPACE'],
set_values=jobArgs,
namespace=self.namespace,
set_values=job_args,
fail_on_err=False
)
......@@ -14,11 +14,16 @@
# 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/>.
from flask_restful import Resource, request
from import_api_app.resources.delete_service import DeleteService
from import_api_app.app import app
from import_api_app.helpers.delete_service import DeleteService
class DeleteInstitution(Resource):
def __init__(self):
self.delete_service = DeleteService(app.logger, app.config['NAMESPACE'], app.root_path)
def post(self, session_id, dryrun=False):
"""
Tells the import process deleter to delete a institution
......@@ -59,7 +64,5 @@ class DeleteInstitution(Resource):
enum: ['SUCCESS', 'FAILURE']
"""
# get institution_id parameter from request-body
institution_id = request.json['institution_id']
return DeleteService.delete_institution(DeleteService, institution_id, session_id, dryrun)
return self.delete_service.delete_institution(institution_id, session_id, dryrun)
......@@ -14,11 +14,16 @@
# 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/>.
from flask_restful import Resource, request
from import_api_app.resources.delete_service import DeleteService
from import_api_app.app import app
from import_api_app.helpers.delete_service import DeleteService
class DeleteRecord(Resource):
def __init__(self):
self.delete_service = DeleteService(app.logger, app.config['NAMESPACE'], app.root_path)
def post(self, session_id, dryrun=False):
"""
Tells the import process deleter to delete a record
......@@ -59,7 +64,5 @@ class DeleteRecord(Resource):
enum: ['SUCCESS', 'FAILURE']
"""
# get record_id parameter from request-body
record_id = request.json['record_id']
return DeleteService.delete_record(DeleteService, record_id, session_id, dryrun)
return self.delete_service.delete_record(record_id, session_id, dryrun)
......@@ -14,11 +14,16 @@
# 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/>.
from flask_restful import Resource, request
from import_api_app.resources.delete_service import DeleteService
from import_api_app.app import app
from import_api_app.helpers.delete_service import DeleteService
class DeleteRecordset(Resource):
def __init__(self):
self.delete_service = DeleteService(app.logger, app.config['NAMESPACE'], app.root_path)
def post(self, session_id, dryrun=False):
"""
Tells the import process deleter to delete a recordset
......@@ -59,7 +64,5 @@ class DeleteRecordset(Resource):
enum: ['SUCCESS', 'FAILURE']
"""
# get recordset_id parameter from request-body
recordset_id = request.json['recordset_id']
return DeleteService.delete_recordset(DeleteService, recordset_id, session_id, dryrun)
return self.delete_service.delete_recordset(recordset_id, session_id, dryrun)
Markdown is supported
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