From addf52600b255ef7cc0209d6962799f06670ca76 Mon Sep 17 00:00:00 2001 From: Shahar Glazner Date: Wed, 13 Dec 2023 09:44:20 +0200 Subject: [PATCH] fix: mongodb (#628) --- .../mongodb_provider/mongodb_provider.py | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/keep/providers/mongodb_provider/mongodb_provider.py b/keep/providers/mongodb_provider/mongodb_provider.py index 94061b726..9ca9857b1 100644 --- a/keep/providers/mongodb_provider/mongodb_provider.py +++ b/keep/providers/mongodb_provider/mongodb_provider.py @@ -3,6 +3,7 @@ """ import dataclasses +import json import os import pydantic @@ -41,12 +42,12 @@ class MongodbProviderAuthConfig: metadata={"required": False, "description": "Mongo authSource database name"}, default=None, ) - additional_options: dict | None = dataclasses.field( + additional_options: str | None = dataclasses.field( metadata={ "required": False, "description": "Mongo kwargs, these will be passed to MongoClient", }, - default_factory=dict, + default=None, ) @@ -93,6 +94,19 @@ def __generate_client(self): pymongo.MongoClient: MongoDB Client """ # removing all None fields, as mongo will not accept None fields} + if self.authentication_config.additional_options: + try: + self.logger.debug("Casting the additional_options to dict") + additional_options = json.loads( + self.authentication_config.additional_options + ) + self.logger.debug("Successfully casted the additional_options to dict") + except Exception: + self.logger.debug("Failed to cast the additional_options to dict") + raise ValueError("additional_options must be a valid dict") + else: + additional_options = {} + client_conf = { k: v for k, v in self.authentication_config.__dict__.items() @@ -101,9 +115,7 @@ def __generate_client(self): and k != "additional_options" # additional_options will go seperately and k != "database" } # database is not a valid mongo option - client = MongoClient( - **client_conf, **self.authentication_config.additional_options - ) + client = MongoClient(**client_conf, **additional_options) return client def dispose(self): @@ -146,6 +158,7 @@ def _query( "username": os.environ.get("MONGODB_USER"), "password": os.environ.get("MONGODB_PASSWORD"), "database": os.environ.get("MONGODB_DATABASE"), + # "additional_options": '{"retryWrites": false}', } ) context_manager = ContextManager(