2020-04-18 23:18:50 -07:00
|
|
|
"""Convenience functions for webhooks."""
|
|
|
|
|
2021-12-14 22:59:05 -07:00
|
|
|
# Standard Library
|
|
|
|
import typing as t
|
|
|
|
|
2020-04-18 23:18:50 -07:00
|
|
|
# Project
|
2021-09-07 22:58:39 -07:00
|
|
|
from hyperglass.exceptions.private import UnsupportedError
|
2020-04-18 23:18:50 -07:00
|
|
|
|
2021-12-14 22:59:05 -07:00
|
|
|
# Local
|
|
|
|
from ._base import BaseExternal
|
|
|
|
from .slack import SlackHook
|
|
|
|
from .generic import GenericHook
|
|
|
|
from .msteams import MSTeams
|
|
|
|
|
|
|
|
if t.TYPE_CHECKING:
|
|
|
|
# Project
|
|
|
|
from hyperglass.models.config.logging import Http
|
|
|
|
|
2020-04-18 23:18:50 -07:00
|
|
|
PROVIDER_MAP = {
|
2020-04-21 03:29:42 -07:00
|
|
|
"generic": GenericHook,
|
2020-06-26 12:23:11 -07:00
|
|
|
"msteams": MSTeams,
|
|
|
|
"slack": SlackHook,
|
2020-04-18 23:18:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class Webhook(BaseExternal):
|
|
|
|
"""Get webhook for provider name."""
|
|
|
|
|
2021-12-14 22:59:05 -07:00
|
|
|
def __new__(cls: "Webhook", config: "Http") -> "BaseExternal":
|
2020-04-18 23:18:50 -07:00
|
|
|
"""Return instance for correct provider handler."""
|
|
|
|
try:
|
2020-04-21 03:29:42 -07:00
|
|
|
provider_class = PROVIDER_MAP[config.provider]
|
|
|
|
return provider_class(config)
|
2022-12-24 17:53:05 -05:00
|
|
|
except KeyError as err:
|
2021-09-07 22:58:39 -07:00
|
|
|
raise UnsupportedError(
|
2021-12-06 17:12:30 -07:00
|
|
|
message="{p} is not yet supported as a webhook target.",
|
|
|
|
p=config.provider.title(),
|
2022-12-24 17:53:05 -05:00
|
|
|
) from err
|