Example #1
0
    def put(self, name):
        params = request.json.get('params')
        if not isinstance(params, dict):
            abort(400, "type of 'params' must be dict")

        is_test = params.get('test', False)
        if is_test:
            del params['test']
            db_type = params.get('type')
            checker_class = CHECKERS.get(db_type, None)
            if checker_class is None:
                abort(400, f"Unknown integration type: {db_type}")
            checker = checker_class(**params)
            return {'success': checker.check_connection()}, 200

        integration = get_integration(name)
        if integration is not None:
            abort(400, f"Integration with name '{name}' already exists")

        try:
            if 'enabled' in params:
                params['publish'] = params['enabled']
                del params['enabled']
            ca.config_obj.add_db_integration(name, params)

            model_data_arr = get_all_models_meta_data(ca.naitve_interface,
                                                      ca.custom_models)
            ca.dbw.setup_integration(name)
            if is_test is False:
                ca.dbw.register_predictors(model_data_arr, name)
        except Exception as e:
            log.error(str(e))
            abort(500, f'Error during config update: {str(e)}')

        return '', 200
Example #2
0
    def put(self, name):
        params = {}
        params.update((request.json or {}).get('params', {}))
        params.update(request.form or {})

        if len(params) == 0:
            abort(400, "type of 'params' must be dict")

        # params from FormData will be as text
        for key in ('publish', 'test', 'enabled'):
            if key in params:
                if isinstance(params[key],
                              str) and params[key].lower() in ('false', '0'):
                    params[key] = False
                else:
                    params[key] = bool(params[key])

        files = request.files
        temp_dir = None
        if files is not None:
            temp_dir = tempfile.mkdtemp(prefix='integration_files_')
            for key, file in files.items():
                temp_dir_path = Path(temp_dir)
                file_name = Path(file.filename)
                file_path = temp_dir_path.joinpath(file_name).resolve()
                if temp_dir_path not in file_path.parents:
                    raise Exception(f'Can not save file at path: {file_path}')
                file.save(file_path)
                params[key] = file_path

        is_test = params.get('test', False)
        if is_test:
            del params['test']
            db_type = params.get('type')
            checker_class = CHECKERS.get(db_type, None)
            if checker_class is None:
                abort(400, f"Unknown integration type: {db_type}")
            checker = checker_class(**params)
            if temp_dir is not None:
                shutil.rmtree(temp_dir)
            return {'success': checker.check_connection()}, 200

        integration = get_db_integration(name, request.company_id, False)
        if integration is not None:
            abort(400, f"Integration with name '{name}' already exists")

        try:
            if 'enabled' in params:
                params['publish'] = params['enabled']
                del params['enabled']
            add_db_integration(name, params, request.company_id)

            model_data_arr = []
            for model in request.model_interface.get_models():
                if model['status'] == 'complete':
                    try:
                        model_data_arr.append(
                            request.model_interface.get_model_data(
                                model['name']))
                    except Exception:
                        pass

            if is_test is False and params.get('publish', False) is True:
                model_data_arr = []
                for model in request.model_interface.get_models():
                    if model['status'] == 'complete':
                        try:
                            model_data_arr.append(
                                request.model_interface.get_model_data(
                                    model['name']))
                        except Exception:
                            pass
                DatabaseWrapper(request.company_id).setup_integration(name)
                DatabaseWrapper(request.company_id).register_predictors(
                    model_data_arr, name)
        except Exception as e:
            log.error(str(e))
            if temp_dir is not None:
                shutil.rmtree(temp_dir)
            abort(500, f'Error during config update: {str(e)}')

        if temp_dir is not None:
            shutil.rmtree(temp_dir)
        return '', 200