Red Hat 3scale API Management — Publishing an API as Custom Resource Definition
As a 3scale administrator, one can use the 3scale operator to configure 3scale services and provision 3scale resources.
This article talks about:
- Available Custom Resource Definition in 3scale.
- Deploy a sample Application.
- Prerequisite for Creating 3scale CRDs.
- Publishing an API via 3scale CRDs
- Testing APIs.
Available Custom Resource Definition
- The 3scale operator provides custom resource definitions and their relations, which are visible in the following diagram.
bash-3.2$ oc api-resources |grep -i 3scale
apimanagerbackups apps.3scale.net/v1alpha1 true APIManagerBackup
apimanagerrestores apps.3scale.net/v1alpha1 true APIManagerRestore
apimanagers apps.3scale.net/v1alpha1 true APIManager
activedocs capabilities.3scale.net/v1beta1 true ActiveDoc
backends capabilities.3scale.net/v1beta1 true Backend
custompolicydefinitions capabilities.3scale.net/v1beta1 true CustomPolicyDefinition
developeraccounts capabilities.3scale.net/v1beta1 true DeveloperAccount
developerusers capabilities.3scale.net/v1beta1 true DeveloperUser
openapis capabilities.3scale.net/v1beta1 true OpenAPI
products capabilities.3scale.net/v1beta1 true Product
proxyconfigpromotes capabilities.3scale.net/v1beta1 true ProxyConfigPromote
tenants capabilities.3scale.net/v1alpha1 true Tenant
Deploy a sample Application
- Deploy the application. One can use this example in the source code repo.
mvn clean package -Dquarkus.kubernetes.deploy=true
oc get svc -n quickstart
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
restjsonquickstart ClusterIP 172.21.48.152 <none> 80/TCP 45h
curl http://restjsonquickstart.xxxxxx/fruits
[{"description":"Winter fruit","name":"Apple"},{"description":"Tropical fruit","name":"Pineapple"}]
Prerequisite for Creating 3scale CRDs
- Create a new project.
oc new-project product
- Make sure it has a 3scale operator deployed in this project.
- Get the 3scale provider hostname.
bash-3.2$ oc get routes -n 3scale -l zync.3scale.net/route-to=system-provider
NAME HOST/PORT SERVICES PORT TERMINATION WILDCARD
zync-3scale-provider-gdb2z 3scale-admin.itzroks-xxxxxappdomain.cloud system-provider http edge/Redirect None
- Get the ADMIN_ACCESS_TOKEN
oc -n 3scale get secret system-seed -o json | jq -r .data.ADMIN_ACCESS_TOKEN | base64 -d
gvePwViSh8Udu6ucbash
- Based on the hostname and ADMIN_ACCESS_TOKEN, create the secret.
oc -n product create secret generic threescale-provider-account --from-literal=adminURL=https://3scale-admin.itzroks-xxxxxappdomain.cloud --from-literal=token=gvePwViSh8Udu6ucbash
Publishing an API via 3scale CRDs
- Create the backend resource based on the application deployed in the last step.
apiVersion: capabilities.3scale.net/v1beta1
kind: Backend
metadata:
name: backend-fruitsapi
spec:
name: "backend api for fruits"
systemName: backend-fruits
privateBaseURL: "http://restjsonquickstart.quickstart.svc.cluster.local:80"
- Create the product based, specifying the backendUsages as the backend created in the previous step.
apiVersion: capabilities.3scale.net/v1beta1
kind: Product
metadata:
name: product-fruit
spec:
name: "product-fruit"
systemName: "product-fruit"
applicationPlans:
plan01:
name: "My Plan 01"
setupFee: "14.56"
appsRequireApproval: false
published: true
plan02:
name: "My Plan 02"
trialPeriod: 3
costMonth: "3"
appsRequireApproval: false
published: true
mappingRules:
- httpMethod: GET
pattern: "/"
increment: 1
metricMethodRef: hits
backendUsages:
backend-fruits:
path: /
- Create a developer account.
apiVersion: capabilities.3scale.net/v1beta1
kind: DeveloperAccount
metadata:
name: api-consumer-org1
spec:
orgName: Ecorp
- Create a user with an Admin role for the above developer account.
apiVersion: v1
kind: Secret
metadata:
name: myusername01
stringData:
password: "admin123"
apiVersion: capabilities.3scale.net/v1beta1
kind: DeveloperUser
metadata:
name: developeruser01
spec:
username: myusername01
email: myusername01@example.com
passwordCredentialsRef:
name: myusername01
role: admin
developerAccountRef:
name: api-consumer-org1
- Create an application via 3scale UI. It can be automated using 3scale toolbox. But for the simplicity of this article, create it via UI as follows.
- Promote the API to staging.
apiVersion: capabilities.3scale.net/v1beta1
kind: ProxyConfigPromote
metadata:
name: product-fruit-promote-staging
spec:
productCRName: product-fruit
Testing APIs
- Find out example curl for testing.
- Test the API.
curl "https://product-fruit-3scale-apicast-staging.itzroks-xxxx.appdomain.cloud/fruits?user_key=122486c7f3c1ce0ecae8944559a13ce4"
[{"description":"Winter fruit","name":"Apple"},{"description":"Tropical fruit","name":"Pineapple"}]