From b333816dc8f18ec02ee2d21a2607235de7db6ee4 Mon Sep 17 00:00:00 2001 From: Ross Scroggs Date: Mon, 22 Mar 2021 06:06:04 -0700 Subject: [PATCH] Update policies and user invitations (#1339) * Update policies and user invitations Show chrome policy schemas in sorted order Change create userintervention to send userintervention to be consistent with API Add state and orderby option to print userinvitations * Sort polices in show chromepolicies --- src/GamCommands.txt | 6 +- src/gam/__init__.py | 13 +++-- src/gam/gapi/chromepolicy.py | 4 +- src/gam/gapi/cloudidentity/userinvitations.py | 57 +++++++++++++++++-- 4 files changed, 67 insertions(+), 13 deletions(-) diff --git a/src/GamCommands.txt b/src/GamCommands.txt index 08f83380..1b0d8ac1 100644 --- a/src/GamCommands.txt +++ b/src/GamCommands.txt @@ -1337,11 +1337,13 @@ gam print group-members|groups-members [todrive] [roles ] [membernames] [fields ] [includederivedmembership] -gam create userinvitation +gam send userinvitation gam cancel userinvitation gam check userinvitation|isinvitable gam info userinvitation -gam print userinvitations [todrive] [filter ] +gam print userinvitations [todrive] + [state notyetsent|invited|accepted|declined]] + [orderby email|updatetime [ascending|descending]] gam check isinvitable [todrive] gam print licenses [todrive] [(products|product )|(skus|sku )|allskus|gsuite] [countsonly] diff --git a/src/gam/__init__.py b/src/gam/__init__.py index 8b24cd2c..6fee9a99 100755 --- a/src/gam/__init__.py +++ b/src/gam/__init__.py @@ -11186,8 +11186,6 @@ def ProcessGAMCommand(args): gapi_directory_roles.create() elif argument in ['browsertoken', 'browsertokens']: gapi_cbcm.createtoken() - elif argument in ['userinvitation', 'userinvitations']: - gapi_cloudidentity_userinvitations.send() elif argument in ['printer']: gapi_directory_printers.create() else: @@ -11498,6 +11496,13 @@ def ProcessGAMCommand(args): else: controlflow.invalid_argument_exit(argument, 'gam print') sys.exit(0) + elif command == 'send': + argument = sys.argv[2].lower() + if argument in ['userinvitation', 'userinvitations']: + gapi_cloudidentity_userinvitations.send() + else: + controlflow.invalid_argument_exit(argument, 'gam send') + sys.exit(0) elif command == 'show': argument = sys.argv[2].lower() if argument in ['schema', 'schemas']: @@ -11512,9 +11517,9 @@ def ProcessGAMCommand(args): doShowServiceAccountKeys() elif argument in ['browsertoken', 'browsertokens']: gapi_cbcm.printshowtokens(False) - elif argument == 'chromeschema': + elif argument in ['chromeschema', 'chromeschemas']: gapi_chromepolicy.printshow_schemas() - elif argument == 'chromepolicy': + elif argument in ['chromepolicy', 'chromepolicies']: gapi_chromepolicy.printshow_policies() else: controlflow.invalid_argument_exit(argument, 'gam show') diff --git a/src/gam/gapi/chromepolicy.py b/src/gam/gapi/chromepolicy.py index 72bc25e3..48ebaa67 100644 --- a/src/gam/gapi/chromepolicy.py +++ b/src/gam/gapi/chromepolicy.py @@ -86,7 +86,7 @@ def printshow_policies(): body=body) except googleapiclient.errors.HttpError: policies = [] - for policy in policies: + for policy in sorted(policies, key=lambda k: k.get('value', {}).get('policySchema', '')): name = policy.get('value', {}).get('policySchema', '') print(name) values = policy.get('value', {}).get('value', {}) @@ -169,7 +169,7 @@ def printshow_schemas(): 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(): + for _, value in sorted(iter(schemas.items())): print(f'{value.get("name")}: {value.get("description")}') for val in value['settings'].values(): vtype = val.get('type') diff --git a/src/gam/gapi/cloudidentity/userinvitations.py b/src/gam/gapi/cloudidentity/userinvitations.py index a1bd6ad0..225fca2d 100644 --- a/src/gam/gapi/cloudidentity/userinvitations.py +++ b/src/gam/gapi/cloudidentity/userinvitations.py @@ -4,7 +4,8 @@ from urllib.parse import quote_plus import googleapiclient -from gam.var import GC_CUSTOMER_ID, GC_Values, MY_CUSTOMER +import gam +from gam.var import GC_CUSTOMER_ID, GC_Values, MY_CUSTOMER, SORTORDER_CHOICES_MAP from gam import controlflow from gam import display from gam import gapi @@ -114,31 +115,77 @@ def send(): _generic_action('send') +USERINVITATION_ORDERBY_CHOICES_MAP = { + 'email': 'email', + 'updatetime': 'update_time', + } + +USERINVITATION_STATE_CHOICES_MAP = { + 'accepted': 'ACCEPTED', + 'declined': 'DECLINED', + 'invited': 'INVITED', + 'notyetsent': 'NOT_YET_SENT', + } + def print_(): '''gam print userinvitations''' svc = gapi_cloudidentity.build('cloudidentity_beta') customer = _get_customerid() todrive = False - titles = [] + titles = ['name', 'state', 'updateTime'] rows = [] filter_ = None + orderByList = [] i = 3 while i < len(sys.argv): myarg = sys.argv[i].lower().replace('_', '') - if myarg == 'filter': - filter_ = sys.argv[i+1] + if myarg == 'state': + state = sys.argv[i + 1].lower().replace('_', '') + if state in USERINVITATION_STATE_CHOICES_MAP: + filter_ = f"state=='{USERINVITATION_STATE_CHOICES_MAP[state]}'" + else: + controlflow.expected_argument_exit('state', + ', '.join(USERINVITATION_STATE_CHOICES_MAP), + state) i += 2 + elif myarg == 'orderby': + fieldName = sys.argv[i + 1].lower() + i += 2 + if fieldName in USERINVITATION_ORDERBY_CHOICES_MAP: + fieldName = USERINVITATION_ORDERBY_CHOICES_MAP[fieldName] + orderBy = '' + if i < len(sys.argv): + orderBy = sys.argv[i].lower() + if orderBy in SORTORDER_CHOICES_MAP: + orderBy = SORTORDER_CHOICES_MAP[orderBy] + i += 1 + if orderBy != 'DESCENDING': + orderByList.append(fieldName) + else: + orderByList.append(f'{fieldName} desc') + else: + controlflow.expected_argument_exit( + 'orderby', ', '.join(sorted(USERINVITATION_ORDERBY_CHOICES_MAP)), + fieldName) elif myarg == 'todrive': todrive = True i += 1 else: controlflow.invalid_argument_exit(sys.argv[i], 'gam print userinvitations') + if orderByList: + orderBy = ' '.join(orderByList) + else: + orderBy = None + gam.printGettingAllItems('User Invitations', filter_) + page_message = gapi.got_total_items_msg('User Invitations', '...\n') invitations = gapi.get_all_pages(svc.customers().userinvitations(), 'list', 'userInvitations', + page_message=page_message, parent=customer, - filter=filter_) + filter=filter_, + orderBy=orderBy) for invitation in invitations: invitation['name'] = _reduce_name(invitation['name']) row = {}