From 7a9405f2ac097c88f4261e806ddd97cb9f96e9f4 Mon Sep 17 00:00:00 2001 From: Dmitry Ratushnyy Date: Thu, 18 Jan 2024 12:51:34 +0000 Subject: [PATCH] Update helpers lib: add function for k8s charm --- lib/charms/mongodb/v1/helpers.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/charms/mongodb/v1/helpers.py b/lib/charms/mongodb/v1/helpers.py index 7c2e52d06..a1997f231 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 ( @@ -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)