From 2732abbc934703599b6bdf7518123971f1174394 Mon Sep 17 00:00:00 2001 From: Ross Scroggs Date: Fri, 25 Sep 2020 05:23:07 -0700 Subject: [PATCH] Make sync license consistent with other license commands (#1252) --- src/GamCommands.txt | 1 + src/gam/__init__.py | 16 ++++++++-------- src/gam/gapi/licensing.py | 28 +++++++--------------------- 3 files changed, 16 insertions(+), 29 deletions(-) diff --git a/src/GamCommands.txt b/src/GamCommands.txt index 6e9b0ed8..6b42502c 100644 --- a/src/GamCommands.txt +++ b/src/GamCommands.txt @@ -1368,6 +1368,7 @@ gam delete|del group|groups gam create|add license [product|productid ] gam update license [product|productid ] [from] gam delete|del license [product|productid ] +gam sync license [product|productid ] gam update photo gam delete|del photo diff --git a/src/gam/__init__.py b/src/gam/__init__.py index 23f981e5..fb593fb2 100755 --- a/src/gam/__init__.py +++ b/src/gam/__init__.py @@ -11262,7 +11262,7 @@ def ProcessGAMCommand(args): elif argument in ['guardian', 'guardians']: doPrintShowGuardians(False) elif argument in ['license', 'licenses', 'licence', 'licences']: - gapi_licensing.show_() + gapi_licensing.show() elif argument in ['project', 'projects']: doPrintShowProjects(False) elif argument in ['sakey', 'sakeys']: @@ -11352,13 +11352,6 @@ def ProcessGAMCommand(args): else: controlflow.invalid_argument_exit(argument, 'gam rotate') sys.exit(0) - elif command == 'sync': - argument = sys.argv[2].lower() - if argument in ['sku', 'skus', 'license', 'licenses']: - gapi_licensing.sync() - else: - controlflow.invalid_argument_exit(argument, 'gam sync') - sys.exit(0) elif command in ['cancelwipe', 'wipe', 'approve', 'block', 'sync']: target = sys.argv[2].lower().replace('_', '') if target in ['device', 'devices']: @@ -11599,6 +11592,13 @@ def ProcessGAMCommand(args): else: controlflow.invalid_argument_exit(addWhat, f'gam {command}') + elif command == 'sync': + syncWhat = sys.argv[4].lower() + if syncWhat in ['license', 'licence']: + gapi_licensing.sync(users) + else: + controlflow.invalid_argument_exit(syncWhat, + f'gam {command}') elif command == 'update': updateWhat = sys.argv[4].lower() if updateWhat == 'calendar': diff --git a/src/gam/gapi/licensing.py b/src/gam/gapi/licensing.py index ea7ac515..8216a8d1 100644 --- a/src/gam/gapi/licensing.py +++ b/src/gam/gapi/licensing.py @@ -1,19 +1,12 @@ -import base64 -import json -import os import re import sys -import googleapiclient - import gam from gam.var import * +from gam import controlflow from gam import display from gam import gapi -from gam import fileutils from gam.gapi import errors as gapi_errors -from gam.gapi.directory import groups as gapi_directory_groups -from gam import utils def build(): @@ -85,16 +78,12 @@ def delete(users, sku=None): if len(lbatch._order) > 0: lbatch.execute() -def sync(): - lic = build() - sku = sys.argv[3] - entity_type = sys.argv[4].lower() - entity = sys.argv[5].lower() - current_users = gam.getUsersToModify(entity_type, entity) +def sync(users): + sku = sys.argv[5] current_licenses = gam.getUsersToModify(entity_type='license', entity=sku) - users_to_license = [user for user in current_users if user not in current_licenses] - users_to_unlicense = [user for user in current_licenses if user not in current_users] + users_to_license = [user for user in users if user not in current_licenses] + users_to_unlicense = [user for user in current_licenses if user not in users] print(f'Need to remove license from {len(users_to_unlicense)} and add to ' \ f'{len(users_to_license)} users...') # do the remove first to free up seats @@ -136,7 +125,7 @@ def update(users, sku=None, old_sku=None): lbatch = lic.new_batch_http_request(callback=user_lic_result) if len(lbatch._order) > 0: lbatch.execute() - + def print_(returnFields=None, skus=None, @@ -296,7 +285,7 @@ def print_(returnFields=None, def show(): - licenseCounts = doPrintLicenses(countsOnly=True, returnCounts=True) + licenseCounts = print_(countsOnly=True, returnCounts=True) for u_license in licenseCounts: line = '' for i in range(0, len(u_license), 2): @@ -313,6 +302,3 @@ def _formatSKUIdDisplayName(skuId): if skuId == skuIdDisplay: return skuId return f'{skuId} ({skuIdDisplay})' - - -