Blog

How To Automate and Test Akamai Configs    

January 21, 2020 · by Aman Alam and Javier Garza ·
Categories:

Many Akamai customers have adopted mature DevOps practices to improve agility and speed, and config creation (or handling) is one of the most commonly automated tasks on our platform. To help you increase your productivity even further, here are some tips for automating and testing Akamai configurations. In this guide, we’ll discuss how to:

  • Test rapidly while you’re coding
  • Automate property/config creation
  • Check the configs before pushing them live

First, let’s look at how to test as you code.

Test Code While You Build

As a developer, you want to make sure new features work well when using Akamai, however, this generally requires pushing new code to a remote server to test it. If you use the Akamai Sandbox, you don’t need this extra step each time you check your code. With the Sandbox, your localhost acts like the origin server, and your property configs are made available in a testing environment.

Akamai Sandbox is a server-side tool that allows you to create a dedicated Akamai server to test your configuration changes before pushing them to the staging network. You can use the Sandbox CLI package to manage your sandboxes and allow other people within your organization to clone them. See the Sandbox page to learn more about the Akamai Sandbox and what can it do for you. 

Automate Akamai Property Creation

There are two ways to automate property creation. You can either use the Property Manager API directly, or use it with the Akamai CLI and its Property subcommand. Let’s take a closer look at each approach.

1) Property manager API (PAPI)

PAPI is part of the Akamai API suite. It’s a REST-based API that lets you manage your properties without having to click through the Control Center. This method allows you to treat Akamai configurations as code to interact with them more seamlessly as you work. You can use the programming language of your choice and use 3rd party tools like HTTPie, cURL, or Postman.

In order to create a new property, you can clone an existing property. You’ll just need a JSON file containing the following:

  • productId - the product ID that you are using with your new property
  • propertyName - ex: “jgarza-dac-blog-test”
  • cloneFrom - details of the property you want to clone: 
    • propertyId - The unique ID of the source property 
    • version - The version of the property you want to clone 
    • copyHostnames - Whether you want to copy the assigned hostnames of the property (use “true” to migrate the hostnames to the cloned property)

$ cat clone-config.json

{

   "productId": "prd_SPM",

   "propertyName": "jgarza-dac-blog-test",

   "cloneFrom": {

       "propertyId": "prp_489714",

       "version": 1,

       "copyHostnames": false

   }

}

Figure 1. Contents of clone-config.json

Now we can use HTTPie to make the API call and create new property:

$ http --auth-type edgegrid --auth devrel-admin: POST ':/papi/v1/properties?contractId=ctr_C-1FRYVV3&groupId=grp_64867' < clone-config.json

 

HTTP/1.1 201 Created

Connection: keep-alive

Content-Encoding: gzip

Content-Length: 116

Content-Type: application/json;charset=UTF-8

Date: Mon, 13 Jan 2020 23:56:13 GMT

Location: https://akab-coq3auwltjdkgwby-ogel7mtggepbet6g.luna.akamaiapis.net/papi/v0/properties/prp_591573?contractId=ctr_C-1FRYVV3&groupId=grp_64867

Papi-Cache-Status: CACHE_MISS

Vary: Accept-Encoding,Accept-Encoding

X-Limit-Properties-Per-Contract-Limit: 5000

X-Limit-Properties-Per-Contract-Remaining: 4763

X-Trace-Id: df755e1d039308f4

 

{

   "propertyLink": "/papi/v0/properties/prp_591573?contractId=ctr_C-1FRYVV3&groupId=grp_64867"

}

Figure 2. HTTPie command to clone property

The above example uses the HTTPie EdgeGrid authentication plugin, a plugin for HTTPie that makes it easier to work with the Akamai authentication model (more on that later). You’ll need to install HTTPie and the HTTPie EdgeGrid authentication plugin before running the command above. Also notice the contractID and groupID will be different for you (see the PAPI endpoints for listing contracts and groups)

In the output shown we can see 3 different sections:

  1. The shell command (white) starting with “http”
  2. The HTTP headers returned by the HTTPie command (orange), notice the status code showing “201 Created”
  3. The JSON response of the Property Manager API showing the endpoint starting with “/papi/v0/properties/” we can use to get the details about the new property (blue)

You can see properties details by running the command below:

http -A edgegrid -a devrel-admin: ':/papi/v0/properties/prp_591573?contractId=ctr_C-1FRYVV3&groupId=grp_64867'

 

HTTP/1.1 200 OK

Cache-Control: no-store

Connection: keep-alive

Content-Encoding: gzip

Content-Length: 224

Content-Type: application/json;charset=UTF-8

Date: Wed, 15 Jan 2020 00:49:27 GMT

Papi-Cache-Status: CACHE_MISS

Vary: Accept-Encoding,Accept-Encoding

X-Trace-Id: 955a5e1e61960081

 

{

   "properties": {

       "items": [

           {

               "accountId": "act_B-C-1FRYVMN",

               "assetId": "aid_10762407",

               "contractId": "ctr_C-1FRYVV3",

               "groupId": "grp_64867",

               "latestVersion": 1,

               "productionVersion": null,

               "propertyId": "prp_591573",

               "propertyName": "jgarza-dac-blog-test",

               "stagingVersion": null

           }

       ]

   }

}

Figure 3. Property details

You can easily adapt the example above to use the PAPI endpoint using your favorite programming language or environment. If you want to dive deeper, check the PAPI documentation.

2) CLI and Property Manager sub-command

The Akamai CLI is a command-line interface that makes it easy to interact with Akamai APIs from the command line. Akamai CLI is available as an open-source package on GitHub along with instructions for installing it. The CLI is also available through the Homebrew package manager on Mac OS. 

In this example, we’ll walk through how to use the Property Manager sub-command—useful in environments where shell commands are easiest to run, such as a Bash script.

To install the Akamai CLI through Homebrew on Mac OS, just run the following command in your terminal:
$ brew install akamai

Once the install is complete, you can run the following command to install the Property Manager sub-command:
$ akamai install property

Note that the Akamai “property” CLI package requires the node.js runtime. We can install it similarly using: brew install node

Once you have everything installed, you can automate your config creation from the CLI. To handle configs through the Property Manager sub-command, let’s take our previous example and create a new property by cloning an existing property:

akamai property --section devrel-admin create jgarza-dac-blog-test2 --clone dev.jgarza-nstest --srcver 1 --nocopy --contract ctr_C-1FRYVV3

 

... searching propertyName for dev.jgarza-nstest

... getting info for prp_489714

... retrieving clone info

... retrieving clone rules for cpcode

Creating property config jgarza-dac-blog-test2

Retrieving jgarza-dac-blog-test2 v1

... retrieving property (jgarza-dac-blog-test2) v1

... updating property (jgarza-dac-blog-test2) v1

... retrieving list of hostnames {ctr_C-1FRYVV3 : grp_64867 : prp_591884}

Updating property hostnames

Adding hostname jgarza-dac-blog-test2

Command time: 0.36 mins

Figure 4. Property creation using property CLI package

To learn more about Akamai CLI and Property Manager go to:

Property Manager CLI — To manage Akamai properties through Akamai CLI from the command line

Akamai Authentication Model

As mentioned earlier, it’s important to understand that all Akamai APIs and the Akamai CLI depend on the Akamai EdgeGrid authentication model, which requires you to create an API client from the Control Center before you can interact with Akamai through the API or CLI. This is a one-time process, after which you can continue using API and CLI commands that you allow for the generated client.

The Akamai APIs expect you to sign your requests either manually, or by using any of the open-source client signing libraries available.

In addition, the Akamai CLI requires a config file called, “.edgerc”. The file should be in your home directory and contains the authentication credentials you’ll need. See the API Getting Started guide for instructions on how to create an API client.

Bonus: If you’d like to play around with Akamai APIs, Postman now features the Akamai Authentication method

Finally, let’s take a look at how you can check changes before they go live. 

Automatically Test Your Properties 

Once you create new properties, you can test them before releasing the new build to your master environment. The Akamai Test Center helps you test changes, so that you can verify the code you’re going to push to production. Test Center APIs allow you automate the process. 

The following documents will help you get started with Test Center:

Automating config creation and testing using Akamai Sandbox, PAPI, CLI, and Test Center will help you continue to streamline your DevOps workflow. 

You Might Also Like: