Pascal Maugeri

Copy n' pasting your Image Manager policies

Blog Post created by Pascal Maugeri Employee on Mar 7, 2017

This blog presents a very simple python program to copy your Akamai Image Manager policies from one API token to another one. The application leverages Image Manager API to read the policies using List Policies and add the policies using Add a Policy calls.

 

The application requires EdgeGrid for Python and you will need to configure a .edgerc file in your working directory in order to run this application:

[default]
host = akab-<hidden>.imaging.akamaiapis.net

client_token = <hidden>

client_secret = <hidden>

access_token = <hidden>

max-body = 131072

Note that if you want to activate a policy on the staging network, add -staging to the host parameter. 

 

Source code of im-policy-clone.py (complete source code is attached to this blog post):

[...]

def copyIMPolicies(sess, srcLunaToken, dstLunaToken):

   result = sess.get(urljoin(baseurl, '/imaging/v0/policies'), headers={'Luna-Token': srcLunaToken })
   if (result.status_code != 200):
      print "Error status code: " + str(result.status_code)
      return
   jobj = result.json()
   print "Read " + str(len(jobj['items'])) + " policy(ies)."
   for j in jobj['items']:
      policyId = j['id']
      policy = json.dumps(j)
      urlPath = '/imaging/v0/policies/' + policyId
      
headers = {'Luna-Token': dstLunaToken , "Content-Type": "application/json"}
      result = sess.put(urljoin(baseurl, urlPath), data=policy, headers=headers)
      if ((result.status_code == 200) or (result.status_code == 201)):
         print "Policy \"" + policyId + "\" copied."
         if ((result.status_code != 200) and (result.status_code != 201)):
            print "Error status code: " + str(result.status_code) + " writing the IM policy: " + policy
            return

[...]

You can call the application passing in argument the "source" and the "destination" API tokens:

$ python im-policy-clone.py <source API token> <destination API token>

 

Warning: note the the copy of the policies will also trigger the automatic activation of those to the chosen network (Staging or Production) in the .edgerc file.

 

DISCLAIMER: The source code of the application is provided "as is" to illustrate a copy and paste of the IM policies. I can't guarantee it is bug-free and it certainly needs testing to be used in a production environment.

Attachments

Outcomes