#!/usr/bin/env python

import json
import argparse
import requests

ccu_base = "https://api.ccu.akamai.com"
ccu_endpoint = ccu_base+"/ccu/v2/queues/default"
headers = {'content-type': 'application/json'}

parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--cpcode", help="CP code to clear")
group.add_argument("--object", help="Object to clear (can specify multiple times)", action='append')
parser.add_argument("--user", help="Akamai user", required=True)
parser.add_argument("--passwd", help="Akamai password", required=True)
args = parser.parse_args()

if args.cpcode is not None:
    postdata = json.dumps({'type': 'cpcode', 'objects': [args.cpcode]})
else:
    postdata = json.dumps({'objects': args.object})

r = requests.post(ccu_endpoint, data=postdata, headers=headers, auth=(args.user, args.passwd))
if r.encoding is not None:
    print "Error, unexpected data received:\n ",r.text
    exit(1)
else:
    resp = json.loads(r.text)
    if resp['httpStatus'] < 200 or resp['httpStatus'] > 299:
        print "Error, request not submitted\n\thttpStatus: ", resp['httpStatus'], "\n\ttitle: ", resp['title'], "\n\tdetail: ", resp['detail'], "\n\tdescribedBy: ", resp['describedBy']
        exit(1)
    else:
        print "Request submitted OK\n\tEstimated time to clear: ", resp['estimatedSeconds']/60, "minutes\n\tCheck URL: ", ccu_base+resp['progressUri']

@@ -1420,8 +1420,402 @@
 ssUri'%5D%0A
+%0A%09status_uri = ccu_base + resp%5B'progressUri'%5D%0A%09end_time = time.time() + (resp%5B'estimatatedSeconds'%5D * 2)%0A%09while end_time %3E time.time():%0A%09%09r = requests.get(status_uri, headers=headers, auth=(args.user, args.passwd))%0A%09%09resp_status = json.loads(r.text)%0A%09%09if resp_status%5B'purgeStatus'%5D == 'Done':%0A%09%09%09print %22Purge complete!%22%0A%09%09%09exit(0)%0A%09%09else:%0A%09%09%09print %22++ purge still running...%22%0A%09%09%09time.sleep(30)%0A

add status checking