Clean up printer commands/documentation (#1330)

* Clean up printer commands/documentation

driverless has to take value so it can be changed from true to false
Drop separate deleteprinters command, merge into delete printers

* Printer delete update

Allow a list of printer IDs
Drop cros from crosfile and croscsvfile to avoid confusion; add cros back when calling getUsersToModify
This commit is contained in:
Ross Scroggs
2021-03-13 08:26:30 -08:00
committed by GitHub
parent bd38b7479f
commit 870fc27c72
3 changed files with 50 additions and 36 deletions

View File

@@ -1,9 +1,12 @@
'''Commands to manage directory printers.'''
# pylint: disable=unused-wildcard-import wildcard-import
import sys
from gam import controlflow
from gam import display
from gam import gapi
from gam import getUsersToModify
from gam.var import *
from gam.gapi import directory as gapi_directory
from gam.gapi.directory import orgunits as gapi_directory_orgunits
@@ -30,39 +33,19 @@ def _get_printer_attributes(i, cdapi=None):
elif myarg == 'makeandmodel':
body['makeAndModel'] = sys.argv[i+1]
i += 2
elif myarg in ['ou', 'orgunit']:
elif myarg in ['ou', 'org', 'orgunit', 'orgunitid']:
_, body['orgUnitId'] = gapi_directory_orgunits.getOrgUnitId(sys.argv[i+1], cdapi)
body['orgUnitId'] = body['orgUnitId'][3:]
i += 2
elif myarg == 'uri':
body['uri'] = sys.argv[i+1]
i += 2
elif myarg == 'driverless':
elif myarg in {'driverless', 'usedriverlessconfig'}:
body['useDriverlessConfig'] = True
i += 1
return body
def batch_delete(printer_ids):
'''gam croscsvfile file:column deleteprinters'''
cdapi = gapi_directory.build()
parent = _get_customerid()
# max 50 per API call
batch_size = 50
for chunk in range(0, len(printer_ids), batch_size):
body = {
'printerIds': printer_ids[chunk:chunk + batch_size]
}
result = gapi.call(cdapi.customers().chrome().printers(),
'batchDeletePrinters',
parent=parent,
body=body)
for printer_id in result.get('printerIds', []):
print(f'Deleted printer {printer_id}')
for printer_id in result.get('failedPrinters', []):
print(f'ERROR: failed to delete {printer_id.get("printerIds")}')
def create():
'''gam create printer'''
cdapi = gapi_directory.build()
@@ -76,15 +59,28 @@ def create():
def delete():
'''gam delete printer'''
'''gam delete printer <PrinterIDList>|(file <FileName>)|(csvfile <FileName>:<FieldName>)'''
cdapi = gapi_directory.build()
customer_id = _get_customerid()
printer_id = sys.argv[3]
name = f'{customer_id}/chrome/printers/{printer_id}'
gapi.call(cdapi.customers().chrome().printers(),
'delete',
name=name)
print(f'Deleted printer {printer_id}')
if printer_id.lower() not in {'file', 'csvfile'}:
printer_ids = printer_id.replace(',', ' ').split()
else:
printer_ids = getUsersToModify(f'cros{printer_id.lower()}', sys.argv[4])
# max 50 per API call
batch_size = 50
for chunk in range(0, len(printer_ids), batch_size):
body = {
'printerIds': printer_ids[chunk:chunk + batch_size]
}
result = gapi.call(cdapi.customers().chrome().printers(),
'batchDeletePrinters',
parent=customer_id,
body=body)
for printer_id in result.get('printerIds', []):
print(f'Deleted printer {printer_id}')
for printer_id in result.get('failedPrinters', []):
print(f'ERROR: failed to delete {printer_id.get("printerIds")}')
def print_():
'''gam print printers'''