From 44cd80a89f3053660416d8e70b2219edc0cf4da7 Mon Sep 17 00:00:00 2001 From: Dmitry Ratushnyy Date: Thu, 18 Jan 2024 12:51:34 +0000 Subject: [PATCH 1/3] Update helpers lib: add function for k8s charm --- lib/charms/mongodb/v1/helpers.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/charms/mongodb/v1/helpers.py b/lib/charms/mongodb/v1/helpers.py index 7c2e52d06..638569073 100644 --- a/lib/charms/mongodb/v1/helpers.py +++ b/lib/charms/mongodb/v1/helpers.py @@ -7,7 +7,7 @@ import secrets import string import subprocess -from typing import List +from typing import List, Optional, Union from charms.mongodb.v0.mongodb import MongoDBConfiguration, MongoDBConnection from ops.model import ( @@ -29,7 +29,7 @@ # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 2 +LIBPATCH = 3 # path to store mongodb ketFile KEY_FILE = "keyFile" @@ -260,6 +260,25 @@ def copy_licenses_to_unit(): ) +_StrOrBytes = Union[str, bytes] + + +def process_pbm_error(error_string: Optional[_StrOrBytes]) -> str: + """Parses pbm error string and returns a user friendly message.""" + message = "couldn't configure s3 backup option" + if not error_string: + return message + if type(error_string) == bytes: + error_string = error_string.decode("utf-8") + if "status code: 403" in error_string: # type: ignore + message = "s3 credentials are incorrect." + elif "status code: 404" in error_string: # type: ignore + message = "s3 configurations are incompatible." + elif "status code: 301" in error_string: # type: ignore + message = "s3 configurations are incompatible." + return message + + def current_pbm_op(pbm_status: str) -> str: """Parses pbm status for the operation that pbm is running.""" pbm_status = json.loads(pbm_status) From 2a57564accd9885ea18dd0c56fd7bea9043c459e Mon Sep 17 00:00:00 2001 From: Dmitry Ratushnyy Date: Thu, 18 Jan 2024 12:58:02 +0000 Subject: [PATCH 2/3] Fix lint error --- lib/charms/mongodb/v1/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/charms/mongodb/v1/helpers.py b/lib/charms/mongodb/v1/helpers.py index 638569073..8080079af 100644 --- a/lib/charms/mongodb/v1/helpers.py +++ b/lib/charms/mongodb/v1/helpers.py @@ -268,7 +268,7 @@ def process_pbm_error(error_string: Optional[_StrOrBytes]) -> str: message = "couldn't configure s3 backup option" if not error_string: return message - if type(error_string) == bytes: + if type(error_string) is bytes: error_string = error_string.decode("utf-8") if "status code: 403" in error_string: # type: ignore message = "s3 credentials are incorrect." From 7a9ca4471496476ee6ee61f3bbfbda04753c2cd2 Mon Sep 17 00:00:00 2001 From: Dmitry Ratushnyy Date: Thu, 18 Jan 2024 16:31:21 +0000 Subject: [PATCH 3/3] Add logging option to startup command only if logging option is not empty --- lib/charms/mongodb/v1/helpers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/charms/mongodb/v1/helpers.py b/lib/charms/mongodb/v1/helpers.py index 8080079af..d3f995716 100644 --- a/lib/charms/mongodb/v1/helpers.py +++ b/lib/charms/mongodb/v1/helpers.py @@ -154,8 +154,9 @@ def get_mongod_args( f"--port={Config.MONGODB_PORT}", "--auditDestination=syslog", # TODO sending logs to syslog until we have a separate mount point for logs f"--auditFormat={Config.AuditLog.FORMAT}", - logging_options, ] + if logging_options: + cmd.extend([logging_options]) if auth: cmd.extend(["--auth"])