From d99bb7b8bc24b4b4d3e5292cf3e39566fe353535 Mon Sep 17 00:00:00 2001 From: Vadim Stepanov Date: Mon, 17 Apr 2023 13:12:21 +0100 Subject: [PATCH] PD migrator: ignore 404s on delete + delete ONCALL_DEFAULT_CONTACT_METHOD (#1764) --- tools/pagerduty-migrator/README.md | 12 ++---------- tools/pagerduty-migrator/migrator/__main__.py | 4 ++-- tools/pagerduty-migrator/migrator/config.py | 3 --- .../pagerduty-migrator/migrator/oncall_api_client.py | 7 ++++++- tools/pagerduty-migrator/migrator/report.py | 4 ++-- .../migrator/resources/notification_rules.py | 9 ++------- 6 files changed, 14 insertions(+), 25 deletions(-) diff --git a/tools/pagerduty-migrator/README.md b/tools/pagerduty-migrator/README.md index 6a1b0a19..f0f38222 100644 --- a/tools/pagerduty-migrator/README.md +++ b/tools/pagerduty-migrator/README.md @@ -79,30 +79,22 @@ docker run --rm \ -e PAGERDUTY_API_TOKEN="" \ -e ONCALL_API_URL="" \ -e ONCALL_API_TOKEN="" \ --e ONCALL_DEFAULT_CONTACT_METHOD="sms" \ -e MODE="migrate" \ pd-oncall-migrator ``` -### Migrate unsupported user notification rules - -It's possible to specify a default contact method type for user notification rules that cannot be migrated as-is by -changing the `ONCALL_DEFAULT_CONTACT_METHOD` env variable. -Options are: `email`, `sms`, `phone_call`, `slack`, `telegram`, `mobile_app` (default is `email`). - ### Migrate unsupported integration types It's possible to migrate unsupported integration types to [Grafana OnCall incoming webhooks](https://grafana.com/docs/oncall/latest/integrations/available-integrations/configure-webhook/). -by changing UNSUPPORTED_INTEGRATION_TO_WEBHOOKS env variable: +To enable this feature, set env variable `UNSUPPORTED_INTEGRATION_TO_WEBHOOKS` to `true`: ```shell docker run --rm \ -e PAGERDUTY_API_TOKEN="" \ -e ONCALL_API_URL="" \ -e ONCALL_API_TOKEN="" \ --e ONCALL_DEFAULT_CONTACT_METHOD="sms" \ --e MODE="migrate" \ -e UNSUPPORTED_INTEGRATION_TO_WEBHOOKS="true" \ +-e MODE="migrate" \ pd-oncall-migrator ``` diff --git a/tools/pagerduty-migrator/migrator/__main__.py b/tools/pagerduty-migrator/migrator/__main__.py index 086273f0..8905f07c 100644 --- a/tools/pagerduty-migrator/migrator/__main__.py +++ b/tools/pagerduty-migrator/migrator/__main__.py @@ -98,7 +98,7 @@ def main() -> None: rulesets = None if EXPERIMENTAL_MIGRATE_EVENT_RULES: - print("▶ Fetching event rules (rulesets) ...") + print("▶ Fetching event rules (global rulesets)...") rulesets = session.list_all("rulesets") for ruleset in rulesets: rules = session.list_all(f"rulesets/{ruleset['id']}/rules") @@ -173,7 +173,7 @@ def main() -> None: print(TAB + format_integration(integration)) if rulesets is not None: - print("▶ Migrating event rules (rulesets) ...") + print("▶ Migrating event rules (global rulesets)...") for ruleset in rulesets: if not ruleset["flawed_escalation_policies"]: migrate_ruleset(ruleset, escalation_policies, services) diff --git a/tools/pagerduty-migrator/migrator/config.py b/tools/pagerduty-migrator/migrator/config.py index e9d65736..0bf60277 100644 --- a/tools/pagerduty-migrator/migrator/config.py +++ b/tools/pagerduty-migrator/migrator/config.py @@ -14,9 +14,6 @@ ONCALL_API_URL = urljoin( ) ONCALL_DELAY_OPTIONS = [1, 5, 15, 30, 60] -ONCALL_DEFAULT_CONTACT_METHOD = "notify_by_" + os.getenv( - "ONCALL_DEFAULT_CONTACT_METHOD", default="email" -) PAGERDUTY_TO_ONCALL_CONTACT_METHOD_MAP = { "sms_contact_method": "notify_by_sms", "phone_contact_method": "notify_by_phone_call", diff --git a/tools/pagerduty-migrator/migrator/oncall_api_client.py b/tools/pagerduty-migrator/migrator/oncall_api_client.py index 59056793..183253f3 100644 --- a/tools/pagerduty-migrator/migrator/oncall_api_client.py +++ b/tools/pagerduty-migrator/migrator/oncall_api_client.py @@ -73,7 +73,12 @@ def create(path: str, payload: dict) -> dict: def delete(path: str) -> None: - api_call("delete", path) + try: + api_call("delete", path) + except requests.exceptions.HTTPError as e: + # ignore 404s on delete so deleting resources manually while running the script doesn't break it + if e.response.status_code != 404: + raise def update(path: str, payload: dict) -> dict: diff --git a/tools/pagerduty-migrator/migrator/report.py b/tools/pagerduty-migrator/migrator/report.py index 69294d0a..86eabcf3 100644 --- a/tools/pagerduty-migrator/migrator/report.py +++ b/tools/pagerduty-migrator/migrator/report.py @@ -76,7 +76,7 @@ def format_integration(integration: dict) -> str: else: # check if integration not supported, but UNSUPPORTED_INTEGRATION_TO_WEBHOOKS set if integration.get("converted_to_webhook", False): - result = "{} {} – Webhook integration will be created, Grafana OnCall not support this type directly ".format( + result = "{} {} – cannot find appropriate Grafana OnCall integration type, integration will be migrated with type 'webhook'".format( WARNING_SIGN, result ) else: @@ -187,7 +187,7 @@ def format_ruleset(ruleset: dict) -> str: def ruleset_report(rulesets: list[dict]) -> str: - result = "Event rules (rulesets) report:" + result = "Event rules (global rulesets) report:" for ruleset in sorted( rulesets, diff --git a/tools/pagerduty-migrator/migrator/resources/notification_rules.py b/tools/pagerduty-migrator/migrator/resources/notification_rules.py index 7a26d71f..2ae78ff3 100644 --- a/tools/pagerduty-migrator/migrator/resources/notification_rules.py +++ b/tools/pagerduty-migrator/migrator/resources/notification_rules.py @@ -1,10 +1,7 @@ import copy from migrator import oncall_api_client -from migrator.config import ( - ONCALL_DEFAULT_CONTACT_METHOD, - PAGERDUTY_TO_ONCALL_CONTACT_METHOD_MAP, -) +from migrator.config import PAGERDUTY_TO_ONCALL_CONTACT_METHOD_MAP from migrator.utils import remove_duplicates, transform_wait_delay @@ -76,10 +73,8 @@ def transform_notification_rule( notification_rule: dict, delay: int, user_id: str ) -> list[dict]: contact_method_type = notification_rule["contact_method"]["type"] + oncall_type = PAGERDUTY_TO_ONCALL_CONTACT_METHOD_MAP[contact_method_type] - oncall_type = PAGERDUTY_TO_ONCALL_CONTACT_METHOD_MAP.get( - contact_method_type, ONCALL_DEFAULT_CONTACT_METHOD - ) notify_rule = {"user_id": user_id, "type": oncall_type, "important": False} if not delay: