merge develop into main

This commit is contained in:
Milan Kolar 2021-05-04 17:07:32 +02:00
parent f82ed5e127
commit a8ac7cd538
418 changed files with 26017 additions and 4665 deletions

View file

@ -14,7 +14,12 @@ from pathlib import Path
import platform
from pymongo import MongoClient
from pymongo.errors import ServerSelectionTimeoutError, InvalidURI
from pymongo.errors import (
ServerSelectionTimeoutError,
InvalidURI,
ConfigurationError,
OperationFailure
)
def decompose_url(url: str) -> Dict:
@ -115,30 +120,20 @@ def validate_mongo_connection(cnx: str) -> (bool, str):
parsed = urlparse(cnx)
if parsed.scheme not in ["mongodb", "mongodb+srv"]:
return False, "Not mongodb schema"
# we have mongo connection string. Let's try if we can connect.
try:
components = decompose_url(cnx)
except RuntimeError:
return False, f"Invalid port specified."
mongo_args = {
"host": compose_url(**components),
"serverSelectionTimeoutMS": 2000
}
port = components.get("port")
if port is not None:
mongo_args["port"] = int(port)
try:
client = MongoClient(**mongo_args)
client = MongoClient(
cnx,
serverSelectionTimeoutMS=2000
)
client.server_info()
client.close()
except ServerSelectionTimeoutError as e:
return False, f"Cannot connect to server {cnx} - {e}"
except ValueError:
return False, f"Invalid port specified {parsed.port}"
except InvalidURI as e:
return False, str(e)
except (ConfigurationError, OperationFailure, InvalidURI) as exc:
return False, str(exc)
else:
return True, "Connection is successful"