Skip to content

Commit

Permalink
Updated gam print browsers to avoid token expiration and minimize m…
Browse files Browse the repository at this point in the history
…emory usage.
  • Loading branch information
taers232c committed May 24, 2021
1 parent 0edcbb9 commit 8383535
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 34 deletions.
8 changes: 8 additions & 0 deletions src/GamUpdate.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
6.03.23

Updated `<BrowserFieldName>`:
* Added `deviceidentifiershistory`
* Removed `installedbrowserversion`

Updated `gam print browsers` to avoid token expiration and minimize memory usage.

6.03.22

Fixed bug in `gam <UserTypeEntity> update drivefile` where the options `retainname | (newfilename <DriveFileName>)`
Expand Down
92 changes: 58 additions & 34 deletions src/gam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"""

__author__ = 'Ross Scroggs <[email protected]>'
__version__ = '6.03.22'
__version__ = '6.03.23'
__license__ = 'Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)'

import base64
Expand Down Expand Up @@ -4344,7 +4344,8 @@ def callGAPI(service, function,
# The error detail indicated that we should retry this request
# We'll refresh credentials and make another pass
try:
service._http.credentials.refresh(getHttpObj())
# service._http.credentials.refresh(getHttpObj())
service._http.credentials.refresh(transportCreateRequest())
except TypeError:
systemErrorExit(HTTP_ERROR_RC, message)
continue
Expand Down Expand Up @@ -17836,8 +17837,8 @@ def _showBrowser(browser, FJQC, i=0, count=0):
'browsers': 'browsers',
'browserversions': 'browserVersions',
'deviceid': 'deviceId',
'deviceidentifiershistory': 'deviceIdentifiersHistory',
'extensioncount': 'extensionCount',
'installedbrowserversion': 'installedBrowserVersion',
'lastactivitytime': 'lastActivityTime',
'lastdeviceuser': 'lastDeviceUser',
'lastdeviceusers': 'lastDeviceUsers',
Expand Down Expand Up @@ -18111,37 +18112,54 @@ def _printBrowser(browser):
sortHeaders = False
substituteQueryTimes(queries, queryTimes)
if entityList is None:
startTime = time.time()
for query in queries:
printGettingAllAccountEntities(Ent.CHROME_BROWSER, query)
pageMessage = getPageMessage()
try:
browsers = callGAPIpages(cbcm.chromebrowsers(), 'list', 'browsers',
pageMessage=pageMessage,
throwReasons=[GAPI.INVALID_INPUT, GAPI.BAD_REQUEST, GAPI.INVALID_ORGUNIT, GAPI.FORBIDDEN],
customer=customerId, orgUnitPath=orgUnitPath, query=query, projection=projection,
orderBy=orderBy, sortOrder=sortOrder, fields=fields)
if not csvPF:
jcount = len(browsers)
if not FJQC.formatJSON:
performActionNumItems(jcount, Ent.CHROME_BROWSER)
Ind.Increment()
j = 0
for browser in browsers:
j += 1
_showBrowser(browser, FJQC, j, jcount)
Ind.Decrement()
else:
for browser in browsers:
_printBrowser(browser)
except GAPI.invalidInput as e:
if query:
entityActionFailedWarning([Ent.CHROME_BROWSER, None], invalidQuery(query))
else:
pageToken = None
totalItems = 0
while True:
try:
feed = callGAPI(cbcm.chromebrowsers(), 'list',
pageToken=pageToken,
throwReasons=[GAPI.INVALID_INPUT, GAPI.BAD_REQUEST, GAPI.INVALID_ORGUNIT, GAPI.FORBIDDEN],
customer=customerId, orgUnitPath=orgUnitPath, query=query, projection=projection,
orderBy=orderBy, sortOrder=sortOrder, fields=fields)
except GAPI.invalidInput as e:
if query:
entityActionFailedWarning([Ent.CHROME_BROWSER, None], invalidQuery(query))
else:
entityActionFailedWarning([Ent.CHROME_BROWSER, None], str(e))
continue
except GAPI.invalidOrgunit as e:
entityActionFailedWarning([Ent.CHROME_BROWSER, None], str(e))
except GAPI.invalidOrgunit as e:
entityActionFailedWarning([Ent.CHROME_BROWSER, None], str(e))
except (GAPI.badRequest, GAPI.resourceNotFound, GAPI.forbidden):
accessErrorExit(None)
return
except (GAPI.badRequest, GAPI.resourceNotFound, GAPI.forbidden):
accessErrorExit(None)
pageToken, totalItems = _processGAPIpagesResult(feed, 'browsers', None, totalItems, pageMessage, 'deviceId', Ent.CHROME_BROWSER)
if feed:
browsers = feed.get('browsers', [])
if not csvPF:
jcount = len(browsers)
if not FJQC.formatJSON:
performActionNumItems(jcount, Ent.CHROME_BROWSER)
Ind.Increment()
j = 0
for browser in browsers:
j += 1
_showBrowser(browser, FJQC, j, jcount)
Ind.Decrement()
else:
for browser in browsers:
_printBrowser(browser)
del feed
if not pageToken:
_finalizeGAPIpagesResult(pageMessage)
break
currentTime = time.time()
if int(currentTime-startTime) > 3000:
cbcm = buildGAPIObject(API.CBCM)
startTime = currentTime
else:
sortRows = True
jcount = len(entityList)
Expand Down Expand Up @@ -32671,6 +32689,7 @@ def _callbackPrintUser(request_id, response, exception):
if printOptions['getLicenseFeed']:
licenses = doPrintLicenses(returnFields=['userId', 'skuId'])
if entityList is None:
startTime = time.time()
sortRows = False
if orgUnitPath is not None and fieldsList:
fieldsList.append('orgUnitPath')
Expand Down Expand Up @@ -32721,26 +32740,31 @@ def _callbackPrintUser(request_id, response, exception):
accessErrorExit(cd)
pageToken, totalItems = _processGAPIpagesResult(feed, 'users', None, totalItems, pageMessage, 'primaryEmail', Ent.USER)
if feed:
users = feed.get('users', [])
if orgUnitPath is None:
if not printOptions['countOnly']:
for user in feed.get('users', []):
for user in users:
_printUser(user, 0, 0)
else:
for user in feed.get('users', []):
for user in users:
_updateDomainCounts(user['primaryEmail'])
else:
if not printOptions['countOnly']:
for user in feed.get('users', []):
for user in users:
if orgUnitPathLower == user.get('orgUnitPath', '').lower():
_printUser(user, 0, 0)
else:
for user in feed.get('users', []):
for user in users:
if orgUnitPathLower == user.get('orgUnitPath', '').lower():
_updateDomainCounts(user['primaryEmail'])
del feed
if not pageToken:
_finalizeGAPIpagesResult(pageMessage)
break
currentTime = time.time()
if int(currentTime-startTime) > 3000:
cd = buildGAPIObject(API.DIRECTORY)
startTime = currentTime
else:
sortRows = True
# If no individual fields were specified (allfields, basic, full) or individual fields other than primaryEmail were specified, look up each user
Expand Down

0 comments on commit 8383535

Please sign in to comment.