pythonpysparktry-exceptgoogle-cloud-identity

Google Cloud Identity API failing when looking membership up of group, how to add try except


I have a method that is reading from a list of groups for an organization and finding the membership details for each of those groups one by one.

I am getting an error:

<HttpError 403 when requesting https://cloudidentity.googleapis.com/v1/groups/040ew0vw43lra72/memberships?view=FULL&alt=json returned "Error(2028): Permission denied for resource groups/040ew0vw43lra72 (or it may not exist).". Details: "Error(2028): Permission denied for resource groups/040ew0vw43lra72 (or it may not exist).">

Pretty much saying this group does not exist, however the API is picking it up the the list of groups under the organization, i try looking up the group by the ID and the email associated and the group isn't in the google groups UI.

This is the method I have created that goes through the Dataframe of group_ids and gets the membership details:

def get_membership_data(service, all_group_df):
    all_member_df = pd.DataFrame()


    try:
        for inx, group in all_group_df.iterrows():
            print(f'Membership details for group {group["name"]}:')
            members = (
                service.groups()
                .memberships()
                .list(parent=group["name"], view="FULL")
                .execute()
            )

            if "memberships" in members:
                for member in members["memberships"]:
                    members_df = pd.DataFrame(index=[6])

                    y = json.dumps(member["preferredMemberKey"])
                    z = json.loads(y)
                    members_df.insert(0, "preferredMemberKey", z["id"])
                    ##print(z['id'])

                    members_df.insert(1, "createTime", member["createTime"])
                    members_df.insert(2, "updateTime", member["updateTime"])
                    members_df.insert(3, "name", group["name"])

                    a = json.dumps(group["groupKey"])
                    b = json.loads(a)
                    members_df.insert(4, "groupKey", b["id"])
                    ##print(b['id'])

                    members_df.insert(5, "displayName", group["displayName"])

                    all_member_df = pd.concat(
                        [all_member_df, members_df], ignore_index=True
                    )

            else:
                print("No 'memberships' key found in the API response")
                
    except Exception as exc:
        print(exc)
        return all_member_df

I tried adding a try except to catch when it gets to that one group and fails, however the task still fails either way. How can I update this method so that when its the iteration of any group that doesn't exist anymore it just skips that iteration/doesn't crash the API and continues on to the next group.


Solution

  • Just had to add continue statement:

    try:
                    members = (
                        service.groups()
                        .memberships()
                        .list(parent=group["name"], view="FULL")
                        .execute()
                    )
    except Exception as exc:
                    print(f'Group {group["name"]} No Longer Exists')
                    continue