#! /usr/bin/python
""" Sample client for CCU
Note that in order for this to work you need to provision credentials
specifically for CCU - you cannot extend existing credentials to add
CCU as it's managed under "CCU" in the API credential system.

Configure->Organization->Manage APIs
Select "CCU APIs"
Create client collections/clients
Add authorization

Put the credentials in ~/.edgerc as demonstrated by api-kickstart/sample_edgerc
"""

import requests, logging, json
from random import randint
from akamai.edgegrid import EdgeGridAuth
from config import EdgeGridConfig
from urlparse import urljoin
import urllib
import os
session = requests.Session()
debug = False

config = EdgeGridConfig({},"user")

# Enable debugging for the requests module
if debug:
  import httplib as http_client
  http_client.HTTPConnection.debuglevel = 1
  logging.basicConfig()
  logging.getLogger().setLevel(logging.DEBUG)
  requests_log = logging.getLogger("requests.packages.urllib3")
  requests_log.setLevel(logging.DEBUG)
  requests_log.propagate = True

# Set the config options
session.auth = EdgeGridAuth(
            client_token=config.client_token,
            client_secret=config.client_secret,
            access_token=config.access_token
)

if hasattr(config, 'headers'):
	session.headers.update(config.headers)

baseurl = '%s://%s/' % ('https', config.host)

def getResult(endpoint, parameters=None):
	if parameters:
		parameter_string = urllib.urlencode(parameters)
		path = ''.join([endpoint + '?',parameter_string])
	else:
		path = endpoint
	endpoint_result = session.get(urljoin(baseurl,path))
	if debug: print ">>>\n" + json.dumps(endpoint_result.json(), indent=2) + "\n<<<\n"
	return endpoint_result.json()

def postResult(endpoint, body, parameters=None):
	headers = {'content-type': 'application/json'}
        if parameters:
                parameter_string = urllib.urlencode(parameters)
                path = ''.join([endpoint + '?',parameter_string])
        else:
                path = endpoint
        endpoint_result = session.post(urljoin(baseurl,path), data=body, headers=headers)
        if debug: print ">>>\n" + json.dumps(endpoint_result.json(), indent=2) + "\n<<<\n"
        return endpoint_result.json()

def putResult(endpoint, body, parameters=None):
	headers = {'content-type': 'application/json'}
        if parameters:
                parameter_string = urllib.urlencode(parameters)
                path = ''.join([endpoint + '?',parameter_string])
        else:
                path = endpoint
        endpoint_result = session.put(urljoin(baseurl,path), data=body, headers=headers)
        if debug: print ">>>\n" + json.dumps(endpoint_result.json(), indent=2) + "\n<<<\n"
        return endpoint_result.json()

def getUsers():
	print
	user_result = getResult('/user-admin/v1/accounts/B-3-112OHLC/users')

def createUserRequest():
    	user_obj = {
    		"roleAssignments": [
      			{
        		"roleId": 14, 
        		"groupId": 41241
      			}
    		], 
    		"firstName": "Kirsten", 
    		"phone": "8315887563", 
    		"lastName": "Hunter", 
    		"email": "kirsten.hunter@akamai.com"
   	}

	print "creating user"
	purge_post_result = postResult('/user-admin/v1/users', json.dumps(user_obj))
	print
	print purge_post_result
	return purge_post_result["contactId"]

def deleteUserRequest(contactid):
	print "Deleting user %s" % contactid
	delete_result = endpoint_result = session.delete(urljoin(baseurl,'/user-admin/v1/users/%s' % contactid))

if __name__ == "__main__":
	Id = {}
	contactid = createUserRequest()
	deleteUserRequest(contactid)
	getUsers()

@@ -19,420 +19,267 @@
 %0A%22%22%22
- Sample client for CCU%0ANote th
+%0AA simple script demonstr
 at
- 
 in
- order for this to work you need to provision credentials%0Aspecifically for CCU - you cannot extend existing credentials to add%0ACCU as it's managed under %22CCU%22 in the API credential system.%0A%0AConfigure-%3EOrganization-%3EManage APIs%0ASelect %22CCU APIs%22%0ACreate client collections/clients%0AAdd authorization%0A%0APut the credentials in ~/.edgerc as demonstrated by api-kickstart/sample_edgerc
+g how to add new users and delete them.%0ANote that this isn't a great script to run on your production system,%0Abut it will create a user unlikely to conflict with the ones you have.%0AThis is more of sample code than anything else.%0A%0A
 %0A%22%22%22

Fixing the instructions in user-admin.py