From e2392988c8e2b94028c1d077fc3f0db91a097ada Mon Sep 17 00:00:00 2001 From: Vadim Stepanov Date: Fri, 24 Feb 2023 11:12:10 +0000 Subject: [PATCH] PD migrator: handle deactivated users in PD when migrating schedules (#1407) # What this PR does Handle deactivated users in PD when migrating schedules using PD migrator. ## Checklist - [x] Tests updated --- .../migrator/resources/schedules.py | 10 +++++ .../migrator/tests/test_schedules.py | 40 +++++++++++++++---- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/tools/pagerduty-migrator/migrator/resources/schedules.py b/tools/pagerduty-migrator/migrator/resources/schedules.py index 9b4c8a02..d7e959e8 100644 --- a/tools/pagerduty-migrator/migrator/resources/schedules.py +++ b/tools/pagerduty-migrator/migrator/resources/schedules.py @@ -132,6 +132,16 @@ class Schedule: shifts = [] errors = [] for layer in self.layers: + # Check if all users in the layer exist in PD + deactivated_user_ids = [ + user_id for user_id in layer.user_ids if user_id not in user_id_map + ] + if deactivated_user_ids: + errors.append( + f"{layer.name}: User IDs {deactivated_user_ids} not found. The users probably have been deactivated in PagerDuty." + ) + continue + # A single PagerDuty layer can result in multiple OnCall shifts layer_shifts, error = layer.to_oncall_shifts(user_id_map) diff --git a/tools/pagerduty-migrator/migrator/tests/test_schedules.py b/tools/pagerduty-migrator/migrator/tests/test_schedules.py index 386396c3..7d26455d 100644 --- a/tools/pagerduty-migrator/migrator/tests/test_schedules.py +++ b/tools/pagerduty-migrator/migrator/tests/test_schedules.py @@ -2,13 +2,11 @@ import datetime from migrator.resources.schedules import Restriction, Schedule - -class IdentityMap(dict): - def __missing__(self, key): - return key - - -user_id_map = IdentityMap() +user_id_map = { + "USER_ID_1": "USER_ID_1", + "USER_ID_2": "USER_ID_2", + "USER_ID_3": "USER_ID_3", +} def test_merge_restrictions(): @@ -177,6 +175,34 @@ def test_current_or_next_restriction(): ) +def test_deactivated_users(): + pd_schedule = { + "name": "No restrictions", + "time_zone": "Europe/London", + "schedule_layers": [ + { + "name": "Layer 1", + "start": "2023-02-19T19:25:55+00:00", + "end": None, + "rotation_virtual_start": "2023-02-07T19:00:00+00:00", + "rotation_turn_length_seconds": 1209600, + "restrictions": [], + "users": [ + {"user": {"id": "USER_ID_1"}}, + {"user": {"id": "USER_ID_DEACTIVATED"}}, + ], + }, + ], + } + + oncall_schedule, errors = Schedule.from_dict(pd_schedule).to_oncall_schedule( + user_id_map + ) + assert errors == [ + "Layer 1: User IDs ['USER_ID_DEACTIVATED'] not found. The users probably have been deactivated in PagerDuty." + ] + + def test_no_restrictions(): pd_schedule = { "name": "No restrictions",