From 6f2ebf8d2d528f29f127111e536b4769edaef912 Mon Sep 17 00:00:00 2001 From: Ross Scroggs Date: Fri, 19 Mar 2021 09:46:07 -0700 Subject: [PATCH] Add info printer command/ChromePolicy cleanup (#1337) * Add info printer command * ChromePolicy cleanup Make update chromepolicy orgunit default to / like delete and print Add `filter ` to print chromeschema Make update_policy code to set additionalTargetKeys consistent with delete_policy I left verb at print for chromepolicy/chromeschema * When printing schemasa, use ":" instead of " - " * Fix print policy indentation * Chrome policy cleanup orgunit must be specified Use verb show, add verb print later * Recognize all ou forms to exit from schema mode * Don't assign multiple variables on same line --- src/GamCommands.txt | 10 +++-- src/gam/__init__.py | 10 +++-- src/gam/gapi/chromepolicy.py | 62 ++++++++++++++++++------------ src/gam/gapi/directory/printers.py | 21 ++++++++-- 4 files changed, 66 insertions(+), 37 deletions(-) diff --git a/src/GamCommands.txt b/src/GamCommands.txt index 311e155b..08f83380 100644 --- a/src/GamCommands.txt +++ b/src/GamCommands.txt @@ -147,6 +147,7 @@ If an item contains spaces, it should be surrounded by ". ::= ::= [user:]|group:|domain[:]|default ::= + ::= ::= ::= ::= @@ -1230,10 +1231,10 @@ The listlimit argument limits the number of recent users, time ranges a The start and end arguments filter the time ranges. Delimiter defaults to comma. -gam delete chromepolicy + [ou|orgunit ] [(printerid )|(appid )] -gam update chromepolicy ( ( )+)+ ou|orgunit [(printerid )|(appid )] -gam print chromepolicy [ou|orgunit ] [(printerid )|(appid )] -gam print chromeschema +gam delete chromepolicy + ou|org|orgunit [(printerid )|(appid )] +gam update chromepolicy ( ( )+)+ ou|org|orgunit [(printerid )|(appid )] +gam show chromepolicy ou|org|orgunit [(printerid )|(appid )] +gam show chromeschema [filter ] ::= devices/ ::= android|chrome_os|google_sync|ios|linux|mac_os|windows @@ -1290,6 +1291,7 @@ gam create printer + gam update printer + gam delete printer |(file )|(csvfile :) +gam info printer gam print printers [todrive] [filter ] gam print printermodels [todrive] [filter ] diff --git a/src/gam/__init__.py b/src/gam/__init__.py index 9dda0fd2..6d644210 100755 --- a/src/gam/__init__.py +++ b/src/gam/__init__.py @@ -11295,6 +11295,8 @@ def ProcessGAMCommand(args): gapi_directory_domainaliases.info() elif argument in ['resoldcustomer', 'resellercustomer']: doGetResoldCustomer() + elif argument in ['printer']: + gapi_directory_printers.info() elif argument in [ 'resoldsubscription', 'resoldsubscriptions', 'resellersubscription', 'resellersubscriptions' @@ -11493,10 +11495,6 @@ def ProcessGAMCommand(args): gapi_directory_printers.print_models() elif argument in ['printers']: gapi_directory_printers.print_() - elif argument == 'chromeschema': - gapi_chromepolicy.print_schemas() - elif argument == 'chromepolicy': - gapi_chromepolicy.print_policies() else: controlflow.invalid_argument_exit(argument, 'gam print') sys.exit(0) @@ -11514,6 +11512,10 @@ def ProcessGAMCommand(args): doShowServiceAccountKeys() elif argument in ['browsertoken', 'browsertokens']: gapi_cbcm.printshowtokens(False) + elif argument == 'chromeschema': + gapi_chromepolicy.printshow_schemas() + elif argument == 'chromepolicy': + gapi_chromepolicy.printshow_policies() else: controlflow.invalid_argument_exit(argument, 'gam show') sys.exit(0) diff --git a/src/gam/gapi/chromepolicy.py b/src/gam/gapi/chromepolicy.py index 96f51452..72bc25e3 100644 --- a/src/gam/gapi/chromepolicy.py +++ b/src/gam/gapi/chromepolicy.py @@ -31,17 +31,17 @@ def build(): return gam.buildGAPIObject('chromepolicy') -def print_policies(): +def printshow_policies(): svc = build() customer = _get_customerid() - orgunit = '/' + orgunit = None printer_id = None app_id = None i = 3 while i < len(sys.argv): myarg = sys.argv[i].lower().replace('_', '') - if myarg in ['orgunit', 'ou']: - orgunit = sys.argv[i+1] + if myarg in ['ou', 'org', 'orgunit']: + orgunit = _get_orgunit(sys.argv[i+1]) i += 2 elif myarg == 'printerid': printer_id = sys.argv[i+1] @@ -52,7 +52,8 @@ def print_policies(): else: msg = f'{myarg} is not a valid argument to "gam print chromepolicy"' controlflow.system_error_exit(3, msg) - orgunit = _get_orgunit(orgunit) + if not orgunit: + controlflow.system_error_exit(3, 'You must specify an orgunit') body = { 'policyTargetKey': { 'targetResource': orgunit, @@ -92,15 +93,16 @@ def print_policies(): for setting, value in values.items(): if isinstance(value, str) and value.find('_ENUM_') != -1: value = value.split('_ENUM_')[-1] - print(f' {setting}: {value}') + print(f' {setting}: {value}') print() -def build_schemas(svc=None): + +def build_schemas(svc=None, sfilter=None): if not svc: svc = build() parent = _get_customerid() schemas = gapi.get_all_pages(svc.customers().policySchemas(), 'list', - items='policySchemas', parent=parent) + items='policySchemas', parent=parent, filter=sfilter) schema_objects = {} for schema in schemas: schema_name = schema.get('name', '').split('/')[-1] @@ -153,11 +155,22 @@ def build_schemas(svc=None): schema_objects[schema_name.lower()] = schema_dict return schema_objects -def print_schemas(): + +def printshow_schemas(): svc = build() - schemas = build_schemas(svc) + sfilter = None + i = 3 + while i < len(sys.argv): + myarg = sys.argv[i].lower().replace('_', '') + if myarg == 'filter': + sfilter = sys.argv[i+1] + i += 2 + else: + msg = f'{myarg} is not a valid argument to "gam print chromeschema"' + controlflow.system_error_exit(3, msg) + schemas = build_schemas(svc, sfilter) for value in schemas.values(): - print(f'{value.get("name")} - {value.get("description")}') + print(f'{value.get("name")}: {value.get("description")}') for val in value['settings'].values(): vtype = val.get('type') print(f' {val.get("name")}: {vtype}') @@ -165,14 +178,14 @@ def print_schemas(): enums = val.get('enums', []) descriptions = val.get('descriptions', []) for i in range(len(val.get('enums', []))): - print(f' {enums[i]} - {descriptions[i]}') + print(f' {enums[i]}: {descriptions[i]}') elif vtype == 'TYPE_BOOL': pvs = val.get('descriptions') for pvi in pvs: if isinstance(pvi, dict): pvalue = pvi.get('value') pdescription = pvi.get('description') - print(f' {pvalue} - {pdescription}') + print(f' {pvalue}: {pdescription}') elif isinstance(pvi, list): print(f' {pvi[0]}') else: @@ -186,15 +199,15 @@ def delete_policy(): svc = build() customer = _get_customerid() schemas = build_schemas(svc) - orgunit = '/' + orgunit = None printer_id = None app_id = None i = 3 body = {'requests': []} while i < len(sys.argv): myarg = sys.argv[i].lower().replace('_', '') - if myarg in ['orgunit', 'ou']: - orgunit = sys.argv[i+1] + if myarg in ['ou', 'org', 'orgunit']: + orgunit = _get_orgunit(sys.argv[i+1]) i += 2 elif myarg == 'printerid': printer_id = sys.argv[i+1] @@ -208,7 +221,8 @@ def delete_policy(): else: msg = f'{myarg} is not a valid argument to "gam delete chromepolicy"' controlflow.system_error_exit(3, msg) - orgunit = _get_orgunit(orgunit) + if not orgunit: + controlflow.system_error_exit(3, 'You must specify an orgunit') for request in body['requests']: request['policyTargetKey'] = {'targetResource': orgunit} if printer_id: @@ -222,14 +236,14 @@ def update_policy(): svc = build() customer = _get_customerid() schemas = build_schemas(svc) - i = 3 - body = {'requests': []} orgunit = None printer_id = None app_id = None + i = 3 + body = {'requests': []} while i < len(sys.argv): myarg = sys.argv[i].lower().replace('_', '') - if myarg in ['orgunit', 'ou']: + if myarg in ['ou', 'org', 'orgunit']: orgunit = _get_orgunit(sys.argv[i+1]) i += 2 elif myarg == 'printerid': @@ -245,7 +259,7 @@ def update_policy(): i += 1 while i < len(sys.argv): field = sys.argv[i].lower() - if field in ['orgunit', 'ou', 'printerid', 'appid'] or '.' in field: + if field in ['ou', 'org', 'orgunit', 'printerid', 'appid'] or '.' in field: break # field is actually a new policy, orgunit or app/printer id expected_fields = ', '.join(schemas[myarg]['settings']) if field not in expected_fields: @@ -283,11 +297,9 @@ def update_policy(): controlflow.system_error_exit(3, 'You must specify an orgunit') for request in body['requests']: request['policyTargetKey'] = {'targetResource': orgunit} - if printer_id: - for request in body['requests']: + if printer_id: request['policyTargetKey']['additionalTargetKeys'] = {'printer_id': printer_id} - elif app_id: - for request in body['requests']: + elif app_id: request['policyTargetKey']['additionalTargetKeys'] = {'app_id': app_id} gapi.call(svc.customers().policies().orgunits(), 'batchModify', diff --git a/src/gam/gapi/directory/printers.py b/src/gam/gapi/directory/printers.py index bf2ce56a..1c0b2050 100644 --- a/src/gam/gapi/directory/printers.py +++ b/src/gam/gapi/directory/printers.py @@ -82,6 +82,19 @@ def delete(): for printer_id in result.get('failedPrinters', []): print(f'ERROR: failed to delete {printer_id.get("printerIds")}') + +def info(): + '''gam info printer''' + cdapi = gapi_directory.build() + customer = _get_customerid() + printer_id = sys.argv[3] + name = f'{customer}/chrome/printers/{printer_id}' + result = gapi.call(cdapi.customers().chrome().printers(), + 'get', + name=name) + display.print_json(result) + + def print_(): '''gam print printers''' cdapi = gapi_directory.build() @@ -161,8 +174,8 @@ def update(): update_mask = ','.join(body) # note clearMask seems unnecessary. Updating field to '' clears it. result = gapi.call(cdapi.customers().chrome().printers(), - 'patch', - name=name, - updateMask=update_mask, - body=body) + 'patch', + name=name, + updateMask=update_mask, + body=body) display.print_json(result)