Skip to content

Instantly share code, notes, and snippets.

@nmasse-itix
Last active June 16, 2021 09:13
Show Gist options
  • Save nmasse-itix/e47d6a61bc1bae7128f3e4517cbd57bf to your computer and use it in GitHub Desktop.
Save nmasse-itix/e47d6a61bc1bae7128f3e4517cbd57bf to your computer and use it in GitHub Desktop.
3scale Admin REST API - Sample code
#!/bin/bash
set -Eeuo pipefail
ADMIN_PORTAL="https://3scale-admin.apps.changeme"
TOKEN="changeme"
APPLICATION_PLAN_ID="changeme"
curl -skf -X POST "$ADMIN_PORTAL/admin/api/signup.json" -o /tmp/signup.json \
--data-urlencode "access_token=$TOKEN" \
--data-urlencode "org_name=Test account" \
--data-urlencode "username=test-account" \
--data-urlencode "email=nmasse@redhat.com" \
--data-urlencode "password=s3cr3t"
account_id="$(jq -r .account.id /tmp/signup.json)"
echo "Created account has id $account_id"
curl -skf -X POST "$ADMIN_PORTAL/admin/api/accounts/$account_id/applications.json" \
-o /tmp/app.json \
--data-urlencode "access_token=$TOKEN" \
--data-urlencode "plan_id=$APPLICATION_PLAN_ID" \
--data-urlencode "name=test app" \
--data-urlencode "description=used for tests"
apikey="$(jq -r .application.user_key /tmp/app.json)"
echo "Created application has api key $apikey"
#!/bin/bash
set -Eeuo pipefail
ADMIN_PORTAL="https://3scale-admin.apps.changeme"
TOKEN="changeme"
PRODUCT_ID="changeme"
# Retrieve the list of all policies
curl -skf -X GET -o /tmp/policies.json \
"$ADMIN_PORTAL/admin/api/policies.json?access_token=$TOKEN"
echo "Available policies:"
jq -r 'to_entries | .[].key' /tmp/policies.json
# Extract the policy schema configuration
jq '.maintenance_mode[0].configuration' /tmp/policies.json > /tmp/maintenance_mode_schema.json
# This is our policy configuration
cat > /tmp/maintenance_mode.json <<EOF
{
"message_content_type": "text/plain; charset=utf-8",
"status": 503,
"message": "Service Unavailable - Maintenance"
}
EOF
# Pre-requisites: install the JSON schema validator with
# sudo dnf install python3-jsonschema
# Validate the policy configuration
if ! jsonschema -i /tmp/maintenance_mode.json /tmp/maintenance_mode_schema.json; then
echo "Policy configuration is invalid"
exit 1
fi
# Retrieve the Product's policy chain
curl -skf -X GET -o /tmp/policy_chain.json \
"$ADMIN_PORTAL/admin/api/services/$PRODUCT_ID/proxy/policies.json?access_token=$TOKEN"
# This is the policy we will add to the policy chain
cat > /tmp/policy.json <<EOF
{
"name": "maintenance_mode",
"version": "builtin",
"enabled": true,
"configuration": $(cat /tmp/maintenance_mode.json)
}
EOF
# Inject it into the policy chain
jq 'to_entries | .[0].value' /tmp/policy_chain.json | jq --arg policy "$(cat /tmp/policy.json)" '[ $policy | fromjson ] + .' > /tmp/new_policy_chain.json
# Update the policy chain
curl -skf -X PUT -o /dev/null \
"$ADMIN_PORTAL/admin/api/services/$PRODUCT_ID/proxy/policies.json" \
--data-urlencode "access_token=$TOKEN" \
--data-urlencode "policies_config=$(cat /tmp/new_policy_chain.json)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment