Last active
August 29, 2015 14:22
-
-
Save richieforeman/f73a7f0387d4b47e138a to your computer and use it in GitHub Desktop.
Reseller List Customers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import httplib2 | |
from argparse import ArgumentParser | |
from oauth2client.client import SignedJwtAssertionCredentials | |
from apiclient.discovery import build | |
SCOPES = [ | |
'https://www.googleapis.com/auth/apps.order' | |
] | |
NUM_RETRIES = 7 | |
http = httplib2.Http() | |
def get_credentials(args): | |
with file(args.private_key_file) as f: | |
credentials = SignedJwtAssertionCredentials( | |
service_account_name=args.service_account_email, | |
private_key=f.read(), | |
scope=SCOPES, | |
sub=args.admin_user) | |
return credentials | |
def main(args): | |
credentials = get_credentials(args) | |
credentials.authorize(http) | |
service = build('reseller', 'v1', http) | |
writer = csv.DictWriter(file(args.out, 'w'), fieldnames=[ | |
'customerId', | |
'skuId', | |
'plan.planName', | |
'seats.numberOfSeats', | |
'seats.maximumNumberOfSeats', | |
'renewalSettings.renewalType', | |
'purchaseOrderId', | |
'status', | |
'suspensionReasons' | |
]) | |
pageToken = '' | |
while pageToken is not None: | |
response = service.subscriptions().list( | |
maxResults=100, pageToken=pageToken).execute(num_retries=NUM_RETRIES) | |
for subscription in response['subscriptions']: | |
print subscription | |
writer.writerow({ | |
'customerId': subscription['customerId'], | |
'skuId': subscription['skuId'], | |
'plan.planName': subscription['plan']['planName'], | |
'seats.numberOfSeats': subscription['seats'].get('numberOfSeats'), | |
'seats.maximumNumberOfSeats': subscription['seats'].get('maximumNumberOfSeats'), | |
'renewalSettings.renewalType': subscription.get('renewalSettings', {}).get('renewalType'), | |
'status': subscription['status'], | |
'suspensionReasons': ",".join(subscription.get('suspensionReasons',[])) | |
}) | |
pageToken = response.get('nextPageToken') | |
if pageToken is None: | |
break | |
if __name__ == "__main__": | |
parser = ArgumentParser() | |
parser.add_argument('--private_key_file', required=True) | |
parser.add_argument('--service_account_email', required=True) | |
parser.add_argument('--admin_user', required=True) | |
parser.add_argument('--out', required=True) | |
main(parser.parse_args()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment