Skip to content

Commit

Permalink
fix: mongodb (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahargl authored Dec 13, 2023
1 parent 29a6c2c commit addf526
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions keep/providers/mongodb_provider/mongodb_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import dataclasses
import json
import os

import pydantic
Expand Down Expand Up @@ -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,
)


Expand Down Expand Up @@ -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()
Expand All @@ -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):
Expand Down Expand Up @@ -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(
Expand Down

1 comment on commit addf526

@vercel
Copy link

@vercel vercel bot commented on addf526 Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

keep – ./

keep-eight.vercel.app
platform.keephq.dev
keep-keephq.vercel.app
keep-git-main-keephq.vercel.app

Please sign in to comment.