From 7e4cca3c88931f2405cac8e4984e8d22e5e7dad9 Mon Sep 17 00:00:00 2001 From: Ross Scroggs Date: Mon, 29 Mar 2021 09:45:22 -0700 Subject: [PATCH] Added command `gam print addresses [todrive *]` --- src/GamUpdate.txt | 7 +++++ src/gam/__init__.py | 53 ++++++++++++++++++++++++++++++++++++-- src/gam/gamlib/glclargs.py | 1 + 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/GamUpdate.txt b/src/GamUpdate.txt index cab0a4f6..dd0cdad8 100644 --- a/src/GamUpdate.txt +++ b/src/GamUpdate.txt @@ -1,3 +1,10 @@ +6.00.06 + +Added command `gam print addresses [todrive *]` that produces a +two column CSV file (headers Type, Email) that displays all group and user primary +email addresses and aliases. The types are: Group, GroupAlias, GroupNEAlias, +SuspendedUser, User, UserAlias, UserNEAlias. 'NE' is and abbreviation for NonEditable. + 6.00.05 Fixed bug in `gam print vaultcounts ... everyone` which caused the following error: diff --git a/src/gam/__init__.py b/src/gam/__init__.py index 14280cf4..3e3b7770 100755 --- a/src/gam/__init__.py +++ b/src/gam/__init__.py @@ -23,7 +23,7 @@ """ __author__ = 'Ross Scroggs ' -__version__ = '6.00.05' +__version__ = '6.00.06' __license__ = 'Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)' import base64 @@ -13769,6 +13769,54 @@ def doPrintAliases(): csvPF.WriteRow({'NonEditableAlias': alias, 'Target': group['email'], 'TargetType': 'Group'}) csvPF.writeCSVfile('Aliases') +# gam print addresses [todrive *] +def doPrintAddresses(): + cd = buildGAPIObject(API.DIRECTORY) + csvPF = CSVPrintFile() + titlesList = ['Type', 'Email'] + userFields = ['primaryEmail', 'aliases', 'suspended'] + groupFields = ['email', 'aliases'] + while Cmd.ArgumentsRemaining(): + myarg = getArgument() + if myarg == 'todrive': + csvPF.GetTodriveParameters() + else: + unknownArgumentExit() + csvPF.SetTitles(titlesList) + printGettingAllAccountEntities(Ent.USER) + try: + entityList = callGAPIpages(cd.users(), 'list', 'users', + pageMessage=getPageMessage(showFirstLastItems=True), messageAttribute='primaryEmail', + throwReasons=[GAPI.RESOURCE_NOT_FOUND, GAPI.FORBIDDEN, GAPI.BAD_REQUEST], + customer=GC.Values[GC.CUSTOMER_ID], orderBy='email', + fields=f'nextPageToken,users({",".join(userFields)})', + maxResults=GC.Values[GC.USER_MAX_RESULTS]) + except (GAPI.resourceNotFound, GAPI.forbidden, GAPI.badRequest): + accessErrorExit(cd) + for user in entityList: + csvPF.WriteRow({'Type': 'User' if not user['suspended'] else 'SuspendedUser', 'Email': user['primaryEmail']}) + for alias in user.get('aliases', []): + csvPF.WriteRow({'Type': 'UserAlias', 'Email': alias}) + for alias in user.get('nonEditableAliases', []): + csvPF.WriteRow({'Type': 'UserNEAlias', 'Email': alias}) + printGettingAllAccountEntities(Ent.GROUP) + try: + entityList = callGAPIpages(cd.groups(), 'list', 'groups', + pageMessage=getPageMessage(showFirstLastItems=True), messageAttribute='email', + throwReasons=GAPI.GROUP_LIST_THROW_REASONS, + customer=GC.Values[GC.CUSTOMER_ID], orderBy='email', + fields=f'nextPageToken,groups({",".join(groupFields)})') + except (GAPI.resourceNotFound, GAPI.domainNotFound, GAPI.forbidden, GAPI.badRequest): + accessErrorExit(cd) + for group in entityList: + csvPF.WriteRow({'Type': 'Group', 'Email': group['email']}) + for alias in group.get('aliases', []): + csvPF.WriteRow({'Type': 'GroupAlias', 'Email': alias}) + for alias in group.get('nonEditableAliases', []): + csvPF.WriteRow({'Type': 'GroupNEAlias', 'Email': alias}) + csvPF.SortRowsTwoTitles('Type', 'Email', False) + csvPF.writeCSVfile('Addresses') + # Contact commands utilities # CONTACT_JSON = 'JSON' @@ -51428,7 +51476,8 @@ def _printVacation(user, result, showDisabled): ), 'print': (Act.PRINT, - {Cmd.ARG_ADMINROLE: doPrintShowAdminRoles, + {Cmd.ARG_ADDRESSES: doPrintAddresses, + Cmd.ARG_ADMINROLE: doPrintShowAdminRoles, Cmd.ARG_ADMIN: doPrintShowAdmins, Cmd.ARG_ALERT: doPrintShowAlerts, Cmd.ARG_ALERTFEEDBACK: doPrintShowAlertFeedback, diff --git a/src/gam/gamlib/glclargs.py b/src/gam/gamlib/glclargs.py index 64fccae6..8059663e 100644 --- a/src/gam/gamlib/glclargs.py +++ b/src/gam/gamlib/glclargs.py @@ -335,6 +335,7 @@ class GamCLArgs(): ARG_3LO = '3lo' ARG_ACL = 'acl' ARG_ACLS = 'acls' + ARG_ADDRESSES = 'addresses' ARG_ADMIN = 'admin' ARG_ADMINS = 'admins' ARG_ADMINROLE = 'adminrole'