Improve printing of app/extension/printer policy

This commit is contained in:
Jay Lee
2021-06-30 11:18:29 -04:00
parent a6aecf4e9d
commit e5f9db129b
2 changed files with 28 additions and 20 deletions

View File

@ -39,6 +39,8 @@ def printshow_policies():
orgunit = None orgunit = None
printer_id = None printer_id = None
app_id = None app_id = None
body = {}
namespaces = []
i = 3 i = 3
while i < len(sys.argv): while i < len(sys.argv):
myarg = sys.argv[i].lower().replace('_', '') myarg = sys.argv[i].lower().replace('_', '')
@ -51,16 +53,15 @@ def printshow_policies():
elif myarg == 'appid': elif myarg == 'appid':
app_id = sys.argv[i+1] app_id = sys.argv[i+1]
i += 2 i += 2
elif myarg == 'namespace':
namespaces.append(sys.argv[i+1])
i += 2
else: else:
msg = f'{myarg} is not a valid argument to "gam print chromepolicy"' msg = f'{myarg} is not a valid argument to "gam print chromepolicy"'
controlflow.system_error_exit(3, msg) controlflow.system_error_exit(3, msg)
if not orgunit: if not orgunit:
controlflow.system_error_exit(3, 'You must specify an orgunit') controlflow.system_error_exit(3, 'You must specify an orgunit')
body = { body['policyTargetKey'] = {'targetResource': orgunit}
'policyTargetKey': {
'targetResource': orgunit,
}
}
if printer_id: if printer_id:
body['policyTargetKey']['additionalTargetKeys'] = {'printer_id': printer_id} body['policyTargetKey']['additionalTargetKeys'] = {'printer_id': printer_id}
namespaces = ['chrome.printers'] namespaces = ['chrome.printers']
@ -69,23 +70,19 @@ def printshow_policies():
namespaces = ['chrome.users.apps', namespaces = ['chrome.users.apps',
'chrome.devices.managedGuest.apps', 'chrome.devices.managedGuest.apps',
'chrome.devices.kiosk.apps'] 'chrome.devices.kiosk.apps']
else: if not namespaces:
namespaces = [ namespaces = [
'chrome.users', 'chrome.users',
# Not yet implemented: 'chrome.users.apps',
# 'chrome.devices', 'chrome.devices',
# 'chrome.devices.managedGuest', 'chrome.devices.kiosk',
# 'chrome.devices.kiosk', 'chrome.devices.managedGuest',
] ]
throw_reasons = [gapi_errors.ErrorReason.FOUR_O_O,] throw_reasons = [gapi_errors.ErrorReason.FOUR_O_O,]
orgunitPath = gapi_directory_orgunits.orgunit_from_orgunitid(orgunit[9:], None) orgunitPath = gapi_directory_orgunits.orgunit_from_orgunitid(orgunit[9:], None)
header = f'Organizational Unit: {orgunitPath}' print(f'Organizational Unit: {orgunitPath}')
if printer_id:
header += f', printerid: {printer_id}'
elif app_id:
header += f', appid: {app_id}'
print(header)
for namespace in namespaces: for namespace in namespaces:
spacing = ' '
body['policySchemaFilter'] = f'{namespace}.*' body['policySchemaFilter'] = f'{namespace}.*'
try: try:
policies = gapi.get_all_pages(svc.customers().policies(), 'resolve', policies = gapi.get_all_pages(svc.customers().policies(), 'resolve',
@ -95,11 +92,23 @@ def printshow_policies():
body=body) body=body)
except googleapiclient.errors.HttpError: except googleapiclient.errors.HttpError:
policies = [] policies = []
for policy in sorted(policies, key=lambda k: k.get('value', {}).get('policySchema', '')): # sort policies first by app/printer id then by schema name
policies = sorted(policies,
key=lambda k: (
list(k.get('targetKey', {}).get('additionalTargetKeys', {}).values()),
k.get('value', {}).get('policySchema', '')))
printed_ids = []
for policy in policies:
print() print()
name = policy.get('value', {}).get('policySchema', '') name = policy.get('value', {}).get('policySchema', '')
schema = CHROME_SCHEMA_TYPE_MESSAGE.get(name) schema = CHROME_SCHEMA_TYPE_MESSAGE.get(name)
print(name) for key, val in policy['targetKey'].get('additionalTargetKeys', {}).items():
additional_id = f'{key} - {val}'
if additional_id not in printed_ids:
print(f' {additional_id}')
printed_ids.append(additional_id)
spacing = ' '
print(f'{spacing}{name}')
values = policy.get('value', {}).get('value', {}) values = policy.get('value', {}).get('value', {})
for setting, value in values.items(): for setting, value in values.items():
# Handle TYPE_MESSAGE fields with durations or counts as a special case # Handle TYPE_MESSAGE fields with durations or counts as a special case
@ -111,7 +120,7 @@ def printshow_policies():
value = int(value) // schema['scale'] value = int(value) // schema['scale']
elif isinstance(value, str) and value.find('_ENUM_') != -1: elif isinstance(value, str) and value.find('_ENUM_') != -1:
value = value.split('_ENUM_')[-1] value = value.split('_ENUM_')[-1]
print(f' {setting}: {value}') print(f'{spacing}{setting}: {value}')
def build_schemas(svc=None, sfilter=None): def build_schemas(svc=None, sfilter=None):

View File

@ -286,7 +286,6 @@ PRODUCTID_NAME_MAPPINGS = {
# Legacy APIs that use v1 discovery. Newer APIs should all use v2. # Legacy APIs that use v1 discovery. Newer APIs should all use v2.
V1_DISCOVERY_APIS = { V1_DISCOVERY_APIS = {
'admin',
'calendar', 'calendar',
'drive', 'drive',
'oauth2', 'oauth2',