Add handling of additonal statuses to guard

Add handling of additional statuses to allow charms to set
waiting, maintenance etc. Also fix bug due to msg missing
from exception

Change-Id: Ib7dee525c6924e4d9f6fb17e0a0181642b32b9b5
This commit is contained in:
Liam Young 2023-02-24 10:54:41 +00:00
parent a086398d86
commit 14c10fe060

View File

@ -24,6 +24,8 @@ from ops.charm import (
)
from ops.model import (
BlockedStatus,
MaintenanceStatus,
WaitingStatus,
)
logger = logging.getLogger(__name__)
@ -35,9 +37,29 @@ class GuardExceptionError(Exception):
pass
class BlockedExceptionError(Exception):
class BaseStatusExceptionError(Exception):
"""Charm is blocked."""
def __init__(self, msg):
self.msg = msg
super().__init__(self.msg)
class BlockedExceptionError(BaseStatusExceptionError):
"""Charm is blocked."""
pass
class MaintenanceExceptionError(BaseStatusExceptionError):
"""Charm is performing maintenance."""
pass
class WaitingExceptionError(BaseStatusExceptionError):
"""Charm is waiting."""
pass
@ -79,6 +101,18 @@ def guard(
"Charm is blocked in section '%s' due to '%s'", section, str(e)
)
charm.status.set(BlockedStatus(e.msg))
except WaitingExceptionError as e:
logger.warning(
"Charm is waiting in section '%s' due to '%s'", section, str(e)
)
charm.status.set(WaitingStatus(e.msg))
except MaintenanceExceptionError as e:
logger.warning(
"Charm performing maintenance in section '%s' due to '%s'",
section,
str(e),
)
charm.status.set(MaintenanceStatus(e.msg))
except Exception as e:
# something else went wrong
if handle_exception: