From 7324256178ed97cb245f336e0f121fd54807b057 Mon Sep 17 00:00:00 2001 From: JCMOSCON1976 <167822375+JCMOSCON1976@users.noreply.github.com> Date: Wed, 14 Aug 2024 05:08:23 -0400 Subject: [PATCH] fix(code):Fix the num_changes logic that was causing false deletions (#268) * fix(code):Fix the num_changes logic that was causing false deletions * fix(code):Remove comments. * fix(code):Add comments --------- Co-authored-by: Julio Cezar Moscon --- .../scripts/api/Workday/Workday.py | 5 ++ .../scripts/api/XMatters/XMatters.py | 58 +++++++++---------- .../scripts/workday_xmatters.py | 25 ++++---- 3 files changed, 44 insertions(+), 44 deletions(-) diff --git a/jobs/eam-integrations/scripts/api/Workday/Workday.py b/jobs/eam-integrations/scripts/api/Workday/Workday.py index ccb4a8a..707d2f0 100644 --- a/jobs/eam-integrations/scripts/api/Workday/Workday.py +++ b/jobs/eam-integrations/scripts/api/Workday/Workday.py @@ -118,6 +118,11 @@ def get_users(): users = [user for user in results["Report_Entry"] if not (user.get("User_Home_Country", "") == "" and user.get("User_Home_Postal_Code", "") == "")] + + # TODO: Avaliate if the code below makes sense after the go-live + # The fields below are no longer wanted to go to XMatters. + # Setting them to empty string will force an update on all the user records + # We may want to delete this code after the go-live for user in users: user['User_Cost_Center'] = '' user['User_Manager_Email_Address'] = '' diff --git a/jobs/eam-integrations/scripts/api/XMatters/XMatters.py b/jobs/eam-integrations/scripts/api/XMatters/XMatters.py index b63925c..0ef4f5f 100644 --- a/jobs/eam-integrations/scripts/api/XMatters/XMatters.py +++ b/jobs/eam-integrations/scripts/api/XMatters/XMatters.py @@ -482,19 +482,18 @@ def add_new_sites(wd_sites, xm_sites, xm_sites_inactive, limit): logger.debug("WD site %s found in XMatters! No action." % wd_site) xm_sites_in_wd[wd_site] = 1 elif wd_site in xm_sites_inactive: - logger.info("WD site %s INACTIVE in XMatters! Reactivating." % wd_site) - set_site_active(xm_sites_inactive[wd_site]) - num_changes +=1 + if num_changes < limit: + logger.info("WD site %s INACTIVE in XMatters! Reactivating." % wd_site) + set_site_active(xm_sites_inactive[wd_site]) + num_changes += 1 else: - logger.info( - "WD site %s NOT found in XMatters! Adding to XMatters." % wd_site - ) - add_site(wd_sites[wd_site]) - num_changes += 1 - if num_changes >= limit: - logger.info(f"Number of added or activated sites:{num_changes}") - return xm_sites_in_wd - + if num_changes < limit: + logger.info( + "WD site %s NOT found in XMatters! Adding to XMatters." % wd_site + ) + add_site(wd_sites[wd_site]) + num_changes += 1 + logger.info(f"Number of added or activated sites:{num_changes}") return xm_sites_in_wd @@ -505,17 +504,14 @@ def delete_sites(xm_sites, xm_sites_in_wd, limit): num_changes = 0 for site in xm_sites: if site not in xm_sites_in_wd and site not in ["Default Site", "Mountain View Office"]: - logger.info( - "Site %s not in WorkDay. INACTIVATING %s from XMatters" - % (site, xm_sites[site]) - ) - set_site_inactive(xm_sites[site]) - num_changes +=1 - - if num_changes >= limit: - logger.info(f"Number of sites that were inactivated:{num_changes}") - return True - + if num_changes < limit: + logger.info( + "Site %s not in WorkDay. INACTIVATING %s from XMatters" + % (site, xm_sites[site]) + ) + set_site_inactive(xm_sites[site]) + num_changes +=1 + logger.info(f"Number of sites that were inactivated:{num_changes}") return True @@ -741,13 +737,13 @@ def delete_users(xm_users, users_seen_in_wd, limit): # let's just skip any usernames that don't look like emails continue if user not in users_seen_in_wd: - logger.info("User %s not seen in workday, will delete from xmatters" % user) - actual_person_delete(user) - num_changes +=1 - if num_changes >= limit: - logger.info(f"Number of updated users:{num_changes}") - return True - - logger.info(f"Number of updated users:{num_changes}") + if num_changes < limit: + logger.info("User %s not seen in workday, will delete from xmatters" % user) + actual_person_delete(user) + num_changes +=1 + else: + logger.info("User %s not seen in workday" % user) + + logger.info(f"Number of deleted users:{num_changes}") return True diff --git a/jobs/eam-integrations/scripts/workday_xmatters.py b/jobs/eam-integrations/scripts/workday_xmatters.py index 965551f..ea40782 100644 --- a/jobs/eam-integrations/scripts/workday_xmatters.py +++ b/jobs/eam-integrations/scripts/workday_xmatters.py @@ -152,12 +152,13 @@ def iterate_thru_wd_users(wd_users, xm_users, xm_sites, limit): wd_users_seen[user["User_Email_Address"]] = 1 if user["User_Email_Address"] in xm_users: logger.debug("User %s found in XM" % user["User_Email_Address"]) - if not user_data_matches(user, xm_users[user["User_Email_Address"]]): - logger.debug("USER DATA NO MATCHES!") - XMatters.update_user( - user, xm_users[user["User_Email_Address"]], xm_sites - ) - num_changes +=1 + if not user_data_matches(user, xm_users[user["User_Email_Address"]]): + if num_changes < limit: + logger.debug("USER DATA NO MATCHES!") + XMatters.update_user( + user, xm_users[user["User_Email_Address"]], xm_sites + ) + num_changes +=1 else: logger.debug("%s good" % user["User_Email_Address"]) else: @@ -166,10 +167,7 @@ def iterate_thru_wd_users(wd_users, xm_users, xm_sites, limit): xm_add_users.append(user) # time.sleep(5) - if num_changes >= limit: - logger.info(f"Number of updated users:{num_changes}") - return wd_users_seen, xm_add_users - + logger.info(f"Number of updated users:{num_changes}") return wd_users_seen, xm_add_users @@ -240,11 +238,11 @@ if __name__ == "__main__": # get the new style (zipcodes) sites from the user list wd_sites = get_wd_sites_from_users(wd_users) - + logger.info(f"Number of XMatters sites: {len(xm_sites)}") logger.info(f"Number of Workday sites: {len(wd_sites)}") logger.info(f"Number of Workday users: {len(wd_users)}") - + # # get list of sites from workday users # wd_sites = Workday.get_sites() @@ -292,9 +290,10 @@ if __name__ == "__main__": # iterate through xmatters users who aren't marked-as-seen # remove from xmatters - XMatters.delete_users(xm_users, users_seen_in_workday,args.max_limit) + XMatters.delete_users(xm_users, users_seen_in_workday, args.max_limit) for user in xm_add_users[:args.max_limit]: + logger.info(f"Adding user: {user['User_Email_Address']}") XMatters.add_user(user, xm_sites) logger.info(f"Number of users added:{len(xm_add_users[:args.max_limit])}")