Skip to content

Commit

Permalink
feat: pd service level operations (#2522)
Browse files Browse the repository at this point in the history
Signed-off-by: 35C4n0r <[email protected]>
Co-authored-by: Tal <[email protected]>
Co-authored-by: Matvey Kukuy <[email protected]>
  • Loading branch information
3 people authored Nov 25, 2024
1 parent bdc8e52 commit 9ccc175
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/providers/documentation/pagerduty-provider.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ To connect Keep to PagerDuty:

- **Routing Key**: Use for event posting via the PagerDuty Events API.
- **API Key**: Use for incident creation and management through the PagerDuty Incidents API.
- **Service Id** (Optional): If provided, keep operates within the service's scope.
- **OAuth2**: Token management handled automatically by Keep.

<Frame>
Expand Down
2 changes: 2 additions & 0 deletions keep/api/models/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ def from_db_incident(cls, db_incident: "Incident"):
assignee=db_incident.assignee,
services=db_incident.affected_services or [],
rule_fingerprint=db_incident.rule_fingerprint,
fingerprint=db_incident.fingerprint,
same_incident_in_the_past_id=db_incident.same_incident_in_the_past_id,
merged_into_incident_id=db_incident.merged_into_incident_id,
merged_by=db_incident.merged_by,
Expand Down Expand Up @@ -550,6 +551,7 @@ def to_db_incident(self) -> "Incident":
is_predicted=self.is_predicted,
is_confirmed=self.is_confirmed,
rule_fingerprint=self.rule_fingerprint,
fingerprint=self.fingerprint,
same_incident_in_the_past_id=self.same_incident_in_the_past_id,
merged_into_incident_id=self.merged_into_incident_id,
merged_by=self.merged_by,
Expand Down
33 changes: 26 additions & 7 deletions keep/providers/pagerduty_provider/pagerduty_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from keep.providers.models.provider_config import ProviderConfig, ProviderScope
from keep.providers.providers_factory import ProvidersFactory


# Todo: think about splitting in to PagerdutyIncidentsProvider and PagerdutyAlertsProvider
# Read this: https://community.pagerduty.com/forum/t/create-incident-using-python/3596/3

Expand Down Expand Up @@ -59,6 +60,14 @@ class PagerdutyProviderAuthConfig:
},
default="",
)
service_id: str | None = dataclasses.field(
metadata={
"required": False,
"description": "Service Id (if provided, keep will only operate on this service)",
"sensitive": False,
},
default=None,
)


class PagerdutyProvider(BaseTopologyProvider, BaseIncidentProvider):
Expand Down Expand Up @@ -505,7 +514,14 @@ def setup_incident_webhook(
"incident.triggered",
"incident.unacknowledged",
],
"filter": {"type": "account_reference"},
"filter": (
{
"type": "service_reference",
"id": self.authentication_config.service_id,
}
if self.authentication_config.service_id
else {"type": "account_reference"}
),
},
}
if webhook_exists:
Expand Down Expand Up @@ -681,14 +697,17 @@ def __get_all_incidents_or_alerts(self, incident_id: str = None):
url += f"/{incident_id}/alerts"
include = ["teams", "services"]
resource = "alerts"
params = {
"include[]": include,
"offset": offset,
"limit": 100,
}
if not incident_id and self.authentication_config.service_id:
params["service_ids[]"] = [self.authentication_config.service_id]
response = requests.get(
url=url,
headers=self.__get_headers(),
params={
"include[]": include,
"offset": offset,
"limit": 100,
},
params=params,
)
response.raise_for_status()
response = response.json()
Expand All @@ -699,7 +718,7 @@ def __get_all_incidents_or_alerts(self, incident_id: str = None):
paginated_response.extend(response.get(resource, []))
self.logger.info("Fetched incidents or alerts", extra={"offset": offset})
# No more results
if response.get("more", False) == False:
if not response.get("more", False):
self.logger.info("No more incidents or alerts")
break
self.logger.info(
Expand Down

0 comments on commit 9ccc175

Please sign in to comment.