Jq Snippets With HTTPie and Akamai API calls

Couple of useful jq snippets merged with Akamai API calls.

Jq Snippets With HTTPie and Akamai API calls

Within this post , I'll be documenting a couple of useful snippets to use with various Akamai API calls which will allow you to grab information about your configuration and settings quite quickly.

Pre-Requisits

I assume that your environment is ready already , if not follow the steps :

  1. Installing HTTPie and httpie-edgegrid plugin , and Creating your API credentials in Control Center - A good guide on how to do that here.

  2. Download and install Jq from here.

Once set , you should be able to run something as the following :

http --auth-type=edgegrid -a aka: :/diagnostic-tools/v2/ghost-locations/available | jq '.locations[0:3]'

which will give you the first 3 locations - from 0(inclusive) to 3(exclusive) :

[
  {
    "id": "adelaide-sa-australia",
    "value": "Adelaide, SA, Australia"
  },
  {
    "id": "amsterdam-netherlands",
    "value": "Amsterdam, Netherlands"
  },
  {
    "id": "ashburn-va-unitedstates",
    "value": "Ashburn, VA, United States"
  }
]

Snippets

Before going down this section , I would suggest if you're new to jq to go over the tutorial and the manual.

Use Case : Grabbing all Adaptive Media delivery Hostnames.

First , Calling the Edge Hostnames End Point and grabbing only the first 2 results with the command :

http --auth-type edgegrid -a aka: ":/hapi/v1/edge-hostnames" | jq -r '.edgeHostnames[0:2]'

The Output should look something like :


[
  {
    "chinaCdn": {
      "isChinaCdn": false
    },
    "comments": "Created by Property-Manager/PAPI on Wed Apr 10 11:01:42 UTC 2019",
    "dnsZone": "akamaized.net",
    "edgeHostnameId": 123456,
    "ipVersionBehavior": "IPV6_IPV4_DUALSTACK",
    "map": "a;dscv.akamai.net",
    "productId": "AMD",
    "recordName": "liveamd-preprod",
    "securityType": "STANDARD-TLS",
    "serialNumber": 1234,
    "ttl": 21600,
    "useDefaultMap": true,
    "useDefaultTtl": true
  },
  {
    "chinaCdn": {
      "isChinaCdn": false
    },
    "comments": "Created by Property-Manager/PAPI on Tue Apr 23 08:11:43 UTC 2019",
    "dnsZone": "akamaized.net",
    "edgeHostnameId": 123457,
    "ipVersionBehavior": "IPV6_IPV4_DUALSTACK",
    "map": "a;dscv.akamai.net",
    "productId": "AMD",
    "recordName": "liveamd-prod",
    "securityType": "STANDARD-TLS",
    "serialNumber": 2345,
    "ttl": 21600,
    "useDefaultMap": true,
    "useDefaultTtl": true
  }
]

to filter on a specific field , we can call the select method select(.productId=="AMD") and with the complete jq command it should look something like :

http --auth-type edgegrid -a aka: ":/hapi/v1/edge-hostnames" | jq '.edgeHostnames[] | select(.productId=="AMD") | {name:.recordName , product:.productId}'

The last pipe that I invoke | {name:.recordName , product:.productId} , is to grab only the 2 fields that are interesting to me. The Output should look something like :

{
  "name": "liveamd-preprod",
  "product": "AMD"
}
{
  "name": "liveamd-prod",
  "product": "AMD"
}
Use Case : Grabbing all Edge Hostnames that are on IPv4 only map

Adapting this to grab the hostnames that are still on IPv4 : Command:

http --auth-type edgegrid -a aka: ":/hapi/v1/edge-hostnames" | jq '.edgeHostnames[] | select(.productId=="AMD" and .ipVersionBehavior=="IPV4") | {name:.recordName , product:.productId , ipversion:.ipVersionBehavior}'

Sample Output :

{
  "name": "vodpreprod.test.tv",
  "product": "AMD",
  "ipversion": "IPV4"
}
{
  "name": "livepreprod.test.tv",
  "product": "AMD",
  "ipversion": "IPV4"
}
Use Case : Grabbing all Edge Hostnames that contains a keyword

Command:

http --auth-type edgegrid -a aka: ":/hapi/v1/edge-hostnames" | jq '.edgeHostnames[] | select(.recordName | contains ("vod") ) | {name:.recordName,product:.productId} '

Sample Output:

{
  "name": "vodpreprod.test.tv",
  "product": "AMD"
}
{
  "name": "vodprod.test.tv",
  "product": "AMD"
}
Use Case : Grabbing all CPcodes in a property

to do this , we would need the Property Rule tree , Assuming that we only know the hostname - We grab the details for the property by doing the following API call :

echo '{"hostname": "vodprod.test.tv"}' | http --auth-type edgegrid -a aka: POST ":/papi/v1/search/find-by-value"

This will return the latest versions on both production and staging - Sample Output :

{
    "versions": {
        "items": [
            {
                "accountId": "act_F-AC-XXXXXX",
                "assetId": "aid_111111",
                "contractId": "ctr_XXX",
                "edgeHostname": "vodprod.test.tv.akamaized.net",
                "groupId": "grp_123456",
                "hostname": "vodprod.test.tv",
                "note": "-- new version to do extra awesome stuff --",
                "productionStatus": "INACTIVE",
                "propertyId": "prp_123456",
                "propertyName": "vod-prod-test",
                "propertyVersion": 47,
                "stagingStatus": "ACTIVE",
                "updatedByUser": "ralzreib",
                "updatedDate": "2022-02-17T09:12:02Z"
            },
            {
                "accountId": "act_F-AC-XXXXXX",
                "assetId": "aid_111111",
                "contractId": "ctr_XXX",
                "edgeHostname": "vodprod.test.tv.akamaized.net",
                "groupId": "grp_123456",
                "hostname": "vodprod.test.tv",
                "note": "--New version to do extra awesome stuff --",
                "productionStatus": "ACTIVE",
                "propertyId": "prp_123456",
                "propertyName": "vod-prod-test",
                "propertyVersion": 42,
                "stagingStatus": "DEACTIVATED",
                "updatedByUser": "ralzreib",
                "updatedDate": "2022-02-05T02:11:45Z"
            }
        ]
    }
}

Once we have the details for the property - we can grab the rule tree :

http --auth-type edgegrid -a aka: GET ":/papi/v1/properties/prp_123456/versions/47/rules" --follow

Note : don't forget your --follow tag to follow the redirect.

We're half way there ^^

The rule tree will contain all the behavior in a configuration - to grab the ones that set the cpcode - we need to filter on the behaviors called cpCode , this can be achived by searching recursively for all the behaviors within a rule tree , selecting the one that have the name CpCode and then filtering the output to only show the name and the cpcode number :

cat prp_ruletree.json | jq '..|objects|.behaviors[]? | select (.name == "cpCode") | { cpcode :.options.value.id, name : .options.value.name }'

Output :

{
  "cpcode": 123456,
  "name": "live-prod"
}
{
  "cpcode": 123457,
  "name": "live-preprod"
}

Or you want to skip redirecting the rule tree into a file - you can do the following :

http --auth-type edgegrid -a aka: GET ":/papi/v1/properties/prp_123456/versions/47/rules" --follow  | jq '..|objects|.behaviors[]? | select (.name == "cpCode") | { cpcode :.options.value.id, name : .options.value.name }'
Use Case : Grabbing all Origin in a property

With a little tweaking , we can grab the origin applied to a property :

http --auth-type edgegrid -a aka: GET ":/papi/v1/properties/prp_123456/versions/47/rules" --follow  | jq '..|objects|.behaviors[]? | select (.name == "origin") | {cachekey:.options.cacheKeyHostname,hostname:.options.hostname , forwardHostHeader:.options.forwardHostHeader}'
{
  "cachekey": "ORIGIN_HOSTNAME",
  "hostname": "mylovelyorigin1.akadns.net",
  "forwardHostHeader": "ORIGIN_HOSTNAME"
}
{
  "cachekey": "ORIGIN_HOSTNAME",
  "hostname": "mylovelyorigin2.akadns.net",
  "forwardHostHeader": "REQUEST_HOST_HEADER"
}
Use Case : Grab the last ECCU purge and check its status

This would be done on 2 stages - first getting the ID of the last purge submitted :

http --auth-type edgegrid -a aka: GET :"/eccu-api/v1/requests" | jq '.requests | max_by(.requestDate) | .requestId'

The command above will grab all the ECCU requests submitted - and return the most recent purge - the last section instructs it to only show the ID.

Once we have the ID , we can redirect that to a variable - and use it to do an API call to get the status of this ECCU Purge ID.

last=$(http --auth-type edgegrid -a aka: GET :"/eccu-api/v1/requests" | jq '.requests | max_by(.requestDate) | .requestId') ; http --auth-type edgegrid -a aka: GET :"/eccu-api/v1/requests/${last}" | jq '.status'

Output :

"SUCCEEDED"

You have now reached the end of this post , hopefully this was useful and can speed up your checks on Properties and configuration using API calls and jq.