|
| 1 | +import dataclasses |
| 2 | +import pydantic |
| 3 | +import requests |
| 4 | + |
| 5 | +from keep.contextmanager.contextmanager import ContextManager |
| 6 | +from keep.exceptions.provider_exception import ProviderException |
| 7 | +from keep.providers.base.base_provider import BaseProvider |
| 8 | +from keep.providers.models.provider_config import ProviderConfig |
| 9 | + |
| 10 | +@pydantic.dataclasses.dataclass |
| 11 | +class FlashdutyProviderAuthConfig: |
| 12 | + """Flashduty authentication configuration.""" |
| 13 | + |
| 14 | + integration_key: str = dataclasses.field( |
| 15 | + metadata= { |
| 16 | + "required": True, |
| 17 | + "description": "Flashduty integration key", |
| 18 | + "sensitive": True, |
| 19 | + } |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +class FlashdutyProvider(BaseProvider): |
| 24 | + """Create incident in Flashduty.""" |
| 25 | + |
| 26 | + PROVIDER_DISPLAY_NAME = "Flashduty" |
| 27 | + PROVIDER_CATEGORY = ["Incident Management"] |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, context_manager: ContextManager, provider_id: str, config: ProviderConfig |
| 31 | + ): |
| 32 | + super().__init__(context_manager, provider_id, config) |
| 33 | + |
| 34 | + def validate_config(self): |
| 35 | + self.authentication_config = FlashdutyProviderAuthConfig( |
| 36 | + **self.config.authentication |
| 37 | + ) |
| 38 | + |
| 39 | + def dispose(self): |
| 40 | + """ |
| 41 | + No need to dispose of anything, so just do nothing. |
| 42 | + """ |
| 43 | + pass |
| 44 | + |
| 45 | + def _notify( |
| 46 | + self, |
| 47 | + title: str = "", |
| 48 | + event_status: str = "", |
| 49 | + description: str = "", |
| 50 | + alert_key: str = "", |
| 51 | + labels: dict = {} |
| 52 | + ): |
| 53 | + """ |
| 54 | + Create incident Flashduty using the Flashduty API |
| 55 | +
|
| 56 | + https://docs.flashcat.cloud/en/flashduty/custom-alert-integration-guide?nav=01JCQ7A4N4WRWNXW8EWEHXCMF5 |
| 57 | +
|
| 58 | + Args: |
| 59 | + title (str): The title of the incident |
| 60 | + event_status (str): The status of the incident, one of: Info, Warning, Critical, Ok |
| 61 | + description (str): The description of the incident |
| 62 | + alert_key (str): Alert identifier, used to update or automatically recover existing alerts. If you're reporting a recovery event, this value must exist. |
| 63 | + labels (dict): The labels of the incident |
| 64 | + """ |
| 65 | + |
| 66 | + self.logger.info("Notifying incident to Flashduty") |
| 67 | + if not title: |
| 68 | + raise ProviderException("Title is required") |
| 69 | + if not event_status: |
| 70 | + raise ProviderException("Event status is required") |
| 71 | + |
| 72 | + body = { |
| 73 | + "title": title, |
| 74 | + "event_status": event_status, |
| 75 | + "description": description, |
| 76 | + "alert_key": alert_key, |
| 77 | + "labels": labels, |
| 78 | + } |
| 79 | + |
| 80 | + headers = { |
| 81 | + "Content-Type": "application/json", |
| 82 | + } |
| 83 | + resp = requests.post( |
| 84 | + url=f"https://api.flashcat.cloud/event/push/alert/standard?integration_key={self.authentication_config.integration_key}", json=body, headers=headers |
| 85 | + ) |
| 86 | + assert resp.status_code == 200 |
| 87 | + self.logger.info("Alert message notified to Flashduty") |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + # Output test messages |
| 92 | + import logging |
| 93 | + |
| 94 | + logging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler()]) |
| 95 | + context_manager = ContextManager( |
| 96 | + tenant_id="singletenant", |
| 97 | + workflow_id="test", |
| 98 | + ) |
| 99 | + # Load environment variables |
| 100 | + import os |
| 101 | + |
| 102 | + integration_key = os.environ.get("INTEGRATION_KEY") |
| 103 | + assert integration_key |
| 104 | + |
| 105 | + # Initalize the provider and provider config |
| 106 | + config = ProviderConfig( |
| 107 | + description="Flashduty Output Provider", |
| 108 | + authentication={"integration_key": integration_key}, |
| 109 | + ) |
| 110 | + provider = FlashdutyProvider( |
| 111 | + context_manager, provider_id="flashduty-test", config=config |
| 112 | + ) |
| 113 | + provider.notify( |
| 114 | + title="Test incident", |
| 115 | + event_status="Info", |
| 116 | + description="Test description", |
| 117 | + alert_key="1234567890", |
| 118 | + labels={"service": "10.10.10.10"}, |
| 119 | + ) |
| 120 | + |
0 commit comments