Print only certain SKUs via "gam print licenses"

This commit is contained in:
Jay Lee
2014-09-08 10:17:23 -04:00
parent 7447e73a37
commit b56fe39bb2

46
gam.py
View File

@ -24,7 +24,7 @@ For more information, see http://code.google.com/p/google-apps-manager
""" """
__author__ = u'Jay Lee <jay0lee@gmail.com>' __author__ = u'Jay Lee <jay0lee@gmail.com>'
__version__ = u'3.31' __version__ = u'3.32-unreleased'
__license__ = u'Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)' __license__ = u'Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)'
import sys, os, time, datetime, random, socket, csv, platform, re, calendar, base64, hashlib import sys, os, time, datetime, random, socket, csv, platform, re, calendar, base64, hashlib
@ -5943,6 +5943,8 @@ def doPrintCrosDevices():
def doPrintLicenses(return_list=False): def doPrintLicenses(return_list=False):
lic = buildGAPIObject(u'licensing') lic = buildGAPIObject(u'licensing')
products = [u'Google-Apps', u'Google-Drive-storage', u'Google-Coordinate', u'Google-Vault'] products = [u'Google-Apps', u'Google-Drive-storage', u'Google-Coordinate', u'Google-Vault']
skus = None
licenses = []
lic_attributes = [{}] lic_attributes = [{}]
todrive = False todrive = False
i = 3 i = 3
@ -5953,24 +5955,36 @@ def doPrintLicenses(return_list=False):
elif sys.argv[i].lower() in [u'products', u'product']: elif sys.argv[i].lower() in [u'products', u'product']:
products = sys.argv[i+1].split(',') products = sys.argv[i+1].split(',')
i += 2 i += 2
elif sys.argv[i].lower() in [u'sku', u'skus']:
skus = sys.argv[i+1].split(',')
i += 2
else: else:
print u'Error: %s is not a valid argument to gam print licenses' % sys.argv[i] print u'Error: %s is not a valid argument to gam print licenses' % sys.argv[i]
sys.exit(3) sys.exit(3)
for productId in products: if skus:
page_message = u'Got %%%%total_items%%%% Licenses for %s...\n' % productId for sku in skus:
try: product, sku = getProductAndSKU(sku)
licenses = callGAPIpages(service=lic.licenseAssignments(), function=u'listForProduct', throw_reasons=[u'invalid', u'forbidden'], page_message=page_message, customerId=domain, productId=productId, fields=u'items(productId,skuId,userId),nextPageToken') page_message = u'Got %%%%total_items%%%% Licenses for %s...\n' % sku
except apiclient.errors.HttpError: try:
licenses = [] licenses += callGAPIpages(service=lic.licenseAssignments(), function=u'listForProductAndSku', throw_reasons=[u'invalid', u'forbidden'], page_message=page_message, customerId=domain, productId=product, skuId=sku, fields=u'items(productId,skuId,userId),nextPageToken')
for license in licenses: except apiclient.errors.HttpError:
a_license = dict() licenses += []
for title in license.keys(): else:
if title in [u'kind', u'etags', u'selfLink']: for productId in products:
continue page_message = u'Got %%%%total_items%%%% Licenses for %s...\n' % productId
if title not in lic_attributes[0]: try:
lic_attributes[0][title] = title licenses += callGAPIpages(service=lic.licenseAssignments(), function=u'listForProduct', throw_reasons=[u'invalid', u'forbidden'], page_message=page_message, customerId=domain, productId=productId, fields=u'items(productId,skuId,userId),nextPageToken')
a_license[title] = license[title] except apiclient.errors.HttpError:
lic_attributes.append(a_license) licenses = +[]
for license in licenses:
a_license = dict()
for title in license.keys():
if title in [u'kind', u'etags', u'selfLink']:
continue
if title not in lic_attributes[0]:
lic_attributes[0][title] = title
a_license[title] = license[title]
lic_attributes.append(a_license)
if return_list: if return_list:
return lic_attributes return lic_attributes
output_csv(lic_attributes, lic_attributes[0], u'Licenses', todrive) output_csv(lic_attributes, lic_attributes[0], u'Licenses', todrive)