Simple tool to extract an Oracle Cloud API GATEWAY deployment to a JSON file


Oracle APIGW deployments can be created using the OCI web console or by creating a JSON file specification.

In this post we are sharing a simple script that extracts in a json file an existing deployment. It can be useful to learn the syntax for the json specification. Simply create a deployment with the UI and then export it with the tool.

Usage example

Usage:
      ./extractapi.sh deployment-name compartment-name gateway-name output-file
mbpj:tests javiermugueta$ ./extractapi.sh pp xplrDV p-g-s-dv.c.wznk.nt extract.json
.
Input vars:
Deployment Name: pp
Compartment Name (case sensitive): xplrDV
Gateway Name: p-g-s-dv.c.wznk.nt
Output file: extract.json
.

Extraction finished!!!

Result

A JSON file like this thing below:

        ...
          "header-transformations": {
          "filter-headers": null,
          "rename-headers": null,
          "set-headers": {
            "items": [
              {
                "if-exists": "OVERWRITE",
                "name": "CNTRY",
                "values": [
                  "SP"
                ]
              },
              {
                "if-exists": "OVERWRITE",
                "name": "PRDCT",
                "values": [
                  "WZ_SP"
                ]
              },
              {
                "if-exists": "OVERWRITE",
                "name": "SESSIONID",
                "values": [
                  "dummy_value"
                ]
              },
              {
                "if-exists": "OVERWRITE",
                "name": "RGNLSRGNT",
                "values": [
                  "\"value\": \"${request.headers[SRGNT]}\""
                ]
              }
            ]
          }
        },
        "header-validations": null,
     ...

SCRIPT CODE

Also available here.

#!/bin/bash
#
# jmu 2/2/1968+54
#
# Purpose:
# Extracts a json file of an existing APIGW deployment 
#
# NOTICE:This tool is experimental
#        Keys are exported like camel-case instead of like camelCase
#
# parameters
deployname=$1 
compname=$2 
gatewayname=$3
outputfile=$4
#
# inerface for humans
echo .
echo "Input vars:"
echo "Deployment Name: "$deployname
echo "Compartment Name (case sensitive): "$compname
echo "Gateway Name: "$gatewayname
echo "Output file: "$outputfile
echo .
#
usage(){
    echo "Usage:"
    echo "      ./extractapi.sh deployment-name compartment-name gateway-name output-file"
    echo "Example:"
    echo "      ./extractapi.sh pet xploraDEV api-oag-es-dev.oci.wizink.net extract.json"
}
#
if [[ "$#" -ne 4 ]]; then
    echo .
    echo "Wrong number of arguments passed!"
    echo .
    usage
    echo 
    exit
fi
#
# hands on
#
compocid=$(oci iam compartment list --compartment-id-in-subtree true --all | jq --arg compname "$compname" '.data[] | select(."name"==$compname)' | jq -r ."id")
#
gwocid=$(oci api-gateway gateway list -c $compocid --lifecycle-state ACTIVE --all --query "data.items[?\"display-name\" == '$gatewayname'].\"id\""  | jq -r .[0])
#
deployid=$(oci api-gateway deployment list -c $compocid --lifecycle-state ACTIVE --gateway-id $gwocid --all --query "data.items[?\"display-name\" == '$deployname'].\"id\"" | jq -r .[0])
#
oci api-gateway deployment get --deployment-id $deployid  > $outputfile
#
mv $outputfile $outputfile.temp
#
# extracts "specification" key value inside "data" key of json extraction
cat $outputfile.temp | jq .data.specification > $outputfile
rm $outputfile.temp
#
echo
echo "Extraction finished!!!"
echo

That’s all, hope it helps!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.