Skip to content

Commit

Permalink
[DPE-2415] sync libraries and update src as necessary (#240)
Browse files Browse the repository at this point in the history
## Problem 
After implementing backups on K8s the libraries in each charm are out of
sync.

## Solution
Update the library code and update the charm to support these new
changes

## Other changes
Testing versions and requirements updates to solve depency issues in
tests

## Future PRs
The backup library has had substantial changes and so the old test suite
is no longer supported. In a future PR these will be added.
Failing TLS tests are resolved in #242 

## do NOT review these files
- `lib/charms/mongodb/v0/helpers.py` - copy and paste from k8s charm
- `lib/charms/mongodb/v0/mongodb_backups.py` - copy and paste from VM
charm
- `tests/unit/test_mongodb_backups.py` - removed as is now out of date,
will be rewritten in a follow up PR
  • Loading branch information
MiaAltieri authored Sep 6, 2023
1 parent 82d0f37 commit 0a959cd
Show file tree
Hide file tree
Showing 8 changed files with 570 additions and 1,033 deletions.
60 changes: 56 additions & 4 deletions lib/charms/mongodb/v0/helpers.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
"""Simple functions, which can be used in both K8s and VM charms."""
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

import json
import logging
import os
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 ActiveStatus, BlockedStatus, StatusBase, WaitingStatus
from ops.model import (
ActiveStatus,
BlockedStatus,
MaintenanceStatus,
StatusBase,
WaitingStatus,
)
from pymongo.errors import AutoReconnect, ServerSelectionTimeoutError

# The unique Charmhub library identifier, never change it
Expand All @@ -21,7 +27,7 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 7
LIBPATCH = 8


# path to store mongodb ketFile
Expand Down Expand Up @@ -194,3 +200,49 @@ def copy_licenses_to_unit():
subprocess.check_output(
"cp -r /snap/charmed-mongodb/current/licenses/* src/licenses", shell=True
)


_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)
return pbm_status["running"] if "running" in pbm_status else ""


def process_pbm_status(pbm_status: str) -> StatusBase:
"""Parses current pbm operation and returns unit status."""
current_op = current_pbm_op(pbm_status)
# no operations are currently running with pbm
if current_op == {}:
return ActiveStatus("")

if current_op["type"] == "backup":
backup_id = current_op["name"]
return MaintenanceStatus(f"backup started/running, backup id:'{backup_id}'")

if current_op["type"] == "restore":
backup_id = current_op["name"]
return MaintenanceStatus(f"restore started/running, backup id:'{backup_id}'")

if current_op["type"] == "resync":
return WaitingStatus("waiting to sync s3 configurations.")

return ActiveStatus()
Loading

0 comments on commit 0a959cd

Please sign in to comment.